Debugging more complex structures like collections or any other container-like objects may be painful, because we don’t have straight access to what we are looking for. Finding the right stuff in container which doesn’t keep order of elements (like HashMap) is even harder. Fortunately Eclipse IDE comes with simple yet powerful solution for this problem – Logical Structures.
What are Logical Structures?
While debugging code we can see in Variables view every instance that is accessible at the point when we suspended code execution. By default this view shows every detail (each field) of each variable. This is pretty good for JavaBeans but when it comes to collections, maps or any container structures (like panels in GUI application) Variables view contains a lot of garbage we are actually not interested in at all (like loadFactor or threshold for HashMap). Logical Structures set us free from boring browsing through properties of properties of properties of object we take under investigation, because they can represent logical view of object instead of structural one.
Logical Struture in action
Let’s look at simple example. On the screenshot we can see Map that in Variables view is very hard to debug – we just can’t access it’s content directly.
When we however turn on “Show Logical Structure” feature, located on this view’s toolbar, we may access content of map directly.
Personally I find this feature of Eclipse Debugger very useful, especially because we may also define our own Logical Structures for any object!
Defining our own logical struture
On preference page Window -> Preferences -> Java -> Debug -> Logica Structures we are free to define our own Logical Structures for any object.
I decided to create Logical Structure for java.util.Map type which will display map contents sorted by keys. This may be useful if we are working with large maps and we want to find quickly entries which keys have similar names. Let’s add new Logical Structure are shown on the screenshot:
[yeah, looks awful, but I was not able to do it with Comparator - Eclipse complains that it cannot create anonymous class in evaluated expressions]
After we do this our new structure will appear on the list of all registered structures.
Note that now we have two Logical Structures for type java.util.Map. It’s not a problem. We can use both – in Variables window we may just switch between them using “Show Structure As” option from context menu of Map instance:
What’s the effect? Pretty nice – we have map entries sorted during our debugging session! That’s really great – try it yourself.