org.unitils.core.util
Class ObjectFormatter

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

public class ObjectFormatter
extends 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  ArrayAndCollectionFormatter arrayAndCollectionFormatter
           
protected  int maxDepth
           
protected  int maxNrArrayOrCollectionElements
           
static String MOCK_NAME_CHAIN_SEPARATOR
           
 
Constructor Summary
ObjectFormatter()
          Creates a formatter with a maximum recursion depth of 3.
ObjectFormatter(int maxDepth, int maxNrArrayOrCollectionElements)
          Creates a formatter with the given maximum recursion depth.
 
Method Summary
 String format(Object object)
          Gets the string representation of the given object.
protected  boolean formatCharacter(Object object, Class<?> type, StringBuilder result)
           
protected  void formatFields(Object object, Class<?> clazz, int currentDepth, StringBuilder result)
          Formats the field values of the given object.
protected  boolean formatFile(Object object, StringBuilder result)
           
protected  void formatImpl(Object object, int currentDepth, StringBuilder result)
          Actual implementation of the formatting.
protected  boolean formatJavaLang(Object object, StringBuilder result, Class<?> type)
           
protected  boolean formatMock(Object object, StringBuilder result)
           
protected  boolean formatNumberOrDate(Object object, StringBuilder result)
           
protected  void formatObject(Object object, int currentDepth, StringBuilder result)
          Formats the given object by formatting the inner fields.
protected  boolean formatPrimitiveOrEnum(Object object, Class<?> type, StringBuilder result)
           
protected  boolean formatProxy(Object object, Class<?> type, StringBuilder result)
           
protected  boolean formatString(Object object, StringBuilder result)
           
protected  Class<?> getDummyObjectClass()
           
protected  Class<?> getProxyUtilsClass()
           
protected  boolean isDummy(Object object)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MOCK_NAME_CHAIN_SEPARATOR

public static final String MOCK_NAME_CHAIN_SEPARATOR
See Also:
Constant Field Values

maxDepth

protected int maxDepth

maxNrArrayOrCollectionElements

protected int maxNrArrayOrCollectionElements

arrayAndCollectionFormatter

protected ArrayAndCollectionFormatter arrayAndCollectionFormatter
Constructor Detail

ObjectFormatter

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


ObjectFormatter

public ObjectFormatter(int maxDepth,
                       int maxNrArrayOrCollectionElements)
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
maxNrArrayOrCollectionElements - The maximum nr of elements for arrays and collections to display > 0
Method Detail

format

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

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

formatImpl

protected void formatImpl(Object object,
                          int currentDepth,
                          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

formatJavaLang

protected boolean formatJavaLang(Object object,
                                 StringBuilder result,
                                 Class<?> type)

formatPrimitiveOrEnum

protected boolean formatPrimitiveOrEnum(Object object,
                                        Class<?> type,
                                        StringBuilder result)

formatCharacter

protected boolean formatCharacter(Object object,
                                  Class<?> type,
                                  StringBuilder result)

formatNumberOrDate

protected boolean formatNumberOrDate(Object object,
                                     StringBuilder result)

formatString

protected boolean formatString(Object object,
                               StringBuilder result)

formatObject

protected void formatObject(Object object,
                            int currentDepth,
                            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(Object object,
                            Class<?> clazz,
                            int currentDepth,
                            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(Object object,
                             StringBuilder result)

isDummy

protected boolean isDummy(Object object)

formatProxy

protected boolean formatProxy(Object object,
                              Class<?> type,
                              StringBuilder result)

formatFile

protected boolean formatFile(Object object,
                             StringBuilder result)

getDummyObjectClass

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

getProxyUtilsClass

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


Copyright © 2011. All Rights Reserved.