Search code examples
eclipsedebuggingreferenceeclipse-neon

eclipse - Always view object when debugging


I have an object in a Set that has it's guts updated by reference. Is there a way I can use the object hash ID (MyObject@93efa2ed) or Eclipse ID (id=356) to always watch in in my Expressions view even when it's out-of-scope?

I am using Eclipse Neon.


Solution

  • ur question is not quite clear to me, but i guess u mean this:

    • u have an object O that is contained in a set S, that contains more objects
    • O is updated in ur code (ie. its internal properties change) and u want to keep an eye on these changes thru the expressions view while u step thru the code
    • u also want this to happen in code lines/methods that dont a have a straitforward path to O

    i dont know of any way (internal eclipse goody) that lets u ref' O by its eclipse debug ID or object hash.

    but it can be done fairly simple: u just need to be able to reach O by some (lengthy) way of references from the current frame that is selected in the debugger to accomplish this.

    the easiest way is to add some extra code to set O to some static var of some sort. U can even assign the static var while debugging manually, with the help of the "Display" view that lets u execute java code, that runs in the context of the current stack frame.

    Steps:

    1. create/add the class MyWatcher to ur code base.
    2. step thru ur code to where u get ur handle on O (eg. in the sample code, on the 5th break when adding o to the set)
    3. open the Display view and add this line MyWatcher.watches.put("a", o) (replace 'o' by the expression that references O at ur breakpoint)
    4. Execute the line with Ctrl+U or thru the context menu
    5. add this to ur "expressions" view: MyWatcher.watches.get("a")
    6. now O will remain visible at all times, eg. in the example below: even when u are in foo(), b/c the map holds ar ref on O.
    public class MyCode {
    
      public static void main(final String[] args) {
        bar();
        foo();
      }
    
      static void bar() {
        final Set<Object> set = new HashSet<>();
        for (int i = 0; i < 10; i++) {
          final List<Integer> o = Arrays.asList(i);
          set.add(o);
        }
    
      }
    
      static void foo() {
        System.out.println("bar");
      }
    }
    
    public class MyWatcher {
        public static final Map<String, Object> watches = new HashMap<>();
    }