java的反射原理:
测试类:
package com.jlee.test;
public class MyBean {
private Integer id ;
private String name ;
private Long amount ;
private boolean isRight ;
private double num ;
private static String clazzProperty ;
public static String getClazzProperty() {
return clazzProperty;
}
public static void setClazzProperty(String clazzProperty) {
MyBean.clazzProperty = clazzProperty;
}
private MyTest myTest ;
public MyTest getMyTest() {
return myTest;
}
public void setMyTest(MyTest myTest) {
this.myTest = myTest;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getAmount() {
return amount;
}
public void setAmount(Long amount) {
this.amount = amount;
}
public boolean getIsRight() {
return isRight;
}
public void setIsRight(boolean isRight) {
this.isRight = isRight;
}
public double getNum() {
return num;
}
public void setNum(double num) {
this.num = num;
}
public static String staticMethod(String arg){
return "执行类方法......." + arg;
}
@Override
public String toString() {
return "bean的值 id:"+this.getId()+" name:"+this.getName()
+" num:"+this.getNum()+" amount:"+this.getAmount()
+ " isRight:" + this.getIsRight()+" 类属性:"+this.getClazzProperty();
}
}
反射实现类:
package com.jlee.test;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import com.jlee.test.MyBean;
/**
*
* @author JLee
* @Date 2010-3-2
*/
public class TestReflaction {
public static void main(String[] args)throws Exception {
MyBean bean = new MyBean();
bean.setAmount(Long.parseLong("123123"));
bean.setId(2);
bean.setIsRight(false);
bean.setName("jlee01");
bean.setNum(1);
System.out.println("修改前:"+bean.toString());
TestReflaction t = new TestReflaction();
t.mf1(bean, "name", "jlee02");
t.mf1(bean, "num", 2) ;
System.out.println("第一次修改:"+bean.toString());
t.mf2("com.jlee.test.MyBean", "clazzProperty", "static");
System.out.println("第二次修改:"+bean.toString());
Class[] argTypes = new Class[]{String.class};
Object[] args1 = new Object[]{"jlee03"};
t.mf3(bean, "setName", argTypes, args1);
System.out.println("第三次修改:"+bean.toString());
String str = t.mf4("com.jlee.test.MyBean", "staticMethod", new Class[]{String.class}, new Object[]{"结束"});
System.out.println(str);
}
//直接操作对象属性
public void mf1(Object o , String fieldName ,Object newValue)throws Exception {
Class c = o.getClass() ;
Field f = c.getDeclaredField(fieldName);
f.setAccessible(true);
f.set(o, newValue);
}
//直接操作类(Static)属性
public void mf2(String className, String fieldName ,Object newValue)throws Exception {
Class c = Class.forName(className) ;
Field f = c.getDeclaredField(fieldName);
f.setAccessible(true);
Object fv = f.get(c);
f.set(c, newValue);
}
//调用对象成员方法
public void mf3(Object o,String methodName,Class[] argTypes,Object[] args)throws Exception{
Class c = o.getClass() ;
Method m = c.getDeclaredMethod(methodName, argTypes);
Object result = m.invoke(o, args);
}
//调用类成员方法(static的方法)
public String mf4(String className,String methodName,Class[] argTypes,Object[] args)throws Exception{
Class c = Class.forName(className);
Method m = c.getDeclaredMethod(methodName, argTypes);
Object result = m.invoke(null, args);
return result.toString() ;
}
}