Here you can find the source of loadClass(String className, Class
Parameter | Description |
---|---|
T | Type of the class |
className | Name of the class to load |
superClass | Type of the class to load |
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. |
@SuppressWarnings("unchecked") public static <T> Class<? extends T> loadClass(String className, Class<T> ofType)
//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); } } }