package reflect;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class ReflectCase {
private static final String PACKAGE_NAME = "reflect.Test";
public static void main(String[] args) {
ReflectCase r = new ReflectCase();
Object obj1 = r.initObject(PACKAGE_NAME, null, null);
Class[] clazzs = { int.class, String.class };
Object[] objs = { 5, "66" };
Object obj2 = r.initObject(PACKAGE_NAME, clazzs, objs);
r.initMethod(obj2, "print", null, null);
Class[] methodClazzs = { int.class, int.class };
Object[] methodValues = { 6, 7 };
r.initMethod(obj2, "print", methodClazzs, methodValues);
}
public Class loadClass(String packgeName) throws ClassNotFoundException {
return Class.forName(packgeName);
}
public void initMethod(Object obj, String methodName, Class[] methodClazzs, Object[] methodVals) {
try {
getMethod(loadClass(PACKAGE_NAME), obj, methodName, methodClazzs, methodVals);
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public Object initObject(String packgeName, Class[] clazzName,
Object[] clazzValues) {
try {
return getConstructor(loadClass(packgeName), clazzName, clazzValues);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return null;
}
private Object getConstructor(Class clazz, Class[] clsName,
Object[] clsValues) throws SecurityException,
NoSuchMethodException, IllegalArgumentException,
InstantiationException, IllegalAccessException,
InvocationTargetException {
Object obj = null;
if (null == clsName) {
Constructor con = clazz.getDeclaredConstructor();
con.setAccessible(true);
obj = con.newInstance();
} else {
Constructor con = clazz.getDeclaredConstructor(clsName);
con.setAccessible(true);
obj = con.newInstance(clsValues);
}
return obj;
}
private void getMethod(Class clazz, Object obj, String methodName, Class[] methodClazzs, Object[] clazzValues) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
Method method = null;
if(null == clazzValues){
method = clazz.getDeclaredMethod(methodName);
method.setAccessible(true);
method.invoke(obj);
}else{
method = clazz.getDeclaredMethod(methodName, methodClazzs);
method.setAccessible(true);
method.invoke(obj, clazzValues);
}
}
}
class Test {
public Test() {
System.out.println("Constructor Test");
}
public Test(int t, String a) {
System.out.println("Constructor Test(" + t + "," + a + ")");
}
public void print() {
System.out.println("Method print()");
}
public void print(int a, int b) {
System.out.println("Method print("+a+","+ b+")");
}
}
运行结果:
Constructor Test
Constructor Test(5,66)
Method print()
Method print(6,7)