The main problem of working with reflection is that, you can only refer to the properties of the given class. If the property you are looking for is declared in the base class, you have a problem, and you are forced to move iteratively throughout the class hierarchy.
This problem does not exists in any method of Reflections class.
Accessing field value
It’s also possible getting nested value:
It’s also possible setting nested value, even if the nested object is null. In this case, null object is replaced by a new object.
Of course it is also possible to set the properties of the base class of the given class.
Obtaining class elements
Obtaining all class elements
The class Reflections also includes getters for fields, annotations, constructors, methods, classes of the given object. These methods retrieve data from the hierarchy of classes, not just from the current class.
Obtaining annotated class elements
You can also get those elements that are annotated by certain annotation. To do so use one of the following methods:
For example, if you would like to get all deprecated methods of java.util.Date class you can write something like this:
Obtaining certain class elements
You can specify which elements should be returned by passing a condition. The following list consists all available methods to do so:
They might look difficult at first but the usage is really simple. For example if you are interested in list of getter methods of Hero class just write: