Set Conditional Breakpoints in IDEA
So, here's the code I wanted to examine:
public Publication parsePublication(String inputLine) throws ParseException {
Publication publication = new Publication();
String[] fields = inputLine.split("\t");
publication.setPublicationType(fields[0]);
...
return publication;
} Essentially, I wanted to break after inputLine.split("\t"); if and only if fields[35]
existed and was equal to "PM:16732581." After examining IDEA's
Breakpoint dialog, I noticed a section in the bottom right-hand corner
that I'd never played with before:
As it turns out, this is exactly what I needed. If you click on the ellipsis next to the drop menu, you get a context-sensitive editor equipped with code completion:

Enter
the desired conditions and voila! A conditional breakpoint. It worked
like a charm the very first time, and I only had to inspect the
breakpoint when the problematic record came up.
Another nice
feature of the conditional breakpoint is that if some sort of exception
(such as a NullPointerException) occurs while attempting to evaluate
the conditional expression, IDEA pops up a dialog informing you what
happened and asking if you want to stop at the breakpoint or continue.
Nice.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)





Comments
Mike P(Okidoky) replied on Tue, 2008/02/05 - 6:27pm
What's the performance impact when a condition is not met? For instance, if I add this in a loop that process a two dimensional array of pixels, and I want the break point to hit when a pixel has an alpha value greater then 50%, I don't want the debugger to delay processing each pixel by an x number of miliseconds.
Does IDEA compile the expression?
Or perhaps it replaces the class with a modified version using hotswap? THAT would certainly have a minimum performance impact, because it'd be just like having written the condition right inside the method, with no actual breakpoints happening unless the condition is met.
How does it do it, and also, how do the other IDE's do it?
Ann Oreshnikova replied on Wed, 2008/02/06 - 8:16am
in response to:
Mike P(Okidoky)
Yes Mike, the performance may suffer when you use conditional breakpoints. It will definitely depend on the complexity of the structure being iterated and the expression that defines your condition.
Technically, IntelliJ IDEA will stop at the breakpoint each time and evaluate the expression. If the condition is not met, it will resume the Java machine until the next breakpoint.
Thus you have to use this feature thoughtfully, in order to get the most benefit out of it ;-)
Shay Shmeltzer replied on Wed, 2008/02/06 - 1:35pm
And here is how you do the same thing if you are using JDeveloper:
http://blogs.oracle.com/shay/2006/12/22#a301
Matt Stine replied on Wed, 2008/02/06 - 2:09pm
Bryan Williams replied on Thu, 2008/02/07 - 3:56pm
Eclipse Conditional Breakpoints:
That's the basics of it. R.J. Lorimer wrote an interesting post a while back about using this feature to dynamically add debug printlns at runtime.
Pazargic Antone... replied on Sat, 2011/11/19 - 2:36pm
I really didn't managed to (easily) change a value in Idea breakpoint condition expression.
Code:
Breakpoint condition:
1. Eclipse:
System.out.println(String.format("foo value is %b%n", foo));
foo = true;
return false;
2. Intellij:
!(foo = true)
I don't know why Idea doesn't allow to author a "closure" which return a boolean as a condition "expression", like the Eclipse does. Does anyone know how to handle this debug situation in Idea? Thank you.