The starting point, the Class<T> class
Tip: AHere we see an important detail. Normally name the identifiers of an instance of Class something like clazz or clz, it may seem weird, but that’s only because class is already a reserved word in the Java language.
The attributes, the Field class
|Field getField(String name)||Returns the Field that reflects to the public attribute of the class with the given name.|
|Field getFields()||Returns the array of Fields that reflect to the public attributes of the class.|
|Field getDeclaredField(String name)||Returns the Field that reflects to the declared attribute of the class with the given name.|
|Field getDeclaredFields()||Returns the array of Fields that reflect to the declared attributes of the class.|
Tip: The usage of the get method may be confusing at first, but it’s rather simple. Having aField in your hands, you send the target instance from which you want to extract the value as an argument. It is necessary to have in mind that the Field is related to the class, and not to the instance, therefore if the attribute is not static, a concrete instance is required so that a value can be extracted. In case we’re talking about a static attribute, you can pass any instance of any type, or even null as an argument.
Tip: The AccessibleObject class has a convenience static method, also named setAccessible, but it receives an array of AccessibleObject and the boolean flag. This method can be used to set multiple Fields, Methods or Constructors as accessible all in one go. In case you want to set all the attributes in a class as accessible, you can just do this:
Actions, the Method class
Object invoke(Object obj, Object... args)