Class RecursiveToStringStyle
- java.lang.Object
-
- org.apache.commons.lang3.builder.ToStringStyle
-
- org.apache.commons.lang3.builder.RecursiveToStringStyle
-
- All Implemented Interfaces:
java.io.Serializable
- Direct Known Subclasses:
MultilineRecursiveToStringStyle
public class RecursiveToStringStyle extends ToStringStyle
Works withToStringBuilderto create a "deep"toString.To use this class write code as follows:
public class Job { String title; ... } public class Person { String name; int age; boolean smoker; Job job; ... public String toString() { return new ReflectionToStringBuilder(this, new RecursiveToStringStyle()).toString(); } }This will produce a toString of the format:
Person@7f54[name=Stephen,age=29,smoker=false,job=Job@43cd2[title=Manager]]Graph safety: within one top-level
toString()call, each object is rendered in detail at most once. A second (identity-equal) occurrence of an object (whether through a true cycle or through a shared (acyclic) reference) is rendered in the abbreviatedObject.toString()format instead of being re-traversed. This keeps traversal cost linear in the size of the object graph; without it, shared references (reference "diamonds") would be re-traversed exponentially. An optional output-length limit can be set viaRecursiveToStringStyle.Builder.setMaxOutputLength(int); once the produced string reaches the limit, further nested objects are replaced by a"...<truncated>"marker.- Since:
- 3.2
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classRecursiveToStringStyle.BuilderBuilder forRecursiveToStringStyleinstances.
-
Field Summary
-
Fields inherited from class org.apache.commons.lang3.builder.ToStringStyle
DEFAULT_STYLE, JSON_STYLE, MULTI_LINE_STYLE, NO_CLASS_NAME_STYLE, NO_FIELD_NAMES_STYLE, SHORT_PREFIX_STYLE, SIMPLE_STYLE
-
-
Constructor Summary
Constructors Constructor Description RecursiveToStringStyle()Constructs a new instance with no output-length limit.RecursiveToStringStyle(int maxOutputLength)Constructs a new instance with an output-length limit.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected booleanaccept(java.lang.Class<?> clazz)Tests whether or not to recursively format the givenClass.voidappend(java.lang.StringBuffer buffer, java.lang.String fieldName, java.lang.Object[] array, java.lang.Boolean fullDetail)Appends to thetoStringanObjectarray.voidappendDetail(java.lang.StringBuffer buffer, java.lang.String fieldName, java.lang.Object value)Appends to thetoStringanObjectvalue, printing the full detail of theObject.protected voidappendDetail(java.lang.StringBuffer buffer, java.lang.String fieldName, java.util.Collection<?> coll)Appends to thetoStringaCollection.protected voidappendInternal(java.lang.StringBuffer buffer, java.lang.String fieldName, java.lang.Object value, boolean detail)Appends to thetoStringanObject, correctly interpreting its type.static RecursiveToStringStyle.Builderbuilder()Creates a newRecursiveToStringStyle.BuilderforRecursiveToStringStyleinstances.-
Methods inherited from class org.apache.commons.lang3.builder.ToStringStyle
append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, appendClassName, appendContentEnd, appendContentStart, appendCyclicObject, appendDetail, appendDetail, appendDetail, appendDetail, appendDetail, appendDetail, appendDetail, appendDetail, appendDetail, appendDetail, appendDetail, appendDetail, appendDetail, appendDetail, appendDetail, appendDetail, appendDetail, appendDetail, appendDetail, appendEnd, appendFieldEnd, appendFieldSeparator, appendFieldStart, appendIdentityHashCode, appendNullText, appendStart, appendSummary, appendSummary, appendSummary, appendSummary, appendSummary, appendSummary, appendSummary, appendSummary, appendSummary, appendSummary, appendSummarySize, appendSuper, appendToString, getArrayEnd, getArraySeparator, getArrayStart, getContentEnd, getContentStart, getFieldNameValueSeparator, getFieldSeparator, getNullText, getRegistry, getShortClassName, getSizeEndText, getSizeStartText, getSummaryObjectEndText, getSummaryObjectStartText, isArrayContentDetail, isDefaultFullDetail, isFieldSeparatorAtEnd, isFieldSeparatorAtStart, isFullDetail, isUseClassName, isUseFieldNames, isUseIdentityHashCode, isUseShortClassName, reflectionAppendArrayDetail, removeLastFieldSeparator, setArrayContentDetail, setArrayEnd, setArraySeparator, setArrayStart, setContentEnd, setContentStart, setDefaultFullDetail, setFieldNameValueSeparator, setFieldSeparator, setFieldSeparatorAtEnd, setFieldSeparatorAtStart, setNullText, setSizeEndText, setSizeStartText, setSummaryObjectEndText, setSummaryObjectStartText, setUseClassName, setUseFieldNames, setUseIdentityHashCode, setUseShortClassName
-
-
-
-
Constructor Detail
-
RecursiveToStringStyle
public RecursiveToStringStyle()
Constructs a new instance with no output-length limit.
-
RecursiveToStringStyle
public RecursiveToStringStyle(int maxOutputLength)
Constructs a new instance with an output-length limit.- Parameters:
maxOutputLength- once the produced string reaches this length, further nested objects are replaced by a"...<truncated>"marker;0means unlimited.- Since:
- 3.21.0
-
-
Method Detail
-
builder
public static RecursiveToStringStyle.Builder builder()
Creates a newRecursiveToStringStyle.BuilderforRecursiveToStringStyleinstances.- Returns:
- a new
RecursiveToStringStyle.BuilderforRecursiveToStringStyleinstances.
-
accept
protected boolean accept(java.lang.Class<?> clazz)
Tests whether or not to recursively format the givenClass.By default, this method always filters out the following:
- Boxed primitives, see
ClassUtils.isPrimitiveWrapper(Class) StringNumbersubclassesAtomicBooleanMutableBoolean
- Parameters:
clazz- The class to test.- Returns:
- Whether or not to recursively format instances of the given
Class.
- Boxed primitives, see
-
append
public void append(java.lang.StringBuffer buffer, java.lang.String fieldName, java.lang.Object[] array, java.lang.Boolean fullDetail)Description copied from class:ToStringStyleAppends to thetoStringanObjectarray.- Overrides:
appendin classToStringStyle- Parameters:
buffer- TheStringBufferto populate.fieldName- The field name.array- The array to add to the toString.fullDetail-truefor detail,falsefor summary info,nullfor style decides.
-
appendDetail
protected void appendDetail(java.lang.StringBuffer buffer, java.lang.String fieldName, java.util.Collection<?> coll)Description copied from class:ToStringStyleAppends to thetoStringaCollection.- Overrides:
appendDetailin classToStringStyle- Parameters:
buffer- TheStringBufferto populate.fieldName- The field name, typically not used as already appended.coll- TheCollectionto add to thetoString, notnull.
-
appendDetail
public void appendDetail(java.lang.StringBuffer buffer, java.lang.String fieldName, java.lang.Object value)Description copied from class:ToStringStyleAppends to thetoStringanObjectvalue, printing the full detail of theObject.- Overrides:
appendDetailin classToStringStyle- Parameters:
buffer- TheStringBufferto populate.fieldName- The field name, typically not used as already appended.value- The value to add to thetoString, notnull.
-
appendInternal
protected void appendInternal(java.lang.StringBuffer buffer, java.lang.String fieldName, java.lang.Object value, boolean detail)Appends to thetoStringanObject, correctly interpreting its type.This method performs the main lookup by Class type to correctly route arrays,
Collections,Maps andObjectsto the appropriate method.Either detail or summary views can be specified.
If a cycle is detected, an object will be appended with the
Object.toString()format.In addition to the cycle check performed by the superclass, this implementation shares a visited-object set across nested builders in the active traversal. The outer builder retains the set between appends and releases it on completion. Values that this style would traverse structurally (see
accept(Class)) are rendered in detail at most once; later identity-equal occurrences are appended in the abbreviatedObject.toString()format. This bounds the traversal to one visit per object, so shared (acyclic) references cannot cause exponential re-traversal. If an output-length limit was configured and the buffer has reached it, a"...<truncated>"marker is appended instead of the value.- Overrides:
appendInternalin classToStringStyle- Parameters:
buffer- TheStringBufferto populate.fieldName- The field name, typically not used as already appended.value- The value to add to thetoString, notnull.detail- output detail or not.
-
-