Matt is the Group Leader of Research Application Development in the Research Informatics Division of Information Sciences at St. Jude Children's Research Hospital in Memphis, Tennessee. Matt has been developing and supporting enterprise Java applications in support of life sciences research for St. Jude since 2001. Matt is a committer to multiple open source projects and is the founding member of the Memphis/Mid-South Java User Group. Matt is also a regular speaker on the No Fluff Just Stuff symposium series tour (as well as other major conferences), and his articles have appeared in GroovyMag and NFJS the Magazine. His current areas of interest include lean/agile software development, modularity and OSGi, mobile application development (iPhone/iPad/Android), web development (HTML5, etc.), and Groovy/Grails. Matt has posted 44 posts at DZone. You can read more from them at their website. View Full User Profile

Set Conditional Breakpoints in IDEA

  • submit to reddit
So yesterday I was attempting to debug an issue in a batch processing module within one of our applications. In short, an assertion was failing deep within Hibernate as it attempted to flush the session. Using a combination of various log statements, I had isolated the problem down to a particular record that the batch process was attempting to update. (BTW: I know you shouldn't be using Hibernate for batch processing - however, we're talking about batches of at most 1000 records here, not millions!) What I really wanted to do was set a breakpoint and examine the state of the objects at runtime; however, I dreaded the thought of clicking through the breakpoint time and time again until I got to the particular record that was causing the problem. "Surely," I thought, "there must be a way to tell the debugger to only break under certain conditions."

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

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.

Your rating: None Average: 5 (4 votes)
Published at DZone with permission of its author, Matt Stine.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


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:

Matt Stine replied on Wed, 2008/02/06 - 2:09pm

Any Eclipse or Netbeans users have a HOWTO for this one? It would be great if we could get a slice across all of the major IDE's.

Bryan Williams replied on Thu, 2008/02/07 - 3:56pm

Eclipse Conditional Breakpoints:

  1. Right click on the little blue ball in your editor that represents the breakpoint for that line of code.
  2. Select "Breakpoint Properties".
  3. Check the "Enable Condition" checkbox.
  4. Type your condition in the text are below it.

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

Personally I found the Eclipse support more powerful that the Idea one.
I really didn't managed to (easily) change a value in Idea breakpoint condition expression.
boolean foo = wouldCompute(); /*this method return false and I want it true without affecting the code*/
if (foo) { // here I put a conditional breakpoint

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.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.