简体   繁体   English

无法断开Eclipse中代码块的最后一个语句

[英]Can't breakpoint the last statement of a code block in Eclipse

if (true) {
    String a = "foo";
    String b = "bar";
}

If I set a breakpoint at String a = "foo"; 如果我在String a = "foo";设置断点String a = "foo"; eclipse will stop, and I can step over and see the value of a inside the variables window. Eclipse将停止,我能跨过,看到的值a变量窗口内。 But I can't step over the 2nd statement, it just leaves the code block and I never see the value of b . 但我不能超越第二个声明,它只是离开代码块而我从未看到b的值。

This forces me to add a noop statement after String b = "bar"; 这迫使我在String b = "bar";之后添加一个noop语句String b = "bar"; just so I can see what b contains. 这样我才能看到b包含的内容。 I also can't add a breakpoint on the closing } which I think are maybe related issues. 我也不能在结束时添加断点}我认为这可能是相关的问题。

I know Visual Studio allows this, so is there a way to do this in Eclipse? 我知道Visual Studio允许这样做,所以在Eclipse中有没有办法做到这一点?

To set a breakpoint at the end of an arbitrary block is not possible (without byte-code hacking). 在任意块的末尾设置断点是不可能的(没有字节码黑客攻击)。

If it is a method body , then it is possible: you can set a Method breakpoint. 如果它是一个方法体 ,则可能:你可以设置一个断点方法 Do this by double-clicking on the method definition line: 通过双击方法定义行来执行此操作:

使用简单方法断点的示例代码

( notice the little arrow? ) and then in the breakpoints view, select the breakpoint to see both an Entry and an Exit option tick-box in the displayed properties: 注意小箭头? )然后在断点视图中,选择断点以在显示的属性中查看EntryExit选项复选框:

修改断点

The little arrow indicates that, by default, we have set a breakpoint on entry to the method. 小箭头表示默认情况下,我们在方法的入口处设置了断点。

Now select Exit (and deselect Entry ) and you will see this in the breakpoints view: 现在选择Exit (并取消选择Entry ),您将在断点视图中看到:

在断点视图中退出断点

( There is a different little arrow, indicating an exit breakpoint. ) 有一个不同的小箭头,表示退出断点。

Now run the debugger on this little breaker ('Debug As a Java Application') and it will stop on the exit brace of the method : 现在在这个小破坏程序('Debug As a Java Application')上运行调试器,它将在方法的退出括号上停止:

调试器停止了

and the local variables (only a in this case) are now visible (with the correct values) in the Variables view: 现在可以在Variables视图中看到局部变量(在这种情况下只有a )(使用正确的值):

退出之前的变量

It is worth noticing that this type of breakpoint traps method exit however this happens -- even, for example, if we exit by throwing an exception. 值得注意的是,这种类型的断点陷阱方法会退出,但这种情况会发生 - 例如,如果我们通过抛出异常退出。

You can highlight the expression on the right hand side of the assignment and press Ctrl + Shift + I (for 'inspect' I think). 您可以突出显示作业右侧的表达式,然后按Ctrl + Shift + I (我认为是“检查”)。 It will evaluate the expression and give you the result. 它将评估表达式并为您提供结果。 That way you can know the value of b without needing a breakpoint after the assignment. 这样,您可以在分配后知道b的值而不需要断点。

I'm honestly not sure what's going on in your Eclipse installation. 老实说,我不确定Eclipse安装中发生了什么。 I'm working in Java and just tried everything I can think of. 我在Java工作,只是尝试了我能想到的一切。 I checked a static method for breakpoint on the last line that is not a } and it works just fine. 我检查的最后一行是不是对断点静态方法}和它工作得很好。 I checked the same for a non-static method. 我检查了相同的非静态方法。

It breaks down this way: you can't breakpoint on a curly brace end, but it will default to whatever is under the curly brace, line-wise. 它以这种方式分解:你不能在大括号末端断点,但它将默认为大括号的大括号的任何东西。 So if you have a nested function: 所以如果你有一个嵌套函数:

public int DoesStuff(int x, int y) {
    if(x > y) {
        DoThing;
    }
    else {
        DoOtherThing;
    }
}

Then the last two braces cannot be break-pointed, but DoThing and DoOtherThing can. 然后最后两个括号不能断点,但DoThingDoOtherThing可以。 Make sense? 说得通?

If ending statement is assignment and it is on local variable why do you want to see its value because it will not be in scope anymore and can't effect. 如果结束语句是赋值并且它在局部变量上,为什么要查看它的值,因为它不再在范围内而且不能生效。

If it's setting same class attribute then you can see it when you return from this call and you have the object on which this method operated. 如果它设置了相同的类属性,那么当您从此调用返回时,您可以看到它,并且您具有此方法所操作的对象。

Although this doesn't answer the question but I am trying to understand the use-case of your problem. 虽然这不回答问题,但我试图了解你的问题的用例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM