Java Class forName forName(final String className, final Class caller)

Here you can find the source of forName(final String className, final Class caller)

Description

Load the specified class.

License

Open Source License

Parameter

Parameter Description
className The name of the class to load.
caller The class of the caller.

Exception

Parameter Description
ClassNotFoundException If the class cannot be found.

Return

The specified class.

Declaration

public static Class forName(final String className, final Class caller) throws ClassNotFoundException 

Method Source Code

//package com.java2s;
/*/*from  w  w  w  .j  a  v a2 s. c  o m*/
 * JBoss, Home of Professional Open Source
 * Copyright 2006, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

public class Main {
    /**
     * Load the specified class.
     * @param className The name of the class to load.
     * @param caller The class of the caller.
     * @return The specified class.
     * @throws ClassNotFoundException If the class cannot be found.
     */
    public static Class forName(final String className, final Class caller) throws ClassNotFoundException {
        final ClassLoader threadClassLoader = Thread.currentThread().getContextClassLoader();
        if (threadClassLoader != null) {
            try {
                return Class.forName(className, true, threadClassLoader);
            } catch (final ClassNotFoundException cnfe) {
                if (cnfe.getException() != null) {
                    throw cnfe;
                }
            }
        }

        final ClassLoader classLoader = caller.getClassLoader();
        if (classLoader != null) {
            try {
                return Class.forName(className, true, classLoader);
            } catch (final ClassNotFoundException cnfe) {
                if (cnfe.getException() != null) {
                    throw cnfe;
                }
            }
        }

        return Class.forName(className, true, ClassLoader.getSystemClassLoader());
    }
}

Related

  1. forName(Class classType, String className)
  2. forName(final Class caller, final String className)
  3. forName(final String className)
  4. forName(String className)
  5. forName(String className)
  6. forName(String className)
  7. forName(String className)