Eclipse debugger part I – Detail formatters

With this entry I would like to start a series of articles about one of the most important feature of Eclipse IDE which is it’s debugger. Eclipse debugger is a very powerful tool, which, when used properly, may result not only in much faster bug neutralization, but also in doing this in very pleasant way.

First feature I’d like to describe are Detail Formatters. Detail formatters are used for overriding toString() methods of any object that is visible in Variables view when we debug our code. They work in non intrusive way, so nothing in our code has to change in order to use them.

How it works?

Let’s start with some Java Bean:

?Download Person.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.jcake.debugger;
 
import java.util.Date;
 
public class Person {
 
  private String name;
  private Date birthDate;
 
  public Person(String name, Date date) {
    this.name = name;
    birthDate = date;
  }
 
  public Date getBirthDate() {
    return birthDate;
  }
 
  public void setBirthDate(Date birthDate) {
    this.birthDate = birthDate;
  }
 
  public String getName() {
    return name;
  }
 
  public void setName(String name) {
    this.name = name;
  }
}

Executing:

System.out.println(new Person("X Y", new Date()));

results in something similar to:

com.jcake.debugger.Person@1d009b4,

which is far from being verbose and descriptive. The same we may see in debugger’s Variables view:

Variables view with default formatter

It would be much more productive if we saw all interesting values from Person object without doing any actions like expanding object’s properties in Variable view. This is where Detail Formatter may help.

Applying Detail Formatter

We may easily change the default behaviour of the debugger and instruct it that any time it shows object of type Person it should not display it’s toString() representation, but instead it should use some custom code written by us which is much more appropriate for debugging purposes. To enable Detail Formatter right-click on the object and choose Edit Detail Formatter… option:

Enabling Detailed Formatter

In window that will pop up we must enter code snippet that will be used as overridden toString() method. Code assist is enabled, so we may use our favourite CTRL+SPACE to auto complete variable, type or method names. We may use direct field access, because Detail Formatter’s snippet is executed “inside the object” – we may write anything we would write in regular toString() method. For example typing:

Sample detail formatter snippet

will result in more verbose, customized object representation which will be applied to any instance of Person class that will appear in debugged code.

Detail Formatter applied

At any time we may remove detail formatter by choosing Remove Detail Formatter from object’s context menu in Variables view, or disable it by selecting checkbox in formatter’s window.

We may also manage all registered formatters on preference page Java -> Debug -> Detail Formatters.

When to use Detail Formatters?

  • when toString() method does not provide enough details for debugging purposes,
  • when we are debugging some complex object, but we are interested only in a subset of information returned by toString() method,
  • when we are debugging collections and default collection formatter is not something we would expect,
  • when we are debugging some third party library and we cannot change it’s toString() method.

 

Share:
  • Digg
  • Wykop
  • del.icio.us
  • Twitter
  • Facebook
  • Google Bookmarks
  • LinkedIn
  • FriendFeed

Tags: ,

3 Responses to “Eclipse debugger part I – Detail formatters”

  1. John says:

    Hey thanks for this post. Is there a way to do all of this automatically? By that I mean having every object display values or being able to look any collection without having to code that into the formatter. I had eclipse 3.4 on my laptop which worked that way out of the box but when I tried to install it on my desktop (which is running ubuntu 3.10 64 bit) it is not the default setting.
    Thanks

  2. Piotr Maj says:

    Well, for almost each project I’m involved in I attach commons-lang library which have ToStringBuilder class. So I create detailed formatters which contains:

    org.apache.commons.lang.builder.ToStringBuilder.reflectionToString(this, org.apache.commons.lang.builder.ToStringStyle.MULTI_LINE_STYLE)

    Attaching this formatter to java.lang.Object should do it globally. Beware however of cycles in collections.

Leave a Reply