Java代码:
class X {
private String aaa;
String getAaa() {
return aaa;
}
void setAaa(String aaa) {
this.aaa = aaa;
}
public static void main(String[] args) {
X x = new X();
x.setAaa("hello");
// how to populate the property names and values from instance 'x'?
Map<String, Object> nameAndValues = someUtil.getPropertyNameAndValues(x);
// it will be:
// aaa -> hello
}
}
虽然我们可以获取到所有的getter方法,通过手动反射调用,但是不知道有没有开源库已经提供了这个功能?
最佳答案
考虑使用 Introspector在 java.bean 包中
示例代码
for (PropertyDescriptor pd:
Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors())
{
pd.getReadMethod();
}
关于java - 是否有任何开源库可以获取java bean的getters的所有值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20628558/