List of usage examples for java.lang ClassLoader loadClass
public Class<?> loadClass(String name) throws ClassNotFoundException
From source file:org.jsonschema2pojo.integration.config.IncludeAccessorsIT.java
@Test public void beansIncludeGettersAndSettersByDefault() throws ClassNotFoundException, SecurityException, NoSuchMethodException, NoSuchFieldException { ClassLoader resultsClassLoader = schemaRule .generateAndCompile("/schema/properties/primitiveProperties.json", "com.example"); Class generatedType = resultsClassLoader.loadClass("com.example.PrimitiveProperties"); // throws NoSuchMethodException if method is not found generatedType.getDeclaredMethod("getA"); generatedType.getDeclaredMethod("setA", Integer.class); assertThat(generatedType.getDeclaredField("a").getModifiers(), is(Modifier.PRIVATE)); }
From source file:com.photon.phresco.util.PhrescoDynamicLoader.java
private Class getClassFromLocal(String className) { ClassLoader classLoader = this.getClass().getClassLoader(); try {/*w ww .j a v a2 s. com*/ return classLoader.loadClass(className); } catch (ClassNotFoundException e) { return null; } }
From source file:iddb.core.model.dao.DAOFactory.java
/** * @param key/*from w w w .j a v a 2 s.com*/ * @param value * @return * @throws Exception * @throws ClassNotFoundException * @throws IllegalAccessException * @throws InstantiationException */ @SuppressWarnings({ "rawtypes", "unchecked" }) private Object createCachedInstance(String iface, Object impl) throws Exception { String[] ifacePart = StringUtils.split(iface, "."); String ifaceName = ifacePart[ifacePart.length - 1]; log.debug("Getting cached instance for {} - {}", iface, ifaceName); ClassLoader loader = this.getClass().getClassLoader(); try { Class clz = loader.loadClass("iddb.core.model.dao.cached." + ifaceName + "Cached"); Constructor cons = clz.getConstructor(new Class[] { Class.forName(iface) }); return cons.newInstance(impl); } catch (SecurityException e) { log.warn("No cached implementation found for {} - {}", ifaceName, e.getMessage()); throw new Exception(e); } catch (IllegalArgumentException e) { log.warn("No cached implementation found for {} - {}", ifaceName, e.getMessage()); throw new Exception(e); } catch (ClassNotFoundException e) { log.warn("No cached implementation found for {} - {}", ifaceName, e.getMessage()); throw new Exception(e); } catch (NoSuchMethodException e) { log.warn("No cached implementation found for {} - {}", ifaceName, e.getMessage()); throw new Exception(e); } catch (InstantiationException e) { log.warn("No cached implementation found for {} - {}", ifaceName, e.getMessage()); throw new Exception(e); } catch (IllegalAccessException e) { log.warn("No cached implementation found for {} - {}", ifaceName, e.getMessage()); throw new Exception(e); } catch (InvocationTargetException e) { e.printStackTrace(); log.warn("No cached implementation found for {} - {}", ifaceName, e.getMessage()); throw new Exception(e); } }
From source file:com.laidians.utils.ClassUtils.java
/** * Replacement for <code>Class.forName()</code> that also returns Class instances * for primitives (e.g."int") and array class names (e.g. "String[]"). * Furthermore, it is also capable of resolving inner class names in Java source * style (e.g. "java.lang.Thread.State" instead of "java.lang.Thread$State"). * @param name the name of the Class// w w w .ja v a 2s. com * @param classLoader the class loader to use * (may be <code>null</code>, which indicates the default class loader) * @return Class instance for the supplied name * @throws ClassNotFoundException if the class was not found * @throws LinkageError if the class file could not be loaded * @see Class#forName(String, boolean, ClassLoader) */ public static Class<?> forName(String name, ClassLoader classLoader) throws ClassNotFoundException, LinkageError { Assert.notNull(name, "Name must not be null"); Class<?> clazz = resolvePrimitiveClassName(name); if (clazz == null) { clazz = commonClassCache.get(name); } if (clazz != null) { return clazz; } // "java.lang.String[]" style arrays if (name.endsWith(ARRAY_SUFFIX)) { String elementClassName = name.substring(0, name.length() - ARRAY_SUFFIX.length()); Class<?> elementClass = forName(elementClassName, classLoader); return Array.newInstance(elementClass, 0).getClass(); } // "[Ljava.lang.String;" style arrays if (name.startsWith(NON_PRIMITIVE_ARRAY_PREFIX) && name.endsWith(";")) { String elementName = name.substring(NON_PRIMITIVE_ARRAY_PREFIX.length(), name.length() - 1); Class<?> elementClass = forName(elementName, classLoader); return Array.newInstance(elementClass, 0).getClass(); } // "[[I" or "[[Ljava.lang.String;" style arrays if (name.startsWith(INTERNAL_ARRAY_PREFIX)) { String elementName = name.substring(INTERNAL_ARRAY_PREFIX.length()); Class<?> elementClass = forName(elementName, classLoader); return Array.newInstance(elementClass, 0).getClass(); } ClassLoader classLoaderToUse = classLoader; if (classLoaderToUse == null) { classLoaderToUse = getDefaultClassLoader(); } try { return classLoaderToUse.loadClass(name); } catch (ClassNotFoundException ex) { int lastDotIndex = name.lastIndexOf('.'); if (lastDotIndex != -1) { String innerClassName = name.substring(0, lastDotIndex) + '$' + name.substring(lastDotIndex + 1); try { return classLoaderToUse.loadClass(innerClassName); } catch (ClassNotFoundException ex2) { // swallow - let original exception get through } } throw ex; } }
From source file:com.googlecode.jsonschema2pojo.integration.json.JsonTypesIT.java
@Test(expected = ClassNotFoundException.class) public void simpleTypeAtRootProducesNoJavaTypes() throws ClassNotFoundException { ClassLoader resultsClassLoader = generateAndCompile("/json/simpleTypeAsRoot.json", "com.example", config("sourceType", "json")); resultsClassLoader.loadClass("com.example.SimpleTypeAsRoot"); }
From source file:de.fuberlin.wiwiss.r2r.FunctionFactoryLoader.java
private FunctionFactory loadFunctionFactory(String functionFactoryClass, ClassLoader classLoader) throws InstantiationException, IllegalAccessException { try {//from w w w. j a v a 2s . c o m return (FunctionFactory) classLoader.loadClass(functionFactoryClass).newInstance(); } catch (ClassNotFoundException e) { // Not found, return null return null; } }
From source file:com.googlecode.jsonschema2pojo.integration.json.RealJsonExamplesIT.java
@Test public void torrentProducesValidTypes() throws Exception { ClassLoader resultsClassLoader = generateAndCompile("/json/examples/torrent.json", "com.example", config("sourceType", "json", "propertyWordDelimiters", "_")); Class<?> torrentType = resultsClassLoader.loadClass("com.example.Torrent"); Object torrent = OBJECT_MAPPER.readValue(this.getClass().getResourceAsStream("/json/examples/torrent.json"), torrentType);//from ww w . jav a 2s.c o m Object props = torrentType.getMethod("getProps").invoke(torrent); Object prop0 = ((List<?>) props).get(0); assertThat((Integer) prop0.getClass().getMethod("getSeedRatio").invoke(prop0), is(1500)); }
From source file:org.jsonschema2pojo.integration.config.Moshi1IT.java
@SuppressWarnings({ "unchecked", "rawtypes" }) private void assertJsonRoundTrip(ClassLoader resultsClassLoader, String className, String jsonResource) throws ClassNotFoundException, IOException { Class generatedType = resultsClassLoader.loadClass(className); String expectedJson = IOUtils.toString(getClass().getResource(jsonResource)); JsonAdapter<Object> jsonAdapter = moshi.adapter(generatedType); Object javaInstance = jsonAdapter.fromJson(expectedJson); String actualJson = jsonAdapter.toJson(javaInstance); assertEqualsJson(expectedJson, actualJson); }
From source file:com.galeoconsulting.leonardinius.api.impl.ChainingClassLoader.java
@Override public Class<?> loadClass(String name) throws ClassNotFoundException { for (ClassLoader classloader : classLoaders) { try {//w w w .j a v a 2 s . co m return classloader.loadClass(name); } catch (ClassNotFoundException e) { // ignoring until we reach the end of the list since we are chaining } } throw new ClassNotFoundException(name); }
From source file:com.smash.revolance.ui.model.application.ApplicationFactory.java
private Class loadClass(String appDir, String appId, String impl, String version) throws ClassNotFoundException, IOException { ClassLoader loader = getLoader(appDir, appId, impl, version); if (loader == null) { loader = Thread.currentThread().getContextClassLoader(); }//from w w w . j a va 2 s . co m return loader.loadClass(impl); }