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.

RSS feed for comments on this post · TrackBack URL

Leave a Comment