1

I know how to set a watchpoint for a field (right click variable in variables view -> watch). But the execution pauses on access/modification of that field of ALL objects of that type.

How can one just pause execution when a certain field of a SPECIFIC OBJECT is accessed or modified?

To make it more clear:

class A { int a; }

public static void main(String[] args) {

    List<A> myList = new ArrayList<A>();

    // assume a lot of A's are added here

    A interestingA = new A();
    myList.add(someIndex, interestingA);

    for(A a : myList) {
        a.a = 42; // debugger shall stop here, but only, 
                  // if it is the interesting A
    }
}

Obviously I cannot set a breakpoint there, because I would have to click on continue a lot of times. Furthermore, interestingA may be modified from other spots I am not aware of and in that case debugger should stop too.

Christian
  • 1,308
  • 3
  • 14
  • 24
  • 1
    I think this will help you. Good luck! http://stackoverflow.com/questions/1709929/is-there-a-way-in-the-eclipse-debugger-to-be-notified-when-the-state-of-a-java-o – user145570 Aug 05 '15 at 14:21
  • I don't understand what you want to do. Are you referring to watch or breakpoint? – Alepac Aug 05 '15 at 14:23
  • I don't think this is possible (at least not in Eclipse). Watches are pairs of names and values, not of type and values.... – Konstantin Yovkov Aug 05 '15 at 14:23
  • @user145570: The question is very similar, but the accepted answer makes no sence to me. Because where exactly would you place that break point in the code as described as the first step. I want to pause on access, not at a specific spot. I dont see how this goes togoether. – Christian Aug 05 '15 at 16:05
  • You set the breakpoint on the line where you want the debugger to stop when the variable is changed. – Kelly S. French Aug 05 '15 at 17:07
  • Seriously @kocko, why do you change the title into something completely wrong. No I understand why my question was hard to understand at first. Should be clear from the content that it is in fact object and not type. – Christian Aug 05 '15 at 17:11
  • @KellyS.French: how shall that work? I dont't know from where the field is changed. Might be at various spots, too. If I set a breakpoint somewhere, maybe the value is changed and execution never reaches that breakpoint. Seems clear, or am I totally of track here? – Christian Aug 05 '15 at 17:18

2 Answers2

0

You need a way to identify which instance you want to debug. Then you can add a condition to the breakpoint.

For example,

class A
    {
    int a;
    final String name;
    public A(final String name)
        { this.name = name; }
    }

public static void main(String[] args) {
    A a1 = new A("foo");
    A a2 = new A("bar");
    a1.a = 42; // debugger shall not stop here
    a2.a = 42; // but here, because I am interested in object a2
}

Then put "bar".equals(name) as the condition on your breakpoint.

Community
  • 1
  • 1
dsh
  • 12,037
  • 3
  • 33
  • 51
  • Thx, for the answer. But again, this requires a certain line to set the breakpoint. With your links I found out, that the condition is evaluated at that spot. Unfortunately, that was not the question. – Christian Aug 05 '15 at 18:39
  • They are called watch points as far as I know and they seem to apply to all instances of a type (hope I am wrong somehow). – Christian Aug 05 '15 at 18:53
0

Define the problem

I would have to say that this is actually two problems; first how to get Eclipse to stop but only when a variable changes, and second where to place the breakpoints that use the technique from point one.

Like other links listed in other answers, my answer to this question has the details on the "how" part but not the "where" part.

Suggest a solution

You might have you use the 'find all references' giving you the places in the code that refer to the variable and place breakpoints at those lines and in each one include the conditional expression. This is a bit brute-force but at least Eclipse can do the hard part (the semantic search) for you.

Commentary

As an aside, I've been wanting the feature you are describing for many many years. I'm sure I'm not the only programmer who discovers that some logic is failing because a certain variable doesn't have the correct value but can't figure out where the value is getting changed. I don't believe there is a good answer to this problem without introducing a new type of breakpoint in Eclipse which halts execution anywhere based on a variable changing. Just typing that makes me realize how inefficient that would be because the variable would have to be evaluated after every JVM instruction which I suspect would make debugging become so CPU-bound as to be useless.

Community
  • 1
  • 1
Kelly S. French
  • 12,198
  • 10
  • 63
  • 93