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

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

Description

Attempt to load the named class.

License

LGPL

Parameter

Parameter Description
caller The caller's class.
className The name of the class.

Exception

Parameter Description
ClassNotFoundException If the class cannot be found.

Return

The class

Declaration

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

Method Source Code

//package com.java2s;
/*/*w  ww .j  a  va  2s. c o m*/
 * JBoss, Home of Professional Open Source
 * Copyright 2006, Red Hat Middleware LLC, and individual contributors
 * as indicated by the @author tags. 
 * See the copyright.txt in the distribution for a full listing 
 * of individual contributors.
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions
 * of the GNU Lesser General Public License, v. 2.1.
 * This program is distributed in the hope that it will be useful, but WITHOUT A
 * 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,
 * v.2.1 along with this distribution; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA  02110-1301, USA.
 * 
 * (C) 2005-2006,
 * @author JBoss Inc.
 */

public class Main {
    /**
     * Attempt to load the named class.
     * @param caller The caller's class.
     * @param className The name of the class.
     * @return The class
     * @throws ClassNotFoundException If the class cannot be found.
     */
    public static Class forName(final Class caller, final String className) throws ClassNotFoundException {
        final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
        if (contextClassLoader != null) {
            try {
                return contextClassLoader.loadClass(className);
            } catch (final ClassNotFoundException cnfe) {
            } // Ignore
        }
        final ClassLoader callerClassLoader = caller.getClassLoader();
        try {
            return callerClassLoader.loadClass(className);
        } catch (final ClassNotFoundException cnfe) {
        } // Ignore
        return ClassLoader.getSystemClassLoader().loadClass(className);
    }
}

Related

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