org.unitils.core.util
Class ObjectFormatter

java.lang.Object
  extended by org.unitils.core.util.ObjectFormatter

public class ObjectFormatter
extends java.lang.Object

A class for generating a string representation of any object, array or primitive value.

Non-primitive objects are processed recursively so that a string representation of inner objects is also generated. Too avoid too much output, this recursion is limited with a given maximum depth.

Author:
Tim Ducheyne, Filip Neven

Field Summary
protected  int maxDepth
          The maximum recursion depth
 
Constructor Summary
ObjectFormatter()
          Creates a formatter with a maximum recursion depth of 5.
ObjectFormatter(int maxDepth)
          Creates a formatter with the given maximum recursion depth.
 
Method Summary
 java.lang.String format(java.lang.Object object)
          Gets the string representation of the given object.
protected  void formatArray(java.lang.Object array, int currentDepth, java.lang.StringBuilder result)
          Formats the given array.
protected  void formatCollection(java.util.Collection<?> collection, int currentDepth, java.lang.StringBuilder result)
          Formats the given collection.
protected  void formatFields(java.lang.Object object, java.lang.Class<?> clazz, int currentDepth, java.lang.StringBuilder result)
          Formats the field values of the given object.
protected  void formatImpl(java.lang.Object object, int currentDepth, java.lang.StringBuilder result)
          Actual implementation of the formatting.
protected  void formatMap(java.util.Map<?,?> map, int currentDepth, java.lang.StringBuilder result)
          Formats the given map.
protected  boolean formatMock(java.lang.Object object, java.lang.StringBuilder result)
           
protected  void formatObject(java.lang.Object object, int currentDepth, java.lang.StringBuilder result)
          Formats the given object by formatting the inner fields.
protected  boolean formatProxy(java.lang.Object object, java.lang.StringBuilder result)
           
protected  java.lang.Class<?> getDummyObjectClass()
           
protected  java.lang.Class<?> getProxyUtilsClass()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

maxDepth

protected int maxDepth
The maximum recursion depth

Constructor Detail

ObjectFormatter

public ObjectFormatter()
Creates a formatter with a maximum recursion depth of 5.


ObjectFormatter

public ObjectFormatter(int maxDepth)
Creates a formatter with the given maximum recursion depth.

NOTE: there is no cycle detection. A large max depth value can cause lots of output in case of a cycle.

Parameters:
maxDepth - The max depth > 0
Method Detail

format

public java.lang.String format(java.lang.Object object)
Gets the string representation of the given object.

Parameters:
object - The instance
Returns:
The string representation, not null

formatImpl

protected void formatImpl(java.lang.Object object,
                          int currentDepth,
                          java.lang.StringBuilder result)
Actual implementation of the formatting.

Parameters:
object - The instance
currentDepth - The current recursion depth
result - The builder to append the result to, not null

formatArray

protected void formatArray(java.lang.Object array,
                           int currentDepth,
                           java.lang.StringBuilder result)
Formats the given array.

Parameters:
array - The array, not null
currentDepth - The current recursion depth
result - The builder to append the result to, not null

formatCollection

protected void formatCollection(java.util.Collection<?> collection,
                                int currentDepth,
                                java.lang.StringBuilder result)
Formats the given collection.

Parameters:
collection - The collection, not null
currentDepth - The current recursion depth
result - The builder to append the result to, not null

formatMap

protected void formatMap(java.util.Map<?,?> map,
                         int currentDepth,
                         java.lang.StringBuilder result)
Formats the given map.

Parameters:
map - The map, not null
currentDepth - The current recursion depth
result - The builder to append the result to, not null

formatObject

protected void formatObject(java.lang.Object object,
                            int currentDepth,
                            java.lang.StringBuilder result)
Formats the given object by formatting the inner fields.

Parameters:
object - The object, not null
currentDepth - The current recursion depth
result - The builder to append the result to, not null

formatFields

protected void formatFields(java.lang.Object object,
                            java.lang.Class<?> clazz,
                            int currentDepth,
                            java.lang.StringBuilder result)
Formats the field values of the given object.

Parameters:
object - The object, not null
clazz - The class for which to format the fields, not null
currentDepth - The current recursion depth
result - The builder to append the result to, not null

formatMock

protected boolean formatMock(java.lang.Object object,
                             java.lang.StringBuilder result)

formatProxy

protected boolean formatProxy(java.lang.Object object,
                              java.lang.StringBuilder result)

getDummyObjectClass

protected java.lang.Class<?> getDummyObjectClass()
Returns:
The interface that represents a dummy object. If the DummyObject interface is not in the classpath, null is returned.

getProxyUtilsClass

protected java.lang.Class<?> getProxyUtilsClass()
Returns:
The proxy utils. null if not in classpath


Copyright © 2009. All Rights Reserved.