mardi 14 août 2018

get associated getter/setter of field (member variable)

I have a Field f of some class MyClass and I want to retrieve the associated getter/setter, if it exists. How do I do this?

  • I checked the methods of Field, but there is not even one that returns a Method.
  • I played around with BeanInfo retrieved by Introspector.getBeanInfo(Class<?> beanClass). It has a method getPropertyDescriptors() which returns an array of PropertyDescriptor, and each PropertyDescriptor allows retrieving the getters and setters via getReadMethod() and getWriteMethod(). But there seems to be no connection to a Field. Also, it seems impossible to get a certain PropertyDescriptor (e.g. retrieved by giving a field name as argument); so even IF there is a way to get the Field from the PropertyDescriptor, I would need to loop through all PropertyDescriptors. This is not performant.

NOTE: I do not want to rely on naming conventions, so please do not give anwers that fiddle with the field name. A getter of a field myField that does not have the name getMyField or isMyField is still a getter, after all.





Aucun commentaire:

Enregistrer un commentaire