Reflecting outer class

Given an inner class object, how do you use reflection to obtain a reference to the outer class? this$0.

  Field parent = inner.getClass().getDeclaredField("this$0");
  Object outer = parent.get(inner);

If it is a nested inner class, increment the 0.

Leave a Reply

Your email address will not be published. Required fields are marked *