Class ToStringBuilder
- Direct Known Subclasses:
- ReflectionToStringBuilder
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:
- allowing field names
- handling all types consistently
- handling nulls consistently
- outputting arrays and multi-dimensional arrays
- enabling the detail level to be controlled for Objects and Collections
- handling class hierarchies
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.
- Since:
- 1.0
- 
Constructor SummaryConstructorsConstructorDescriptionToStringBuilder(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 defined output style.ToStringBuilder(Object object, ToStringStyle style, StringBuffer buffer) Constructs a builder for the specified object.
- 
Method SummaryModifier and TypeMethodDescriptionappend(boolean value) Append to thetoStringabooleanvalue.append(boolean[] array) Append to thetoStringabooleanarray.append(byte value) Append to thetoStringabytevalue.append(byte[] array) Append to thetoStringabytearray.append(char value) Append to thetoStringacharvalue.append(char[] array) Append to thetoStringachararray.append(double value) Append to thetoStringadoublevalue.append(double[] array) Append to thetoStringadoublearray.append(float value) Append to thetoStringafloatvalue.append(float[] array) Append to thetoStringafloatarray.append(int value) Append to thetoStringanintvalue.append(int[] array) Append to thetoStringanintarray.append(long value) Append to thetoStringalongvalue.append(long[] array) Append to thetoStringalongarray.append(short value) Append to thetoStringashortvalue.append(short[] array) Append to thetoStringashortarray.Append to thetoStringanObjectvalue.Append to thetoStringanObjectarray.Append to thetoStringabooleanvalue.Append to thetoStringabooleanarray.Append to thetoStringabooleanarray.Append to thetoStringanbytevalue.Append to thetoStringabytearray.Append to thetoStringabytearray.Append to thetoStringacharvalue.Append to thetoStringachararray.Append to thetoStringachararray.Append to thetoStringadoublevalue.Append to thetoStringadoublearray.Append to thetoStringadoublearray.Append to thetoStringanfloatvalue.Append to thetoStringafloatarray.Append to thetoStringafloatarray.Append to thetoStringanintvalue.Append to thetoStringanintarray.Append to thetoStringanintarray.Append to thetoStringalongvalue.Append to thetoStringalongarray.Append to thetoStringalongarray.Append to thetoStringanshortvalue.Append to thetoStringashortarray.Append to thetoStringashortarray.Append to thetoStringanObjectvalue.Append to thetoStringanObjectarray.Append to thetoStringanObjectarray.Append to thetoStringanObjectvalue.appendAsObjectToString(Object srcObject) Appends with the same format as the defaultObject toString()method.appendSuper(String superToString) Append thetoStringfrom the superclass.appendToString(String toString) Append thetoStringfrom another object.build()Returns the String that was build as an object representation.static ToStringStyleGets the defaultToStringStyleto use.Returns theObjectbeing output.Gets theStringBufferbeing populated.getStyle()Gets theToStringStylebeing used.static StringreflectionToString(Object object) UsesReflectionToStringBuilderto generate atoStringfor the specified object.static StringreflectionToString(Object object, ToStringStyle style) UsesReflectionToStringBuilderto generate atoStringfor the specified object.static StringreflectionToString(Object object, ToStringStyle style, boolean outputTransients) UsesReflectionToStringBuilderto generate atoStringfor the specified object.static <T> StringreflectionToString(T object, ToStringStyle style, boolean outputTransients, Class<? super T> reflectUpToClass) UsesReflectionToStringBuilderto generate atoStringfor the specified object.static voidsetDefaultStyle(ToStringStyle style) Sets the defaultToStringStyleto use.toString()Returns the builttoString.
- 
Constructor Details- 
ToStringBuilderConstructs a builder for the specified object using the default output style.This default style is obtained from getDefaultStyle().- Parameters:
- object- the Object to build a- toStringfor, not recommended to be null
 
- 
ToStringBuilderConstructs a builder for the specified object using the defined output style.If the style is null, the default style is used.- Parameters:
- object- the Object to build a- toStringfor, not recommended to be null
- style- the style of the- toStringto create, null uses the default style
 
- 
ToStringBuilderConstructs 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.- Parameters:
- object- the Object to build a- toStringfor, not recommended to be null
- style- the style of the- toStringto create, null uses the default style
- buffer- the- StringBufferto populate, may be null
 
 
- 
- 
Method Details- 
getDefaultStyleGets the defaultToStringStyleto 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 ToStringStyleto the constructor instead of using this global default.This method can be used from multiple threads. Internally, a volatilevariable is used to provide the guarantee that the latest value set usingsetDefaultStyle(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. - Returns:
- the default ToStringStyle, never null
 
- 
reflectionToStringUsesReflectionToStringBuilderto generate atoStringfor the specified object.- Parameters:
- object- the Object to be output
- Returns:
- the String result
- See Also:
 
- 
reflectionToStringUsesReflectionToStringBuilderto generate atoStringfor the specified object.- Parameters:
- object- the Object to be output
- style- the style of the- toStringto create, may be- null
- Returns:
- the String result
- See Also:
 
- 
reflectionToStringpublic static String reflectionToString(Object object, ToStringStyle style, boolean outputTransients) UsesReflectionToStringBuilderto generate atoStringfor the specified object.- Parameters:
- object- the Object to be output
- style- the style of the- toStringto create, may be- null
- outputTransients- whether to include transient fields
- Returns:
- the String result
- See Also:
 
- 
reflectionToStringpublic static <T> String reflectionToString(T object, ToStringStyle style, boolean outputTransients, Class<? super T> reflectUpToClass) UsesReflectionToStringBuilderto generate atoStringfor the specified object.- Type Parameters:
- T- the type of the object
- Parameters:
- object- the Object to be output
- style- the style of the- toStringto create, may be- null
- outputTransients- whether to include transient fields
- reflectUpToClass- the superclass to reflect up to (inclusive), may be- null
- Returns:
- the String result
- Since:
- 2.0
- See Also:
 
- 
setDefaultStyleSets the defaultToStringStyleto 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 ToStringStyleto the constructor instead of changing this global default.This method is not intended for use from multiple threads. Internally, a volatilevariable is used to provide the guarantee that the latest value set is the value returned fromgetDefaultStyle().- Parameters:
- style- the default- ToStringStyle
- Throws:
- NullPointerException- if the style is- null
 
- 
appendAppend to thetoStringabooleanvalue.- Parameters:
- value- the value to add to the- toString
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringabooleanarray.- Parameters:
- array- the array to add to the- toString
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringabytevalue.- Parameters:
- value- the value to add to the- toString
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringabytearray.- Parameters:
- array- the array to add to the- toString
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringacharvalue.- Parameters:
- value- the value to add to the- toString
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringachararray.- Parameters:
- array- the array to add to the- toString
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringadoublevalue.- Parameters:
- value- the value to add to the- toString
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringadoublearray.- Parameters:
- array- the array to add to the- toString
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringafloatvalue.- Parameters:
- value- the value to add to the- toString
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringafloatarray.- Parameters:
- array- the array to add to the- toString
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringanintvalue.- Parameters:
- value- the value to add to the- toString
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringanintarray.- Parameters:
- array- the array to add to the- toString
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringalongvalue.- Parameters:
- value- the value to add to the- toString
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringalongarray.- Parameters:
- array- the array to add to the- toString
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringanObjectvalue.- Parameters:
- obj- the value to add to the- toString
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringanObjectarray.- Parameters:
- array- the array to add to the- toString
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringashortvalue.- Parameters:
- value- the value to add to the- toString
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringashortarray.- Parameters:
- array- the array to add to the- toString
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringabooleanvalue.- Parameters:
- fieldName- the field name
- value- the value to add to the- toString
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringabooleanarray.- Parameters:
- fieldName- the field name
- array- the array to add to the- hashCode
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringabooleanarray.A boolean parameter controls the level of detail to show. Setting truewill output the array in full. Settingfalsewill output a summary, typically the size of the array.- Parameters:
- fieldName- the field name
- array- the array to add to the- toString
- fullDetail-- truefor detail,- falsefor summary info
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringanbytevalue.- Parameters:
- fieldName- the field name
- value- the value to add to the- toString
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringabytearray.- Parameters:
- fieldName- the field name
- array- the array to add to the- toString
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringabytearray.A boolean parameter controls the level of detail to show. Setting truewill output the array in full. Settingfalsewill output a summary, typically the size of the array.- Parameters:
- fieldName- the field name
- array- the array to add to the- toString
- fullDetail-- truefor detail,- falsefor summary info
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringacharvalue.- Parameters:
- fieldName- the field name
- value- the value to add to the- toString
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringachararray.- Parameters:
- fieldName- the field name
- array- the array to add to the- toString
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringachararray.A boolean parameter controls the level of detail to show. Setting truewill output the array in full. Settingfalsewill output a summary, typically the size of the array.- Parameters:
- fieldName- the field name
- array- the array to add to the- toString
- fullDetail-- truefor detail,- falsefor summary info
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringadoublevalue.- Parameters:
- fieldName- the field name
- value- the value to add to the- toString
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringadoublearray.- Parameters:
- fieldName- the field name
- array- the array to add to the- toString
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringadoublearray.A boolean parameter controls the level of detail to show. Setting truewill output the array in full. Settingfalsewill output a summary, typically the size of the array.- Parameters:
- fieldName- the field name
- array- the array to add to the- toString
- fullDetail-- truefor detail,- falsefor summary info
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringanfloatvalue.- Parameters:
- fieldName- the field name
- value- the value to add to the- toString
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringafloatarray.- Parameters:
- fieldName- the field name
- array- the array to add to the- toString
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringafloatarray.A boolean parameter controls the level of detail to show. Setting truewill output the array in full. Settingfalsewill output a summary, typically the size of the array.- Parameters:
- fieldName- the field name
- array- the array to add to the- toString
- fullDetail-- truefor detail,- falsefor summary info
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringanintvalue.- Parameters:
- fieldName- the field name
- value- the value to add to the- toString
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringanintarray.- Parameters:
- fieldName- the field name
- array- the array to add to the- toString
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringanintarray.A boolean parameter controls the level of detail to show. Setting truewill output the array in full. Settingfalsewill output a summary, typically the size of the array.- Parameters:
- fieldName- the field name
- array- the array to add to the- toString
- fullDetail-- truefor detail,- falsefor summary info
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringalongvalue.- Parameters:
- fieldName- the field name
- value- the value to add to the- toString
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringalongarray.- Parameters:
- fieldName- the field name
- array- the array to add to the- toString
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringalongarray.A boolean parameter controls the level of detail to show. Setting truewill output the array in full. Settingfalsewill output a summary, typically the size of the array.- Parameters:
- fieldName- the field name
- array- the array to add to the- toString
- fullDetail-- truefor detail,- falsefor summary info
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringanObjectvalue.- Parameters:
- fieldName- the field name
- obj- the value to add to the- toString
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringanObjectvalue.- Parameters:
- fieldName- the field name
- obj- the value to add to the- toString
- fullDetail-- truefor detail,- falsefor summary info
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringanObjectarray.- Parameters:
- fieldName- the field name
- array- the array to add to the- toString
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringanObjectarray.A boolean parameter controls the level of detail to show. Setting truewill output the array in full. Settingfalsewill output a summary, typically the size of the array.- Parameters:
- fieldName- the field name
- array- the array to add to the- toString
- fullDetail-- truefor detail,- falsefor summary info
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringanshortvalue.- Parameters:
- fieldName- the field name
- value- the value to add to the- toString
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringashortarray.- Parameters:
- fieldName- the field name
- array- the array to add to the- toString
- Returns:
- thisinstance.
 
- 
appendAppend to thetoStringashortarray.A boolean parameter controls the level of detail to show. Setting truewill output the array in full. Settingfalsewill output a summary, typically the size of the array.- Parameters:
- fieldName- the field name
- array- the array to add to the- toString
- fullDetail-- truefor detail,- falsefor summary info
- Returns:
- thisinstance.
 
- 
appendAsObjectToStringAppends with the same format as the defaultObject toString()method. Appends the class name followed bySystem.identityHashCode(Object).- Parameters:
- srcObject- the- Objectwhose class name and id to output
- Returns:
- thisinstance.
- Throws:
- NullPointerException- if- srcObjectis- null
- Since:
- 2.0
 
- 
appendSuperAppend thetoStringfrom the superclass.This method assumes that the superclass uses the same ToStringStyleas this one.If superToStringisnull, no change is made.- Parameters:
- superToString- the result of- super.toString()
- Returns:
- thisinstance.
- Since:
- 2.0
 
- 
appendToStringAppend thetoStringfrom 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 ToStringStyleas this one.If the toStringisnull, no change is made.- Parameters:
- toString- the result of- toString()on another object
- Returns:
- thisinstance.
- Since:
- 2.0
 
- 
buildReturns the String that was build as an object representation. The default implementation utilizes thetoString()implementation.
- 
getObjectReturns theObjectbeing output.- Returns:
- The object being output.
- Since:
- 2.0
 
- 
getStringBufferGets theStringBufferbeing populated.- Returns:
- the StringBufferbeing populated
 
- 
getStyleGets theToStringStylebeing used.- Returns:
- the ToStringStylebeing used
- Since:
- 2.0
 
- 
toStringReturns the builttoString.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'snullText
 
-