List of usage examples for java.lang ClassLoader loadClass
public Class<?> loadClass(String name) throws ClassNotFoundException
From source file:org.jberet.support.io.MappingJsonFactoryObjectFactory.java
static void configureDeserializationProblemHandlers(final ObjectMapper objectMapper, final String deserializationProblemHandlers, final ClassLoader classLoader) throws Exception { final StringTokenizer st = new StringTokenizer(deserializationProblemHandlers, ", "); while (st.hasMoreTokens()) { final Class<?> c = classLoader.loadClass(st.nextToken()); objectMapper.addHandler((DeserializationProblemHandler) c.newInstance()); }//from w ww . j a va 2 s . co m }
From source file:com.qualogy.qafe.business.integration.adapter.MappingAdapter.java
private static Object createServiceObj(String className, ClassLoader classLoader) { Object result = null;/*from w ww.j ava2s. c om*/ try { if (className == null) throw new UnableToAdaptException("Adapter states null outputclass"); Class<?> clazz = classLoader.loadClass(className); Constructor<?> c = clazz.getConstructor(new Class[0]); c.setAccessible(true); result = c.newInstance(new Object[0]); } catch (InstantiationException e) { throw new UnableToAdaptException( "Cannot instantiate [" + className + "], hint: define a default constructor", e); } catch (IllegalAccessException e) { throw new UnableToAdaptException(e); } catch (Exception e) { throw new UnableToAdaptException(e); } return result; }
From source file:com.izforge.izpack.integration.UninstallHelper.java
/** * Runs the destroyer obtained from the supplied container. * * @param container the container//from ww w . j a v a 2 s . c om * @param loader the isolated class loader to use to load classes * @throws Exception for any error */ private static void runDestroyer(Object container, ClassLoader loader, File jar) throws Exception { Method getComponent = container.getClass().getMethod("getComponent", Class.class); // get the destroyer class @SuppressWarnings("unchecked") Class<Destroyer> destroyerClass = (Class<Destroyer>) loader.loadClass(Destroyer.class.getName()); Object destroyer = getComponent.invoke(container, destroyerClass); Method forceDelete = destroyerClass.getMethod("setForceDelete", boolean.class); forceDelete.invoke(destroyer, true); // create the Destroyer and run it Method run = destroyerClass.getMethod("run"); run.invoke(destroyer); FileUtils.deleteQuietly(jar); // probably won't delete as the class loader will still have a reference to it? }
From source file:com.hazelcast.simulator.test.TestContainer.java
private static Object getTestClassInstance(TestCase testCase) { if (testCase == null) { throw new NullPointerException(); }//from w ww.j av a2s .c om String classname = testCase.getClassname(); Object testObject; try { ClassLoader classLoader = TestContainer.class.getClassLoader(); testObject = classLoader.loadClass(classname).newInstance(); } catch (Exception e) { throw new IllegalTestException("Could not create instance of " + classname, e); } bindProperties(testObject, testCase, TestContainer.OPTIONAL_TEST_PROPERTIES); return testObject; }
From source file:com.hurence.logisland.classloading.PluginProxy.java
/** * Check whether the given class is visible in the given ClassLoader. * * @param clazz the class to check (typically an interface) * @param classLoader the ClassLoader to check against (may be {@code null}, * in which case this method will always return {@code true}) *///ww w.j av a 2s . c o m public static boolean isVisible(Class<?> clazz, ClassLoader classLoader) { if (classLoader == null) { classLoader = Thread.currentThread().getContextClassLoader(); } try { Class<?> actualClass = classLoader.loadClass(clazz.getName()); //return (clazz == actualClass); return true; // Else: different interface class found... } catch (ClassNotFoundException ex) { // No interface class found... return false; } }
From source file:ReflectUtils.java
/** * /*from w w w . j a v a2 s .c om*/ * @param className * The qualified name of the class to search for. * * @param classLoader * The class loader to use in the class lookup. * * @return True if the class is in the JVM's classpath. * */ public static boolean exists(String className, ClassLoader classLoader) { try { classLoader.loadClass(className); return true; } catch (ClassNotFoundException error) { return false; } }
From source file:org.jsonschema2pojo.integration.CustomDateTimeFormatIT.java
/** * We are going to generate the same class twice: * Once with the configuration option formatDateTimes set to TRUE * Once with the configuration option formatDateTimes set to FALSE * //from ww w. j a va2s .c o m * @throws ClassNotFoundException * @throws IOException */ @BeforeClass public static void generateClasses() throws ClassNotFoundException, IOException { // The SimpleDateFormat instances created and configured here are based on the json schema defined in customDateTimeFormat.json dateTimeMilliSecFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"); dateTimeMilliSecFormatter.setTimeZone(TimeZone.getTimeZone("UTC")); dateTimeFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); dateTimeFormatter.setTimeZone(TimeZone.getTimeZone("UTC")); dateFormatter = new SimpleDateFormat("yyyy-MM-dd"); dateFormatter.setTimeZone(TimeZone.getTimeZone("PST")); Map<String, Object> configValues = new HashMap<String, Object>(); // Generate class with config option formatDateTimes = TRUE configValues.put("formatDateTimes", Boolean.TRUE); classSchemaRule.generate("/schema/format/customDateTimeFormat.json", "com.example.config_true", configValues); // Generate class with config option formatDateTimes = FALSE configValues.put("formatDateTimes", Boolean.FALSE); classSchemaRule.generate("/schema/format/customDateTimeFormat.json", "com.example.config_false", configValues); ClassLoader loader = classSchemaRule.compile(); // Class generated when formatDateTimes = TRUE in configuration classWhenConfigIsTrue = loader.loadClass("com.example.config_true.CustomDateTimeFormat"); // Class generated when formatDateTimes = FALSE in configuration classWhenConfigIsFalse = loader.loadClass("com.example.config_false.CustomDateTimeFormat"); }
From source file:ReflectUtils.java
/** * // w w w . j a v a 2 s. c o m * @param className * The name of the class to load. * * @param classLoader * The class loader to use for class lookup. * * @return The Class representing the given class name. A RuntimeException is * thrown if the class is not found. * * @see #exists(String) * */ public static Class getClass(String className, ClassLoader classLoader) { try { return classLoader.loadClass(className); } catch (Throwable error) { // // if it failed, try the default loader, if applicable // if (_helper != null) { Class clzz = _helper.getClass(className); if (clzz != null) return clzz; } if (classLoader != _DEFAULT_CLASS_LOADER) { try { return _DEFAULT_CLASS_LOADER.loadClass(className); } catch (Throwable error2) { // // still failed - ignore this one and throw from the // original error // } } Object[] filler = { className }; String message = "JavaClassNotFound"; throw new RuntimeException(message); } }
From source file:SocketFetcher.java
/** * Return a socket factory of the specified class. *//*from ww w.j a v a 2 s. co m*/ private static SocketFactory getSocketFactory(String sfClass) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { if (sfClass == null || sfClass.length() == 0) return null; // dynamically load the class ClassLoader cl = getContextClassLoader(); Class clsSockFact = null; if (cl != null) { try { clsSockFact = cl.loadClass(sfClass); } catch (ClassNotFoundException cex) { } } if (clsSockFact == null) clsSockFact = Class.forName(sfClass); // get & invoke the getDefault() method Method mthGetDefault = clsSockFact.getMethod("getDefault", new Class[] {}); SocketFactory sf = (SocketFactory) mthGetDefault.invoke(new Object(), new Object[] {}); return sf; }
From source file:com.glaf.core.util.ClassUtils.java
public static Class<?> loadClass(String className, ClassLoader classLoader) { Class<?> cls = ClassUtils.cache.get(className); if (cls == null) { try {//w ww . ja v a2 s . c o m cls = Class.forName(className); } catch (Exception e) { } if (cls == null && classLoader != null) { try { cls = classLoader.loadClass(className); } catch (Exception e) { } } if (cls == null) { try { cls = ClassUtils.class.getClassLoader().loadClass(className); } catch (Exception e) { } } if (cls == null) { try { cls = Thread.currentThread().getContextClassLoader().loadClass(className); } catch (Exception e) { } } if (cls == null) { try { cls = ClassLoader.getSystemClassLoader().loadClass(className); } catch (Exception e) { } } if (cls == null) { } if (cls != null) { ClassUtils.cache.put(className, cls); } else { throw new RuntimeException("Unable to load class '" + className + "'"); } } return cls; }