Top Description Fields Constructors Methods
org.apache.commons.lang3.builder

public Class ToStringBuilder

extends Object
implements Builder<String>
Class Inheritance
All Implemented Interfaces
org.apache.commons.lang3.builder.Builder
Known Direct Subclasses
org.apache.commons.lang3.builder.ReflectionToStringBuilder
Imports
java.util.Objects, org.apache.commons.lang3.ObjectUtils

Assists in implementing Object#toString() methods.

This class enables a good and consistent toString() to be built for any class or object. This class aims to simplify the process by:

To use this class write code as follows:

public class Person {
  String name;
  int age;
  boolean smoker;

  ...

  public String toString() {
    return new ToStringBuilder(this).
      append("name", name).
      append("age", age).
      append("smoker", smoker).
      toString();
  }
}

This will produce a toString of the format: Person@7f54[name=Stephen,age=29,smoker=false]

To add the superclass toString, use appendSuper. To append the toString from an object that is delegated to (or any other object), use appendToString.

Alternatively, there is a method that uses reflection to determine the fields to test. Because these fields are usually private, the method, reflectionToString, uses AccessibleObject.setAccessible to change the visibility of the fields. This will fail under a security manager, unless the appropriate permissions are set up correctly. It is also slower than testing explicitly.

A typical invocation for this method would look like:

public String toString() {
  return ToStringBuilder.reflectionToString(this);
}

You can also use the builder to debug 3rd party objects:

System.out.println("An object: " + ToStringBuilder.reflectionToString(anObject));

The exact format of the toString is determined by the ToStringStyle passed into the constructor.

Since
1.0

Field Summary

Modifier and TypeField and Description
private final StringBuffer
buffer

Current toString buffer, not null.

private static volatile ToStringStyle
defaultStyle

The default style of output to use, not null.

private final Object
object

The object being output, may be null.

private final ToStringStyle
style

The style of output to use, not null.

Constructor Summary

AccessConstructor and Description
public
ToStringBuilder(final Object
the Object to build a toString for, not recommended to be null
object
)

Constructs a builder for the specified object using the default output style.

public
ToStringBuilder(final Object
the Object to build a toString for, not recommended to be null
object
,
final ToStringStyle
the style of the toString to create, null uses the default style
style
)

Constructs a builder for the specified object using the defined output style.

public
ToStringBuilder(final Object
the Object to build a toString for, not recommended to be null
object
,
ToStringStyle
the style of the toString to create, null uses the default style
style
,
StringBuffer
the StringBuffer to populate, may be null
buffer
)

Constructs a builder for the specified object.

Method Summary

Modifier and TypeMethod and Description
public ToStringBuilder

Returns:

this instance.
append
(final boolean
the value to add to the toString
value
)

Append to the toString a boolean value.

public ToStringBuilder

Returns:

this instance.
append
(final boolean[]
the array to add to the toString
array
)

Append to the toString a boolean array.

public ToStringBuilder

Returns:

this instance.
append
(final byte
the value to add to the toString
value
)

Append to the toString a byte value.

public ToStringBuilder

Returns:

this instance.
append
(final byte[]
the array to add to the toString
array
)

Append to the toString a byte array.

public ToStringBuilder

Returns:

this instance.
append
(final char
the value to add to the toString
value
)

Append to the toString a char value.

public ToStringBuilder

Returns:

this instance.
append
(final char[]
the array to add to the toString
array
)

Append to the toString a char array.

public ToStringBuilder

Returns:

this instance.
append
(final double
the value to add to the toString
value
)

Append to the toString a double value.

public ToStringBuilder

Returns:

this instance.
append
(final double[]
the array to add to the toString
array
)

Append to the toString a double array.

public ToStringBuilder

Returns:

this instance.
append
(final float
the value to add to the toString
value
)

Append to the toString a float value.

public ToStringBuilder

Returns:

this instance.
append
(final float[]
the array to add to the toString
array
)

Append to the toString a float array.

public ToStringBuilder

Returns:

this instance.
append
(final int
the value to add to the toString
value
)

Append to the toString an int value.

public ToStringBuilder

Returns:

this instance.
append
(final int[]
the array to add to the toString
array
)

Append to the toString an int array.

public ToStringBuilder

