Java examples for Reflection:Class Loader
get Class Instance
//package com.java2s; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; public class Main { public static Object getClassInstance(String className, Class[] paramTypes, Object[] params) throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { Object obj = null;//from w w w . j av a 2s. c o m Class actionClassObj; actionClassObj = Class.forName(className); Constructor cons = actionClassObj.getConstructor(paramTypes); obj = cons.newInstance(params); return obj; } public static <T> T getClassInstance(String className, Class[] paramTypes, Object[] params, Class<T> c) throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { Object obj = null; Class actionClassObj; actionClassObj = Class.forName(className); Constructor cons = actionClassObj.getConstructor(paramTypes); obj = cons.newInstance(params); return (T) obj; } public static Object getClassInstance(String className, Class[] paramTypes, Object[] params, ClassLoader customClassLoader) throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { Object obj = null; Class actionClassObj; actionClassObj = customClassLoader.loadClass(className);//, false, customClassLoader); Constructor cons = actionClassObj.getConstructor(paramTypes); obj = cons.newInstance(params); return obj; } }