[ACCEPTED]-Can I find out the return value before returning while debugging in Eclipse?-return-value

Accepted answer
Score: 38

This feature was added to Eclipse version 1 4.7 M2 under Eclipse bug 40912.

To use it:

  • step over the return statement (using "Step Over" or "Step Return")
  • now the first line in the variable view will show the result of the return statement, as "[statement xxx] returned: "

See Eclipse Project Oxygen (4.7) M2 - New and Noteworthy for details.

Score: 35

Found a really good shortcut for this. Select 8 the expression which returns the value and 7 press

Ctrl + Shift + D

This will display the value of the 6 return statement. This is really helpful 5 in cases where you can't or don't want to 4 change just for debugging purpose.

Hope this 3 helps.

Note: Have not tested this with third 2 party libraries, but it is working fine 1 for my code. Tested this on Eclipse Java EE IDE for Web Developers. Version: Juno Service Release 1

Score: 6

That's why I always stick with the following 2 pattern for methods:

MyReturnedType foo() {
     MyReturnedType   result = null;

     // do your stuff, modify the result or not

     return result;

My rules:

  1. Only one return statement, only at the end of the method (finally allowed after it)
  2. Always have a local called result which holds the returned value, starting from a default.

Naturally, the 1 most trivial getters are exempt.

Score: 5

This is actually a long standing bug in 2 Eclipse, dating back from the very first 1 days of the IDE: https://bugs.eclipse.org/bugs/show_bug.cgi?id=40912

Score: 2

I am curious about to learn the answer to 6 this question also.

In the past, when dealing 5 with 3rd party library like that, what I 4 did is to create a wrapper class or child 3 class that delegate to the parent class 2 and do my debugging in the wrapper/child 1 class. It takes extra work though.

Score: 1

"Now when you return from a method, in 3 the upper method, in the variable view it 2 shows the return value of the previously 1 finished call" [1]

[1] https://coffeeorientedprogramming.wordpress.com/2016/09/23/eclipse-see-return-value-during-debugging/

Score: 0

Tough one. My experience, outside of Eclipse, is 9 that if you might need to see the return 8 value, it is best to assign it to a local 7 variable in the function so that the return 6 statement is a simple return varname; and not return(some * expression || other);. However, that's 5 not dreadfully helpful to you since you 4 say you can't (or don't want to) modify 3 or even recompile the code. So, I don't 2 have a good answer for you - perhaps you 1 need to reconsider your requirement.

Score: 0

Depending on the return statement, you can 6 highlight the expression that is being returned 5 and from the right-click menu, there should 4 be something like "evaluate expression" (I 3 don't have eclipse in front of me now, but 2 it's something like that). It will show 1 you what is going to be returned.

Score: 0

This is a bit far-fetched, but as there 12 doesn't seem to be a simple way:

You could 11 use AspectJ to instrument the JAR with aspects 10 that get hold of the return value of the 9 methods you're interested in. According 8 to Eclipse's documentation, AspectJ programs 7 can be debugged like other programs.

There are two 6 options to weave your classes without recompiling 5 the library :

  • Post-compile weaving if processing 4 the binary JAR is acceptable;

  • Load-time weaving, which 3 requires activating a weaving agent in the 2 VM.

See the eclipse documentation (link above) and 1 also the AspectJ Development Environment Guide.

More Related questions