Java 在 Eclipse 中调试时,我可以在返回之前找出返回值吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/217116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I find out the return value before returning while debugging in Eclipse?
提问by RodeoClown
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 Displaytab.
我正在调试我无法修改的代码(阅读:不想重新编译第三方库),有时它会跳转到我没有源代码的代码,或者返回表达式有副作用阻止我能够只在“显示”选项卡中运行表达式。
Often the return value is used in a compound statement, and so the Variablesview will never show me the value (hence wanting to see the result before control returns to the calling function).
通常在复合语句中使用返回值,因此Variables视图永远不会向我显示该值(因此希望在控制返回到调用函数之前查看结果)。
UPDATE:I can't use the expression viewer as there are side-effects in the statement.
更新:我无法使用表达式查看器,因为语句中有副作用。
采纳答案by zvikico
This feature was added to Eclipse version 4.7 M2 under Eclipse bug 40912.
此功能已添加到 Eclipse 版本 4.7 M2 的Eclipse 错误 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: "
- 跳过
return
语句(使用“Step Over”或“Step Return”) - 现在变量视图中的第一行将显示返回语句的结果,如“[statement xxx] 返回:”
See Eclipse Project Oxygen (4.7) M2 - New and Noteworthyfor details.
有关详细信息,请参阅Eclipse Project Oxygen (4.7) M2 - New and Noteworthy。
回答by Jonathan Leffler
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.
艰难的一个。我在 Eclipse 之外的经验是,如果您可能需要查看返回值,最好将其分配给函数中的局部变量,以便返回语句是简单的return varname;
而不是return(some * expression || other);
. 但是,这对您并没有太大帮助,因为您说您不能(或不想)修改甚至重新编译代码。所以,我没有给你一个好的答案——也许你需要重新考虑你的要求。
回答by stephmara
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.
根据返回语句,您可以突出显示正在返回的表达式,并且从右键单击菜单中,应该有类似“评估表达式”的内容(我现在面前没有 eclipse,但它是类似的东西)。它会告诉你什么将被退回。
回答by DJ.
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.
过去,在处理这样的 3rd 方库时,我所做的是创建一个包装类或子类,委托给父类,并在包装/子类中进行调试。但这需要额外的工作。
回答by zvikico
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:
我的规则:
- Only one return statement, only at the end of the method (finally allowed after it)
- Always have a local called result which holds the returned value, starting from a default.
- 只有一个return语句,只在方法的最后(最后允许在它之后)
- 总是有一个本地调用的结果,它保存返回的值,从默认值开始。
Naturally, the most trivial getters are exempt.
自然,最琐碎的吸气剂是豁免的。
回答by Olivier
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 debuggedlike other programs.
您可以使用 AspectJ 用获取您感兴趣的方法的返回值的方面来检测 JAR。根据 Eclipse 的文档,可以像其他程序一样调试AspectJ程序。
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.
如果可以接受处理二进制 JAR,则编译后编织;
加载时编织,这需要在 VM 中激活编织代理。
See the eclipse documentation (link above) and also the AspectJ Development Environment Guide.
请参阅 eclipse 文档(上面的链接)以及AspectJ 开发环境指南。
回答by Arjan Tijms
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
这实际上是 Eclipse 中一个长期存在的错误,可以追溯到 IDE 的最初几天:https: //bugs.eclipse.org/bugs/show_bug.cgi?id=40912
回答by Satish
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
注意:尚未使用第三方库对此进行测试,但它对我的代码运行良好。在Eclipse Java EE IDE for Web Developers上对此进行了测试。版本:Juno 服务版本 1
回答by ejaenv
"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]