Java Class Load loadClass(Class context, String... names)

Here you can find the source of loadClass(Class context, String... names)

Description

Attempt to load a class for one ore more supplied class names, returning the first one found.

License

Open Source License

Parameter

Parameter Description
context The class to use as the context (uses this class's classloader).
names One or more fully qualified class names.

Return

A Class for the first class found or null if none found.

Declaration

@SuppressWarnings("rawtypes")
public static Class<?> loadClass(Class context, String... names) 

Method Source Code

//package com.java2s;
/**/*w ww. j ava2s .c  o  m*/
 * Copyright (C) 2012 - 2013  Eric Van Dewoestine
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Attempt to load a class for one ore more supplied class names, returning
     * the first one found. Useful for handling cross eclipse version
     * compatibility.
     *
     * @param context The class to use as the context (uses this class's
     * classloader).
     * @param names One or more fully qualified class names.
     * @return A Class for the first class found or null if none found.
     */
    @SuppressWarnings("rawtypes")
    public static Class<?> loadClass(Class context, String... names) {
        for (String name : names) {
            try {
                Class<?> clazz = Class.forName(name, true, context.getClassLoader());
                return clazz;
            } catch (ClassNotFoundException cnfe) {
                // ignore, try the next name.
            }
        }
        return null;
    }
}

Related

  1. getClassHierarchyFrom(Class clazz)
  2. getClassHierarchyLeaf( Collection> classes)
  3. getClassName(String logicalName, String trailingName)
  4. getClassNameRepresentation(String name)
  5. getClassNames( StackTraceElement[] currentStack)
  6. loadClass(Class sourceClass, final String className)
  7. loadClass(Class base, String name)
  8. loadClass(Class clazz)
  9. loadClass(Class contextClass, String className)