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 aMethod
. - I played around with
BeanInfo
retrieved byIntrospector.getBeanInfo(Class<?> beanClass)
. It has a methodgetPropertyDescriptors()
which returns an array ofPropertyDescriptor
, and eachPropertyDescriptor
allows retrieving the getters and setters viagetReadMethod()
andgetWriteMethod()
. But there seems to be no connection to aField
. Also, it seems impossible to get a certainPropertyDescriptor
(e.g. retrieved by giving a field name as argument); so even IF there is a way to get theField
from thePropertyDescriptor
, I would need to loop through allPropertyDescriptors
. 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