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.google.gdt.eclipse.designer.smart.parser.ClassLoaderValidator.java

/**
 * Initialize SmartGWT environment./*  w  w  w  .ja  v a  2  s .c  o  m*/
 */
private static void initializeSmartGWT(final ClassLoader classLoader) throws Exception {
    // try process SmartGWT initialization routine
    try {
        // SmartGwtEntryPoint for version 2.3
        Class<?> smartGWTEntryPointClass = classLoader.loadClass("com.smartgwt.client.SmartGwtEntryPoint");
        Object smartGWTEntryPoint = smartGWTEntryPointClass.newInstance();
        ReflectionUtils.invokeMethod2(smartGWTEntryPoint, "onModuleLoad");
    } catch (ClassNotFoundException e) {
        // do nothing
    }
}

From source file:com.agimatec.validation.jsr303.util.SecureActions.java

public static Class<?> loadClass(final String className, final Class<?> caller) {
    return run(new PrivilegedAction<Class<?>>() {
        public Class<?> run() {
            try {
                ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
                if (contextClassLoader != null) {
                    return contextClassLoader.loadClass(className);
                }//from   www .ja v  a 2  s  .c o  m
            } catch (Throwable e) {
                // ignore
            }
            try {
                return Class.forName(className, true, caller.getClassLoader());
            } catch (ClassNotFoundException e) {
                throw new ValidationException("Unable to load class: " + className, e);
            }
        }
    });
}

From source file:Main.java

/**
 * Helper method to determine caller outer class.
 * <p/>/*from  ww w  .  j a  v  a 2  s.c o  m*/
 * Takes caller class and finds its top enclosing class (which is supposed to be test class).
 * 
 * @param level
 *            on which level this call is being made. 0 - call is made immediately in the method of HotSwapTool.
 * @return outer class reference
 */
private static Class<?> determineOuter(int level) {
    StackTraceElement[] stack = Thread.currentThread().getStackTrace();
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    // one for Thread#getStackTrace
    // one for #determineOuter
    // one for the caller
    String callerName = stack[level + 3].getClassName();
    try {
        Class<?> clazz = cl.loadClass(callerName);
        while (clazz.getEnclosingClass() != null) {
            clazz = clazz.getEnclosingClass();
        }
        return clazz;
    } catch (ClassNotFoundException e) {
        throw new IllegalArgumentException("Cannot find caller class: " + callerName, e);
    }
}

From source file:Loader.java

/** Load a class.
 * //from w ww  .  j  av  a 2 s . com
 * @param loadClass
 * @param name
 * @param checkParents If true, try loading directly from parent classloaders.
 * @return Class
 * @throws ClassNotFoundException
 */
public static Class loadClass(Class loadClass, String name, boolean checkParents)
        throws ClassNotFoundException {
    ClassNotFoundException ex = null;
    Class c = null;
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    while (c == null && loader != null) {
        try {
            c = loader.loadClass(name);
        } catch (ClassNotFoundException e) {
            if (ex == null)
                ex = e;
        }
        loader = (c == null && checkParents) ? loader.getParent() : null;
    }

    loader = loadClass == null ? null : loadClass.getClassLoader();
    while (c == null && loader != null) {
        try {
            c = loader.loadClass(name);
        } catch (ClassNotFoundException e) {
            if (ex == null)
                ex = e;
        }
        loader = (c == null && checkParents) ? loader.getParent() : null;
    }

    if (c == null) {
        try {
            c = Class.forName(name);
        } catch (ClassNotFoundException e) {
            if (ex == null)
                ex = e;
        }
    }

    if (c != null)
        return c;
    throw ex;
}

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

/**
 * Uninstalls the application at the specified path, by running the GUI uninstaller.
 * <p/>/*from  ww w.  ja v a 2  s . c  o  m*/
 * The uninstaller 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 guiUninstall(File uninstallJar) throws Exception {
    File copy = copy(uninstallJar);
    ClassLoader loader = getClassLoader(copy);

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

    // now run the Destroyer. Can't do it via the UninstallerFrame as can't access the uninstall button
    runDestroyer(container, loader, copy);
}

From source file:carstore.CarStore.java

static Class loadClass(String name, Object fallbackClass) throws ClassNotFoundException {
    ClassLoader loader = getCurrentLoader(fallbackClass);
    return loader.loadClass(name);
}

From source file:com.taobao.tddl.common.config.impl.DefaultConfigDataHandlerFactory.java

