Top Description Inners Fields Constructors Methods
org.eclipse.xtext.xbase.lib.util

public final Class ToStringBuilder

extends Object
Class Inheritance
Annotations
@Beta
@GwtCompatible
Imports
java.lang.reflect.Field, .Modifier, java.util.ArrayList, .Arrays, .Iterator, .List, org.eclipse.xtext.xbase.lib.Exceptions, com.google.common.annotations.Beta, .GwtCompatible, .GwtIncompatible, com.google.common.base.Strings

Helps with the construction of good Object#toString() representations.

You can customize the output using the builder-style methods ToStringBuilder#singleLine() ToStringBuilder#skipNulls() and ToStringBuilder#hideFieldNames().

You can either directly list fields to include via ToStringBuilder#add(String, Object) and ToStringBuilder#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.
Since
2.7

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private static class

Field Summary

Modifier and TypeField and Description
private final Object
private boolean
private final List<ToStringBuilder.Part>
private boolean
private boolean
private boolean
private static ToStringContext
private final String

Constructor Summary

AccessConstructor and Description
public
ToStringBuilder(final Object
the object to convert to a String
instance
)

Creates a new ToStringBuilder for the given object.

Method Summary

Modifier and TypeMethod and Description
public ToStringBuilder

Returns:

this
add
(final String
the field name to list the value under
fieldName
,
final Object
the value to add to the output
value
)

public ToStringBuilder

Returns:

this
add
(final Object
the value to add to the output without a field name
value
)

public ToStringBuilder

Returns:

this
addAllFields
()

Adds all fields declared in the object's class and its superclasses to the output.

public ToStringBuilder

Returns:

this
addDeclaredFields
()

Adds all fields declared directly in the object's class to the output

public ToStringBuilder

Returns:

this
addField
(final String
the name of the field to add to the output using reflection
fieldName
)

private ToStringBuilder
addField(final Field field)

private ToStringBuilder.Part
private ToStringBuilder
addPart(final Object value)

private ToStringBuilder
addPart(final String fieldName, final Object value)

private List<Field>
getAllDeclaredFields(final Class<?> clazz)

public ToStringBuilder

Returns:

this
hideFieldNames
()

Field names will not be included in the output.

private void
private void
public ToStringBuilder

Returns:

this
singleLine
()

Fields are printed on a single line, separated by commas instead of newlines

public ToStringBuilder

Returns:

this
skipNulls
()

Fields with null values will be excluded from the output

private String
public String

Returns:

the String representation of the processed object
toString
()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

public ToStringBuilder

Returns:

this
verbatimValues
()

By default, Iterables, Arrays and multiline Strings are pretty-printed.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

instanceback to summary
private final Object instance
multiLineback to summary
private boolean multiLine
partsback to summary
private final List<ToStringBuilder.Part> parts
prettyPrintback to summary
private boolean prettyPrint
showFieldNamesback to summary
private boolean showFieldNames
skipNullsback to summary
private boolean skipNulls
toStringContextback to summary
private static ToStringContext toStringContext
typeNameback to summary
private final String typeName

Constructor Detail

ToStringBuilderback to summary
public ToStringBuilder(final Object instance)

Creates a new ToStringBuilder for the given object. If you don't use reflection, then this instance is only used for obtaining its classes' simple name.

Parameters
instance:Object

the object to convert to a String

Method Detail

addback to summary
public ToStringBuilder add(final String fieldName, final Object value)
Parameters
fieldName:String

the field name to list the value under

value:Object

the value to add to the output

Returns:ToStringBuilder

this

addback to summary
public ToStringBuilder add(final Object value)
Parameters
value:Object

the value to add to the output without a field name

Returns:ToStringBuilder

this

addAllFieldsback to summary
public ToStringBuilder addAllFields()

Adds all fields declared in the object's class and its superclasses to the output.

Returns:ToStringBuilder

this

