List of usage examples for java.lang ClassLoader loadClass
public Class<?> loadClass(String name) throws ClassNotFoundException
From source file:com.moss.schematrax.DynamicSchemaUpdate.java
public DynamicSchemaUpdate(String updateId, ClassLoader loader, String updateClassName) { this.updateId = updateId; try {// w w w .ja v a 2 s . c om updateClass = loader.loadClass(updateClassName); if (!JavaSchemaUpdate.class.isAssignableFrom(updateClass)) { throw new RuntimeException("Update classes must implement interface " + JavaSchemaUpdate.class.getName() + ": " + updateClass.getName()); } } catch (ClassNotFoundException ex) { throw new RuntimeException("Could not find update class " + updateClassName, ex); } catch (Exception ex) { throw new RuntimeException("Could not load update class: " + updateClassName, ex); } try { updateClass.getConstructor(new Class[0]); } catch (NoSuchMethodException ex) { throw new RuntimeException("Update classes must have a no-arg constructor " + updateClassName, ex); } SupportedDatabases supported = (SupportedDatabases) updateClass.getAnnotation(SupportedDatabases.class); if (supported == null) { throw new RuntimeException("Update classes must have a class level " + SupportedDatabases.class.getName() + " annotation: " + updateClass.getName()); } supportedDatabases = new ArrayList<DatabaseType>(); for (String dbId : supported.value()) { DatabaseType dbType = DatabaseType.getDatabaseType(dbId); if (dbType == null) { log.warn("Ignoring unknown supported database type " + dbId + " for update " + updateId + "(" + updateClass.getName() + ")"); } else { supportedDatabases.add(dbType); } } }
From source file:com.googlecode.jsonschema2pojo.integration.json.JsonTypesIT.java
@Test public void simpleTypesInExampleAreMappedToCorrectJavaTypes() throws Exception { ClassLoader resultsClassLoader = generateAndCompile("/json/simpleTypes.json", "com.example", config("sourceType", "json")); Class<?> generatedType = resultsClassLoader.loadClass("com.example.SimpleTypes"); Object deserialisedValue = OBJECT_MAPPER .readValue(this.getClass().getResourceAsStream("/json/simpleTypes.json"), generatedType); assertThat((String) generatedType.getMethod("getA").invoke(deserialisedValue), is("abc")); assertThat((Integer) generatedType.getMethod("getB").invoke(deserialisedValue), is(123)); assertThat((Double) generatedType.getMethod("getC").invoke(deserialisedValue), is(12999999999999999999999.99d)); assertThat((Boolean) generatedType.getMethod("getD").invoke(deserialisedValue), is(true)); assertThat(generatedType.getMethod("getE").invoke(deserialisedValue), is(nullValue())); }
From source file:com.github.helenusdriver.commons.lang3.reflect.ReflectionUtils.java
/** * Find all classes from a given file or directory and add them to the provided * list.// www . j ava 2 s . com * * @author paouelle * * @param classes the non-<code>null</code> collection of classes where to add new * found classes * @param file the non-<code>null</code> file or directory from which to find * classes * @param resource the non-<code>null</code> resource being scanned that * corresponds to given file or directory * @param cl the classloader to find the classes with */ private static void findClassesFromFile(Collection<Class<?>> classes, File file, String resource, ClassLoader cl) { if (file.isDirectory()) { for (File f : file.listFiles()) { ReflectionUtils.findClassesFromFile(classes, f, resource + "." + f.getName(), cl); } } else if (resource.endsWith(".class")) { final String cname = resource.substring(0, resource.length() - 6); // 6 for .class try { classes.add(cl.loadClass(cname)); } catch (ClassNotFoundException e) { // ignore it } } }
From source file:be.wegenenverkeer.common.resteasy.json.RestJsonMapper.java
/** * No-arguments constructor.//ww w . j ava 2s .co m */ public RestJsonMapper() { super(); this.setSerializationInclusion(JsonInclude.Include.NON_NULL); this.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); this.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); this.setDateFormat(new Iso8601AndOthersDateFormat()); SimpleModule testModule = new SimpleModule("jsr310", new Version(1, 0, 0, "", "be.wegenenverkeer.common", "common-resteasy")); testModule.addDeserializer(LocalDate.class, new LocalDateDeserializer()); testModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer()); testModule.addSerializer(LocalDate.class, new LocalDateSerializer()); testModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer()); this.registerModule(testModule); ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); if (null == classLoader) { classLoader = this.getClass().getClassLoader(); } try { Class clazz = classLoader.loadClass("com.fasterxml.jackson.datatype.joda.JodaModule"); Object instance = clazz.newInstance(); this.registerModule((Module) instance); } catch (Exception ex) { // ignore, we do not require joda-time, but inform the user LOG.warn("Add jackson-datatype-joda dependency for joda-time support."); } }
From source file:com.googlecode.jsonschema2pojo.integration.config.AnnotationStyleIT.java
@Test @SuppressWarnings({ "rawtypes", "unchecked" }) public void defaultAnnotationStyeIsJackson2() throws ClassNotFoundException, SecurityException, NoSuchMethodException { ClassLoader resultsClassLoader = generateAndCompile("/schema/properties/primitiveProperties.json", "com.example"); Class generatedType = resultsClassLoader.loadClass("com.example.PrimitiveProperties"); Method getter = generatedType.getMethod("getA"); assertThat(generatedType.getAnnotation(JsonPropertyOrder.class), is(notNullValue())); assertThat(generatedType.getAnnotation(JsonInclude.class), is(notNullValue())); assertThat(getter.getAnnotation(JsonProperty.class), is(notNullValue())); }
From source file:org.jsonschema2pojo.integration.config.IncludeAccessorsIT.java
@Test public void beansWithoutAccessorsRoundTripJsonCorrectly() throws ClassNotFoundException, SecurityException, NoSuchMethodException, NoSuchFieldException, InstantiationException, IllegalAccessException, IOException { ClassLoader resultsClassLoader = schemaRule.generateAndCompile( "/schema/properties/primitiveProperties.json", "com.example", config("includeAccessors", false)); Class generatedType = resultsClassLoader.loadClass("com.example.PrimitiveProperties"); Object instance = generatedType.newInstance(); generatedType.getDeclaredField("a").set(instance, 12); generatedType.getDeclaredField("b").set(instance, 1.12); generatedType.getDeclaredField("c").set(instance, true); ObjectMapper objectMapper = new ObjectMapper(); String instanceAsJson = objectMapper.writeValueAsString(instance); Object instanceAfterRoundTrip = objectMapper.readValue(instanceAsJson, generatedType); assertThat(instanceAfterRoundTrip, is(equalTo(instance))); }
From source file:org.jsonschema2pojo.integration.config.IncludeAccessorsIT.java
@Test public void beansOmitGettersAndSettersWhenAccessorsAreDisabled() throws ClassNotFoundException, SecurityException, NoSuchMethodException, NoSuchFieldException { ClassLoader resultsClassLoader = schemaRule.generateAndCompile( "/schema/properties/primitiveProperties.json", "com.example", config("includeAccessors", false)); Class generatedType = resultsClassLoader.loadClass("com.example.PrimitiveProperties"); try {//from w w w . j a va 2 s. co m generatedType.getDeclaredMethod("getA"); fail("Disabled accessors but getter was generated"); } catch (NoSuchMethodException e) { } try { generatedType.getDeclaredMethod("setA", Integer.class); fail("Disabled accessors but getter was generated"); } catch (NoSuchMethodException e) { } assertThat(generatedType.getDeclaredField("a").getModifiers(), is(Modifier.PUBLIC)); }
From source file:com.googlecode.jsonschema2pojo.integration.json.JsonTypesIT.java
@Test @SuppressWarnings("unchecked") public void complexTypesProduceObjects() throws Exception { ClassLoader resultsClassLoader = generateAndCompile("/json/complexObject.json", "com.example", config("sourceType", "json")); Class<?> complexObjectClass = resultsClassLoader.loadClass("com.example.ComplexObject"); Object complexObject = OBJECT_MAPPER .readValue(this.getClass().getResourceAsStream("/json/complexObject.json"), complexObjectClass); Object a = complexObjectClass.getMethod("getA").invoke(complexObject); Object aa = a.getClass().getMethod("getAa").invoke(a); assertThat(aa.getClass().getMethod("getAaa").invoke(aa).toString(), is("aaaa")); Object b = complexObjectClass.getMethod("getB").invoke(complexObject); assertThat(b.getClass().getMethod("getAa").invoke(b), is(notNullValue())); Object _1 = complexObjectClass.getMethod("get1").invoke(complexObject); Object _2 = _1.getClass().getMethod("get2").invoke(_1); assertThat(_2, is(notNullValue()));/* w w w. ja v a 2 s . co m*/ Object _3 = _1.getClass().getMethod("get3").invoke(_1); assertThat((List<Integer>) _3, is(equalTo(asList(1, 2, 3)))); }
From source file:com.openteach.diamond.container.DefaultClassLoaderDelegateHook.java
public Class preFindClass(String className, BundleClassLoader classloader, BundleData data) throws ClassNotFoundException { boolean loadFromExternal = false; for (String prefix : excludedPrefixes) { if (className.startsWith(prefix)) { loadFromExternal = true;// w w w . ja va 2 s . co m break; } } if (loadFromExternal) { for (ClassLoader thirdClassLoader : thirdClassLoaders) { if (thirdClassLoader != null) { try { return thirdClassLoader.loadClass(className); } catch (ClassNotFoundException e) { // IGNORE,continue find } } } } return null; }
From source file:org.jboss.spring.facade.ControllerBeanFactory.java
/** * Get the bean class.//from w w w. ja va2s .c o m * * @param className the class name * @param context the context * @return bean's class * @throws BeansException for any error */ protected Class<?> getBeanClass(String className, KernelControllerContext context) throws BeansException { try { ClassLoader cl = context.getClassLoader(); return cl.loadClass(className); } catch (Throwable t) { throw new FatalBeanException("Cannot load class: " + className + ", context: " + context, t); } }