Thursday, June 01, 2006

Detail Formatter in Eclipse

Detail Formatter in Eclipse

While debugging the code, we put breakpoints in the code to see the value of a particular variable at that point. If the variable we are interested is a simple type we can easily see the value at the display frame but most of the time we need to look through the objects like Collection. Below code shows the composition of Hashtable.


If you select the collection variable on Debug mode the display frame will show something similar to this



If you want to see what is the value stored inside the Hashtable you need to navigator through the hierarchy like below



Needless to say it's laborious and boring task. Not a problem with Eclipse anymore!!!. If you know what the collection contains then you can see the value at the display frame without navigating through the hierarchy. Right click on the variable name in Variables panel and select "New Detail Formatter..." a new frame pop's-up. In the formatter frame enter the below code


Code inside the Detail Formatter:

StringBuffer buff = new StringBuffer();
Enumeration enum = this.keys();
while(enum.hasMoreElements())
{
Object key = enum.nextElement();
java.lang.ref.WeakReference obj = (java.lang.ref.WeakReference)this.get(key);
Object internalObj = obj.get();
buff.append(key.toString()+"="+internalObj.toString()+",");
}
return buff.toString();

Now if you select the variable again you can see the content of the Hashtable in the display frame without navigating through the hierarchy: