The visitor pattern often comes to mind when you need to operate on a graph of objects (like JSON, XML, or Java beans). Unfortunately, the visitor pattern uses call backs which are difficult to control from the calling code.  For example, it’s not easy to conditionally skip a branch with all its child branches and leaves from a callback.  This how-to will instead use the iterator pattern to traverse your Java object graph and create a human readable debug string.  The iterator will be general enough for you to use in other ways, as I did in building a tool to search Java objects using XPath or to record exceptions in StackHunter.

The APIs

This blog creates two separate tools for you to use: StringGenerator and ObjectIterator.

 

Continue reading: Convert Java Objects to String With the Iterator Pattern...