public class ToStringBuilder extends Object implements Builder<String>
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(java.lang.String).
 To append the toString from an object that is delegated
 to (or any other object), use appendToString(java.lang.String).
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.
| Constructor and Description | 
|---|
| ToStringBuilder(Object object)Constructs a builder for the specified object using the default output style. | 
| ToStringBuilder(Object object,
               ToStringStyle style)Constructs a builder for the specified object using the a defined output style. | 
| ToStringBuilder(Object object,
               ToStringStyle style,
               StringBuffer buffer)Constructs a builder for the specified object. | 
| Modifier and Type | Method and Description | 
|---|---|
| ToStringBuilder | append(boolean value)Append to the  toStringabooleanvalue. | 
| ToStringBuilder | append(boolean[] array)Append to the  toStringabooleanarray. | 
| ToStringBuilder | append(byte value)Append to the  toStringabytevalue. | 
| ToStringBuilder | append(byte[] array)Append to the  toStringabytearray. | 
| ToStringBuilder | append(char value)Append to the  toStringacharvalue. | 
| ToStringBuilder | append(char[] array)Append to the  toStringachararray. | 
| ToStringBuilder | append(double value)Append to the  toStringadoublevalue. | 
| ToStringBuilder | append(double[] array)Append to the  toStringadoublearray. | 
| ToStringBuilder | append(float value)Append to the  toStringafloatvalue. | 
| ToStringBuilder | append(float[] array)Append to the  toStringafloatarray. | 
| ToStringBuilder | append(int value)Append to the  toStringanintvalue. | 
| ToStringBuilder | append(int[] array)Append to the  toStringanintarray. | 
| ToStringBuilder | append(long value)Append to the  toStringalongvalue. | 
| ToStringBuilder | append(long[] array)Append to the  toStringalongarray. | 
| ToStringBuilder | append(Object obj)Append to the  toStringanObjectvalue. | 
| ToStringBuilder | append(Object[] array)Append to the  toStringanObjectarray. | 
| ToStringBuilder | append(short value)Append to the  toStringashortvalue. | 
| ToStringBuilder | append(short[] array)Append to the  toStringashortarray. | 
| ToStringBuilder | append(String fieldName,
      boolean value)Append to the  toStringabooleanvalue. | 
| ToStringBuilder | append(String fieldName,
      boolean[] array)Append to the  toStringabooleanarray. | 
| ToStringBuilder | append(String fieldName,
      boolean[] array,
      boolean fullDetail)Append to the  toStringabooleanarray. | 
| ToStringBuilder | append(String fieldName,
      byte value)Append to the  toStringanbytevalue. | 
| ToStringBuilder | append(String fieldName,
      byte[] array)Append to the  toStringabytearray. | 
| ToStringBuilder | append(String fieldName,
      byte[] array,
      boolean fullDetail)Append to the  toStringabytearray. | 
| ToStringBuilder | append(String fieldName,
      char value)Append to the  toStringacharvalue. | 
| ToStringBuilder | append(String fieldName,
      char[] array)Append to the  toStringachararray. | 
| ToStringBuilder | append(String fieldName,
      char[] array,
      boolean fullDetail)Append to the  toStringachararray. | 
| ToStringBuilder | append(String fieldName,
      double value)Append to the  toStringadoublevalue. | 
| ToStringBuilder | append(String fieldName,
      double[] array)Append to the  toStringadoublearray. | 
| ToStringBuilder | append(String fieldName,
      double[] array,
      boolean fullDetail)Append to the  toStringadoublearray. | 
| ToStringBuilder | append(String fieldName,
      float value)Append to the  toStringanfloatvalue. | 
| ToStringBuilder | append(String fieldName,
      float[] array)Append to the  toStringafloatarray. | 
| ToStringBuilder | append(String fieldName,
      float[] array,
      boolean fullDetail)Append to the  toStringafloatarray. | 
| ToStringBuilder | append(String fieldName,
      int value)Append to the  toStringanintvalue. | 
| ToStringBuilder | append(String fieldName,
      int[] array)Append to the  toStringanintarray. | 
| ToStringBuilder | append(String fieldName,
      int[] array,
      boolean fullDetail)Append to the  toStringanintarray. | 
| ToStringBuilder | append(String fieldName,
      long value)Append to the  toStringalongvalue. | 
| ToStringBuilder | append(String fieldName,
      long[] array)Append to the  toStringalongarray. | 
| ToStringBuilder | append(String fieldName,
      long[] array,
      boolean fullDetail)Append to the  toStringalongarray. | 
| ToStringBuilder | append(String fieldName,
      Object obj)Append to the  toStringanObjectvalue. | 
| ToStringBuilder | append(String fieldName,
      Object[] array)Append to the  toStringanObjectarray. | 
| ToStringBuilder | append(String fieldName,
      Object[] array,
      boolean fullDetail)Append to the  toStringanObjectarray. | 
| ToStringBuilder | append(String fieldName,
      Object obj,
      boolean fullDetail)Append to the  toStringanObjectvalue. | 
| ToStringBuilder | append(String fieldName,
      short value)Append to the  toStringanshortvalue. | 
| ToStringBuilder | append(String fieldName,
      short[] array)Append to the  toStringashortarray. | 
| ToStringBuilder | append(String fieldName,
      short[] array,
      boolean fullDetail)Append to the  toStringashortarray. | 
| ToStringBuilder | appendAsObjectToString(Object srcObject)Appends with the same format as the default  Object toString()
 method. | 
| ToStringBuilder | appendSuper(String superToString)Append the  toStringfrom the superclass. | 
| ToStringBuilder | appendToString(String toString)Append the  toStringfrom another object. | 
| String | build()Returns the String that was build as an object representation. | 
| static ToStringStyle | getDefaultStyle()Gets the default  ToStringStyleto use. | 
| Object | getObject()Returns the  Objectbeing output. | 
| StringBuffer | getStringBuffer()Gets the  StringBufferbeing populated. | 
| ToStringStyle | getStyle()Gets the  ToStringStylebeing used. | 
| static String | reflectionToString(Object object)Uses  ReflectionToStringBuilderto generate atoStringfor the specified object. | 
| static String | reflectionToString(Object object,
                  ToStringStyle style)Uses  ReflectionToStringBuilderto generate atoStringfor the specified object. | 
| static String | reflectionToString(Object object,
                  ToStringStyle style,
                  boolean outputTransients)Uses  ReflectionToStringBuilderto generate atoStringfor the specified object. | 
| static <T> String | reflectionToString(T object,
                  ToStringStyle style,
                  boolean outputTransients,
                  Class<? super T> reflectUpToClass)Uses  ReflectionToStringBuilderto generate atoStringfor the specified object. | 
| static void | setDefaultStyle(ToStringStyle style)Sets the default  ToStringStyleto use. | 
| String | toString()Returns the built  toString. | 
public ToStringBuilder(Object object)
Constructs a builder for the specified object using the default output style.
This default style is obtained from getDefaultStyle().
object - the Object to build a toString for, not recommended to be nullpublic ToStringBuilder(Object object, ToStringStyle style)
Constructs a builder for the specified object using the a defined output style.
If the style is null, the default style is used.
object - the Object to build a toString for, not recommended to be nullstyle - the style of the toString to create, null uses the default stylepublic ToStringBuilder(Object object, ToStringStyle style, StringBuffer buffer)
Constructs a builder for the specified object.
If the style is null, the default style is used.
If the buffer is null, a new one is created.
object - the Object to build a toString for, not recommended to be nullstyle - the style of the toString to create, null uses the default stylebuffer - the StringBuffer to populate, may be nullpublic static ToStringStyle getDefaultStyle()
Gets the default ToStringStyle to use.
This method gets a singleton default value, typically for the whole JVM.
 Changing this default should generally only be done during application startup.
 It is recommended to pass a ToStringStyle to the constructor instead
 of using this global default.
This method can be used from multiple threads.
 Internally, a volatile variable is used to provide the guarantee
 that the latest value set using setDefaultStyle(org.apache.commons.lang3.builder.ToStringStyle) is the value returned.
 It is strongly recommended that the default style is only changed during application startup.
One reason for changing the default could be to have a verbose style during development and a compact style in production.
ToStringStyle, never nullpublic static void setDefaultStyle(ToStringStyle style)
Sets the default ToStringStyle to use.
This method sets a singleton default value, typically for the whole JVM.
 Changing this default should generally only be done during application startup.
 It is recommended to pass a ToStringStyle to the constructor instead
 of changing this global default.
This method is not intended for use from multiple threads.
 Internally, a volatile variable is used to provide the guarantee
 that the latest value set is the value returned from getDefaultStyle().
style - the default ToStringStyleIllegalArgumentException - if the style is nullpublic static String reflectionToString(Object object)
Uses ReflectionToStringBuilder to generate a
 toString for the specified object.
object - the Object to be outputReflectionToStringBuilder.toString(Object)public static String reflectionToString(Object object, ToStringStyle style)
Uses ReflectionToStringBuilder to generate a
 toString for the specified object.
object - the Object to be outputstyle - the style of the toString to create, may be nullReflectionToStringBuilder.toString(Object,ToStringStyle)public static String reflectionToString(Object object, ToStringStyle style, boolean outputTransients)
Uses ReflectionToStringBuilder to generate a
 toString for the specified object.
object - the Object to be outputstyle - the style of the toString to create, may be nulloutputTransients - whether to include transient fieldsReflectionToStringBuilder.toString(Object,ToStringStyle,boolean)public static <T> String reflectionToString(T object, ToStringStyle style, boolean outputTransients, Class<? super T> reflectUpToClass)
Uses ReflectionToStringBuilder to generate a
 toString for the specified object.
T - the type of the objectobject - the Object to be outputstyle - the style of the toString to create, may be nulloutputTransients - whether to include transient fieldsreflectUpToClass - the superclass to reflect up to (inclusive), may be nullReflectionToStringBuilder.toString(Object,ToStringStyle,boolean,boolean,Class)public ToStringBuilder append(boolean value)
Append to the toString a boolean
 value.
value - the value to add to the toStringpublic ToStringBuilder append(boolean[] array)
Append to the toString a boolean
 array.
array - the array to add to the toStringpublic ToStringBuilder append(byte value)
Append to the toString a byte
 value.
value - the value to add to the toStringpublic ToStringBuilder append(byte[] array)
Append to the toString a byte
 array.
array - the array to add to the toStringpublic ToStringBuilder append(char value)
Append to the toString a char
 value.
value - the value to add to the toStringpublic ToStringBuilder append(char[] array)
Append to the toString a char
 array.
array - the array to add to the toStringpublic ToStringBuilder append(double value)
Append to the toString a double
 value.
value - the value to add to the toStringpublic ToStringBuilder append(double[] array)
Append to the toString a double
 array.
array - the array to add to the toStringpublic ToStringBuilder append(float value)
Append to the toString a float
 value.
value - the value to add to the toStringpublic ToStringBuilder append(float[] array)
Append to the toString a float
 array.
array - the array to add to the toStringpublic ToStringBuilder append(int value)
Append to the toString an int
 value.
value - the value to add to the toStringpublic ToStringBuilder append(int[] array)
Append to the toString an int
 array.
array - the array to add to the toStringpublic ToStringBuilder append(long value)
Append to the toString a long
 value.
value - the value to add to the toStringpublic ToStringBuilder append(long[] array)
Append to the toString a long
 array.
array - the array to add to the toStringpublic ToStringBuilder append(Object obj)
Append to the toString an Object
 value.
obj - the value to add to the toStringpublic ToStringBuilder append(Object[] array)
Append to the toString an Object
 array.
array - the array to add to the toStringpublic ToStringBuilder append(short value)
Append to the toString a short
 value.
value - the value to add to the toStringpublic ToStringBuilder append(short[] array)
Append to the toString a short
 array.
array - the array to add to the toStringpublic ToStringBuilder append(String fieldName, boolean value)
Append to the toString a boolean
 value.
fieldName - the field namevalue - the value to add to the toStringpublic ToStringBuilder append(String fieldName, boolean[] array)
Append to the toString a boolean
 array.
fieldName - the field namearray - the array to add to the hashCodepublic ToStringBuilder append(String fieldName, boolean[] array, boolean fullDetail)
Append to the toString a boolean
 array.
A boolean parameter controls the level of detail to show.
 Setting true will output the array in full. Setting
 false will output a summary, typically the size of
 the array.
fieldName - the field namearray - the array to add to the toStringfullDetail - true for detail, false
  for summary infopublic ToStringBuilder append(String fieldName, byte value)
Append to the toString an byte
 value.
fieldName - the field namevalue - the value to add to the toStringpublic ToStringBuilder append(String fieldName, byte[] array)
Append to the toString a byte array.
fieldName - the field namearray - the array to add to the toStringpublic ToStringBuilder append(String fieldName, byte[] array, boolean fullDetail)
Append to the toString a byte
 array.
A boolean parameter controls the level of detail to show.
 Setting true will output the array in full. Setting
 false will output a summary, typically the size of
 the array.
fieldName - the field namearray - the array to add to the toStringfullDetail - true for detail, false
  for summary infopublic ToStringBuilder append(String fieldName, char value)
Append to the toString a char
 value.
fieldName - the field namevalue - the value to add to the toStringpublic ToStringBuilder append(String fieldName, char[] array)
Append to the toString a char
 array.
fieldName - the field namearray - the array to add to the toStringpublic ToStringBuilder append(String fieldName, char[] array, boolean fullDetail)
Append to the toString a char
 array.
A boolean parameter controls the level of detail to show.
 Setting true will output the array in full. Setting
 false will output a summary, typically the size of
 the array.
fieldName - the field namearray - the array to add to the toStringfullDetail - true for detail, false
  for summary infopublic ToStringBuilder append(String fieldName, double value)
Append to the toString a double
 value.
fieldName - the field namevalue - the value to add to the toStringpublic ToStringBuilder append(String fieldName, double[] array)
Append to the toString a double
 array.
fieldName - the field namearray - the array to add to the toStringpublic ToStringBuilder append(String fieldName, double[] array, boolean fullDetail)
Append to the toString a double
 array.
A boolean parameter controls the level of detail to show.
 Setting true will output the array in full. Setting
 false will output a summary, typically the size of
 the array.
fieldName - the field namearray - the array to add to the toStringfullDetail - true for detail, false
  for summary infopublic ToStringBuilder append(String fieldName, float value)
Append to the toString an float
 value.
fieldName - the field namevalue - the value to add to the toStringpublic ToStringBuilder append(String fieldName, float[] array)
Append to the toString a float
 array.
fieldName - the field namearray - the array to add to the toStringpublic ToStringBuilder append(String fieldName, float[] array, boolean fullDetail)
Append to the toString a float
 array.
A boolean parameter controls the level of detail to show.
 Setting true will output the array in full. Setting
 false will output a summary, typically the size of
 the array.
fieldName - the field namearray - the array to add to the toStringfullDetail - true for detail, false
  for summary infopublic ToStringBuilder append(String fieldName, int value)
Append to the toString an int
 value.
fieldName - the field namevalue - the value to add to the toStringpublic ToStringBuilder append(String fieldName, int[] array)
Append to the toString an int
 array.
fieldName - the field namearray - the array to add to the toStringpublic ToStringBuilder append(String fieldName, int[] array, boolean fullDetail)
Append to the toString an int
 array.