Annotations
@GwtIncompatible:Class.getDeclaredFields
addDeclaredFieldsback to summary
public ToStringBuilder addDeclaredFields()

Adds all fields declared directly in the object's class to the output

Returns:ToStringBuilder

this

Annotations
@GwtIncompatible:Class.getDeclaredFields
addFieldback to summary
public ToStringBuilder addField(final String fieldName)
Parameters
fieldName:String

the name of the field to add to the output using reflection

Returns:ToStringBuilder

this

Annotations
@GwtIncompatible:Class.getDeclaredField(String)
addFieldback to summary
private ToStringBuilder addField(final Field field)
Annotations
@GwtIncompatible:Field
addPartback to summary
private ToStringBuilder.Part addPart()
addPartback to summary
private ToStringBuilder addPart(final Object value)
addPartback to summary
private ToStringBuilder addPart(final String fieldName, final Object value)
getAllDeclaredFieldsback to summary
private List<Field> getAllDeclaredFields(final Class<?> clazz)
Annotations
@GwtIncompatible:Field
hideFieldNamesback to summary
public ToStringBuilder hideFieldNames()

Field names will not be included in the output. Useful for small classes.

Returns:ToStringBuilder

this

internalToStringback to summary
private void internalToString(final Object object, final ToStringBuilder.IndentationAwareStringBuilder sb)
serializeIterableback to summary
private void serializeIterable(final Iterable<?> object, final ToStringBuilder.IndentationAwareStringBuilder sb)
singleLineback to summary
public ToStringBuilder singleLine()

Fields are printed on a single line, separated by commas instead of newlines

Returns:ToStringBuilder

this

skipNullsback to summary
public ToStringBuilder skipNulls()

Fields with null values will be excluded from the output

Returns:ToStringBuilder

this

toSimpleReferenceStringback to summary
private String toSimpleReferenceString(final Object obj)
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

the String representation of the processed object

Annotations
@Override
verbatimValuesback to summary
public ToStringBuilder verbatimValues()

By default, Iterables, Arrays and multiline Strings are pretty-printed. Switching to their normal representation makes the toString method significantly faster.

Returns:ToStringBuilder

this

Since
2.9
org.eclipse.xtext.xbase.lib.util back to summary

private Class ToStringBuilder.IndentationAwareStringBuilder

extends Object
Class Inheritance
  • java.lang.Object
  • org.eclipse.xtext.xbase.lib.util.ToStringBuilder.IndentationAwareStringBuilder

Field Summary

Modifier and TypeField and Description
private final StringBuilder
private int
private final String
private final String

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public ToStringBuilder.IndentationAwareStringBuilder
append(final CharSequence string)

public ToStringBuilder.IndentationAwareStringBuilder
public ToStringBuilder.IndentationAwareStringBuilder
public ToStringBuilder.IndentationAwareStringBuilder
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

builderback to summary
private final StringBuilder builder
indentationback to summary
private int indentation
indentationStringback to summary
private final String indentationString
newLineStringback to summary
private final String newLineString

Constructor Detail

IndentationAwareStringBuilderback to summary
private IndentationAwareStringBuilder()

Method Detail

appendback to summary
public ToStringBuilder.IndentationAwareStringBuilder append(final CharSequence string)
decreaseIndentback to summary
public ToStringBuilder.IndentationAwareStringBuilder decreaseIndent()
increaseIndentback to summary
public ToStringBuilder.IndentationAwareStringBuilder increaseIndent()
newLineback to summary
public ToStringBuilder.IndentationAwareStringBuilder newLine()
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Annotations
@Override
org.eclipse.xtext.xbase.lib.util back to summary

private final Class ToStringBuilder.Part

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
private String
private Object

Constructor Summary

AccessConstructor and Description
private
Part()

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

fieldNameback to summary
private String fieldName
valueback to summary
private Object value

Constructor Detail

Partback to summary
private Part()