Lang tip: ToStringBuilder - Quick trace statements

December 14th, 2007 by Hen

Resurrecting this from the data for the Commons blog.

It’s always a pain to be putting in tracing statements (aka System.out.println) and find that the object’s data is hidden away in private attributes. One option is to break out the debugger, another (security manager willing) is to use the org.apache.commons.lang.builder.ToStringBuilder to output the Object in full. Here’s the very, very simple line of code to get this done:

System.out.println("TASK: " + ToStringBuilder.reflectionToString(task));

One Response to “Lang tip: ToStringBuilder - Quick trace statements”

  1. Tom White Says:

    Here’s an equivalent, XML version: http://problemsworthyofattack.blogspot.com/2007/09/debugging-with-xstream.html

    Tom