Java reflection: finding fields of subclasses
I have a class hierarchy as follows: (= > indicates the subclass of "yes")
anonymous instance class => abstract class => generic abstract class
Or more succinctly:
C => B => A
When executed, "C" calls one of the "a" methods In this method in "a", I want to use reflection to find the protected field of the object defined in class "B" (so these are the fields that "C" and "B" can see, but not "a")
How do I do this with java reflection? If I add something between a & A, how can I face the future? B or B & C?
Solution
You must reuse getdeclaredfields() (via getsuperclass()) on each class in the inheritance hierarchy of the object class
But your plan sounds like a malicious violation of the concept of inheritance The best way to face the future is to avoid this kind of thing completely What do you want to do, do you think you need such a reflection prank?