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

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

问题描述

我已经反复尝试了很长时间,但不知何故它不起作用.

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 语句返回结果.

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

我的断点条件截图

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

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