Returns:

this instance.
append
(final long
the value to add to the toString
value
)

Append to the toString a long value.

public ToStringBuilder

Returns:

this instance.
append
(final long[]
the array to add to the toString
array
)

Append to the toString a long array.

public ToStringBuilder

Returns:

this instance.
append
(final Object
the value to add to the toString
obj
)

Append to the toString an Object value.

public ToStringBuilder

Returns:

this instance.
append
(final Object[]
the array to add to the toString
array
)

Append to the toString an Object array.

public ToStringBuilder

Returns:

this instance.
append
(final short
the value to add to the toString
value
)

Append to the toString a short value.

public ToStringBuilder

Returns:

this instance.
append
(final short[]
the array to add to the toString
array
)

Append to the toString a short array.

public ToStringBuilder

Returns:

this instance.
append
(final String
the field name
fieldName
,
final boolean
the value to add to the toString
value
)

Append to the toString a boolean value.

public ToStringBuilder

Returns:

this instance.
append
(final String
the field name
fieldName
,
final boolean[]
the array to add to the hashCode
array
)

Append to the toString a boolean array.

public ToStringBuilder

Returns:

this instance.
append
(final String
the field name
fieldName
,
final boolean[]
the array to add to the toString
array
,
final boolean
true for detail, false for summary info
fullDetail
)

Append to the toString a boolean array.

public ToStringBuilder

Returns:

this instance.
append
(final String
the field name
fieldName
,
final byte
the value to add to the toString
value
)

Append to the toString an byte value.

public ToStringBuilder

Returns:

this instance.
append
(final String
the field name
fieldName
,
final byte[]
the array to add to the toString
array
)

Append to the toString a byte array.

public ToStringBuilder

Returns:

this instance.
append
(final String
the field name
fieldName
,
final byte[]
the array to add to the toString
array
,
final boolean
true for detail, false for summary info
fullDetail
)

Append to the toString a byte array.

public ToStringBuilder

Returns:

this instance.
append
(final String
the field name
fieldName
,
final char
the value to add to the toString
value
)

Append to the toString a char value.

public ToStringBuilder

Returns:

this instance.
append
(final String
the field name
fieldName
,
final char[]
the array to add to the toString
array
)

Append to the toString a char array.

public ToStringBuilder

Returns:

this instance.
append
(final String
the field name
fieldName
,
final char[]
the array to add to the toString
array
,
final boolean
true for detail, false for summary info
fullDetail
)

Append to the toString a char array.

public ToStringBuilder

Returns:

this instance.
append
(final String
the field name
fieldName
,
final double
the value to add to the toString
value
)

Append to the toString a double value.

public ToStringBuilder

Returns:

this instance.
append
(final String
the field name
fieldName
,
final double[]
the array to add to the toString
array
)

Append to the toString a double array.

public ToStringBuilder

Returns:

this instance.
append
(final String
the field name
fieldName
,
final double[]
the array to add to the toString
array
,
final boolean
true for detail, false for summary info
fullDetail
)

Append to the toString a double array.

public ToStringBuilder

Returns:

this instance.
append
(final String
the field name
fieldName
,
final float
the value to add to the toString
value
)

Append to the toString an float value.

public ToStringBuilder

Returns:

this instance.
append
(final String
the field name
fieldName
,
final float[]
the array to add to the toString
array
)

Append to the toString a float array.

public ToStringBuilder

Returns:

this instance.
append
(final String
the field name
fieldName
,
final float[]
the array to add to the toString
array
,
final boolean
true for detail, false for summary info
fullDetail
)

Append to the toString a float array.

public ToStringBuilder

Returns:

this instance.
append
(final String
the field name
fieldName
,
final int
the value to add to the toString
value
)

Append to the toString an int value.

public ToStringBuilder

Returns:

this instance.
append
(final String
the field name
fieldName
,
final int[]
the array to add to the toString
array
)

Append to the toString an int array.

public ToStringBuilder

Returns:

this instance.
append
(final String
the field name
fieldName
,
final int[]
the array to add to the toString
array
,
final boolean
true for detail, false for summary info
fullDetail
)

Append to the toString an int array.

public ToStringBuilder

Returns:

