RecursiveToStringStyle¶
- public class org.apache.commons.lang3.builder.RecursiveToStringStyle extends ToStringStyle[source]¶
Works with
ToStringBuilderto 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
Constructors¶
Constructs a new instance with no output-length limit. |
|
Constructs a new instance with an output-length limit. |
Methods¶
Tests whether or not to recursively format the given |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Creates a new |
- protected boolean accept(Class<?> clazz)[source]¶
Tests whether or not to recursively format the given
Class.By default, this method always filters out the following:
Numbersubclasses
- Parameters:
clazz – The class to test.
- Returns:
Whether or not to recursively format instances of the given
Class.
- @Override public void appendDetail(StringBuffer buffer, String fieldName, Object value)[source]¶
Appends to the
toStringanObjectvalue, printing the full detail of theObject.- Parameters:
buffer – The
StringBufferto populate.fieldName – The field name, typically not used as already appended.
value – The value to add to the
toString, notnull.
- @Override protected void appendDetail(StringBuffer buffer, String fieldName, Collection<?> coll)[source]¶
Appends to the
toStringaCollection.- Parameters:
buffer – The
StringBufferto populate.fieldName – The field name, typically not used as already appended.
coll – The
Collectionto add to thetoString, notnull.
- @Override protected void appendInternal(StringBuffer buffer, String fieldName, Object value, boolean detail)[source]¶
Appends to the
toStringanObject, 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 keeps a per-thread set of objects already rendered in detail during the current top-level call. 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.- Parameters:
buffer – The
StringBufferto populate.fieldName – The field name, typically not used as already appended.
value – The value to add to the
toString, notnull.detail – output detail or not.
- public static RecursiveToStringStyle.Builder builder()[source]¶
Creates a new
BuilderforRecursiveToStringStyleinstances.- Returns:
a new
BuilderforRecursiveToStringStyleinstances.