mercredi 22 novembre 2023

Can I get a runnable get method from a class knowing the attribute using reflection? [duplicate]

Using reflection, following these examples, I can obtain the attributes and the methods of a class.

Now, given an object, I would like to take, for each attribute (let's say 'surname') of the given object, the associated value (the value of obj.getSurname()).

But I would like to do this at runtime, so I can't call the getSurname() getter method since I don't know that there is the 'surname' attribute.

One possibile idea is to get the attributes and methods of a class and, for each attribute, to search in the methods a method whose name starts with "get" and contains Capitalize(attribute.getName()).

Do you have any other idea to do this? Specifically, using reflection and given an attribute, can I directly obtain the getter method for that attribute?





Aucun commentaire:

Enregistrer un commentaire