Tips and Tricks for Debugging in Eclipse
In this article I will describe some tips and tricks for debugging your applications in Eclipse. I will cover the debug details formatter, breakpoints on classes only, as well as how to display all references and instances of a particular object or class.
Debug details formatter
download music from mp3 search engine
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:download Eclipse
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:
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? You just have to go to the main menu and choose Run->Add Class Load Breakpoint…
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.
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.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)