java - How do you get the enclosing instance of an instance? (com.sun.jdi) -


com.sun.jdi package lets information running vm, add breakpoints, through stack frames, etc.

how can enclosing instance of instance? example, here code creates 4 instances of inner class garfield.lasagna, 2 each being enclosed 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 imagine com.sun.jdi.objectreference have way @ instance enclosing instance, doesn't seem case.

or, try use reflection in debugged vm, java.lang.class.getenclosing{class,constructor,method}() don't see related method applies objects/instances.

you can both access through jdi , through reflection. enclosing instance stored field of instances of inner class lasanga. automatically-generated name field this$0 (so in example above, field name has type garfield).

to access in jdi, have use referencetype of objectreference. there three relevant methods of referencetype:

  • fields() gives simple fields , these synthetic fields
  • visiblefields() additionally gives inherited fields
  • allfields() additionally gives hidden fields (and possibly repeats synthetic fields)

accessing through reflection same usual, ask field of name "this$0".

but, can't access synthetically-defined variable @ compile time, asking field this$0 cause compile-time error.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -