Can I find out the return value before returning while debugging in Eclipse?

JavaEclipseDebuggingReturn Value

Java Problem Overview


Is it possible to see the return value of a method after the line has been run and before the instruction pointer returns to the calling function?

I am debugging code I can't modify (read: don't want to re-compile a third party library), and sometimes it jumps to code I don't have source to or the return expression has side effects that stop me being able to just run the expression in the Display tab.

Often the return value is used in a compound statement, and so the Variables view will never show me the value (hence wanting to see the result before control returns to the calling function).

UPDATE: I can't use the expression viewer as there are side-effects in the statement.

Java Solutions


Solution 1 - Java

This feature was added to Eclipse version 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.

Solution 2 - Java

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

Ctrl + Shift + D

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

Hope this helps.

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

Solution 3 - Java

That's why I always stick with the following 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 most trivial getters are exempt.

Solution 4 - Java

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

Solution 5 - Java

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

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

Solution 6 - Java

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

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

Solution 7 - Java

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

Solution 8 - Java

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

Solution 9 - Java

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

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

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

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

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

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRodeoClownView Question on Stackoverflow
Solution 1 - JavaDaniel MeyerView Answer on Stackoverflow
Solution 2 - JavaSatishView Answer on Stackoverflow
Solution 3 - JavazvikicoView Answer on Stackoverflow
Solution 4 - JavaArjan TijmsView Answer on Stackoverflow
Solution 5 - JavaDJ.View Answer on Stackoverflow
Solution 6 - JavaejaenvView Answer on Stackoverflow
Solution 7 - JavaJonathan LefflerView Answer on Stackoverflow
Solution 8 - JavastephmaraView Answer on Stackoverflow
Solution 9 - JavaOlivierView Answer on Stackoverflow