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 Summary
ConstructorDescriptionToStringBuilder
(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 Summary
Modifier and TypeMethodDescriptionappend
(boolean value) Append to thetoString
aboolean
value.append
(boolean[] array) Append to thetoString
aboolean
array.append
(byte value) Append to thetoString
abyte
value.append
(byte[] array) Append to thetoString
abyte
array.append
(char value) Append to thetoString
achar
value.append
(char[] array) Append to thetoString
achar
array.append
(double value) Append to thetoString
adouble
value.append
(double[] array) Append to thetoString
adouble
array.append
(float value) Append to thetoString
afloat
value.append
(float[] array) Append to thetoString
afloat
array.append
(int value) Append to thetoString
anint
value.append
(int[] array) Append to thetoString
anint
array.append
(long value) Append to thetoString
along
value.append
(long[] array) Append to thetoString
along
array.append
(short value) Append to thetoString
ashort
value.append
(short[] array) Append to thetoString
ashort
array.Append to thetoString
anObject
value.Append to thetoString
anObject
array.Append to thetoString
aboolean
value.Append to thetoString
aboolean
array.Append to thetoString
aboolean
array.Append to thetoString
anbyte
value.Append to thetoString
abyte
array.Append to thetoString
abyte
array.Append to thetoString
achar
value.Append to thetoString
achar
array.Append to thetoString
achar
array.Append to thetoString
adouble
value.Append to thetoString
adouble
array.Append to thetoString
adouble
array.Append to thetoString
anfloat
value.Append to thetoString
afloat
array.Append to thetoString
afloat
array.Append to thetoString
anint
value.Append to thetoString
anint
array.Append to thetoString
anint
array.Append to thetoString
along
value.Append to thetoString
along
array.Append to thetoString
along
array.Append to thetoString
anshort
value.Append to thetoString
ashort
array.Append to thetoString
ashort
array.Append to thetoString
anObject
value.Append to thetoString
anObject
array.Append to thetoString
anObject
array.Append to thetoString
anObject
value.appendAsObjectToString
(Object srcObject) Appends with the same format as the defaultObject toString()
method.appendSuper
(String superToString) Append thetoString
from the superclass.appendToString
(String toString) Append thetoString
from another object.build()
Returns the String that was build as an object representation.static ToStringStyle
Gets the defaultToStringStyle
to use.Returns theObject
being output.Gets theStringBuffer
being populated.getStyle()
Gets theToStringStyle
being used.static String
reflectionToString
(Object object) UsesReflectionToStringBuilder
to generate atoString
for the specified object.static String
reflectionToString
(Object object, ToStringStyle style) UsesReflectionToStringBuilder
to generate atoString
for the specified object.static String
reflectionToString
(Object object, ToStringStyle style, boolean outputTransients) UsesReflectionToStringBuilder
to generate atoString
for the specified object.static <T> String
reflectionToString
(T object, ToStringStyle style, boolean outputTransients, Class<? super T> reflectUpToClass) UsesReflectionToStringBuilder
to generate atoString
for the specified object.static void
setDefaultStyle
(ToStringStyle style) Sets the defaultToStringStyle
to use.toString()
Returns the builttoString
.
-
Constructor Details
-
ToStringBuilder
Constructs a builder for the specified object using the default output style.This default style is obtained from
getDefaultStyle()
.- Parameters:
object
- the Object to build atoString
for, not recommended to be null
-
ToStringBuilder
Constructs 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 atoString
for, not recommended to be nullstyle
- the style of thetoString
to create, null uses the default style
-
ToStringBuilder
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.- Parameters:
object
- the Object to build atoString
for, not recommended to be nullstyle
- the style of thetoString
to create, null uses the default stylebuffer
- theStringBuffer
to populate, may be null
-
-
Method Details
-
getDefaultStyle
Gets the defaultToStringStyle
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 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
-
reflectionToString
UsesReflectionToStringBuilder
to generate atoString
for the specified object.- Parameters:
object
- the Object to be output- Returns:
- the String result
- See Also:
-
reflectionToString
UsesReflectionToStringBuilder
to generate atoString
for the specified object.- Parameters:
object
- the Object to be outputstyle
- the style of thetoString
to create, may benull
- Returns:
- the String result
- See Also:
-
reflectionToString
public static String reflectionToString(Object object, ToStringStyle style, boolean outputTransients) UsesReflectionToStringBuilder
to generate atoString
for the specified object.- Parameters:
object
- the Object to be outputstyle
- the style of thetoString
to create, may benull
outputTransients
- whether to include transient fields- Returns:
- the String result
- See Also:
-
reflectionToString
public static <T> String reflectionToString(T object, ToStringStyle style, boolean outputTransients, Class<? super T> reflectUpToClass) UsesReflectionToStringBuilder
to generate atoString
for the specified object.- Type Parameters:
T
- the type of the object- Parameters:
object
- the Object to be outputstyle
- the style of thetoString
to create, may benull
outputTransients
- whether to include transient fieldsreflectUpToClass
- the superclass to reflect up to (inclusive), may benull
- Returns:
- the String result
- Since:
- 2.0
- See Also:
-
setDefaultStyle
Sets the defaultToStringStyle
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 fromgetDefaultStyle()
.- Parameters:
style
- the defaultToStringStyle
- Throws:
NullPointerException
- if the style isnull
-
append
Append to thetoString
aboolean
value.- Parameters:
value
- the value to add to thetoString
- Returns:
this
instance.
-
append
Append to thetoString
aboolean
array.- Parameters:
array
- the array to add to thetoString
- Returns:
this
instance.
-
append
Append to thetoString
abyte
value.- Parameters:
value
- the value to add to thetoString
- Returns:
this
instance.
-
append
Append to thetoString
abyte
array.- Parameters:
array
- the array to add to thetoString
- Returns:
this
instance.
-
append
Append to thetoString
achar
value.- Parameters:
value
- the value to add to thetoString
- Returns:
this
instance.
-
append
Append to thetoString
achar
array.- Parameters:
array
- the array to add to thetoString
- Returns:
this
instance.
-
append
Append to thetoString
adouble
value.- Parameters:
value
- the value to add to thetoString
- Returns:
this
instance.
-
append
Append to thetoString
adouble
array.- Parameters:
array
- the array to add to thetoString
- Returns:
this
instance.
-
append
Append to thetoString
afloat
value.- Parameters:
value
- the value to add to thetoString
- Returns:
this
instance.
-
append
Append to thetoString
afloat
array.- Parameters:
array
- the array to add to thetoString
- Returns:
this
instance.
-
append
Append to thetoString
anint
value.- Parameters:
value
- the value to add to thetoString
- Returns:
this
instance.
-
append
Append to thetoString
anint
array.- Parameters:
array
- the array to add to thetoString
- Returns:
this
instance.
-
append
Append to thetoString
along
value.- Parameters:
value
- the value to add to thetoString
- Returns:
this
instance.
-
append
Append to thetoString
along
array.- Parameters:
array
- the array to add to thetoString
- Returns:
this
instance.
-
append
Append to thetoString
anObject
value.- Parameters:
obj
- the value to add to thetoString
- Returns:
this
instance.
-
append
Append to thetoString
anObject
array.- Parameters:
array
- the array to add to thetoString
- Returns:
this
instance.
-
append
Append to thetoString
ashort
value.- Parameters:
value
- the value to add to thetoString
- Returns:
this
instance.
-
append
Append to thetoString
ashort
array.- Parameters:
array
- the array to add to thetoString
- Returns:
this
instance.
-
append
Append to thetoString
aboolean
value.- Parameters:
fieldName
- the field namevalue
- the value to add to thetoString
- Returns:
this
instance.
-
append
Append to thetoString
aboolean
array.- Parameters:
fieldName
- the field namearray
- the array to add to thehashCode
- Returns:
this
instance.
-
append
Append to thetoString
aboolean
array.A boolean parameter controls the level of detail to show. Setting
true
will output the array in full. Settingfalse
will output a summary, typically the size of the array.- Parameters:
fieldName
- the field namearray
- the array to add to thetoString
fullDetail
-true
for detail,false
for summary info- Returns:
this
instance.
-
append
Append to thetoString
anbyte
value.- Parameters:
fieldName
- the field namevalue
- the value to add to thetoString
- Returns:
this
instance.
-
append
Append to thetoString
abyte
array.- Parameters:
fieldName
- the field namearray
- the array to add to thetoString
- Returns:
this
instance.
-
append
Append to thetoString
abyte
array.A boolean parameter controls the level of detail to show. Setting
true
will output the array in full. Settingfalse
will output a summary, typically the size of the array.- Parameters:
fieldName
- the field namearray
- the array to add to thetoString
fullDetail
-true
for detail,false
for summary info- Returns:
this
instance.
-
append
Append to thetoString
achar
value.- Parameters:
fieldName
- the field namevalue
- the value to add to thetoString
- Returns:
this
instance.
-
append
Append to thetoString
achar
array.- Parameters:
fieldName
- the field namearray
- the array to add to thetoString
- Returns:
this
instance.
-
append
Append to thetoString
achar
array.A boolean parameter controls the level of detail to show. Setting
true
will output the array in full. Settingfalse
will output a summary, typically the size of the array.- Parameters:
fieldName
- the field namearray
- the array to add to thetoString
fullDetail
-true
for detail,false
for summary info- Returns:
this
instance.
-
append
Append to thetoString
adouble
value.- Parameters:
fieldName
- the field namevalue
- the value to add to thetoString
- Returns:
this
instance.
-
append
Append to thetoString
adouble
array.- Parameters:
fieldName
- the field namearray
- the array to add to thetoString
- Returns:
this
instance.
-
append
Append to thetoString
adouble
array.A boolean parameter controls the level of detail to show. Setting
true
will output the array in full. Settingfalse
will output a summary, typically the size of the array.- Parameters:
fieldName
- the field namearray
- the array to add to thetoString
fullDetail
-true
for detail,false
for summary info- Returns:
this
instance.
-
append
Append to thetoString
anfloat
value.- Parameters:
fieldName
- the field namevalue
- the value to add to thetoString
- Returns:
this
instance.
-
append
Append to thetoString
afloat
array.- Parameters:
fieldName
- the field namearray
- the array to add to thetoString
- Returns:
this
instance.
-
append
Append to thetoString
afloat
array.A boolean parameter controls the level of detail to show. Setting
true
will output the array in full. Settingfalse
will output a summary, typically the size of the array.- Parameters:
fieldName
- the field namearray
- the array to add to thetoString
fullDetail
-true
for detail,false
for summary info- Returns:
this
instance.
-
append
Append to thetoString
anint
value.- Parameters:
fieldName
- the field namevalue
- the value to add to thetoString
- Returns:
this
instance.
-
append
Append to thetoString
anint
array.- Parameters:
fieldName
- the field namearray
- the array to add to thetoString
- Returns:
this
instance.
-
append
Append to thetoString
anint
array.A boolean parameter controls the level of detail to show. Setting
true
will output the array in full. Settingfalse
will output a summary, typically the size of the array.- Parameters:
fieldName
- the field namearray
- the array to add to thetoString
fullDetail
-true
for detail,false
for summary info- Returns:
this
instance.
-
append
Append to thetoString
along
value.- Parameters:
fieldName
- the field namevalue
- the value to add to thetoString
- Returns:
this
instance.
-
append
Append to thetoString
along
array.- Parameters:
fieldName
- the field namearray
- the array to add to thetoString
- Returns:
this
instance.
-
append
Append to thetoString
along
array.A boolean parameter controls the level of detail to show. Setting
true
will output the array in full. Settingfalse
will output a summary, typically the size of the array.- Parameters:
fieldName
- the field namearray
- the array to add to thetoString
fullDetail
-true
for detail,false
for summary info- Returns:
this
instance.
-
append
Append to thetoString
anObject
value.- Parameters:
fieldName
- the field nameobj
- the value to add to thetoString
- Returns:
this
instance.
-
append
Append to thetoString
anObject
value.- Parameters:
fieldName
- the field nameobj
- the value to add to thetoString
fullDetail
-true
for detail,false
for summary info- Returns:
this
instance.
-
append
Append to thetoString
anObject
array.- Parameters:
fieldName
- the field namearray
- the array to add to thetoString
- Returns:
this
instance.
-
append
Append to thetoString
anObject
array.A boolean parameter controls the level of detail to show. Setting
true
will output the array in full. Settingfalse
will output a summary, typically the size of the array.- Parameters:
fieldName
- the field namearray
- the array to add to thetoString
fullDetail
-true
for detail,false
for summary info- Returns:
this
instance.
-
append
Append to thetoString
anshort
value.- Parameters:
fieldName
- the field namevalue
- the value to add to thetoString
- Returns:
this
instance.
-
append
Append to thetoString
ashort
array.- Parameters:
fieldName
- the field namearray
- the array to add to thetoString
- Returns:
this
instance.
-
append
Append to thetoString
ashort
array.A boolean parameter controls the level of detail to show. Setting
true
will output the array in full. Settingfalse
will output a summary, typically the size of the array.- Parameters:
fieldName
- the field namearray
- the array to add to thetoString
fullDetail
-true
for detail,false
for summary info- Returns:
this
instance.
-
appendAsObjectToString
Appends with the same format as the defaultObject toString()
method. Appends the class name followed bySystem.identityHashCode(Object)
.- Parameters:
srcObject
- theObject
whose class name and id to output- Returns:
this
instance.- Throws:
NullPointerException
- ifsrcObject
isnull
- Since:
- 2.0
-
appendSuper
Append thetoString
from the superclass.This method assumes that the superclass uses the same
ToStringStyle
as this one.If
superToString
isnull
, no change is made.- Parameters:
superToString
- the result ofsuper.toString()
- Returns:
this
instance.- Since:
- 2.0
-
appendToString
Append thetoString
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
isnull
, no change is made.- Parameters:
toString
- the result oftoString()
on another object- Returns:
this
instance.- Since:
- 2.0
-
build
Returns the String that was build as an object representation. The default implementation utilizes thetoString()
implementation. -
getObject
Returns theObject
being output.- Returns:
- The object being output.
- Since:
- 2.0
-
getStringBuffer
Gets theStringBuffer
being populated.- Returns:
- the
StringBuffer
being populated
-
getStyle
Gets theToStringStyle
being used.- Returns:
- the
ToStringStyle
being used - Since:
- 2.0
-
toString
Returns 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
-