-------------------------TestReflection01.java-------------------------
-------------------------TestReflection02.java-------------------------
-------------------------TestReflection03.java-------------------------
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Scanner;
@SuppressWarnings("unchecked")
public class TestReflection01 {
public static void main(String[] args) {
System.out.println("请输入类型名称:");
Scanner scan = new Scanner(System.in);
String className = scan.next();
System.out.println("当前类型:" + className);
try {
//获取Class对象
Class c = Class.forName(className);
//解析属性信息
Field[] fs = c.getDeclaredFields();
for (Field f : fs) {
System.out.println("----------------------------------------------");
System.out.println("属性:" + f.toString());
System.out.println("\t数据类型:" + f.getType());
System.out.println("\t属性名:" + f.getName());
int mod = f.getModifiers();
System.out.println("\t属性修饰符:" + Modifier.toString(mod));
}
//解析方法信息
Method[] ms = c.getDeclaredMethods();
for(Method m : ms){
System.out.println("----------------------------------------------");
System.out.println("方法:" + m.toString());
System.out.println("\t方法名:" + m.getName());
int mod = m.getModifiers();
System.out.println("\t方法修饰符:" + Modifier.toString(mod));
System.out.print("\t方法参数列表:");
Class pts[] = m.getParameterTypes();
for (int i = 0; i < pts.length; i++) {
Class class1 = pts[i];
if(i != 0 )
System.out.print(", ");
System.out.print(class1);
}
System.out.println();
System.out.println("\t返回值类型:" + m.getReturnType());
}
//解析构造方法信息
Constructor[] cs = c.getDeclaredConstructors();
for (Constructor con : cs) {
System.out.println("----------------------------------------------");
System.out.println("构造方法:" + con.toString());
System.out.println("\t方法名:" + con.getName());
int mod = con.getModifiers();
System.out.println("\t方法修饰符:" + Modifier.toString(mod));
System.out.print("\t方法参数列表:");
Class pts[] = con.getParameterTypes();
for (int i = 0; i < pts.length; i++) {
Class class1 = pts[i];
if(i != 0 )
System.out.print(", ");
System.out.print(class1);
}
System.out.println();
}
System.out.println("----------------------------------------------");
//解析当前类类型的父类
Class superClass = c.getSuperclass();
System.out.println("当前类的父类:" + superClass.toString());
System.out.println("----------------------------------------------");
//解析当前类实现的接口
Class[] interfaces = c.getInterfaces();
System.out.print("当前类所实现接口:");
for (Class class1 : interfaces) {
System.out.print(class1.toString() + "\t");
}
System.out.println("\n----------------------------------------------");
//解析当前类型所在包信息
Package p = c.getPackage();
System.out.println("当前类所在包" + p.toString());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
-------------------------TestReflection02.java-------------------------
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import p2.User;
@SuppressWarnings("unchecked")
public class TestReflection02 {
public static void main(String[] a) throws Exception{
User p1 = new User("张三",18);
TestReflection02 t = new TestReflection02();
System.out.println("----------------------------------------");
t.mf1(p1,"name","李四");
t.mf1(p1,"age",30);
System.out.println(p1);
System.out.println("----------------------------------------");
t.mf2("p2.User", "total",88);
System.out.println("----------------------------------------");
Class[] argTypes = {String.class,int.class};
Object[] args = new Object[]{"王五",99};
t.mf3(p1, "setAll",argTypes,args);
System.out.println(p1);
System.out.println("----------------------------------------");
t.mf4("p2.User", "showTotal",null,null);
}
//直接操作对象属性
public void mf1(Object o,String fieldName,Object newValue) throws NoSuchFieldException, IllegalAccessException {
Class c = o.getClass();
Field f = c.getField(fieldName);
Object fv = f.get(o);
System.out.print("修改前:" + fieldName + "=" + fv);
f.set(o,newValue);
System.out.println("\t修改后:" + fieldName + "=" + f.get(o));
}
//直接操作类属性
public void mf2(String className,String fieldName,Object newValue) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
Class c = Class.forName(className);
Field f = c.getField(fieldName);
Object fv = f.get(c);
System.out.print("修改前:" + fieldName + "=" + fv);
f.set(c,newValue);
System.out.println("\t修改后:" + fieldName + "=" + f.get(c));
}
//调用对象成员方法
public void mf3(Object o,String methodName,Class[]argTypes,Object[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
Class c = o.getClass();
Method m = c.getMethod(methodName, argTypes);
Object result = m.invoke(o, args);
System.out.println(result);
}
//调用类成员方法
public void mf4(String className,String methodName,Class[]argTypes,Object[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
Class c = Class.forName(className);
Method m = c.getMethod(methodName, argTypes);
Object result = m.invoke(null, args);
System.out.println("result:" + result);
}
}
-------------------------TestReflection03.java-------------------------
/**
*
*/
import java.lang.reflect.*;
@SuppressWarnings("unchecked")
public class TestReflection03 {
/**
* @param args
*/
public static void main(String[] args) {
Class c;
try {
c = Class.forName("p1.Person");
//调用有参数的构造方法
Class[] argTypes = {String.class,int.class};
Constructor constructor = c.getConstructor(argTypes);
Object obj = constructor.newInstance("中国",5000);
System.out.println(obj);
//调用无参构造方法1
constructor = c.getConstructor();
//constructor = c.getConstructor(new Class[0]);
//constructor = c.getConstructor(null);
obj = constructor.newInstance();
System.out.println(obj);
//调用无参构造方法2
obj = c.newInstance();
System.out.println(obj);
} catch (Exception e) {
e.printStackTrace();
}
}
}