17

Suppose I have a handle on an object of type , and I'm told by configuration that it has a bean property of type int with the name age. How can I retrieve the getter for this document?

Is there a better way than prepending "get" and capitalizing the "a" in age, and looking for a method of that name via reflection?

skaffman
  • 398,947
  • 96
  • 818
  • 769
C. Ross
  • 31,137
  • 42
  • 147
  • 238

1 Answers1

48

Take a look at java.beans.Introspector. This class allows you to get the list of properties on a class.

If you know property name you can call

Method getter = new PropertyDescriptor(propertyName, beanClass).getReadMethod();

See Also:

Community
  • 1
  • 1
Sergey Aslanov
  • 2,397
  • 15
  • 16