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");
  parent.setAccessible(true);
  Object outer = parent.get(inner);

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

Leave a Reply