propertydescriptor(propertydescriptor获取字段的注解)
使用java反射类的get方法问题,怎么解决
public void method(TblFwlx obj) {
Class clazz = obj.getClass();
Field[] fields = obj.getClass().getDeclaredFields();//获得属性
for (Field field : fields) {
PropertyDescriptor pd = new PropertyDescriptor(field.getName(),
clazz);
Method getMethod = pd.getReadMethod();//获得get方法
Object o = getMethod.invoke(obj);//执行get方法返回一个Object
}
}
java根据对象属性从对象列表中取出对象
package?test;
import?java.beans.IntrospectionException;
import?java.beans.PropertyDescriptor;
import?java.lang.reflect.InvocationTargetException;
import?java.lang.reflect.Method;
import?java.util.ArrayList;
import?java.util.Arrays;
import?java.util.Iterator;
public?class?AList?extends?ArrayListA?{
/**
?*?
?*/
private?static?final?long?serialVersionUID?=?-5626405721508120479L;
public?A?getSubject(String?name,?Object?value)
throws?IllegalAccessException,?IllegalArgumentException,
InvocationTargetException,?IntrospectionException?{
A[]?result?=?getSubjects(name,?value);
if?(result.length?=?0)
return?null;
else
return?result[0];
}
public?A[]?getSubjects(String?name,?Object?value)
throws?IntrospectionException,?IllegalAccessException,
IllegalArgumentException,?InvocationTargetException?{
PropertyDescriptor?pd?=?new?PropertyDescriptor(name,?A.class);
Method?getter?=?pd.getReadMethod();
IteratorA?it?=?this.iterator();
A[]?result?=?new?A[0];
while?(it.hasNext())?{
A?obj?=?it.next();
Object?theValue?=?getter.invoke(obj);
if?(theValue?!=?null??theValue.equals(value))?{
result?=?Arrays.copyOf(result,?result.length?+?1);
result[result.length?-?1]?=?obj;
}
}
return?result;
}
public?static?void?main(String[]?args)?throws?IllegalAccessException,
IllegalArgumentException,?InvocationTargetException,
IntrospectionException?{
AList?al?=?new?AList();
A?a?=?new?A("张三");
a.setAge(18);
a.setGender("男");
a.setSchool("XX大学");
A?b?=?new?A("李四");
b.setAge(20);
b.setGender("男");
b.setSchool("XX大学");
A?c?=?new?A("王五");
c.setAge(18);
c.setGender("女");
c.setSchool("XX大学");
al.add(a);
al.add(b);
al.add(c);
System.out.println(Arrays.toString(al.getSubjects("name",?"张三")));
System.out.println(Arrays.toString(al.getSubjects("age",?18)));
System.out.println(Arrays.toString(al.getSubjects("gender",?"男")));
System.out.println(Arrays.toString(al.getSubjects("school",?"XX大学")));
//?[A?[name=张三,?age=18,?gender=男]]
//?[A?[name=张三,?age=18,?gender=男],?A?[name=王五,?age=18,?gender=女]]
//?[A?[name=张三,?age=18,?gender=男],?A?[name=李四,?age=20,?gender=男]]
//?[A?[name=张三,?age=18,?gender=男],?A?[name=李四,?age=20,?gender=男],?A?[name=王五,?age=18,?gender=女]]
}
}
class?A?{
private?String?name;
private?int?age;
private?String?gender;
private?String?school;
public?String?getSchool()?{
return?school;
}
public?void?setSchool(String?school)?{
this.school?=?school;
}
public?A(String?name)?{
super();
this.name?=?name;
}
public?String?getGender()?{
return?gender;
}
public?void?setGender(String?gender)?{
this.gender?=?gender;
}
public?int?getAge()?{
return?age;
}
public?void?setAge(int?age)?{
this.age?=?age;
}
public?String?getName()?{
return?name;
}
public?void?setName(String?name)?{
this.name?=?name;
}
@Override
public?String?toString()?{
return?"A?[name="?+?name?+?",?age="?+?age?+?",?gender="?+?gender?+?"]";
}
}
Java有没有专门的方法把字段多的对象放到一个字段少的对象里?
spring提供了类似你需要的方法,位于其BeanUtils中:
使用BeanUtils.copyProperties()即可:
运行结果:
其实现本质有点类似java的反射机制,围绕PropertyDescriptor这个类实现,有兴趣可以读一下它的源码:
想自己写的话也可以利用java的反射机制自己写一个方法来实现。