-
- All Implemented Interfaces:
-
external.org.apache.commons.lang3.builder.Builder
public class ToStringBuilder implements Builder<String>
Assists in implementing 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. To append thetoStringfrom 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, usesAccessibleObject.setAccessibleto 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
toStringis determined by the ToStringStyle passed into the constructor.
-
-
Field Summary
Fields Modifier and Type Field Description private static volatile ToStringStyledefaultStyleprivate final Objectobjectprivate final ToStringStylestyle
-
Constructor Summary
Constructors Constructor 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.
-
Method Summary
Modifier and Type Method Description static ToStringStylegetDefaultStyle()Gets the default ToStringStyleto use.static voidsetDefaultStyle(ToStringStyle style)Sets the default ToStringStyleto use.ObjectgetObject()Returns the Objectbeing output.ToStringStylegetStyle()Gets the ToStringStylebeing used.static StringreflectionToString(Object object)Uses ReflectionToStringBuilderto generate atoStringfor the specified object.static StringreflectionToString(Object object, ToStringStyle style)Uses ReflectionToStringBuilderto generate atoStringfor the specified object.static StringreflectionToString(Object object, ToStringStyle style, boolean outputTransients)Uses ReflectionToStringBuilderto generate atoStringfor the specified object.static <T> StringreflectionToString(T object, ToStringStyle style, boolean outputTransients, Class<out Object> reflectUpToClass)Uses ReflectionToStringBuilderto generate atoStringfor the specified object.ToStringBuilderappend(boolean value)Append to the toStringabooleanvalue.ToStringBuilderappend(Array<boolean> array)Append to the toStringabooleanarray.ToStringBuilderappend(byte value)Append to the toStringabytevalue.ToStringBuilderappend(Array<byte> array)Append to the toStringabytearray.ToStringBuilderappend(char value)Append to the toStringacharvalue.ToStringBuilderappend(Array<char> array)Append to the toStringachararray.ToStringBuilderappend(double value)Append to the toStringadoublevalue.ToStringBuilderappend(Array<double> array)Append to the toStringadoublearray.ToStringBuilderappend(float value)Append to the toStringafloatvalue.ToStringBuilderappend(Array<float> array)Append to the toStringafloatarray.ToStringBuilderappend(int value)Append to the toStringanintvalue.ToStringBuilderappend(Array<int> array)Append to the toStringanintarray.ToStringBuilderappend(long value)Append to the toStringalongvalue.ToStringBuilderappend(Array<long> array)Append to the toStringalongarray.ToStringBuilderappend(Object obj)Append to the toStringanObjectvalue.ToStringBuilderappend(Array<Object> array)Append to the toStringanObjectarray.ToStringBuilderappend(short value)Append to the toStringashortvalue.ToStringBuilderappend(Array<short> array)Append to the toStringashortarray.ToStringBuilderappend(String fieldName, boolean value)Append to the toStringabooleanvalue.ToStringBuilderappend(String fieldName, Array<boolean> array)Append to the toStringabooleanarray.ToStringBuilderappend(String fieldName, Array<boolean> array, boolean fullDetail)Append to the toStringabooleanarray.ToStringBuilderappend(String fieldName, byte value)Append to the toStringanbytevalue.ToStringBuilderappend(String fieldName, Array<byte> array)Append to the toStringabytearray.ToStringBuilderappend(String fieldName, Array<byte> array, boolean fullDetail)Append to the toStringabytearray.ToStringBuilderappend(String fieldName, char value)Append to the toStringacharvalue.ToStringBuilderappend(String fieldName, Array<char> array)Append to the toStringachararray.ToStringBuilderappend(String fieldName, Array<char> array, boolean fullDetail)Append to the toStringachararray.ToStringBuilderappend(String fieldName, double value)Append to the toStringadoublevalue.ToStringBuilderappend(String fieldName, Array<double> array)Append to the toStringadoublearray.ToStringBuilderappend(String fieldName, Array<double> array, boolean fullDetail)Append to the toStringadoublearray.ToStringBuilderappend(String fieldName, float value)Append to the toStringanfloatvalue.ToStringBuilderappend(String fieldName, Array<float> array)Append to the toStringafloatarray.ToStringBuilderappend(String fieldName, Array<float> array, boolean fullDetail)Append to the toStringafloatarray.ToStringBuilderappend(String fieldName, int value)Append to the toStringanintvalue.ToStringBuilderappend(String fieldName, Array<int> array)Append to the toStringanintarray.ToStringBuilderappend(String fieldName, Array<int> array, boolean fullDetail)Append to the toStringanintarray.ToStringBuilderappend(String fieldName, long value)Append to the toStringalongvalue.ToStringBuilderappend(String fieldName, Array<long> array)Append to the toStringalongarray.ToStringBuilderappend(String fieldName, Array<long> array, boolean fullDetail)Append to the toStringalongarray.ToStringBuilderappend(String fieldName, Object obj)Append to the toStringanObjectvalue.ToStringBuilderappend(String fieldName, Object obj, boolean fullDetail)Append to the toStringanObjectvalue.ToStringBuilderappend(String fieldName, Array<Object> array)Append to the toStringanObjectarray.ToStringBuilderappend(String fieldName, Array<Object> array, boolean fullDetail)Append to the toStringanObjectarray.ToStringBuilderappend(String fieldName, short value)Append to the toStringanshortvalue.ToStringBuilderappend(String fieldName, Array<short> array)Append to the toStringashortarray.ToStringBuilderappend(String fieldName, Array<short> array, boolean fullDetail)Append to the toStringashortarray.ToStringBuilderappendAsObjectToString(Object object)Appends with the same format as the default Object toString()method.ToStringBuilderappendSuper(String superToString)Append the toStringfrom the superclass.ToStringBuilderappendToString(String toString)Append the toStringfrom another object.StringBuffergetStringBuffer()Gets the StringBufferbeing populated.StringtoString()Returns the built toString.Stringbuild()Returns the String that was build as an object representation. -
-
Constructor Detail
-
ToStringBuilder
ToStringBuilder(Object object)
Constructs a builder for the specified object using the default output style.- Parameters:
object- the Object to build atoStringfor, not recommended to be null
-
ToStringBuilder
ToStringBuilder(Object object, ToStringStyle style)
Constructs a builder for the specified object using the a defined output style.- Parameters:
object- the Object to build atoStringfor, not recommended to be nullstyle- the style of thetoStringto create, null uses the default style
-
ToStringBuilder
ToStringBuilder(Object object, ToStringStyle style, StringBuffer buffer)
Constructs a builder for the specified object.- Parameters:
object- the Object to build atoStringfor, not recommended to be nullstyle- the style of thetoStringto create, null uses the default stylebuffer- theStringBufferto populate, may be null
-
-
Method Detail
-
getDefaultStyle
static ToStringStyle getDefaultStyle()
Gets the default
ToStringStyleto 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 insteadof using this global default.This method can be used from multiple threads.Internally, a
volatilevariable is used to provide the guaranteethat the latest value set using setDefaultStyle 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 duringdevelopment and a compact style in production.
-
setDefaultStyle
static void setDefaultStyle(ToStringStyle style)
Sets the default
ToStringStyleto 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 insteadof changing this global default.This method is not intended for use from multiple threads.Internally, a
volatilevariable is used to provide the guaranteethat the latest value set is the value returned from getDefaultStyle.- Parameters:
style- the defaultToStringStyle
-
getStyle
ToStringStyle getStyle()
Gets the
ToStringStylebeing used.
-
reflectionToString
static String reflectionToString(Object object)
Uses
ReflectionToStringBuilderto generate atoStringfor the specified object.- Parameters:
object- the Object to be output
-
reflectionToString
static String reflectionToString(Object object, ToStringStyle style)
Uses
ReflectionToStringBuilderto generate atoStringfor the specified object.- Parameters:
object- the Object to be outputstyle- the style of thetoStringto create, may benull
-
reflectionToString
static String reflectionToString(Object object, ToStringStyle style, boolean outputTransients)
Uses
ReflectionToStringBuilderto generate atoStringfor the specified object.- Parameters:
object- the Object to be outputstyle- the style of thetoStringto create, may benulloutputTransients- whether to include transient fields
-
reflectionToString
static <T> String reflectionToString(T object, ToStringStyle style, boolean outputTransients, Class<out Object> reflectUpToClass)
Uses
ReflectionToStringBuilderto generate atoStringfor the specified object.- Parameters:
object- the Object to be outputstyle- the style of thetoStringto create, may benulloutputTransients- whether to include transient fieldsreflectUpToClass- the superclass to reflect up to (inclusive), may benull
-
append
ToStringBuilder append(boolean value)
Append to the
toStringabooleanvalue.- Parameters:
value- the value to add to thetoString
-
append
ToStringBuilder append(Array<boolean> array)
Append to the
toStringabooleanarray.- Parameters:
array- the array to add to thetoString
-
append
ToStringBuilder append(byte value)
Append to the
toStringabytevalue.- Parameters:
value- the value to add to thetoString
-
append
ToStringBuilder append(Array<byte> array)
Append to the
toStringabytearray.- Parameters:
array- the array to add to thetoString
-
append
ToStringBuilder append(char value)
Append to the
toStringacharvalue.- Parameters:
value- the value to add to thetoString
-
append
ToStringBuilder append(Array<char> array)
Append to the
toStringachararray.- Parameters:
array- the array to add to thetoString
-
append
ToStringBuilder append(double value)
Append to the
toStringadoublevalue.- Parameters:
value- the value to add to thetoString
-
append
ToStringBuilder append(Array<double> array)
Append to the
toStringadoublearray.- Parameters:
array- the array to add to thetoString
-
append
ToStringBuilder append(float value)
Append to the
toStringafloatvalue.- Parameters:
value- the value to add to thetoString
-
append
ToStringBuilder append(Array<float> array)
Append to the
toStringafloatarray.- Parameters:
array- the array to add to thetoString
-
append
ToStringBuilder append(int value)
Append to the
toStringanintvalue.- Parameters:
value- the value to add to thetoString
-
append
ToStringBuilder append(Array<int> array)
Append to the
toStringanintarray.- Parameters:
array- the array to add to thetoString
-
append
ToStringBuilder append(long value)
Append to the
toStringalongvalue.- Parameters:
value- the value to add to thetoString
-
append
ToStringBuilder append(Array<long> array)
Append to the
toStringalongarray.- Parameters:
array- the array to add to thetoString
-
append
ToStringBuilder append(Object obj)
Append to the
toStringanObjectvalue.- Parameters:
obj- the value to add to thetoString
-
append
ToStringBuilder append(Array<Object> array)
Append to the
toStringanObjectarray.- Parameters:
array- the array to add to thetoString
-
append
ToStringBuilder append(short value)
Append to the
toStringashortvalue.- Parameters:
value- the value to add to thetoString
-
append
ToStringBuilder append(Array<short> array)
Append to the
toStringashortarray.- Parameters:
array- the array to add to thetoString
-
append
ToStringBuilder append(String fieldName, boolean value)
Append to the
toStringabooleanvalue.- Parameters:
fieldName- the field namevalue- the value to add to thetoString
-
append
ToStringBuilder append(String fieldName, Array<boolean> array)
Append to the
toStringabooleanarray.- Parameters:
fieldName- the field namearray- the array to add to thehashCode
-
append
ToStringBuilder append(String fieldName, Array<boolean> array, boolean fullDetail)
Append to the
toStringabooleanarray.A boolean parameter controls the level of detail to show.Setting
truewill output the array in full. Settingfalsewill output a summary, typically the size ofthe array.- Parameters:
fieldName- the field namearray- the array to add to thetoStringfullDetail-truefor detail,falsefor summary info
-
append
ToStringBuilder append(String fieldName, byte value)
Append to the
toStringanbytevalue.- Parameters:
fieldName- the field namevalue- the value to add to thetoString
-
append
ToStringBuilder append(String fieldName, Array<byte> array)
Append to the
toStringabytearray.- Parameters:
fieldName- the field namearray- the array to add to thetoString
-
append
ToStringBuilder append(String fieldName, Array<byte> array, boolean fullDetail)
Append to the
toStringabytearray.A boolean parameter controls the level of detail to show.Setting
truewill output the array in full. Settingfalsewill output a summary, typically the size ofthe array.- Parameters:
fieldName- the field namearray- the array to add to thetoStringfullDetail-truefor detail,falsefor summary info
-
append
ToStringBuilder append(String fieldName, char value)
Append to the
toStringacharvalue.- Parameters:
fieldName- the field namevalue- the value to add to thetoString
-
append
ToStringBuilder append(String fieldName, Array<char> array)
Append to the
toStringachararray.- Parameters:
fieldName- the field namearray- the array to add to thetoString
-
append
ToStringBuilder append(String fieldName, Array<char> array, boolean fullDetail)
Append to the
toStringachararray.A boolean parameter controls the level of detail to show.Setting
truewill output the array in full. Settingfalsewill output a summary, typically the size ofthe array.- Parameters:
fieldName- the field namearray- the array to add to thetoStringfullDetail-truefor detail,falsefor summary info
-
append
ToStringBuilder append(String fieldName, double value)
Append to the
toStringadoublevalue.- Parameters:
fieldName- the field namevalue- the value to add to thetoString
-
append
ToStringBuilder append(String fieldName, Array<double> array)
Append to the
toStringadoublearray.- Parameters:
fieldName- the field namearray- the array to add to thetoString
-
append
ToStringBuilder append(String fieldName, Array<double> array, boolean fullDetail)
Append to the
toStringadoublearray.A boolean parameter controls the level of detail to show.Setting
truewill output the array in full. Settingfalsewill output a summary, typically the size ofthe array.- Parameters:
fieldName- the field namearray- the array to add to thetoStringfullDetail-truefor detail,falsefor summary info
-
append
ToStringBuilder append(String fieldName, float value)
Append to the
toStringanfloatvalue.- Parameters:
fieldName- the field namevalue- the value to add to thetoString
-
append
ToStringBuilder append(String fieldName, Array<float> array)
Append to the
toStringafloatarray.- Parameters:
fieldName- the field namearray- the array to add to thetoString
-
append
ToStringBuilder append(String fieldName, Array<float> array, boolean fullDetail)
Append to the
toStringafloatarray.A boolean parameter controls the level of detail to show.Setting
truewill output the array in full. Settingfalsewill output a summary, typically the size ofthe array.- Parameters:
fieldName- the field namearray- the array to add to thetoStringfullDetail-truefor detail,falsefor summary info
-
append
ToStringBuilder append(String fieldName, int value)
Append to the
toStringanintvalue.- Parameters:
fieldName- the field namevalue- the value to add to thetoString
-
append
ToStringBuilder append(String fieldName, Array<int> array)
Append to the
toStringanintarray.- Parameters:
fieldName- the field namearray- the array to add to thetoString
-
append
ToStringBuilder append(String fieldName, Array<int> array, boolean fullDetail)
Append to the
toStringanintarray.A boolean parameter controls the level of detail to show.Setting
truewill output the array in full. Settingfalsewill output a summary, typically the size ofthe array.- Parameters:
fieldName- the field namearray- the array to add to thetoStringfullDetail-truefor detail,falsefor summary info
-
append
ToStringBuilder append(String fieldName, long value)
Append to the
toStringalongvalue.- Parameters:
fieldName- the field namevalue- the value to add to thetoString
-
append
ToStringBuilder append(String fieldName, Array<long> array)
Append to the
toStringalongarray.- Parameters:
fieldName- the field namearray- the array to add to thetoString
-
append
ToStringBuilder append(String fieldName, Array<long> array, boolean fullDetail)
Append to the
toStringalongarray.A boolean parameter controls the level of detail to show.Setting
truewill output the array in full. Settingfalsewill output a summary, typically the size ofthe array.- Parameters:
fieldName- the field namearray- the array to add to thetoStringfullDetail-truefor detail,falsefor summary info
-
append
ToStringBuilder append(String fieldName, Object obj)
Append to the
toStringanObjectvalue.- Parameters:
fieldName- the field nameobj- the value to add to thetoString
-
append
ToStringBuilder append(String fieldName, Object obj, boolean fullDetail)
Append to the
toStringanObjectvalue.- Parameters:
fieldName- the field nameobj- the value to add to thetoStringfullDetail-truefor detail,falsefor summary info
-
append
ToStringBuilder append(String fieldName, Array<Object> array)
Append to the
toStringanObjectarray.- Parameters:
fieldName- the field namearray- the array to add to thetoString
-
append
ToStringBuilder append(String fieldName, Array<Object> array, boolean fullDetail)
Append to the
toStringanObjectarray.A boolean parameter controls the level of detail to show.Setting
truewill output the array in full. Settingfalsewill output a summary, typically the size ofthe array.- Parameters:
fieldName- the field namearray- the array to add to thetoStringfullDetail-truefor detail,falsefor summary info
-
append
ToStringBuilder append(String fieldName, short value)
Append to the
toStringanshortvalue.- Parameters:
fieldName- the field namevalue- the value to add to thetoString
-
append
ToStringBuilder append(String fieldName, Array<short> array)
Append to the
toStringashortarray.- Parameters:
fieldName- the field namearray- the array to add to thetoString
-
append
ToStringBuilder append(String fieldName, Array<short> array, boolean fullDetail)
Append to the
toStringashortarray.A boolean parameter controls the level of detail to show.Setting
truewill output the array in full. Settingfalsewill output a summary, typically the size ofthe array.- Parameters:
fieldName- the field namearray- the array to add to thetoStringfullDetail-truefor detail,falsefor summary info
-
appendAsObjectToString
ToStringBuilder appendAsObjectToString(Object object)
Appends with the same format as the default
Object toString()method. Appends the class name followed by identityHashCode.- Parameters:
object- theObjectwhose class name and id to output
-
appendSuper
ToStringBuilder appendSuper(String superToString)
Append the
toStringfrom the superclass.This method assumes that the superclass uses the same
ToStringStyleas this one.If
superToStringisnull, no change is made.- Parameters:
superToString- the result ofsuper.toString()
-
appendToString
ToStringBuilder appendToString(String toString)
Append the
toStringfrom another object.This method is useful where a class delegates most of the implementation ofits properties to another class. You can then call
toString()onthe 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 oftoString()on another object
-
getStringBuffer
StringBuffer getStringBuffer()
Gets the
StringBufferbeing populated.
-
toString
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'snullText
-
-
-
-