Java Class Load loadClass(String theClassName, Class theReferrer)

Here you can find the source of loadClass(String theClassName, Class theReferrer)

Description

Try loading a class first by using the context class loader or by using the classloader of the referrer class if the context classloader failed to load the class.

License

Apache License

Parameter

Parameter Description
theClassName the name of the test class
theReferrer the class will be loaded using the classloader which has loaded this referrer class

Return

the class object the test class to call

Declaration

public static Class loadClass(String theClassName, Class theReferrer) throws ClassNotFoundException 

Method Source Code

//package com.java2s;
/* /* w  w w.  j a v a 2 s  . co  m*/
 * ========================================================================
 * 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * ========================================================================
 */

public class Main {
    /**
     * Try loading a class first by using the context class loader or by using
     * the classloader of the referrer class if the context classloader failed
     * to load the class.
     *
     * @param theClassName the name of the test class
     * @param theReferrer the class will be loaded using the classloader which
     *        has loaded this referrer class
     * @return the class object the test class to call
     * @exception ClassNotFoundException if the class cannot be loaded through
     *            either classloader
     */
    public static Class loadClass(String theClassName, Class theReferrer) throws ClassNotFoundException {
        // Get the class to call and build an instance of it.
        Class clazz = null;

        try {
            // try loading from webapp classloader first
            clazz = loadClassFromWebappClassLoader(theClassName, theReferrer);
        } catch (Throwable internalException) {
            // Then try first from Context class loader so that we can put the
            // Cactus jar as an external library.
            clazz = loadClassFromContextClassLoader(theClassName);
        }

        return clazz;
    }

    /**
     * Try loading class using the Webapp class loader.
     *
     * @param theClassName the class to load
     * @param theReferrer the class will be loaded using the classloader which
     *        has loaded this referrer class
     * @return the <code>Class</code> object for the class to load
     * @exception ClassNotFoundException if the class cannot be loaded through
     *            this class loader
     */
    public static Class loadClassFromWebappClassLoader(String theClassName, Class theReferrer)
            throws ClassNotFoundException {
        return Class.forName(theClassName, true, theReferrer.getClassLoader());
    }

    /**
     * Try loading class using the Context class loader.
     *
     * @param theClassName the class to load
     * @return the <code>Class</code> object for the class to load
     * @exception ClassNotFoundException if the class cannot be loaded through
     *            this class loader
     */
    public static Class loadClassFromContextClassLoader(String theClassName) throws ClassNotFoundException {
        return Class.forName(theClassName, true, Thread.currentThread().getContextClassLoader());
    }
}

Related

  1. loadClass(String name)
  2. loadClass(String name)
  3. loadClass(String name, ClassLoader cl)
  4. loadClass(String name, ClassLoader ldr)
  5. loadClass(String name, ClassLoader loader)
  6. loadClass(T instance)
  7. loadClassByName(final String className, final ClassLoader loader)
  8. loadClassByName(String className)
  9. loadClassElseThrow(final String enumType)