Java Class Load loadClass(String className, Class callingClass)

Here you can find the source of loadClass(String className, Class callingClass)

Description

Loads the given class using the current Thread's context class loader first otherwise use the class loader which loaded this class.

License

Open Source License

Parameter

Parameter Description
className The class to be loaded
callingClass The class which is calling this method

Return

The Loaded class

Declaration

public static Class loadClass(String className, Class callingClass) throws ClassNotFoundException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2011 MadRobot.//from  w w w  .  ja v  a2s.  c  o  m
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *  Elton Kent - initial API and implementation
 ******************************************************************************/

public class Main {
    /**
     * Loads the given class using the current Thread's context class loader
     * first
     * otherwise use the class loader which loaded this class.
     * 
     * @param className
     *            The class to be loaded
     * @param callingClass
     *            The class which is calling this method
     * @return The Loaded class
     */
    public static Class loadClass(String className, Class callingClass) throws ClassNotFoundException {
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        if (loader == null) {
            return getClassLoader(callingClass).loadClass(className);
        } else {
            return loader.loadClass(className);
        }
    }

    /**
     * Loads the given class using:
     * <ol>
     * <li>the specified classloader,</li>
     * <li>the current Thread's context class loader first, if asked</li>
     * <li>otherwise use the class loader which loaded this class.</li>
     * </ol>
     */
    public static Class loadClass(String className, ClassLoader specifiedLoader, boolean useContextLoader,
            Class callingClass) throws ClassNotFoundException {
        Class clazz = null;
        if (specifiedLoader != null) {
            try {
                clazz = specifiedLoader.loadClass(className);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        if (clazz == null && useContextLoader) {
            ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
            if (contextLoader != null) {
                try {
                    clazz = contextLoader.loadClass(className);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }
        if (clazz == null) {
            ClassLoader loader = getClassLoader(callingClass);
            clazz = loader.loadClass(className);
        }
        return clazz;
    }

    /**
     * Get the loader for the given class.
     * 
     * @param clazz
     *            the class to retrieve the loader for
     * @return the class loader that loaded the provided class
     */
    public static ClassLoader getClassLoader(Class clazz) {
        ClassLoader callersLoader = clazz.getClassLoader();
        if (callersLoader == null) {
            callersLoader = ClassLoader.getSystemClassLoader();
        }
        return callersLoader;
    }

    /**
     * Return the class loader to be used for instantiating application objects
     * when required. This is determined based upon the following rules:
     * <ul>
     * <li>The specified class loader, if any</li>
     * <li>The thread context class loader, if it exists and
     * <code>useContextClassLoader</code> is true</li>
     * <li>The class loader used to load the calling class.
     * <li>The System class loader.
     * </ul>
     */
    public static ClassLoader getClassLoader(ClassLoader specifiedLoader, boolean useContextClassLoader,
            Class callingClass) {
        if (specifiedLoader != null) {
            return specifiedLoader;
        }
        if (useContextClassLoader) {
            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
            if (classLoader != null) {
                return classLoader;
            }
        }
        return getClassLoader(callingClass);
    }

    /**
     * Return the class loader to be used for instantiating application objects
     * when a context class loader is not specified. This is determined based
     * upon the following rules:
     * <ul>
     * <li>The specified class loader, if any</li>
     * <li>The class loader used to load the calling class.
     * <li>The System class loader.
     * </ul>
     */
    public static ClassLoader getClassLoader(ClassLoader specifiedLoader, Class callingClass) {
        if (specifiedLoader != null) {
            return specifiedLoader;
        }
        return getClassLoader(callingClass);
    }
}

Related

  1. loadClass(String className)
  2. loadClass(String className)
  3. loadClass(String className)
  4. loadClass(String className)
  5. loadClass(String className, boolean isInitialized)
  6. loadClass(String classname, Class clazz)
  7. loadClass(String classname, Class clazz)
  8. loadClass(String classname, Class clazz)
  9. loadClass(String className, Class context, boolean checkParent)