Monday, October 1, 2007

Tips & Tricks for debugging in Eclipse

During the Eclipse Summer School, which took place last week at our university, I had a chance to be a tutor. On Thurdsay I had classes about debugging in Eclipse. People were very eager to learn new things about the Eclipse debugger, so they asked me questions, thanks to which I had a chance to remind (and learn) myself some tricks in the debugger . So I thought that maybe not everyone knew about new features available in Eclipse 3.3 or even about features that had been in Eclipse for ages. Here are the features I find most interesting (and not so obvious):

1) Debug details formatter

When you debug your application you probably use the Variables view. One of the disadvantages is that you cannot see the data inside complex objects (unless they implement toString() method). Eclipse allows you to write your own formatter for displaying the content you are interested in. You can do it in the Eclipse preferences under Java->Debug->Detail Formatters.

Lets consider the following class:

When you want to check the value of the an object of this class during the debugging session you have to expand the object and inspect the value of all it’s parts. (see the picture below).

Let’s write our own formatter:

And now in the Variables view we can see that the person object is shown differently:

If you want to see the formatter in the labels for variables you have to select the following option in the Eclipse preferences:

This allows you to see the result of your formatter in the value field of the Variables view:

I find this option very useful also for the standard classes (e.g. ArrayList), then instead of:

I can see:

2) Toggling class breakpoints on the class without the source code

When you want to stop the execution of your application in the moment when some of your class is loaded by the class loader you can use the class breakpoints (just toggle the breakpoint on the line when the declaration of the class begins). But what about the situation when you want the same behavior when a class, which you don’t have the sources for, is loaded? How to toggle such a breakpoint without the source code? Actually this was the question from one of our students and I had no idea how to answer it. Fortunately Jacek was in the room with me and he found the answer very quickly. You just have to go to the main menu and choose Run->Add Class Load Breakpoint…

3) Displaying all references of an object (new in Eclipse 3.3)

Sometimes it is worth to see all references of some object. How to achieve this? There are two ways of doing this. If you want to display references of the specific object, right click on the object in the Variables view and choose All References. The popup window will appear with all references. If you want to see all references of all available objects go to the menu in the Variables view and choose Java->Show References.

4) Displaying all instances of a class (new in Eclipse 3.3)

If you want to see all instances of a class just mark this class in the Java editor and choose All Instanced from the context menu.