Class RecursiveToStringStyle
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
MultilineRecursiveToStringStyle
ToStringBuilder to 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 abbreviated Object.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 via RecursiveToStringStyle.Builder.setMaxOutputLength(int); once the produced string
reaches the limit, further nested objects are replaced by a "...<truncated>" marker.
- Since:
- 3.2
- See Also:
-
Nested Class Summary
Nested Classes -
Field Summary
Fields inherited from class ToStringStyle
DEFAULT_STYLE, JSON_STYLE, MULTI_LINE_STYLE, NO_CLASS_NAME_STYLE, NO_FIELD_NAMES_STYLE, SHORT_PREFIX_STYLE, SIMPLE_STYLEModifier and TypeFieldDescriptionstatic final ToStringStyleThe default toString style.static final ToStringStyleThe JSON toString style.static final ToStringStyleThe multi line toString style.static final ToStringStyleThe no class name toString style.static final ToStringStyleThe no field names toString style.static final ToStringStyleThe short prefix toString style.static final ToStringStyleThe simple toString style. -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new instance with no output-length limit.RecursiveToStringStyle(int maxOutputLength) Constructs a new instance with an output-length limit. -
Method Summary
Modifier and TypeMethodDescriptionprotected booleanTests whether or not to recursively format the givenClass.voidappend(StringBuffer buffer, String fieldName, Object[] array, Boolean fullDetail) Appends to thetoStringanObjectarray.voidappendDetail(StringBuffer buffer, String fieldName, Object value) protected voidappendDetail(StringBuffer buffer, String fieldName, Collection<?> coll) Appends to thetoStringaCollection.protected voidappendInternal(StringBuffer buffer, String fieldName, Object value, boolean detail) Appends to thetoStringanObject, correctly interpreting its type.builder()Creates a newRecursiveToStringStyle.BuilderforRecursiveToStringStyleinstances.Methods inherited from class 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, setUseShortClassNameModifier and TypeMethodDescriptionvoidappend(StringBuffer buffer, String fieldName, boolean value) Appends to thetoStringabooleanvalue.voidappend(StringBuffer buffer, String fieldName, boolean[] array, Boolean fullDetail) Appends to thetoStringabooleanarray.voidappend(StringBuffer buffer, String fieldName, byte value) Appends to thetoStringabytevalue.voidappend(StringBuffer buffer, String fieldName, byte[] array, Boolean fullDetail) Appends to thetoStringabytearray.voidappend(StringBuffer buffer, String fieldName, char value) Appends to thetoStringacharvalue.voidappend(StringBuffer buffer, String fieldName, char[] array, Boolean fullDetail) Appends to thetoStringachararray.voidappend(StringBuffer buffer, String fieldName, double value) Appends to thetoStringadoublevalue.voidappend(StringBuffer buffer, String fieldName, double[] array, Boolean fullDetail) Appends to thetoStringadoublearray.voidappend(StringBuffer buffer, String fieldName, float value) Appends to thetoStringafloatvalue.voidappend(StringBuffer buffer, String fieldName, float[] array, Boolean fullDetail) Appends to thetoStringafloatarray.voidappend(StringBuffer buffer, String fieldName, int value) Appends to thetoStringanintvalue.voidappend(StringBuffer buffer, String fieldName, int[] array, Boolean fullDetail) Appends to thetoStringanintarray.voidappend(StringBuffer buffer, String fieldName, long value) Appends to thetoStringalongvalue.voidappend(StringBuffer buffer, String fieldName, long[] array, Boolean fullDetail) Appends to thetoStringalongarray.voidappend(StringBuffer buffer, String fieldName, short value) Appends to thetoStringashortvalue.voidappend(StringBuffer buffer, String fieldName, short[] array, Boolean fullDetail) Appends to thetoStringashortarray.voidappend(StringBuffer buffer, String fieldName, Object value, Boolean fullDetail) protected voidappendClassName(StringBuffer buffer, Object object) Appends to thetoStringthe class name.protected voidappendContentEnd(StringBuffer buffer) Appends to thetoStringthe content end.protected voidappendContentStart(StringBuffer buffer) Appends to thetoStringthe content start.protected voidappendCyclicObject(StringBuffer buffer, String fieldName, Object value) Appends to thetoStringanObjectvalue that has been detected to participate in a cycle.protected voidappendDetail(StringBuffer buffer, String fieldName, boolean value) Appends to thetoStringabooleanvalue.protected voidappendDetail(StringBuffer buffer, String fieldName, boolean[] array) Appends to thetoStringthe detail of abooleanarray.protected voidappendDetail(StringBuffer buffer, String fieldName, byte value) Appends to thetoStringabytevalue.protected voidappendDetail(StringBuffer buffer, String fieldName, byte[] array) Appends to thetoStringthe detail of abytearray.protected voidappendDetail(StringBuffer buffer, String fieldName, char value) Appends to thetoStringacharvalue.protected voidappendDetail(StringBuffer buffer, String fieldName, char[] array) Appends to thetoStringthe detail of achararray.protected voidappendDetail(StringBuffer buffer, String fieldName, double value) Appends to thetoStringadoublevalue.protected voidappendDetail(StringBuffer buffer, String fieldName, double[] array) Appends to thetoStringthe detail of adoublearray.protected voidappendDetail(StringBuffer buffer, String fieldName, float value) Appends to thetoStringafloatvalue.protected voidappendDetail(StringBuffer buffer, String fieldName, float[] array) Appends to thetoStringthe detail of afloatarray.protected voidappendDetail(StringBuffer buffer, String fieldName, int value) Appends to thetoStringanintvalue.protected voidappendDetail(StringBuffer buffer, String fieldName, int[] array) Appends to thetoStringthe detail of anintarray.protected voidappendDetail(StringBuffer buffer, String fieldName, int i, Object item) Appends to thetoStringthe detail of anObjectarray item.protected voidappendDetail(StringBuffer buffer, String fieldName, long value) Appends to thetoStringalongvalue.protected voidappendDetail(StringBuffer buffer, String fieldName, long[] array) Appends to thetoStringthe detail of alongarray.protected voidappendDetail(StringBuffer buffer, String fieldName, short value) Appends to thetoStringashortvalue.protected voidappendDetail(StringBuffer buffer, String fieldName, short[] array) Appends to thetoStringthe detail of ashortarray.protected voidappendDetail(StringBuffer buffer, String fieldName, Object[] array) Appends to thetoStringthe detail of anObjectarray.protected voidappendDetail(StringBuffer buffer, String fieldName, Map<?, ?> map) Appends to thetoStringaMap.voidappendEnd(StringBuffer buffer, Object object) Appends to thetoStringthe end of data indicator.protected voidappendFieldEnd(StringBuffer buffer, String fieldName) Appends to thetoStringthe field end.protected voidappendFieldSeparator(StringBuffer buffer) Appends to thetoStringthe field separator.protected voidappendFieldStart(StringBuffer buffer, String fieldName) Appends to thetoStringthe field start.protected voidappendIdentityHashCode(StringBuffer buffer, Object object) Appends theSystem.identityHashCode(java.lang.Object).protected voidappendNullText(StringBuffer buffer, String fieldName) Appends to thetoStringan indicator fornull.voidappendStart(StringBuffer buffer, Object object) Appends to thetoStringthe start of data indicator.protected voidappendSummary(StringBuffer buffer, String fieldName, boolean[] array) Appends to thetoStringa summary of abooleanarray.protected voidappendSummary(StringBuffer buffer, String fieldName, byte[] array) Appends to thetoStringa summary of abytearray.protected voidappendSummary(StringBuffer buffer, String fieldName, char[] array) Appends to thetoStringa summary of achararray.protected voidappendSummary(StringBuffer buffer, String fieldName, double[] array) Appends to thetoStringa summary of adoublearray.protected voidappendSummary(StringBuffer buffer, String fieldName, float[] array) Appends to thetoStringa summary of afloatarray.protected voidappendSummary(StringBuffer buffer, String fieldName, int[] array) Appends to thetoStringa summary of anintarray.protected voidappendSummary(StringBuffer buffer, String fieldName, long[] array) Appends to thetoStringa summary of alongarray.protected voidappendSummary(StringBuffer buffer, String fieldName, short[] array) Appends to thetoStringa summary of ashortarray.protected voidappendSummary(StringBuffer buffer, String fieldName, Object value) protected voidappendSummary(StringBuffer buffer, String fieldName, Object[] array) Appends to thetoStringa summary of anObjectarray.protected voidappendSummarySize(StringBuffer buffer, String fieldName, int size) Appends to thetoStringa size summary.voidappendSuper(StringBuffer buffer, String superToString) Appends to thetoStringthe superclass toString.voidappendToString(StringBuffer buffer, String toString) Appends to thetoStringanother toString.protected StringGets the array end text.protected StringGets the array separator text.protected StringGets the array start text.protected StringGets the content end text.protected StringGets the content start text.protected StringGets the field name value separator text.protected StringGets the field separator text.protected StringGets the text to output whennullfound.Gets the registry of objects being traversed by thereflectionToStringmethods in the current thread.protected StringgetShortClassName(Class<?> cls) Gets the short class name for a class.protected StringGets the end text to output when aCollection,Mapor array size is output.protected StringGets the start text to output when aCollection,Mapor array size is output.protected StringGets the end text to output when anObjectis output in summary mode.protected StringGets the start text to output when anObjectis output in summary mode.protected booleanTests whether to output array content detail.protected booleanTests whether full detail is used when the caller does not specify a detail level.protected booleanTests whether the field separator should be added at the end of each buffer.protected booleanTests whether the field separator should be added at the start of each buffer.protected booleanisFullDetail(Boolean fullDetailRequest) Tests whether this field should be output in full detail.protected booleanTests whether to use the class name.protected booleanTests whether to use the field names passed in.protected booleanTests whether to use the identity hash code.protected booleanTests whether short class names should be output.protected voidreflectionAppendArrayDetail(StringBuffer buffer, String fieldName, Object array) Appends to thetoStringthe detail of an array type.protected voidremoveLastFieldSeparator(StringBuffer buffer) Remove the last field separator from the buffer.protected voidsetArrayContentDetail(boolean arrayContentDetail) Sets whether to output array content detail.protected voidsetArrayEnd(String arrayEnd) Sets the array end text.protected voidsetArraySeparator(String arraySeparator) Sets the array separator text.protected voidsetArrayStart(String arrayStart) Sets the array start text.protected voidsetContentEnd(String contentEnd) Sets the content end text.protected voidsetContentStart(String contentStart) Sets the content start text.protected voidsetDefaultFullDetail(boolean defaultFullDetail) Sets whether to use full detail when the caller doesn't specify.protected voidsetFieldNameValueSeparator(String fieldNameValueSeparator) Sets the field name value separator text.protected voidsetFieldSeparator(String fieldSeparator) Sets the field separator text.protected voidsetFieldSeparatorAtEnd(boolean fieldSeparatorAtEnd) Sets whether the field separator should be added at the end of each buffer.protected voidsetFieldSeparatorAtStart(boolean fieldSeparatorAtStart) Sets whether the field separator should be added at the start of each buffer.protected voidsetNullText(String nullText) Sets the text to output whennullfound.protected voidsetSizeEndText(String sizeEndText) Sets the end text to output when aCollection,Mapor array size is output.protected voidsetSizeStartText(String sizeStartText) Sets the start text to output when aCollection,Mapor array size is output.protected voidsetSummaryObjectEndText(String summaryObjectEndText) Sets the end text to output when anObjectis output in summary mode.protected voidsetSummaryObjectStartText(String summaryObjectStartText) Sets the start text to output when anObjectis output in summary mode.protected voidsetUseClassName(boolean useClassName) Sets whether to use the class name.protected voidsetUseFieldNames(boolean useFieldNames) Sets whether to use the field names passed in.protected voidsetUseIdentityHashCode(boolean useIdentityHashCode) Sets whether to use the identity hash code.protected voidsetUseShortClassName(boolean useShortClassName) Sets whether to output short or long class names.
-
Constructor Details
-
RecursiveToStringStyle
public RecursiveToStringStyle()Constructs a new instance with no output-length limit. -
RecursiveToStringStyle
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 Details
-
builder
Creates a newRecursiveToStringStyle.BuilderforRecursiveToStringStyleinstances.- Returns:
- a new
RecursiveToStringStyle.BuilderforRecursiveToStringStyleinstances.
-
accept
-
append
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
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
Description copied from class:ToStringStyle- 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
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.
-