Retrieving the inherited attribute names/values using Java Reflection
You can procure public and non-public fields of an object and its superclasses using Java Reflection. Just stroll up the class hierarchy with getDeclaredFields
and set them accessible.
This snippet declaims every single field name and value, including ones passed down from ancestors, for the specified object (obj
). Keep an eye out for IllegalAccessException
when attempting to access fields.
Fine-tuning field retrieval
The fast answer cruises to the destination, but let's slow down and take a scenic route. Parsing the fields from classes and superclasses, and storing them for further usage.
Gathering all fields in a list
Here's a public static
method to not just print but collect fields:
The Apache Way
Save the day using FieldUtils.getAllFieldsList
from Apache Commons Lang (version 3.2+ required), to avoid the hustle:
Don't forget the Test Drive
Put your code on a test bench before letting it out in the wild. Render it immune to quirks of complex class hierarchies and make sure it fetches all the fields.
Reflexivity with Generics
By using Class<?>
in your code, you can ensure reusability and flexibility.
Avoid Redundancy
There's no need to reinvent the wheel. Reuse solutions from the Java community unless you can significantly spruce them up.
Accepting External Help
Don't shy away from external libraries like Apache Commons Lang. They reduce boilerplate code and ease reflection tasks.
Was this article helpful?