Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't breakpoint the last statement of a code block in Eclipse

if (true) {
    String a = "foo";
    String b = "bar";
}

If I set a breakpoint at String a = "foo"; eclipse will stop, and I can step over and see the value of a inside the variables window. But I can't step over the 2nd statement, it just leaves the code block and I never see the value of b.

This forces me to add a noop statement after String b = "bar"; just so I can see what b contains. I also can't add a breakpoint on the closing } which I think are maybe related issues.

I know Visual Studio allows this, so is there a way to do this in Eclipse?

like image 856
rtaranu Avatar asked Oct 16 '11 17:10

rtaranu


2 Answers

To set a breakpoint at the end of an arbitrary block is not possible (without byte-code hacking).

If it is a method body, then it is possible: you can set a Method breakpoint. Do this by double-clicking on the method definition line:

sample code with simple method breakpoint

(notice the little arrow?) and then in the breakpoints view, select the breakpoint to see both an Entry and an Exit option tick-box in the displayed properties:

modifying the breakpoint

The little arrow indicates that, by default, we have set a breakpoint on entry to the method.

Now select Exit (and deselect Entry) and you will see this in the breakpoints view:

exit breakpoint in breakpoints view

(There is a different little arrow, indicating an exit breakpoint.)

Now run the debugger on this little breaker ('Debug As a Java Application') and it will stop on the exit brace of the method:

debugger stopped

and the local variables (only a in this case) are now visible (with the correct values) in the Variables view:

variables just before exit

It is worth noticing that this type of breakpoint traps method exit however this happens -- even, for example, if we exit by throwing an exception.

like image 137
Steve Powell Avatar answered Sep 20 '22 12:09

Steve Powell


You can highlight the expression on the right hand side of the assignment and press Ctrl+Shift+I (for 'inspect' I think). It will evaluate the expression and give you the result. That way you can know the value of b without needing a breakpoint after the assignment.

like image 36
Gyan aka Gary Buyn Avatar answered Sep 23 '22 12:09

Gyan aka Gary Buyn