is it possible to use Java Reflection to print out the attributes of a parent class.
Asked
Active
Viewed 4,324 times
4
-
2You can navigate the class hierarchy, you can reflect class attributes, so why not? – Jim Blackler May 23 '11 at 15:11
2 Answers
12
Yes, you could do something like this:
Class<?> parentClass = getClass().getSuperclass();
Field[] fields = parentClass.getDeclaredFields();
for (Field field : fields) {
System.out.println("field: " + field.getName());
}
Method[] methods = parentClass.getDeclaredMethods();
for (Method method : methods) {
System.out.println("method: " + method.getName());
}
WhiteFang34
- 68,826
- 17
- 104
- 110
1
Given an appropriately permissive security policy, it is possible to print out any class/instance's attributes using reflection. See How to limit setAccessible to only "legitimate" uses? for some interesting discussion.
Community
- 1
- 1
Dilum Ranatunga
- 13,104
- 3
- 40
- 50