我现在要修改当前的bean和他的父类的所有属性的值并进行修改,如果用自省的方式应该怎么实现啊~!
我现在的做法是用到下面的代码!
BeanInfo info = Introspector.getBeanInfo(beanClass, stopClass);
PropertyDescriptor[] propertys = info.getPropertyDescriptors();
但是得到了property后该怎么修改他的值啊如果是用getReadMethod()方法来获得bean 的get方法的但是怎么用invoke来调用这个方法啊??
我现在的做法是用到下面的代码!
BeanInfo info = Introspector.getBeanInfo(beanClass, stopClass);
PropertyDescriptor[] propertys = info.getPropertyDescriptors();
但是得到了property后该怎么修改他的值啊如果是用getReadMethod()方法来获得bean 的get方法的但是怎么用invoke来调用这个方法啊??
public static void main(String[] args) { try {
TestBean tb = new TestBean();
System.out.println(tb.getName());
BeanInfo bi = Introspector.getBeanInfo(tb.getClass());
PropertyDescriptor[] m_Properties = bi.getPropertyDescriptors();
Method setter = m_Properties[1].getWriteMethod(); System.out.println(setter);
//相当于tb.setxxx("no abc");
setter.invoke(tb, "no abc");
System.out.println(tb.getName());
} catch (IntrospectionException e) {
e.printStackTrace();
} catch (Exception e2) {
e2.printStackTrace();
} }
}class TestBean {
String name = "abc"; public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}