I’m using this code to get all fields associated with an Object. The Object in question has three super classes, and other classes extend from this object.
for (Field f : this.getClass().getFields()){
try{
fieldName = f.getName();
fieldType = f.getType().toString();
//Do more stuf
}
I do not want to get the fields associated with the super classes, but I want all fields in the current class and all those that may extend from it.
How do I filter out which fields are the ones I need?
For each
Fieldyou can usegetDeclaringClass()to determine the class that declared the field, so if you have a hierarchy likeand you want only fields declared in
C,DorE, then you can sayIf
thisis an instance ofCyou’ll get just the public fields ofC, ifthisis aDyou’ll get the public fields ofCandD, ifthisis anAorByou’ll get no fields at all.