private static Class loadClass(String className, ClassLoader currentCL) {
    log.info("Trying to load '" + className);
    try {/* www. j  a  v a2  s  .  co m*/
        Class clazz = currentCL.loadClass(handlerClassName);
        if (clazz != null) {
            return clazz;
        }
    } catch (ClassNotFoundException e) {
        log.error("can not load the class ");
    }

    return null;

}

From source file:com.google.code.ddom.commons.cl.ClassLoaderLocal.java

private static Map<ClassLoaderLocal<?>, Object> getClassLoaderLocalMap(ClassLoader cl) {
    if (cl == ClassLoaderLocal.class.getClassLoader()) {
        return ClassLoaderLocalMapHolder.locals;
    } else {//from  w ww .  j a v a  2 s  . c o  m
        Class<?> holderClass;
        try {
            holderClass = cl.loadClass(holderClassName);
            if (holderClass.getClassLoader() != cl) {
                holderClass = null;
            }
        } catch (ClassNotFoundException ex) {
            holderClass = null;
        }
        if (holderClass == null) {
            try {
                holderClass = (Class<?>) defineClassMethod.invoke(cl, holderClassName, holderClassDef, 0,
                        holderClassDef.length);
            } catch (Exception ex) {
                // TODO: can we get here? maybe if two threads call getClassLoaderLocalMap concurrently?
                throw new Error(ex);
            }
        }
        Field field;
        try {
            field = holderClass.getDeclaredField("locals");
        } catch (NoSuchFieldException ex) {
            throw new NoSuchFieldError(ex.getMessage());
        }
        try {
            return (Map<ClassLoaderLocal<?>, Object>) field.get(0);
        } catch (IllegalAccessException ex) {
            throw new Error(ex);
        }
    }
}

From source file:com.qualogy.qafe.business.integration.adapter.MappingAdapter.java

private static Object adaptToJavaObject(String mappingId, String attributeName, Object result,
        Object valueToAdapt, ClassLoader classLoader) {
    try {/*from  ww w  .j a  va 2  s. c om*/
        if (valueToAdapt != null) {
            Class resultClazz = classLoader.loadClass(result.getClass().getName());

            Field field = resultClazz.getDeclaredField(attributeName);
            field.setAccessible(true);
            String fieldName = field.getType().getName();
            String valueName = valueToAdapt.getClass().getName();
            logger.info(field.getType().getName());

            if (!fieldName.equals(valueName)) {
                if (PredefinedClassTypeConverter.isPredefined(field.getType()))
                    valueToAdapt = PredefinedAdapterFactory.create(field.getType()).convert(valueToAdapt);
                else
                    throw new IllegalArgumentException("Object passed [" + valueToAdapt
                            + "] cannot be converted to type wanted[" + field.getType() + "] for field["
                            + field.getName() + "] in adapter[" + mappingId + "]");
            } else {
                if (!PredefinedClassTypeConverter.isPredefined(field.getType())) {
                    Class paramClazz = classLoader.loadClass(fieldName);
                    valueToAdapt = paramClazz.cast(valueToAdapt);
                }
            }
            field.set(result, valueToAdapt);
        }
    } catch (IllegalArgumentException e) {
        throw new UnableToAdaptException(
                "arg [" + valueToAdapt + "] is illegal for field with name [" + attributeName + "]", e);
    } catch (SecurityException e) {
        throw new UnableToAdaptException(e);
    } catch (IllegalAccessException e) {
        throw new UnableToAdaptException(
                "field [" + attributeName + "] does not accessible on class [" + result.getClass() + "]", e);
    } catch (NoSuchFieldException e) {
        logger.log(Level.WARNING,
                "field [" + attributeName + "] does not exist on class [" + result.getClass() + "]", e);
    } catch (ClassNotFoundException e) {
        throw new UnableToAdaptException(
                "field [" + attributeName + "] class not found [" + result.getClass() + "]", e);
    }
    return result;
}

From source file:com.googlecode.jsonschema2pojo.integration.EnumIT.java

@BeforeClass
@SuppressWarnings("unchecked")
public static void generateAndCompileEnum() throws ClassNotFoundException {

    ClassLoader resultsClassLoader = generateAndCompile("/schema/enum/typeWithEnumProperty.json", "com.example",
            config("propertyWordDelimiters", "_"));

    parentClass = resultsClassLoader.loadClass("com.example.TypeWithEnumProperty");
    enumClass = (Class<Enum>) resultsClassLoader.loadClass("com.example.TypeWithEnumProperty$EnumProperty");

}