Search This Blog

Loading...

Wednesday, May 26, 2010

IDEA candy - Harnessing debugger

Table of contents




Foreword



This article contains convenient tricks that may be used during debugging under IDEA. I've been IDEA user for more than five years but got know about them only today. They are so cool that I believe I use them all the time now.



Smart step into



There is a possible situation that break point is set for the statement that contains multiple method calls. Consider example below:


package com.spring.example.test;

public class MyTest {

public static void main(String[] args) {
MyTest test = new MyTest();
test.bar(test.baz(test), test.foo());
System.out.println(test.foo());
System.out.println(test);
}

public MyTest foo() {
return this;
}

public MyTest bar(MyTest ... t) {
return this;
}

public MyTest baz(MyTest t) {
return t;
}
}



We can set break point to the line containing statement 'test.bar(test.baz(test), test.foo());', start the application and wait for break point hit.

We can go into method body now via 'Step Into' action ('F7' shortcut), however, we'll be dispatched to the first method body ('bar()'). It's possible that we want to proceed to 'foo()'/'baz()' instead.

I used to do the following at such situations - skip any number of uninteresting calls via performing 'Step Into' + 'Step Out' ('Shift + F8'). I was really surprised that IDEA provides much more convenient way to manage that - 'Main Menu -> Run -> Smart Step Into' ('Shift + F7' shortcut). Here is its execution result:



I.e. we can choose target method which body should be inspected. Really enjoy this feature!

Run to cursor



Another cool debugger feature I want to talk about is 'Run to cursor'. Consider the same code as above. Suppose that we setup breakpoints as mentioned at screenshot below, start application and wait while the first one is hit:



We can ask to proceed debugging and automatically pause when any of conditions below is met:

  • control flow reaches the line where editor caret is located at the moment;

  • any existing break point is hit before condition above;



I.e. we can execute 'Main Menu -> Run -> Run to Cursor' ('Alt + F9' shortcut) - that produces behavior described above. Here break point at 'baz()' method is reached before the line where editor caret is located now, hence, debug session automatically stops there.

However, it's possible to execute 'Force Run to Cursor' ('Ctrl + Alt + F9') - that makes debugger to temporary ignore any intermediate breakpoints and stop exactly at the line with caret.

I used to use the following trick before - set break point at target line, perform 'Resume Debug' ('F9'), remove break point. Will make a habit to use feature above instead since it's provide much more convenient way to achieve the same result and is more powerful in that it allows to ignore intermediate break points.

0 comments:

Post a Comment