首先需要一个被测试的类,,这个类需要有属性和方法,并且这些属性和方法需要有不同权限修饰符
package com.memorycat.aoptest.reflect;
public class Reflect1 {
private int id;
protected String myName;
public Reflect1() {
super();
System.out.println("正在执行方法:public Reflect1() <- 默认构造方法");
}
private void foo1() {
System.out.println("正在执行方法:private void foo1()");
}
public void foo2() {
System.out.println("正在执行方法:public void foo2()");
}
public void printAttr() {
System.out.println("正在执行方法: printAttr() -> [id=" + id + ", myName="
+ myName + "]");
}
int add(int num1, int num2) {
return num1 + num2;
}
int add(int num1, int num2, int num3) {
return num1 + num2 + num3;
}
}
然后,随便写一个测试类,从反射得到被测试类的各种属性和方法,并尝试去修改和调用
package com.memorycat.aoptest.reflect;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class ReflectTest {
public static void main(String[] args) throws Throwable {
Class<Reflect1> clazz = Reflect1.class;
Reflect1 reflect1 = (Reflect1) clazz.newInstance();// 调用默认构造方法
System.out.println("***********现在获取类属性***********");
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);// 设置为可以访问<-private
// 为id设置为9<-private
if ((field.getType().getCanonicalName().equals("int") || field
.getType().getCanonicalName().equals("java.lang.Integer"))
&& field.getName().equals("id")) {
field.setInt(reflect1, 9);
}
System.out.println("-----------------------");
System.out.println("属性名" + field.getName());
System.out.println("属性类型" + field.getType());
System.out.println("属性值" + field.get(reflect1));
System.out.println(field.getType() + " " + field.getName() + " = "
+ field.get(reflect1) + ";");
}
System.out.println("***********现在获取类方法***********");
Method[] declaredMethods = clazz.getDeclaredMethods();
for (Method method : declaredMethods) {
method.setAccessible(true);// 设置可以访问权限 <-private
Class[] parameterTypes = method.getParameterTypes();
System.out.println(">>执行方法:" + method);
if (parameterTypes.length == 0) {
method.invoke(reflect1, null);
} else {
// 判断int add(int num1, int num2)
if (method.getName().equals("add")
&& parameterTypes.length == 2) {
int result = (int) method.invoke(reflect1, new Object[] {
2, 7 });
System.out.println("2+7=" + result);
} else {
if (method.getName().equals("add")
&& parameterTypes.length == 3) {
System.out.println("这还有3个参数的add()");
}
}
}
}
}
}
最后输出结果
正在执行方法:public Reflect1() <- 默认构造方法 ***********现在获取类属性*********** ----------------------- 属性名id 属性类型int 属性值9 int id = 9; ----------------------- 属性名myName 属性类型class java.lang.String 属性值null class java.lang.String myName = null; ***********现在获取类方法*********** >>执行方法:int com.memorycat.aoptest.reflect.Reflect1.add(int,int) 2+7=9 >>执行方法:int com.memorycat.aoptest.reflect.Reflect1.add(int,int,int) 这还有3个参数的add() >>执行方法:private void com.memorycat.aoptest.reflect.Reflect1.foo1() 正在执行方法:private void foo1() >>执行方法:public void com.memorycat.aoptest.reflect.Reflect1.foo2() 正在执行方法:public void foo2() >>执行方法:public void com.memorycat.aoptest.reflect.Reflect1.printAttr() 正在执行方法: printAttr() -> [id=9, myName=null]