102

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
RodeoClown
  • 13,338
  • 13
  • 52
  • 56
  • 8
    That's why I switched to the Community Version of IntelliJ -- the Eclipse folks just don't seem to understand how important this is. (If they ever fix it, I'll switch back the day it's released.) –  Mar 23 '11 at 03:36
  • @James Mitchell this looks like a great idea for a plugin. I will add it to me todo list and will try to do it when I find time(not soon) – IAdapter Dec 05 '11 at 13:03
  • @user672348 But how to do that in IntelliJ IDEA? – Alexey Tigarev Sep 15 '13 at 07:06
  • @AlexeyTigarev: IIRC, it's just displayed when you do "Step Return" (or the equivalent). – Blaisorblade Feb 15 '14 at 14:24
  • 2
    Brace yourself for Eclipse Oxygen (mid 2017 release date). [The M2 milestone includes this feature](https://www.eclipse.org/eclipse/news/4.7/M2/#step-show-methodresult). – Abdull Dec 13 '16 at 15:17
  • @user672348 and did you switch? :> – Line May 24 '17 at 12:28
  • @Abdull: Thanks for pointing this out. I added it to the top answer. – sleske Jan 17 '19 at 15:30

10 Answers10

39

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.

sleske
  • 81,358
  • 34
  • 189
  • 227
  • Me, too. I miss this feature when using Eclipse. – m24p Sep 15 '14 at 18:18
  • 1
    This Eclipse bug was subsequently [reopened in late 2009](https://bugs.eclipse.org/bugs/show_bug.cgi?id=40912#c16). Still awaiting a fix, though, with no one assigned, and no Target Milestone. :) – Mark A. Fitzgerald Jan 12 '15 at 18:02
  • 1
    This has been fixed in the recent builds of Eclipse 4.7 (M2 onwards) – AbdullahC Dec 21 '16 at 06:25
  • 1
    Some information for how to use the fix can be found in the New And Noteworthy for 4.7 M2. Look for "Method result after step operations" here: https://www.eclipse.org/eclipse/news/4.7/M2/ – Joshua Goldberg Mar 06 '17 at 18:56
  • @JoshuaGoldberg: Thanks for pointing this out - I edited it into the answer. – sleske Jan 17 '19 at 15:34
35

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

Satish
  • 738
  • 6
  • 8
  • 4
    +1 because this works not only for return values, but for expressions in general. Much more convenient than using the expressions tab. It should be noted this executes the code, so if it has side effects, beware. http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fmenus%2Frun%2Fref-menu-run.htm – goat Apr 11 '14 at 17:44
  • 1
    `Ctrl + Shift + I` was also helpful for me. – Ciro Santilli OurBigBook.com Feb 25 '15 at 13:54
  • 2
    as @goat mentioned, this executes the code multiple times, so for example if the function did this `return System.currentTimeMillis();`, then you'd get a different result than the function actually returned! – Brad Parks Jul 15 '15 at 14:48
  • Equivalent on an `Apple machine` ? – Adriano Nov 24 '16 at 11:34
  • 1
    This is problematic advice - this will re-evaluate the expression, which can be disastrous if it has side-effects. – sleske Jan 17 '19 at 15:35
6

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.

Blaisorblade
  • 6,438
  • 1
  • 43
  • 76
zvikico
  • 9,765
  • 4
  • 38
  • 49
  • 3
    Hey zvikico - when I'm working on my own code, I usually use a similar pattern. Unfortunately, my problem is when I'm using code that is not written or modifiable by me. :S – RodeoClown Nov 19 '08 at 18:42
  • I do this too, but SonarLint is complaining....now I am equivocating: http://stackoverflow.com/questions/31733811/local-variables-before-return-statements-does-it-matter/31734226#31734226 – Dave Jun 11 '16 at 22:48
  • @WitoldKaczurba that's your opinion, and that's fine. Just accept that there are other opinions, and show some respect to those who do. – zvikico May 20 '19 at 17:13
  • Hi zvikco. Thanks for message. I accept that there are different approaches but I referred to https://pmd.sourceforge.io/pmd-4.3.0/rules/design.html#UnnecessaryLocalBeforeReturn . Cheers – Witold Kaczurba May 20 '19 at 19:14
5

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

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
2

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.

DJ.
  • 6,664
  • 1
  • 33
  • 48
  • 1
    The problem with this solution (apart from the one you noted about extra work) is that it only works if you aren't multiple classes/methods deep inside an external library. But it is useful when you are dealing with the outer layer of methods. – RodeoClown Oct 20 '08 at 03:51
1

"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/

ejaenv
  • 2,117
  • 1
  • 23
  • 28
0

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • In my code I generally have the intermediate step so I can check the result, but some of the code I am using has too much organisational overhead to modify (esp. if the library gets updated, don't really want to maintain a forked version just for debugging). – RodeoClown Oct 19 '08 at 23:30
0

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.

stephmara
  • 43
  • 2
  • 4
    The problem is when evaluating the return feature has side-effects (like database entries being created for instance), evaluating the return expression will change the system state. Thanks for the suggestion though. – RodeoClown Oct 19 '08 at 23:35
0

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.

Olivier
  • 1,232
  • 6
  • 10
0

For @Daniel Meyer answer's to work, ensure that 'Show method result after a step operation (if supported by the VM; may be slow)' is checked. The option is accessible via;

Windows> Preferences> Java> Debug> Show method result......

drac_o
  • 427
  • 5
  • 11