views:

911

answers:

7

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. Please read the question before upvoting an incorrect answer. Thanks.

A: 

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
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
A: 

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
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
+1  A: 

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
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
A: 

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 MyReturnedType;
}

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.

zvikico
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
+7  A: 

It looks like this is currently a missing feature in Eclipse. See Eclipse bug 40912 (currently languishing with a "LATER" resolution ... but I added my vote to it!

A: 

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  A: 

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%5Fbug.cgi?id=40912

arjan