PropertyDescriptors are the way to go, but Spring makes using them a lot easier if you use the BeanWrapper interface.
Here's a stupid test class:
public class Thingy{
private final String foo = "hey";
private final int bar = 123;
private final List<String> grr = Arrays.asList("1", "2", "3");
public String getFoo(){
return this.foo;
}
public int getBar(){
return this.bar;
}
public List<String> getGrr(){
return this.grr;
}
}
And here's a main method to inspect an instance of it:
public static void main(final String[] args) throws Exception{
final Thingy thingy = new Thingy();
final BeanWrapper wrapper = new BeanWrapperImpl(thingy);
for(final PropertyDescriptor descriptor : wrapper.getPropertyDescriptors()){
System.out.println(descriptor.getName() + ":"
+ descriptor.getReadMethod().invoke(thingy));
}
}
Output:
bar:123
class:class com.mypackage.Thingy
foo:hey
grr:[1, 2, 3]
Read this for reference: