Class ToStringBuilder
- java.lang.Object
-
- org.apache.commons.lang.builder.ToStringBuilder
-
- Direct Known Subclasses:
ReflectionToStringBuilder
public class ToStringBuilder extends Object
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:- 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
, useappendSuper(java.lang.String)
. To append thetoString
from an object that is delegated to (or any other object), useappendToString(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
, usesAccessibleObject.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 theToStringStyle
passed into the constructor.- Since:
- 1.0
- Version:
- $Id: ToStringBuilder.java 905636 2010-02-02 14:03:32Z niallp $
-
-
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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ToStringBuilder
append(boolean value)
Append to thetoString
aboolean
value.ToStringBuilder
append(boolean[] array)
Append to thetoString
aboolean
array.ToStringBuilder
append(byte value)
Append to thetoString
abyte
value.ToStringBuilder
append(byte[] array)
Append to thetoString
abyte
array.ToStringBuilder
append(char value)
Append to thetoString
achar
value.ToStringBuilder
append(char[] array)
Append to thetoString
achar
array.ToStringBuilder
append(double value)
Append to thetoString
adouble
value.ToStringBuilder
append(double[] array)
Append to thetoString
adouble
array.ToStringBuilder
append(float value)
Append to thetoString
afloat
value.ToStringBuilder
append(float[] array)
Append to thetoString
afloat
array.ToStringBuilder
append(int value)
Append to thetoString
anint
value.ToStringBuilder
append(int[] array)
Append to thetoString
anint
array.ToStringBuilder
append(long value)
Append to thetoString
along
value.ToStringBuilder
append(long[] array)
Append to thetoString
along
array.ToStringBuilder
append(short value)
Append to thetoString
ashort
value.ToStringBuilder
append(short[] array)
Append to thetoString
ashort
array.ToStringBuilder
append(Object obj)
Append to thetoString
anObject
value.ToStringBuilder
append(Object[] array)
Append to thetoString
anObject
array.ToStringBuilder
append(String fieldName, boolean value)
Append to thetoString
aboolean
value.ToStringBuilder
append(String fieldName, boolean[] array)
Append to thetoString
aboolean
array.ToStringBuilder
append(String fieldName, boolean[] array, boolean fullDetail)
Append to thetoString
aboolean
array.ToStringBuilder
append(String fieldName, byte value)
Append to thetoString
anbyte
value.ToStringBuilder
append(String fieldName, byte[] array)
Append to thetoString
abyte
array.ToStringBuilder
append(String fieldName, byte[] array, boolean fullDetail)
Append to thetoString
abyte
array.ToStringBuilder
append(String fieldName, char value)
Append to thetoString
achar
value.ToStringBuilder
append(String fieldName, char[] array)
Append to thetoString
achar
array.ToStringBuilder
append(String fieldName, char[] array, boolean fullDetail)
Append to thetoString
achar
array.ToStringBuilder
append(String fieldName, double value)
Append to thetoString
adouble
value.ToStringBuilder
append(String fieldName, double[] array)
Append to thetoString
adouble
array.ToStringBuilder
append(String fieldName, double[] array, boolean fullDetail)
Append to thetoString
adouble
array.ToStringBuilder
append(String fieldName, float value)
Append to thetoString
anfloat
value.ToStringBuilder
append(String fieldName, float[] array)
Append to thetoString
afloat
array.ToStringBuilder
append(String fieldName, float[] array, boolean fullDetail)
Append to thetoString
afloat
array.ToStringBuilder
append(String fieldName, int value)
Append to thetoString
anint
value.ToStringBuilder
append(String fieldName, int[] array)
Append to thetoString
anint
array.ToStringBuilder
append(String fieldName, int[] array, boolean fullDetail)
Append to thetoString
anint
array.ToStringBuilder
append(String fieldName, long value)
Append to thetoString
along
value.ToStringBuilder
append(String fieldName, long[] array)
Append to thetoString
along
array.ToStringBuilder
append(String fieldName, long[] array, boolean fullDetail)
Append to thetoString
along
array.ToStringBuilder
append(String fieldName, short value)
Append to thetoString
anshort
value.ToStringBuilder
append(String fieldName, short[] array)
Append to thetoString
ashort
array.ToStringBuilder
append(String fieldName, short[] array, boolean fullDetail)
Append to thetoString
ashort
array.ToStringBuilder
append(String fieldName, Object obj)
Append to thetoString
anObject
value.ToStringBuilder
append(String fieldName, Object[] array)
Append to thetoString
anObject
array.ToStringBuilder
append(String fieldName, Object[] array, boolean fullDetail)
Append to thetoString
anObject
array.ToStringBuilder
append(String fieldName, Object obj, boolean fullDetail)
Append to thetoString
anObject
value.ToStringBuilder
appendAsObjectToString(Object object)
Appends with the same format as the defaultObject toString()
method.ToStringBuilder
appendSuper(String superToString)
Append thetoString
from the superclass.ToStringBuilder
appendToString(String toString)
Append thetoString
from another object.static ToStringStyle
getDefaultStyle()
Gets the defaultToStringStyle
to use.Object
getObject()
Returns theObject
being output.StringBuffer
getStringBuffer()
Gets theStringBuffer
being populated.ToStringStyle
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 String
reflectionToString(Object object, ToStringStyle style, boolean outputTransients, Class reflectUpToClass)
UsesReflectionToStringBuilder
to generate atoString
for the specified object.static void
setDefaultStyle(ToStringStyle style)
Sets the defaultToStringStyle
to use.String
toString()
Returns the builttoString
.
-
-
-
Constructor Detail
-
ToStringBuilder
public ToStringBuilder(Object object)
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
public 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.- 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
public 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.- 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 Detail
-
getDefaultStyle
public 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 usingsetDefaultStyle(org.apache.commons.lang.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
-
setDefaultStyle
public 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 fromgetDefaultStyle()
.- Parameters:
style
- the defaultToStringStyle
- Throws:
IllegalArgumentException
- if the style isnull
-
reflectionToString
public static String reflectionToString(Object object)
Uses
ReflectionToStringBuilder
to generate atoString
for the specified object.- Parameters:
object
- the Object to be output- Returns:
- the String result
- See Also:
ReflectionToStringBuilder.toString(Object)
-
reflectionToString
public static String reflectionToString(Object object, ToStringStyle style)
Uses
ReflectionToStringBuilder
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:
ReflectionToStringBuilder.toString(Object,ToStringStyle)
-
reflectionToString
public static String reflectionToString(Object object, ToStringStyle style, boolean outputTransients)
Uses
ReflectionToStringBuilder
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:
ReflectionToStringBuilder.toString(Object,ToStringStyle,boolean)
-
reflectionToString
public static String reflectionToString(Object object, ToStringStyle style, boolean outputTransients, Class reflectUpToClass)
Uses
ReflectionToStringBuilder
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 fieldsreflectUpToClass
- the superclass to reflect up to (inclusive), may benull
- Returns:
- the String result
- Since:
- 2.0
- See Also:
ReflectionToStringBuilder.toString(Object,ToStringStyle,boolean,boolean,Class)
-
append
public ToStringBuilder append(boolean value)
Append to the
toString
aboolean
value.- Parameters:
value
- the value to add to thetoString
- Returns:
- this
-
append
public ToStringBuilder append(boolean[] array)
Append to the
toString
aboolean
array.- Parameters:
array
- the array to add to thetoString
- Returns:
- this
-
append
public ToStringBuilder append(byte value)
Append to the
toString
abyte
value.- Parameters:
value
- the value to add to thetoString
- Returns:
- this
-
append
public ToStringBuilder append(byte[] array)
Append to the
toString
abyte
array.- Parameters:
array
- the array to add to thetoString
- Returns:
- this
-
append
public ToStringBuilder append(char value)
Append to the
toString
achar
value.- Parameters:
value
- the value to add to thetoString
- Returns:
- this
-
append
public ToStringBuilder append(char[] array)
Append to the
toString
achar
array.- Parameters:
array
- the array to add to thetoString
- Returns:
- this
-
append
public ToStringBuilder append(double value)
Append to the
toString
adouble
value.- Parameters:
value
- the value to add to thetoString
- Returns:
- this
-
append
public ToStringBuilder append(double[] array)
Append to the
toString
adouble
array.- Parameters:
array
- the array to add to thetoString
- Returns:
- this
-
append
public ToStringBuilder append(float value)
Append to the
toString
afloat
value.- Parameters:
value
- the value to add to thetoString
- Returns:
- this
-
append
public ToStringBuilder append(float[] array)
Append to the
toString
afloat
array.- Parameters:
array
- the array to add to thetoString
- Returns:
- this
-
append
public ToStringBuilder append(int value)
Append to the
toString
anint
value.- Parameters:
value
- the value to add to thetoString
- Returns:
- this
-
append
public ToStringBuilder append(int[] array)
Append to the
toString
anint
array.- Parameters:
array
- the array to add to thetoString
- Returns:
- this
-
append
public ToStringBuilder append(long value)
Append to the
toString
along
value.- Parameters:
value
- the value to add to thetoString
- Returns:
- this
-
append
public ToStringBuilder append(long[] array)
Append to the
toString
along
array.- Parameters:
array
- the array to add to thetoString
- Returns:
- this
-
append
public ToStringBuilder append(Object obj)
Append to the
toString
anObject
value.- Parameters:
obj
- the value to add to thetoString
- Returns:
- this
-
append
public ToStringBuilder append(Object[] array)
Append to the
toString
anObject
array.- Parameters:
array
- the array to add to thetoString
- Returns:
- this
-
append
public ToStringBuilder append(short value)
Append to the
toString
ashort
value.- Parameters:
value
- the value to add to thetoString
- Returns:
- this
-
append
public ToStringBuilder append(short[] array)
Append to the
toString
ashort
array.- Parameters:
array
- the array to add to thetoString
- Returns:
- this
-
append
public ToStringBuilder append(String fieldName, boolean value)
Append to the
toString
aboolean
value.- Parameters:
fieldName
- the field namevalue
- the value to add to thetoString
- Returns:
- this
-
append
public ToStringBuilder append(String fieldName, boolean[] array)
Append to the
toString
aboolean
array.- Parameters:
fieldName
- the field namearray
- the array to add to thehashCode
- Returns:
- this
-
append
public ToStringBuilder append(String fieldName, boolean[] array, boolean fullDetail)
Append to the
toString
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
-
append
public ToStringBuilder append(String fieldName, byte value)
Append to the
toString
anbyte
value.- Parameters:
fieldName
- the field namevalue
- the value to add to thetoString
- Returns:
- this
-
append
public ToStringBuilder append(String fieldName, byte[] array)
Append to the
toString
abyte
array.- Parameters:
fieldName
- the field namearray
- the array to add to thetoString
- Returns:
- this
-
append
public ToStringBuilder append(String fieldName, byte[] array, boolean fullDetail)
Append to the
toString
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
-
append
public ToStringBuilder append(String fieldName, char value)
Append to the
toString
achar
value.- Parameters:
fieldName
- the field namevalue
- the value to add to thetoString
- Returns:
- this
-
append
public ToStringBuilder append(String fieldName, char[] array)
Append to the
toString
achar
array.- Parameters:
fieldName
- the field namearray
- the array to add to thetoString
- Returns:
- this
-
append
public ToStringBuilder append(String fieldName, char[] array, boolean fullDetail)
Append to the
toString
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
-
append
public ToStringBuilder append(String fieldName, double value)
Append to the
toString
adouble
value.- Parameters:
fieldName
- the field namevalue
- the value to add to thetoString
- Returns:
- this
-
append
public ToStringBuilder append(String fieldName, double[] array)
Append to the
toString
adouble
array.- Parameters:
fieldName
- the field namearray
- the array to add to thetoString
- Returns:
- this
-
append
public ToStringBuilder append(String fieldName, double[] array, boolean fullDetail)
Append to the
toString
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
-
append
public ToStringBuilder append(String fieldName, float value)
Append to the
toString
anfloat
value.- Parameters:
fieldName
- the field namevalue
- the value to add to thetoString
- Returns:
- this
-
append
public ToStringBuilder append(String fieldName, float[] array)
Append to the
toString
afloat
array.- Parameters:
fieldName
- the field namearray
- the array to add to thetoString
- Returns:
- this
-
append
public ToStringBuilder append(String fieldName, float[] array, boolean fullDetail)
Append to the
toString
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
-
append
public ToStringBuilder append(String fieldName, int value)
Append to the
toString
anint
value.- Parameters:
fieldName
- the field namevalue
- the value to add to thetoString
- Returns:
- this
-
append
public ToStringBuilder append(String fieldName, int[] array)
Append to the
toString
anint
array.- Parameters:
fieldName
- the field namearray
- the array to add to thetoString
- Returns:
- this
-
append
public ToStringBuilder append(String fieldName, int[] array, boolean fullDetail)
Append to the
toString
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
-
append
public ToStringBuilder append(String fieldName, long value)
Append to the
toString
along
value.- Parameters:
fieldName
- the field namevalue
- the value to add to thetoString
- Returns:
- this
-
append
public ToStringBuilder append(String fieldName, long[] array)
Append to the
toString
along
array.- Parameters:
fieldName
- the field namearray
- the array to add to thetoString
- Returns:
- this
-
append
public ToStringBuilder append(String fieldName, long[] array, boolean fullDetail)
Append to the
toString
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
-
append
public ToStringBuilder append(String fieldName, Object obj)
Append to the
toString
anObject
value.- Parameters:
fieldName
- the field nameobj
- the value to add to thetoString
- Returns:
- this
-
append
public ToStringBuilder append(String fieldName, Object obj, boolean fullDetail)
Append to the
toString
anObject
value.- Parameters:
fieldName
- the field nameobj
- the value to add to thetoString
fullDetail
-true
for detail,false
for summary info- Returns:
- this
-
append
public ToStringBuilder append(String fieldName, Object[] array)
Append to the
toString
anObject
array.- Parameters:
fieldName
- the field namearray
- the array to add to thetoString
- Returns:
- this
-
append
public ToStringBuilder append(String fieldName, Object[] array, boolean fullDetail)
Append to the
toString
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
-
append
public ToStringBuilder append(String fieldName, short value)
Append to the
toString
anshort
value.- Parameters:
fieldName
- the field namevalue
- the value to add to thetoString
- Returns:
- this
-
append
public ToStringBuilder append(String fieldName, short[] array)
Append to the
toString
ashort
array.- Parameters:
fieldName
- the field namearray
- the array to add to thetoString
- Returns:
- this
-
append
public ToStringBuilder append(String fieldName, short[] array, boolean fullDetail)
Append to the
toString
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
-
appendAsObjectToString
public ToStringBuilder appendAsObjectToString(Object object)
Appends with the same format as the default
Object toString()
method. Appends the class name followed bySystem.identityHashCode(java.lang.Object)
.- Parameters:
object
- theObject
whose class name and id to output- Returns:
- this
- Since:
- 2.0
-
appendSuper
public 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
isnull
, no change is made.- Parameters:
superToString
- the result ofsuper.toString()
- Returns:
- this
- Since:
- 2.0
-
appendToString
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
isnull
, no change is made.- Parameters:
toString
- the result oftoString()
on another object- Returns:
- this
- Since:
- 2.0
-
getObject
public Object getObject()
Returns the
Object
being output.- Returns:
- The object being output.
- Since:
- 2.0
-
getStringBuffer
public StringBuffer getStringBuffer()
Gets the
StringBuffer
being populated.- Returns:
- the
StringBuffer
being populated
-
getStyle
public ToStringStyle getStyle()
Gets the
ToStringStyle
being used.- Returns:
- the
ToStringStyle
being used - Since:
- 2.0
-
toString
public 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
-
-