Spring has a nice Utility class ReflectionUtils that does just that: it defines a method to loop over all fields of all super classes with a callback: ReflectionUtils.doWithFields()
Documentation:
Invoke the given callback on all fields in the target class, going up the class hierarchy to get all declared fields.Parameters:
- clazz - the target class to analyze
- fc - the callback to invoke for each field
- ff - the filter that determines the fields to apply the callback to
Sample Code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ReflectionUtils.doWithFields(RoleUnresolvedList.class, | |
new FieldCallback(){ | |
@Override | |
public void doWith(final Field field) throws IllegalArgumentException, | |
IllegalAccessException{ | |
System.out.println("Found field " + field + " in type " | |
+ field.getDeclaringClass()); | |
} | |
}, | |
new FieldFilter(){ | |
@Override | |
public boolean matches(final Field field){ | |
final int modifiers = field.getModifiers(); | |
// no static fields please | |
return !Modifier.isStatic(modifiers); | |
} | |
}); |
Output:
Found field private int java.util.ArrayList.size in type class java.util.ArrayList
0 comments:
Post a Comment