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:org.jsonschema2pojo.integration.config.PrefixSuffixIT.java

@Test
public void defaultClassSufix() throws ClassNotFoundException {

    ClassLoader resultsClassLoader = generateAndCompile("/schema/properties/primitiveProperties.json",
            "com.example");
    resultsClassLoader.loadClass("com.example.PrimitiveProperties");
}

From source file:Main.java

public static Class loadClass(String className, ClassLoader classLoader) throws ClassNotFoundException {
    try {//from  w  ww  .  j  a  va 2s.  c  o  m
        char start = className.charAt(0);
        if (start == '[') {
            return Class.forName(className);
        }
        if (className.equals("boolean")) {
            return boolean.class;
        }
        if (className.equals("byte")) {
            return byte.class;
        }
        if (className.equals("char")) {
            return char.class;
        }
        if (className.equals("short")) {
            return short.class;
        }
        if (className.equals("int")) {
            return int.class;
        }
        if (className.equals("long")) {
            return long.class;
        }
        if (className.equals("float")) {
            return float.class;
        }
        if (className.equals("double")) {
            return double.class;
        }
        if (className.equals("void")) {
            return void.class;
        }
        return Class.forName(className);
    } catch (ClassNotFoundException e) {
        return classLoader.loadClass(className);
    }
}

From source file:org.ops4j.pax.carrot.spring.SpringTestContextFixtureLoader.java

private Object createTarget(String fixtureName) {
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    try {//from   w w w.j av a 2s. co  m
        Class<?> klass = cl.loadClass(fixtureName);
        Object target = klass.newInstance();
        return target;
    } catch (ClassNotFoundException exc) {
        throw new CarrotException(exc);
    } catch (InstantiationException exc) {
        throw new CarrotException(exc);
    } catch (IllegalAccessException exc) {
        throw new CarrotException(exc);
    }
}

From source file:com.espertech.esper.util.ObjectInputStreamWithTCCL.java

@Override
public Class resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {

    if (log.isDebugEnabled()) {
        log.debug("Resolving class " + desc.getName() + " id " + desc.getSerialVersionUID() + " classloader "
                + Thread.currentThread().getContextClassLoader().getClass());
    }/*from   w  w  w . j  av a 2s .c  om*/

    ClassLoader currentTccl = null;
    try {
        currentTccl = Thread.currentThread().getContextClassLoader();
        if (currentTccl != null) {
            return currentTccl.loadClass(desc.getName());
        }
    } catch (Exception e) {
    }
    return super.resolveClass(desc);
}

From source file:com.liferay.maven.arquillian.internal.tasks.ExecuteDeployerTask.java

protected void initUtils(ClassLoader classLoader) throws Exception {

    Class<?> clazz = classLoader.loadClass("com.liferay.portal.util.EntityResolver");

    EntityResolver entityResolver = (EntityResolver) clazz.newInstance();

    SAXReaderUtil.setEntityResolver(entityResolver);
}

From source file:ResourceBundleSupport.java

/**
 * Tries to create a new instance of the given class. This is a short cut
 * for the common bean instantiation code.
 *
 * @param className the class name as String, never null.
 * @param source    the source class, from where to get the classloader.
 * @return the instantiated object or null, if an error occured.
 *//*w  ww.j a v a  2s .  com*/
public static Object loadAndInstantiate(final String className, final Class source) {
    try {
        final ClassLoader loader = getClassLoader(source);
        final Class c = loader.loadClass(className);
        return c.newInstance();
    } catch (Exception e) {
        return null;
    }
}

From source file:ResourceBundleSupport.java

/**
 * Tries to create a new instance of the given class. This is a short cut
 * for the common bean instantiation code. This method is a type-safe method
 * and will not instantiate the class unless it is an instance of the given
 * type./*from  w  w  w  .j a  v  a2s  . c  o  m*/
 *
 * @param className the class name as String, never null.
 * @param source    the source class, from where to get the classloader.
 * @param type  the type.
 * @return the instantiated object or null, if an error occurred.
 */
public static Object loadAndInstantiate(final String className, final Class source, final Class type) {
    try {
        final ClassLoader loader = getClassLoader(source);
        final Class c = loader.loadClass(className);
        if (type.isAssignableFrom(c)) {
            return c.newInstance();
        }
    } catch (Exception e) {
        return null;
    }
    return null;
}

From source file:com.liferay.arquillian.maven.internal.tasks.ExecuteDeployerTask.java

protected void initUtils(ClassLoader classLoader) throws Exception {
    Class<?> clazz = classLoader.loadClass("com.liferay.portal.util.EntityResolver");

    EntityResolver entityResolver = (EntityResolver) clazz.newInstance();

    SAXReaderUtil.setEntityResolver(entityResolver);
}

From source file:com.ocpsoft.pretty.faces.el.resolver.SeamBeanNameResolver.java

public boolean init(ServletContext servletContext, ClassLoader classLoader) {

    try {/*ww w  .  jav  a2s.  c o m*/
        // get Seam class
        Class<?> seamClass = classLoader.loadClass(SEAM_CLASS);

        // get getComponentName method
        getComponentNameMethod = seamClass.getMethod(GET_COMPONENT_NAME_METHOD, Class.class);

        // initialization completed
        if (log.isDebugEnabled()) {
            log.debug("Seam environment detected. Enabling bean name resolving via Seam.");
        }
        return true;

    } catch (ClassNotFoundException e) {
        // Will happen in enviroments without Seam
        if (log.isDebugEnabled()) {
            log.debug("Seam class has not been found. Seam resolver will be disabled.");
        }
    } catch (NoSuchMethodException e) {
        // This method is expected on the Seam class
        log.warn("Cannot find method getComponentName() on Seam class.", e);
    } catch (SecurityException e) {
        log.warn("Unable to init resolver due to security restrictions", e);
    }

    // disable resolver
    return false;

}

From source file:de.micromata.genome.gwiki.plugin.CombinedClassLoader.java

@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
    for (ClassLoader cl : parents) {
        try {//from  w ww.  jav  a 2  s  .c  om
            Class<?> cls = cl.loadClass(name);
            return cls;
        } catch (ClassNotFoundException ign) {
        } catch (NoClassDefFoundError ign2) {

        }

    }
    throw new ClassNotFoundException(name);
}