在 Intellij 中调试时,我可以在返回之前找出返回值吗? [英] Can I find out the return value before returning while debugging in Intellij?

查看:20
本文介绍了在 Intellij 中调试时,我可以在返回之前找出返回值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与:

Object method(){
    ...
    return /* some complex expression */
}

有什么方法可以让我看到调试时将返回什么值?就像以某种方式设置一个断点,该断点将在返回后立即命中,但在执行之前转到调用代码?我希望能够知道返回值是什么,并且能够看到所有局部变量的值是什么.

Is there a way I can see what value will be returned while debugging? Like somehow set a breakpoint that would be hit right after the return, but before execution goes to the calling code? I'd like to be able to know what the return value is and also be able to see what values of all of the local variables are.

我现在的做法是创建一个临时变量:Object ret =/* something complex */;返回 ret;.但这很麻烦,而且还增加了额外的不必要的代码行.

The way I do it now is by making a temporary variable: Object ret = /* something complex */; return ret;. But thats a hassle and also adds an extra unnecessary line of code.

注意:这是与 我可以在 Visual Studio 中调试时在返回之前找出返回值吗,但对于 intellij.

Note: This is the same question as Can I find out the return value before returning while debugging in Visual Studio, but for intellij.

推荐答案

似乎有几种方法可以做到这一点.第一个涉及在方法签名上设置断点,在您的情况下,您将在 Object method(){ 上设置断点.这将允许您观察方法的入口和出口.我相信您必须将它与Watch 方法返回值" 结合使用,如上所述,但我无法完全测试它,因为计算时间太长.请注意,这确实会显着降低调试器的性能,并且调试时间会更长.

There seems to be a couple ways you can do this. The first one involves setting up the breakpoint on the method signature, in your case you would setup a breakpoint on Object method(){ . This will allow you to watch for the entrance and exit of the method. I believe you have to use this in conjunction with "Watch method return values" like stated above, but I haven't been able to completely test this as it takes too long to compute. Beware, this does dramatically decrease the performance of the debugger and it will take longer to debug.

您也可以手动执行以下操作.

Also you can do the following manually.

  1. 在返回行上设置断点.
  2. 当回车行被击中时,点击回车行,具体将光标放在你想看的操作上,然后去运行->评估表达式(或Alt-F8),它应该运行表达式那个点并返回它返回的东西.

注意:如果你创建了一个断点,你可以告诉 IDEA 用它们做很多事情,比如如果你在断点处中断,你可以告诉他们执行一个表达式并记录下来.如果您右键单击断点并单击属性,您可以查看可以执行的操作.

Note: If you create a breakpoint, there are a lot of things you can tell IDEA to do with them, such as if you break on a breakpoint, you can tell them to perform an expression and log it. You can look around with what you can do if you right-click on a breakpoint and click properties.

更新:将此与第一种方法结合使用.不要使用观察方法返回值",因为它似乎会减慢或冻结调试会话.而是执行以下操作

UPDATE: Try this in conjunction with the first way of doing it. Don't use "Watch method return values" as it seems to slow down or freeze up the debugging session. Instead do the following

  1. 右键单击要查看的返回表达式,然后单击添加到监视"
  2. 接下来添加一个如上所述的方法断点.
  3. 调试您的程序,您的方法将在方法签名上中断.
  4. 按 F9 键继续,它应该再次中断 AFTER 已计算返回表达式并且您的返回表达式应在监视列中.

请记住,方法断点较慢,因此可能需要更多时间,如果您注意到性能受到太大影响,则可以通过仅设置 return 语句断点(没有方法断点)然后添加方法断点 AFTER 返回语句断点命中然后继续.

Remember that method breakpoints are slower so it might take more time, a tricky way to improve this if you are noticing too much of a performance hit is by just setting the return statement breakpoint (without having a method breakpoint) and then adding the method breakpoint AFTER the return statement breakpoint hits and then continuing.

希望这会有所帮助,

这篇关于在 Intellij 中调试时,我可以在返回之前找出返回值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