java introspector 是操作java bean 的属性 用反射比较繁琐 !
字段拥有get或者set方法后 就有了属性
Student bean = new Student();
//得到bean的所有属性
BeanInfo info = Introspector.getBeanInfo(Student.class);
//得到bean的所有属性描述器
PropertyDescriptor pds[] = info.getPropertyDescriptors();
for(PropertyDescriptor pd : pds){ //name
String propertyName = pd.getName();
if(propertyName.equals("name")){
Method m = pd.getWriteMethod(); //setName(String name)
m.invoke(bean, "flx");
}
}
System.out.println(bean.getName());
Student bean = new Student();
PropertyDescriptor pd = new PropertyDescriptor("age",bean.getClass());
Method method = pd.getWriteMethod(); //setAge(int age)
method.invoke(bean, 12);
//通过内省获取bean的age属性
method = pd.getReadMethod(); // getAge()
int age = (Integer) method.invoke(bean, null);
System.out.println(age);