Example usage for java.lang ClassLoader loadClass

List of usage examples for java.lang ClassLoader loadClass

Introduction

In this page you can find the example usage for java.lang ClassLoader loadClass.

Prototype

public Class<?> loadClass(String name) throws ClassNotFoundException 

Source Link

Document

Loads the class with the specified binary name.

Usage

From source file:com.izforge.izpack.integration.UninstallHelper.java

/**
 * Uninstalls the application at the specified path, by running the {@link Destroyer} in the supplied uninstall
 * jar, using the console uninstaller container.
 * <p/>//  w w  w .j av  a 2s .  com
 * The Destroyer is launched in an isolated class loader as it locates resources using its class loader.
 * This also ensures it has all the classes it needs to run.
 *
 * @param uninstallJar the uninstall jar
 * @throws Exception for any error
 */
public static void consoleUninstall(File uninstallJar) throws Exception {
    File copy = copy(uninstallJar);
    ClassLoader loader = getClassLoader(copy);

    // create the container
    @SuppressWarnings("unchecked")
    Class<ConsoleUninstallerContainer> containerClass = (Class<ConsoleUninstallerContainer>) loader
            .loadClass(ConsoleUninstallerContainer.class.getName());
    Object container = containerClass.newInstance();

    runDestroyer(container, loader, copy);
}

From source file:ReflectionUtils.java

/**
 * beanClassAnnotation//from   w ww.  j  a  v a2 s.com
 * 
 * @return
 */
public static boolean isAnnotationPresent(String beanClass, Class annotation) {
    ClassLoader classLoader = annotation.getClassLoader();
    Class clz = null;
    try {
        clz = classLoader.loadClass(beanClass);
    } catch (ClassNotFoundException e) {
        logger.warn("" + beanClass);
        return false;
    }
    return clz.isAnnotationPresent(annotation);
}

From source file:com.cprassoc.solr.auth.util.Utils.java

public static RequestHandler getRequestHandler(String className) {
    RequestHandler handler = null;//from ww w .j  a  v a 2 s.  com
    try {
        // Create a new JavaClassLoader

        ClassLoader classLoader = Utils.class.getClassLoader();

        // Load the target class using its binary name

        Class loadedMyClass = classLoader.loadClass(className);

        System.out.println("Loaded class name: " + loadedMyClass.getName());

        // Create a new instance from the loaded class

        Constructor constructor = loadedMyClass.getConstructor();

        Object myClassObject = constructor.newInstance();
        handler = (RequestHandler) myClassObject;

    } catch (Exception e) {
        e.printStackTrace();
    }
    return handler;
}

From source file:com.cprassoc.solr.auth.util.Utils.java

public static Handler getHandler(String className) {
    Handler handler = null;// www .  ja  va 2  s.  c o  m
    try {
        // Create a new JavaClassLoader

        ClassLoader classLoader = Utils.class.getClassLoader();

        // Load the target class using its binary name

        Class loadedMyClass = classLoader.loadClass(className);

        System.out.println("Loaded class name: " + loadedMyClass.getName());

        // Create a new instance from the loaded class

        Constructor constructor = loadedMyClass.getConstructor();

        Object myClassObject = constructor.newInstance();
        handler = (Handler) myClassObject;

    } catch (Exception e) {
        e.printStackTrace();
    }
    return handler;
}

From source file:com.oltpbenchmark.util.ClassUtil.java

/**
 * /* w w w . jav  a 2s.c  o m*/
 * @param class_name
 * @return
 */
public static Class<?> getClass(String class_name) {
    Class<?> target_class = null;
    try {
        ClassLoader loader = ClassLoader.getSystemClassLoader();
        target_class = (Class<?>) loader.loadClass(class_name);
    } catch (Exception ex) {
        throw new RuntimeException("Failed to retrieve class for " + class_name, ex);
    }
    return (target_class);

}

From source file:hm.binkley.util.ServiceBinder.java

@SuppressWarnings("unchecked")
private static <T> Class<? extends T> loadClass(final Class<T> service, final ClassLoader classLoader,
        final URL config, final String className) {
    try {/*w  ww  .j  a v a2 s .  c o m*/
        return (Class<? extends T>) classLoader.loadClass(className);
    } catch (final ClassNotFoundException e) {
        return fail(service, config, "Cannot bind implementation for " + className, e);
    }
}

From source file:com.google.api.server.spi.tools.GetDiscoveryDocAction.java

private static Class<?>[] loadClasses(ClassLoader classLoader, List<String> classNames)
        throws ClassNotFoundException {
    Class<?>[] classes = new Class<?>[classNames.size()];
    for (int i = 0; i < classNames.size(); i++) {
        classes[i] = classLoader.loadClass(classNames.get(i));
    }//from w ww .ja  v  a2s.c  o  m
    return classes;
}

From source file:ClassUtils.java

/**
 * Replacement for <code>Class.forName()</code> that also returns Class instances
 * for primitives (like "int") and array class names (like "String[]").
 * @param name the name of the Class/*from www.  j  av  a  2  s . c  o m*/
 * @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 {

    Class clazz = resolvePrimitiveClassName(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
    int internalArrayMarker = name.indexOf(INTERNAL_ARRAY_PREFIX);
    if (internalArrayMarker != -1 && name.endsWith(";")) {
        String elementClassName = null;
        if (internalArrayMarker == 0) {
            elementClassName = name.substring(INTERNAL_ARRAY_PREFIX.length(), name.length() - 1);
        } else if (name.startsWith("[")) {
            elementClassName = name.substring(1);
        }
        Class elementClass = forName(elementClassName, classLoader);
        return Array.newInstance(elementClass, 0).getClass();
    }

    ClassLoader classLoaderToUse = classLoader;
    if (classLoaderToUse == null) {
        classLoaderToUse = getDefaultClassLoader();
    }
    return classLoaderToUse.loadClass(name);
}

From source file:org.jsonschema2pojo.integration.ArrayIT.java

@BeforeClass
public static void generateAndCompileClass() throws ClassNotFoundException {

    ClassLoader resultsClassLoader = classSchemaRule
            .generateAndCompile("/schema/array/typeWithArrayProperties.json", "com.example");

    classWithArrayProperties = resultsClassLoader.loadClass("com.example.TypeWithArrayProperties");

}

From source file:net.solarnetwork.node.util.ClassUtils.java

/**
 * Load a class of a particular type./*from  ww  w.  ja  v  a  2s  . c o  m*/
 * 
 * <p>
 * This uses the {@code type}'s ClassLoader to load the class. If that is
 * not available, it will use the current thread's context class loader.
 * </p>
 * 
 * @param <T>
 *        the desired interface type
 * @param className
 *        the class name that implements the interface
 * @param type
 *        the desired interface
 * @return the class
 */
public static <T> Class<? extends T> loadClass(String className, Class<T> type) {
    try {
        ClassLoader loader = type.getClassLoader();
        if (loader == null) {
            loader = Thread.currentThread().getContextClassLoader();
        }
        Class<?> clazz = loader.loadClass(className);
        if (!type.isAssignableFrom(clazz)) {
            throw new RuntimeException("Class [" + clazz + "] is not a [" + type + ']');
        }
        return clazz.asSubclass(type);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException("Unable to load class [" + className + ']', e);
    }
}