Java Class Load loadClass(String className, Class ofType)

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

Description

Loads and returns the class named className of type superClass.

License

Apache License

Parameter

Parameter Description
T Type of the class
className Name of the class to load
superClass Type of the class to load

Exception

Parameter Description
IllegalArgumentException if the class with className could not be loaded orif the that class does not extend the class supplied in the superClass parameter.

Return

The loaded class of type superClass.

Declaration

@SuppressWarnings("unchecked")
public static <T> Class<? extends T> loadClass(String className, Class<T> ofType) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**//from  w  w  w.  ja  v a 2s  .co m
     * Loads and returns the class named className of type superClass.
     * 
     * @param <T> Type of the class
     * @param className Name of the class to load
     * @param superClass Type of the class to load
     * @return The loaded class of type superClass.
     * 
     * @throws IllegalArgumentException if the class with className could not be loaded or
     * if the that class does not extend the class supplied in the superClass parameter.
     **/
    @SuppressWarnings("unchecked")
    public static <T> Class<? extends T> loadClass(String className, Class<T> ofType) {
        try {
            Class<?> clazz = Class.forName(className);

            if (ofType == null || !ofType.isAssignableFrom(clazz)) {
                throw new IllegalArgumentException(
                        "Class " + className + " must extend or implement " + ofType + "!");
            }
            return (Class<? extends T>) clazz;
        } catch (ClassNotFoundException cnfe) {
            throw new IllegalArgumentException("Class " + className + " could not be found!", cnfe);
        }
    }
}

Related

  1. loadClass(String className, Class callingClass)
  2. loadClass(String classname, Class clazz)
  3. loadClass(String classname, Class clazz)
  4. loadClass(String classname, Class clazz)
  5. loadClass(String className, Class context, boolean checkParent)
  6. loadClass(String className, Class targetType, ClassLoader cl)
  7. loadClass(String className, Class type)
  8. loadClass(String className, ClassLoader cl)
  9. loadClass(String className, ClassLoader classLoader)