this instance.
append
(final String
the field name
fieldName
,
final long
the value to add to the toString
value
)

Append to the toString a long value.

public ToStringBuilder

Returns:

this instance.
append
(final String
the field name
fieldName
,
final long[]
the array to add to the toString
array
)

Append to the toString a long array.

public ToStringBuilder

Returns:

this instance.
append
(final String
the field name
fieldName
,
final long[]
the array to add to the toString
array
,
final boolean
true for detail, false for summary info
fullDetail
)

Append to the toString a long array.

public ToStringBuilder

Returns:

this instance.
append
(final String
the field name
fieldName
,
final Object
the value to add to the toString
obj
)

Append to the toString an Object value.

public ToStringBuilder

Returns:

this instance.
append
(final String
the field name
fieldName
,
final Object
the value to add to the toString
obj
,
final boolean
true for detail, false for summary info
fullDetail
)

Append to the toString an Object value.

public ToStringBuilder

Returns:

this instance.
append
(final String
the field name
fieldName
,
final Object[]
the array to add to the toString
array
)

Append to the toString an Object array.

public ToStringBuilder

Returns:

this instance.
append
(final String
the field name
fieldName
,
final Object[]
the array to add to the toString
array
,
final boolean
true for detail, false for summary info
fullDetail
)

Append to the toString an Object array.

public ToStringBuilder

Returns:

this instance.
append
(final String
the field name
fieldName
,
final short
the value to add to the toString
value
)

Append to the toString an short value.

public ToStringBuilder

Returns:

this instance.
append
(final String
the field name
fieldName
,
final short[]
the array to add to the toString
array
)

Append to the toString a short array.

public ToStringBuilder

Returns:

this instance.
append
(final String
the field name
fieldName
,
final short[]
the array to add to the toString
array
,
final boolean
true for detail, false for summary info
fullDetail
)

Append to the toString a short array.

public ToStringBuilder

Returns:

this instance.
appendAsObjectToString
(final Object
the Object whose class name and id to output
srcObject
)

Appends with the same format as the default Object toString() method.

public ToStringBuilder

Returns:

this instance.
appendSuper
(final String
the result of super.toString()
superToString
)

Append the toString from the superclass.

public ToStringBuilder

Returns:

this instance.
appendToString
(final String
the result of toString() on another object
toString
)

Append the toString from another object.

public String

Returns:

the String toString
build
()

Implements org.apache.commons.lang3.builder.Builder.build.

Returns the String that was build as an object representation.

public static ToStringStyle

Returns:

the default ToStringStyle, never null
getDefaultStyle
()

Gets the default ToStringStyle to use.

public Object

Returns:

The object being output.
getObject
()

Gets the Object being output.

public StringBuffer

Returns:

the StringBuffer being populated
getStringBuffer
()

Gets the StringBuffer being populated.

public ToStringStyle

Returns:

the ToStringStyle being used
getStyle
()

Gets the ToStringStyle being used.

public static String

Returns:

the String result
reflectionToString
(final Object
the Object to be output
object
)

Uses ReflectionToStringBuilder to generate a toString for the specified object.

public static String

Returns:

the String result
reflectionToString
(final Object
the Object to be output
object
,
final ToStringStyle
the style of the toString to create, may be null
style
)

Uses ReflectionToStringBuilder to generate a toString for the specified object.

public static String

Returns:

the String result
reflectionToString
(final Object
the Object to be output
object
,
final ToStringStyle
the style of the toString to create, may be null
style
,
final boolean
whether to include transient fields
outputTransients
)

Uses ReflectionToStringBuilder to generate a toString for the specified object.

public static <
the type of the object
T
>
String

Returns:

the String result
reflectionToString
(final T
the Object to be output
object
,
final ToStringStyle
the style of the toString to create, may be null
style
,
final boolean
whether to include transient fields
outputTransients
,
final Class<? super T>
the superclass to reflect up to (inclusive), may be null
reflectUpToClass
)

Uses ReflectionToStringBuilder to generate a toString for the specified object.

public static void
setDefaultStyle(final ToStringStyle
the default ToStringStyle
style
)

Sets the default ToStringStyle to use.

public String

Returns:

the String toString
toString
()

Overrides java.lang.Object.toString.

Returns the built toString.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait