@Beta
@GwtCompatible
public final class ToStringBuilder
extends java.lang.Object
Object.toString()
representations.
You can customize the output using the builder-style methods singleLine()
skipNulls()
and hideFieldNames()
.
You can either directly list fields to include via add(String, Object)
and add(Object)
or you can let the builder do it automatically using reflection, either including the fields declared in this class or including all superclasses.
The builder will automatically handle cycles in the object tree. It also pretty prints arrays and Iterables.
This class is not thread safe.Constructor and Description |
---|
ToStringBuilder(java.lang.Object instance)
Creates a new ToStringBuilder for the given object.
|
Modifier and Type | Method and Description |
---|---|
ToStringBuilder |
add(java.lang.Object value) |
ToStringBuilder |
add(java.lang.String fieldName,
java.lang.Object value) |
ToStringBuilder |
addAllFields()
Adds all fields declared in the object's class and its superclasses to the output.
|
ToStringBuilder |
addDeclaredFields()
Adds all fields declared directly in the object's class to the output
|
ToStringBuilder |
addField(java.lang.String fieldName) |
ToStringBuilder |
hideFieldNames()
Field names will not be included in the output.
|
ToStringBuilder |
singleLine()
Fields are printed on a single line, separated by commas instead of newlines
|
ToStringBuilder |
skipNulls()
Fields with null values will be excluded from the output
|
java.lang.String |
toString() |
ToStringBuilder |
verbatimValues()
By default, Iterables, Arrays and multiline Strings are pretty-printed.
|
public ToStringBuilder(java.lang.Object instance)
instance
- the object to convert to a Stringpublic ToStringBuilder singleLine()
public ToStringBuilder skipNulls()
public ToStringBuilder hideFieldNames()
public ToStringBuilder verbatimValues()
@GwtIncompatible(value="Class.getDeclaredFields") public ToStringBuilder addDeclaredFields()
@GwtIncompatible(value="Class.getDeclaredFields") public ToStringBuilder addAllFields()
@GwtIncompatible(value="Class.getDeclaredField(String)") public ToStringBuilder addField(java.lang.String fieldName)
fieldName
- the name of the field to add to the output using reflectionpublic ToStringBuilder add(java.lang.String fieldName, java.lang.Object value)
value
- the value to add to the outputfieldName
- the fiel name to list the value underpublic ToStringBuilder add(java.lang.Object value)
value
- the value to add to the output without a field namepublic java.lang.String toString()
toString
in class java.lang.Object