ahc.util.string
Class ToString

java.lang.Object
  |
  +--ahc.util.string.ToString

public class ToString
extends java.lang.Object

This class provides a generic toString method for arbitrary objects. It uses reflection and recursive descent to create a string representation for an object.

If there are several references to the same object (i.e. ref1 == ref2, not just equals) in the graph, the object is printed only once, and only a marker is inserted for further references. This also guarantees that cyclic references are handled gracefully rather than causing endless recursion.

USAGE:

The simplest way to use this class is to just call ToString.defaultToString (o);. That returns a default string representation of the object.

This class is however configurable to display varying levels of detail. Three preconfigured instances MIN_DETAIL, DEFAULT and MAX_DETAIL are provided as public static final variables that create minimum, common and maximum detail respectively. To create maximum detail, you would write ToString.MAX_DETAIL.toString (o). Note that the instance method is toString whereas the static method is genericToString.

Even finer control over the level of detail is possible by creating a custom instance of ToString using the desired combination of flags.

Author:
Arno Haase

Field Summary
static ToString DEFAULT
           
static ToString MAX_DETAIL
           
static ToString MIN_DETAIL
           
 
Constructor Summary
ToString(boolean filterStatic, boolean filterTransient, boolean alwaysUseToStringImpl, boolean collectionsAsPrimitives, boolean javaLangAsPrimitives, boolean escapeStrings)
          Creates a ToString instance based on a custom configuration.
 
Method Summary
static java.lang.String defaultToString(java.lang.Object o)
          returns a string representation of an object that is configured at a default level of detail.
 java.lang.String genericToString(java.lang.Object o, int maxDepth)
          returns a string representation of an object with a limit on the depth of the recursive descent.
 java.lang.String primitiveToString(java.lang.Object o)
          returns a string representation of an object without recursive descent.
 java.lang.String toString(java.lang.Object o)
          returns a string representation of an object
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT

public static final ToString DEFAULT

MIN_DETAIL

public static final ToString MIN_DETAIL

MAX_DETAIL

public static final ToString MAX_DETAIL
Constructor Detail

ToString

public ToString(boolean filterStatic,
                boolean filterTransient,
                boolean alwaysUseToStringImpl,
                boolean collectionsAsPrimitives,
                boolean javaLangAsPrimitives,
                boolean escapeStrings)
Creates a ToString instance based on a custom configuration.

Parameters:
filterStatic - controls whether static variables are filtered out (true) or included (false).
filterTransient - controls whether transient variables are filtered out (true) or included (false).
alwaysUseToStringImpl - controls what to do if one of the objects overrides toString(). A value of true means that an overridden toString method is used, false means that the reflective algorithm is applied regardless of an overridden toString method.
collectionsAsPrimitives - controls how instances of java.util.Collection and java.util.Map are handled. A value of true means that a compact default representation is used, false switches of special treatment.
javaLangAsPrimitives - controls whether instances of classes from java.lang are displayed using their toString methods (true) or like any other object (false).
escapeStrings - controls whether strings are displayed "as is" (false) or preprocessed so that special characters that affect layout are replaced by standard character sequences (true).
Method Detail

defaultToString

public static java.lang.String defaultToString(java.lang.Object o)
returns a string representation of an object that is configured at a default level of detail.


toString

public java.lang.String toString(java.lang.Object o)
returns a string representation of an object


genericToString

public java.lang.String genericToString(java.lang.Object o,
                                        int maxDepth)
returns a string representation of an object with a limit on the depth of the recursive descent. This can e.g. be helpful to display just the vicinity of a node in a large graph.


primitiveToString

public java.lang.String primitiveToString(java.lang.Object o)
returns a string representation of an object without recursive descent. This method provides special treatment of collections and escapes strings, but otherwise the toString method is called (but in a null-safe way).