I have a class hierarchy like so: (=> means “is a subclass of”)
anonymous instance class => abstract class => generic abstract class
or more succinctly:
C => B => A
When executing, “C” calls one of “A”‘s methods. Within that method in “A”, I want to use reflection to find protected fields of the object that are defined in class “B”. (So these are fields that “C” and “B” can see, but not “A”.)
How would I do this with Java reflection? And how can I future-proof it in case I add something between A & B or B & C?
You have to use
getDeclaredFields()repeatedly on each class in the inheritance hierarchy of your object’s class (viagetSuperclass()).However, what you are planning sounds like a nasty violation of the concept of inheritance. The best way of future-proofing would be to avoid this kind of thing entirely. What are you trying to do that you think requires such reflection shenanigans?