Java Class Load classForName(String name)

Here you can find the source of classForName(String name)

Description

Perform resolution of a class name.

License

Open Source License

Parameter

Parameter Description
name The class name

Exception

Parameter Description
ClassNotFoundException From Class#forName(String).

Return

The class reference.

Declaration

@SuppressWarnings("rawtypes")
public static Class classForName(String name) throws ClassNotFoundException 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from w w  w.j a  v  a  2  s  . c o  m*/
     * <p>Perform resolution of a class name.<p/>
     *
     * @param name The class name
     * @return The class reference.
     * @throws ClassNotFoundException From {@link Class#forName(String)}.
     */
    @SuppressWarnings("rawtypes")
    public static Class classForName(String name) throws ClassNotFoundException {
        try {
            ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
            if (contextClassLoader != null) {
                return contextClassLoader.loadClass(name);
            }
        } catch (Throwable ignore) {

        }
        return Class.forName(name);
    }

    /**
     * Perform resolution of a class name.
     * <p/>
     * Here we first check the context classloader, if one, before delegating to
     * {@link Class#forName(String, boolean, ClassLoader)} using the caller's classloader
     *
     * @param name The class name
     * @param caller The class from which this call originated (in order to access that class's loader).
     * @return The class reference.
     * @throws ClassNotFoundException From {@link Class#forName(String, boolean, ClassLoader)}.
     */
    @SuppressWarnings("rawtypes")
    public static Class classForName(String name, Class caller) throws ClassNotFoundException {
        try {
            ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
            if (contextClassLoader != null) {
                return contextClassLoader.loadClass(name);
            }
        } catch (Throwable ignore) {
        }
        return Class.forName(name, true, caller.getClassLoader());
    }
}

Related

  1. classForName(String name)
  2. classForName(String name)
  3. classForName(String name)
  4. classForName(String name)
  5. classForName(String name)
  6. classForName(String name)
  7. classForName(String name, ClassLoader classLoader)
  8. classForName(String str)
  9. classForName(String typeName)