com.sun.jdi
is a package that lets you get information about a running VM, add breakpoints, look through stack frames, etc.
How can I get the enclosing instance of another instance? For example, here is some code that creates 4 instances of the inner class Garfield.Lasagna, with two each being enclosed by a different instance of Garfield.
public class Garfield {
int belly;
public class Lasagna {
Lasagna() {belly++;}
}
public static void main(String[] args) {
Garfield g1 = new Garfield();
Lasagna l11 = g1.new Lasagna();
Lasagna l12 = g1.new Lasagna();
Garfield g2 = new Garfield();
Lasagna l21 = g2.new Lasagna();
Lasagna l22 = g2.new Lasagna();
}
}
I would imagine that com.sun.jdi.ObjectReference would have a way to get at the instance enclosing an instance, but it doesn't seem to be the case.
Or, I would try to use reflection in the debugged VM, something like java.lang.Class.getEnclosing{Class,Constructor,Method}() but I don't see any related method that applies to objects/instances.