如何在断点中使用条件? [英] How to use conditions in breakpoints in idea?

查看:66
本文介绍了如何在断点中使用条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经来回尝试了很长时间,但是以某种方式它不起作用.

I have tried this back and forth for a longer time, but somehow it does not work.

我也尝试使用和不使用分号.我还尝试过在断点之前运行此行,以确保条件确实有效,

I have also tried with and without semicolon. I have also tried to run this line right before the breakpoint to make sure the condition really works,

logger.error("I am Here! " + "#PDU Elements: " + pdu.getVariables().size());

并按预期返回13.

有人知道如何使它正常工作吗?

Does anyone know how to get this to work?

编辑

根据请求,我将在断点处添加运行的代码行

On request I will add the lines of code run, at the breakpoint,

logger.error("I am Here! " + "#PDU Elements: " + pdu.getVariables().size());
Trap trap = Trap.createTrapFrom(variableMap, instanceIdentificationNumber); // Breakpoint in margin on this line.

编辑2

问题似乎与IDEA随机错过一些断点有关.我还尝试了一些无条件的断点(应始终停止),这些断点仅在特定时间停止.

The problem seems to be related to that IDEA randomly misses some breakpoints. I have also tried some unconditional breakpoints (should always stop) and these only stops at specific times.

推荐答案

在断点处快速按CTRL+SHIFT+F8两次将打开一个对话框,而不是弹出对话框来配置条件.然后按F1打开帮助对话框.

press CTRL+SHIFT+F8 twice quickly at your breakpoints will open a dialog not a popup dialog to configure a condition. then press F1 to opening the helping dialog.

intellij帮助文档说断点条件是:

as intellij help documentation says a breakpoint condition is:

选中此复选框,然后在文本字段中指定击中断点的条件. 条件是Java布尔表达式(包括返回true或false的方法),例如str1.equals(str2). 此表达式应该在设置断点的行上有效,并在每次到达断点时进行评估.如果评估结果为真,则执行用户选择的操作. 如果结果为假,则断点不会产生任何影响.如果调试器无法对表达式求值,则会显示条件求值错误消息.您可以选择是要在此断点处停止还是忽略它. 在给定的字段/方法/例外的上下文中计算字段/方法/例外断点的条件. 在条件字段的右侧,有一个(Shift + Enter)按钮可打开多行编辑器.

Select this check box and specify a condition for hitting a breakpoint in the text field. A condition is a Java Boolean expression (including a method returning true or false), for example, str1.equals(str2). This expression should be valid at the line where the breakpoint is set, and is evaluated every time the breakpoint is reached. If the evaluation result is true, user-selected actions are performed. If the result is false, the breakpoint does not produce any effect. If the Debugger cannot evaluate the expression, it displays the Condition evaluation error message. You can select whether you would like to stop at this breakpoint or ignore it. Conditions for field/method/exception breakpoints are calculated in the context for the given field/method/exception. To the right of the Condition field, there is the button (Shift+Enter) that opens the multiline editor.

注意

断点条件由Java代码组成,因此条件中发生的任何错误都将在断点处停止.并且它不支持任何lambda表达式.当您使用多语句计算条件时,需要使用return语句返回结果.

Note

breakpoint condition is consist with java code, so any error occurs in condition will stop at the breakpoint. and it does not supports any lambda expressions. when you calculate the condition with multi-statements you need using return statement to return the result.

AND 条件通常会抛出NullPointerException来停止断点.您需要在断点条件下检查null:

AND the condition often throws NullPointerException to stop the breakpoint. you need check null in breakpoint condition:

//change the condition
pdu.getVariables().size() == 13
                  ^-----throws a NullPointerException if variables is null

//to the condition using ternary operator for checking null
pdu.getVariables()==null ? false : pdu.getVariables().size()==13

示例

例如:

private String[] run(Class<?> mainClass
     , Optional<String> launcherClass, String[] args) {
    ...
    ^-----I mark a breakpoint here
}

我的状况是,请记住选中状况复选框:

my condition is and remeber to check the condition checkbox:

launcherClass != null

我的断点条件屏幕截图

这篇关于如何在断点中使用条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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