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(String).
To append the toString from an object that is delegated
to (or any other object), use appendToString(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.
See also AbstractReflection.AbstractBuilder.setForceAccessible(boolean)
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
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilds instances of CompareToBuilder.Nested classes/interfaces inherited from class AbstractReflection
AbstractReflection.AbstractBuilder<B> -
Constructor Summary
ConstructorsConstructorDescriptionToStringBuilder(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) Appends to thetoStringabooleanvalue.append(boolean[] array) Appends to thetoStringabooleanarray.append(byte value) Appends to thetoStringabytevalue.append(byte[] array) Appends to thetoStringabytearray.append(char value) Appends to thetoStringacharvalue.append(char[] array) Appends to thetoStringachararray.append(double value) Appends to thetoStringadoublevalue.append(double[] array) Appends to thetoStringadoublearray.append(float value) Appends to thetoStringafloatvalue.append(float[] array) Appends to thetoStringafloatarray.append(int value) Appends to thetoStringanintvalue.append(int[] array) Appends to thetoStringanintarray.append(long value) Appends to thetoStringalongvalue.append(long[] array) Appends to thetoStringalongarray.append(short value) Appends to thetoStringashortvalue.append(short[] array) Appends to thetoStringashortarray.Appends to thetoStringanObjectvalue.Appends to thetoStringanObjectarray.Appends to thetoStringabooleanvalue.Appends to thetoStringabooleanarray.Appends to thetoStringabooleanarray.Appends to thetoStringanbytevalue.Appends to thetoStringabytearray.Appends to thetoStringabytearray.Appends to thetoStringacharvalue.Appends to thetoStringachararray.Appends to thetoStringachararray.Appends to thetoStringadoublevalue.Appends to thetoStringadoublearray.Appends to thetoStringadoublearray.Appends to thetoStringanfloatvalue.Appends to thetoStringafloatarray.Appends to thetoStringafloatarray.Appends to thetoStringanintvalue.Appends to thetoStringanintarray.Appends to thetoStringanintarray.Appends to thetoStringalongvalue.Appends to thetoStringalongarray.Appends to thetoStringalongarray.Appends to thetoStringanshortvalue.Appends to thetoStringashortarray.Appends to thetoStringashortarray.Appends to thetoStringanObjectvalue.Appends to thetoStringanObjectarray.Appends to thetoStringanObjectarray.Appends 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 ToStringBuilder.Builderbuilder()Constructs a new Builder.static ToStringStyleGets the defaultToStringStyleto use.Gets 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.Methods inherited from class AbstractReflection
getForceAccessible, isForceAccessible, setAccessible
-
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 atoStringfor, 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 atoStringfor, not recommended to be nullstyle- The style of thetoStringto 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 atoStringfor, not recommended to be nullstyle- The style of thetoStringto create, null uses the default stylebuffer- TheStringBufferto populate, may be null
-
-
Method Details
-
builder
Constructs a new Builder.- Returns:
- A new Builder.
- Since:
- 3.21.0
-
getDefaultStyle
Gets 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(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
UsesReflectionToStringBuilderto generate atoStringfor the specified object.- Parameters:
object- The Object to be output- Returns:
- The String result
- See Also:
-
reflectionToString
UsesReflectionToStringBuilderto generate atoStringfor the specified object.- Parameters:
object- The Object to be outputstyle- The style of thetoStringto create, may benull- Returns:
- The String result
- See Also:
-
reflectionToString
public static String reflectionToString(Object object, ToStringStyle style, boolean outputTransients) UsesReflectionToStringBuilderto generate atoStringfor the specified object.- Parameters:
object- The Object to be outputstyle- The style of thetoStringto create, may benulloutputTransients- 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) UsesReflectionToStringBuilderto generate atoStringfor the specified object.- Type Parameters:
T- The type of the 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- Returns:
- The String result
- Since:
- 2.0
- See Also:
-
setDefaultStyle
Sets 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 defaultToStringStyle- Throws:
NullPointerException- Thrown if the style isnull.
-
append
Appends to thetoStringabooleanvalue.- Parameters:
value- The value to add to thetoString- Returns:
thisinstance.
-
append
Appends to thetoStringabooleanarray.- Parameters:
array- The array to add to thetoString- Returns:
thisinstance.
-
append
Appends to thetoStringabytevalue.- Parameters:
value- The value to add to thetoString- Returns:
thisinstance.
-
append
Appends to thetoStringabytearray.- Parameters:
array- The array to add to thetoString- Returns:
thisinstance.
-
append
Appends to thetoStringacharvalue.- Parameters:
value- The value to add to thetoString- Returns:
thisinstance.
-
append
Appends to thetoStringachararray.- Parameters:
array- The array to add to thetoString- Returns:
thisinstance.
-
append
Appends to thetoStringadoublevalue.- Parameters:
value- The value to add to thetoString- Returns:
thisinstance.
-
append
Appends to thetoStringadoublearray.- Parameters:
array- The array to add to thetoString- Returns:
thisinstance.
-
append
Appends to thetoStringafloatvalue.- Parameters:
value- The value to add to thetoString- Returns:
thisinstance.
-
append
Appends to thetoStringafloatarray.- Parameters:
array- The array to add to thetoString- Returns:
thisinstance.
-
append
Appends to thetoStringanintvalue.- Parameters:
value- The value to add to thetoString- Returns:
thisinstance.
-
append
Appends to thetoStringanintarray.- Parameters:
array- The array to add to thetoString- Returns:
thisinstance.
-
append
Appends to thetoStringalongvalue.- Parameters:
value- The value to add to thetoString- Returns:
thisinstance.
-
append
Appends to thetoStringalongarray.- Parameters:
array- The array to add to thetoString- Returns:
thisinstance.
-
append
Appends to thetoStringanObjectvalue.- Parameters:
obj- The value to add to thetoString- Returns:
thisinstance.
-
append
Appends to thetoStringanObjectarray.- Parameters:
array- The array to add to thetoString- Returns:
thisinstance.
-
append
Appends to thetoStringashortvalue.- Parameters:
value- The value to add to thetoString- Returns:
thisinstance.
-
append
Appends to thetoStringashortarray.- Parameters:
array- The array to add to thetoString- Returns:
thisinstance.
-
append
Appends to thetoStringabooleanvalue.- Parameters:
fieldName- The field namevalue- The value to add to thetoString- Returns:
thisinstance.
-
append
Appends to thetoStringabooleanarray.- Parameters:
fieldName- The field namearray- The array to add to thehashCode- Returns:
thisinstance.
-
append
Appends 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 namearray- The array to add to thetoStringfullDetail-truefor detail,falsefor summary info- Returns:
thisinstance.
-
append
Appends to thetoStringanbytevalue.- Parameters:
fieldName- The field namevalue- The value to add to thetoString- Returns:
thisinstance.
-
append
Appends to thetoStringabytearray.- Parameters:
fieldName- The field namearray- The array to add to thetoString- Returns:
thisinstance.
-
append
Appends 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 namearray- The array to add to thetoStringfullDetail-truefor detail,falsefor summary info- Returns:
thisinstance.
-
append
Appends to thetoStringacharvalue.- Parameters:
fieldName- The field namevalue- The value to add to thetoString- Returns:
thisinstance.
-
append
Appends to thetoStringachararray.- Parameters:
fieldName- The field namearray- The array to add to thetoString- Returns:
thisinstance.
-
append
Appends 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 namearray- The array to add to thetoStringfullDetail-truefor detail,falsefor summary info- Returns:
thisinstance.
-
append
Appends to thetoStringadoublevalue.- Parameters:
fieldName- The field namevalue- The value to add to thetoString- Returns:
thisinstance.
-
append
Appends to thetoStringadoublearray.- Parameters:
fieldName- The field namearray- The array to add to thetoString- Returns:
thisinstance.
-
append
Appends 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 namearray- The array to add to thetoStringfullDetail-truefor detail,falsefor summary info- Returns:
thisinstance.
-
append
Appends to thetoStringanfloatvalue.- Parameters:
fieldName- The field namevalue- The value to add to thetoString- Returns:
thisinstance.
-
append
Appends to thetoStringafloatarray.- Parameters:
fieldName- The field namearray- The array to add to thetoString- Returns:
thisinstance.
-
append
Appends 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 namearray- The array to add to thetoStringfullDetail-truefor detail,falsefor summary info- Returns:
thisinstance.
-
append
Appends to thetoStringanintvalue.- Parameters:
fieldName- The field namevalue- The value to add to thetoString- Returns:
thisinstance.
-
append
Appends to thetoStringanintarray.- Parameters:
fieldName- The field namearray- The array to add to thetoString- Returns:
thisinstance.
-
append
Appends 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 namearray- The array to add to thetoStringfullDetail-truefor detail,falsefor summary info- Returns:
thisinstance.
-
append
Appends to thetoStringalongvalue.- Parameters:
fieldName- The field namevalue- The value to add to thetoString- Returns:
thisinstance.
-
append
Appends to thetoStringalongarray.- Parameters:
fieldName- The field namearray- The array to add to thetoString- Returns:
thisinstance.
-
append
Appends 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 namearray- The array to add to thetoStringfullDetail-truefor detail,falsefor summary info- Returns:
thisinstance.
-
append
Appends to thetoStringanObjectvalue.- Parameters:
fieldName- The field nameobj- The value to add to thetoString- Returns:
thisinstance.
-
append
Appends to thetoStringanObjectvalue.- Parameters:
fieldName- The field nameobj- The value to add to thetoStringfullDetail-truefor detail,falsefor summary info- Returns:
thisinstance.
-
append
Appends to thetoStringanObjectarray.- Parameters:
fieldName- The field namearray- The array to add to thetoString- Returns:
thisinstance.
-
append
Appends 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 namearray- The array to add to thetoStringfullDetail-truefor detail,falsefor summary info- Returns:
thisinstance.
-
append
Appends to thetoStringanshortvalue.- Parameters:
fieldName- The field namevalue- The value to add to thetoString- Returns:
thisinstance.
-
append
Appends to thetoStringashortarray.- Parameters:
fieldName- The field namearray- The array to add to thetoString- Returns:
thisinstance.
-
append
Appends 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 namearray- The array to add to thetoStringfullDetail-truefor detail,falsefor summary info- Returns:
thisinstance.
-
appendAsObjectToString
Appends with the same format as the defaultObject toString()method. Appends the class name followed bySystem.identityHashCode(Object).- Parameters:
srcObject- TheObjectwhose class name and id to output- Returns:
thisinstance.- Throws:
NullPointerException- Thrown ifsrcObjectisnull.- Since:
- 2.0
-
appendSuper
Append 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 ofsuper.toString()- Returns:
thisinstance.- Since:
- 2.0
-
appendToString
Append 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 oftoString()on another object- Returns:
thisinstance.- Since:
- 2.0
-
build
-
getObject
-
getStringBuffer
Gets theStringBufferbeing populated.- Returns:
- The
StringBufferbeing populated
-
getStyle
Gets theToStringStylebeing used.- Returns:
- The
ToStringStylebeing 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
-