Java Class Load loadClass(Class contextClass, String className)

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

Description

Loads a class using the class loader.

License

Apache License

Parameter

Parameter Description
contextClass The name of a context class to use.
className The name of the class to load

Return

The class or null if no class loader could load the class.

Declaration

public static Class<?> loadClass(Class<?> contextClass, String className) 

Method Source Code

//package com.java2s;
/**/*  w ww.ja v  a  2  s.  c  o  m*/
 * Licensed to jclouds, Inc. (jclouds) under one or more
 * contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  jclouds 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 {
    /**
     * Loads a class using the class loader.
     * 1. The class loader of the context class is being used.
     * 2. The thread context class loader is being used.
     * If both approaches fail, returns null.
     *
     * @param contextClass The name of a context class to use.
     * @param className    The name of the class to load
     * @return The class or null if no class loader could load the class.
     */
    public static Class<?> loadClass(Class<?> contextClass, String className) {
        Class<?> clazz = null;
        if (contextClass.getClassLoader() != null) {
            clazz = silentLoadClass(className, contextClass.getClassLoader());
        }
        if (clazz == null && Thread.currentThread().getContextClassLoader() != null) {
            clazz = silentLoadClass(className, Thread.currentThread().getContextClassLoader());
        }
        return clazz;
    }

    /**
     * Loads a {@link Class} from the specified {@link ClassLoader} without throwing {@ClassNotFoundException}.
     *
     * @param className
     * @param classLoader
     * @return
     */
    private static Class<?> silentLoadClass(String className, ClassLoader classLoader) {
        Class<?> clazz = null;
        if (classLoader != null && className != null) {
            try {
                clazz = classLoader.loadClass(className);
            } catch (ClassNotFoundException e) {
                //Ignore and proceed to the next class loader.
            }
        }
        return clazz;
    }
}

Related

  1. getClassNames( StackTraceElement[] currentStack)
  2. loadClass(Class context, String... names)
  3. loadClass(Class sourceClass, final String className)
  4. loadClass(Class base, String name)
  5. loadClass(Class clazz)
  6. loadClass(ClassLoader classLoader, Class clazz, String postfix)
  7. loadClass(ClassLoader classLoader, String className)
  8. loadClass(ClassLoader classLoader, String className)
  9. loadClass(ClassLoader classLoader, String fullyQualifiedClassName)