Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
105 views
in Technique[技术] by (71.8m points)

java - How to use conditions in breakpoints in idea?

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

enter image description here

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());

and it returns 13 as expected.

Does anyone know how to get this to work?

EDIT

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.

EDIT 2

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

as intellij help documentation says a breakpoint condition is:

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.

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 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

Examples

for example:

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

My Breakpoint Condition Screenshot

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...