A boolean parameter controls the level of detail to show.
 Setting true will output the array in full. Setting
 false will output a summary, typically the size of
 the array.
fieldName - the field namearray - the array to add to the toStringfullDetail - true for detail, false
  for summary infopublic ToStringBuilder append(String fieldName, long value)
Append to the toString a long
 value.
fieldName - the field namevalue - the value to add to the toStringpublic ToStringBuilder append(String fieldName, long[] array)
Append to the toString a long
 array.
fieldName - the field namearray - the array to add to the toStringpublic ToStringBuilder append(String fieldName, long[] array, boolean fullDetail)
Append to the toString a long
 array.
A boolean parameter controls the level of detail to show.
 Setting true will output the array in full. Setting
 false will output a summary, typically the size of
 the array.
fieldName - the field namearray - the array to add to the toStringfullDetail - true for detail, false
  for summary infopublic ToStringBuilder append(String fieldName, Object obj)
Append to the toString an Object
 value.
fieldName - the field nameobj - the value to add to the toStringpublic ToStringBuilder append(String fieldName, Object obj, boolean fullDetail)
Append to the toString an Object
 value.
fieldName - the field nameobj - the value to add to the toStringfullDetail - true for detail,
  false for summary infopublic ToStringBuilder append(String fieldName, Object[] array)
Append to the toString an Object
 array.
fieldName - the field namearray - the array to add to the toStringpublic ToStringBuilder append(String fieldName, Object[] array, boolean fullDetail)
Append to the toString an Object
 array.
A boolean parameter controls the level of detail to show.
 Setting true will output the array in full. Setting
 false will output a summary, typically the size of
 the array.
fieldName - the field namearray - the array to add to the toStringfullDetail - true for detail, false
  for summary infopublic ToStringBuilder append(String fieldName, short value)
Append to the toString an short
 value.
fieldName - the field namevalue - the value to add to the toStringpublic ToStringBuilder append(String fieldName, short[] array)
Append to the toString a short
 array.
fieldName - the field namearray - the array to add to the toStringpublic ToStringBuilder append(String fieldName, short[] array, boolean fullDetail)
Append to the toString a short
 array.
A boolean parameter controls the level of detail to show.
 Setting true will output the array in full. Setting
 false will output a summary, typically the size of
 the array.
fieldName - the field namearray - the array to add to the toStringfullDetail - true for detail, false
  for summary infopublic ToStringBuilder appendAsObjectToString(Object srcObject)
Appends with the same format as the default Object toString()
  method. Appends the class name followed by
 System.identityHashCode(java.lang.Object).
srcObject - the Object whose class name and id to outputpublic ToStringBuilder appendSuper(String superToString)
Append the toString from the superclass.
This method assumes that the superclass uses the same ToStringStyle
 as this one.
If superToString is null, no change is made.
superToString - the result of super.toString()public ToStringBuilder appendToString(String toString)
Append the toString from another object.
This method is useful where a class delegates most of the implementation of
 its properties to another class. You can then call toString() on
 the other class and pass the result into this method.
   private AnotherObject delegate;
   private String fieldInThisClass;
   public String toString() {
     return new ToStringBuilder(this).
       appendToString(delegate.toString()).
       append(fieldInThisClass).
       toString();
   }
 This method assumes that the other object uses the same ToStringStyle
 as this one.
If the toString is null, no change is made.
toString - the result of toString() on another objectpublic Object getObject()
Returns the Object being output.
public StringBuffer getStringBuffer()
Gets the StringBuffer being populated.
StringBuffer being populatedpublic ToStringStyle getStyle()
Gets the ToStringStyle being used.
ToStringStyle being usedpublic String toString()
Returns the built toString.
This method appends the end of data indicator, and can only be called once.
 Use getStringBuffer() to get the current string state.
If the object is null, return the style's nullText
public String build()
toString() implementation.build in interface Builder<String>toStringtoString()Copyright © 2001–2016 The Apache Software Foundation. All rights reserved.