Java Class Load loadClassUsingClass(Class clazz, String name)

Here you can find the source of loadClassUsingClass(Class clazz, String name)

Description

This is a simple utility class that attempts to load the named class using the class loader of the supplied class or the class loader of one of its super classes or their implemented interfaces.

License

Apache License

Parameter

Parameter Description
svcObj the class that is the root of the search.
name the name of the class to load.

Return

the loaded class or null if it could not be loaded.

Declaration

public static Class loadClassUsingClass(Class clazz, String name) 

Method Source Code

//package com.java2s;
/* //from w w  w.j  av  a  2s  . co m
 * Copyright 2011 Karl Pauls karlpauls@gmail.com
 *
 * Licensed 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 {
    /**
     * <p>
     * This is a simple utility class that attempts to load the named class
     * using the class loader of the supplied class or the class loader of one
     * of its super classes or their implemented interfaces. This is necessary
     * during service registration to test whether a given service object
     * implements its declared service interfaces.
     * </p>
     * <p>
     * To perform this test, the framework must try to load the classes
     * associated with the declared service interfaces, so it must choose a
     * class loader. The class loader of the registering bundle cannot be used,
     * since this disallows third parties to register service on behalf of
     * another bundle. Consequently, the class loader of the service object must
     * be used. However, this is also not sufficient since the class loader of
     * the service object may not have direct access to the class in question.
     * </p>
     * <p>
     * The service object's class loader may not have direct access to its
     * service interface if it extends a super class from another bundle which
     * implements the service interface from an imported bundle or if it
     * implements an extension of the service interface from another bundle
     * which imports the base interface from another bundle. In these cases, the
     * service object's class loader only has access to the super class's class
     * or the extended service interface, respectively, but not to the actual
     * service interface.
     * </p>
     * <p>
     * Thus, it is necessary to not only try to load the service interface class
     * from the service object's class loader, but from the class loaders of any
     * interfaces it implements and the class loaders of all super classes.
     * </p>
     * 
     * @param svcObj
     *            the class that is the root of the search.
     * @param name
     *            the name of the class to load.
     * @return the loaded class or <tt>null</tt> if it could not be loaded.
     **/
    public static Class loadClassUsingClass(Class clazz, String name) {
        Class loadedClass = null;

        while (clazz != null) {
            // Get the class loader of the current class object.
            ClassLoader loader = clazz.getClassLoader();
            // A null class loader represents the system class loader.
            loader = (loader == null) ? ClassLoader.getSystemClassLoader() : loader;
            try {
                return loader.loadClass(name);
            } catch (ClassNotFoundException ex) {
                // Ignore and try interface class loaders.
            }

            // Try to see if we can load the class from
            // one of the class's implemented interface
            // class loaders.
            Class[] ifcs = clazz.getInterfaces();
            for (int i = 0; i < ifcs.length; i++) {
                loadedClass = loadClassUsingClass(ifcs[i], name);
                if (loadedClass != null) {
                    return loadedClass;
                }
            }

            // Try to see if we can load the class from
            // the super class class loader.
            clazz = clazz.getSuperclass();
        }

        return null;
    }
}

Related

  1. loadClassOrNull(String className)
  2. loadClassOrReturnNull(ClassLoader loader, String className)
  3. loadClassReflectively(ClassLoader classLoader, String fullyQualifiedClassName)
  4. loadClassSafe(final String klassName)
  5. loadClassSilently(ClassLoader cl, String name)
  6. loadClassViaContext(String fqcn)
  7. loadClassWithTCCL(String name)