ToStringBuilder¶
- public class org.apache.commons.lang3.builder.ToStringBuilder extends AbstractReflection implements Builder<String>[source]¶
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, 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.See also
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
toStringis determined by theToStringStylepassed into the constructor.- See also:
- Since:
1.0
Constructors¶
Constructs a builder for the specified object using the default output style. |
|
Constructs a builder for the specified object using the defined output style. |
|
Constructs a builder for the specified object. |
- public ToStringBuilder(Object object)[source]¶
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 a
toStringfor, not recommended to be null
- public ToStringBuilder(Object object, ToStringStyle style)[source]¶
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 a
toStringfor, not recommended to be nullstyle – The style of the
toStringto create, null uses the default style
- public ToStringBuilder(Object object, ToStringStyle style, StringBuffer buffer)[source]¶
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 a
toStringfor, not recommended to be nullstyle – The style of the
toStringto create, null uses the default stylebuffer – The
StringBufferto populate, may be null
Methods¶
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends to the |
|
Appends with the same format as the default |
|
Append the |
|
Append the |
|
Returns the String that was build as an object representation. |
|
Constructs a new Builder. |
|
Gets the default |
|
Gets the |
|
Gets the |
|
Gets the |
|
Uses |
|
Uses |
|
Uses |
|
Uses |
|
Sets the default |
|
Returns the built |
- public ToStringBuilder append(boolean value)[source]¶
Appends to the
toStringabooleanvalue.- Parameters:
value – The value to add to the
toString- Returns:
thisinstance.
- public ToStringBuilder append(boolean[] array)[source]¶
Appends to the
toStringabooleanarray.- Parameters:
array – The array to add to the
toString- Returns:
thisinstance.
- public ToStringBuilder append(byte value)[source]¶
Appends to the
toStringabytevalue.- Parameters:
value – The value to add to the
toString- Returns:
thisinstance.
- public ToStringBuilder append(byte[] array)[source]¶
Appends to the
toStringabytearray.- Parameters:
array – The array to add to the
toString- Returns:
thisinstance.
- public ToStringBuilder append(char value)[source]¶
Appends to the
toStringacharvalue.- Parameters:
value – The value to add to the
toString- Returns:
thisinstance.
- public ToStringBuilder append(char[] array)[source]¶
Appends to the
toStringachararray.- Parameters:
array – The array to add to the
toString- Returns:
thisinstance.
- public ToStringBuilder append(double value)[source]¶
Appends to the
toStringadoublevalue.- Parameters:
value – The value to add to the
toString- Returns:
thisinstance.
- public ToStringBuilder append(double[] array)[source]¶
Appends to the
toStringadoublearray.- Parameters:
array – The array to add to the
toString- Returns:
thisinstance.
- public ToStringBuilder append(float value)[source]¶
Appends to the
toStringafloatvalue.- Parameters:
value – The value to add to the
toString- Returns:
thisinstance.
- public ToStringBuilder append(float[] array)[source]¶
Appends to the
toStringafloatarray.- Parameters:
array – The array to add to the
toString- Returns:
thisinstance.
- public ToStringBuilder append(int value)[source]¶
Appends to the
toStringanintvalue.- Parameters:
value – The value to add to the
toString- Returns:
thisinstance.
- public ToStringBuilder append(int[] array)[source]¶
Appends to the
toStringanintarray.- Parameters:
array – The array to add to the
toString- Returns:
thisinstance.
- public ToStringBuilder append(Object obj)[source]¶
Appends to the
toStringanObjectvalue.- Parameters:
obj – The value to add to the
toString- Returns:
thisinstance.
- public ToStringBuilder append(Object[] array)[source]¶
Appends to the
toStringanObjectarray.- Parameters:
array – The array to add to the
toString- Returns:
thisinstance.
- public ToStringBuilder append(String fieldName, boolean value)[source]¶
Appends to the
toStringabooleanvalue.- Parameters:
fieldName – The field name
value – The value to add to the
toString
- Returns:
thisinstance.
- public ToStringBuilder append(String fieldName, boolean[] array)[source]¶
Appends to the
toStringabooleanarray.- Parameters:
fieldName – The field name
array – The array to add to the
hashCode
- Returns:
thisinstance.
- public ToStringBuilder append(String fieldName, boolean[] array, boolean fullDetail)[source]¶
Appends 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 of the array.- Parameters:
fieldName – The field name
array – The array to add to the
toStringfullDetail –
truefor detail,falsefor summary info
- Returns:
thisinstance.
- public ToStringBuilder append(String fieldName, byte value)[source]¶
Appends to the
toStringanbytevalue.- Parameters:
fieldName – The field name
value – The value to add to the
toString
- Returns:
thisinstance.
- public ToStringBuilder append(String fieldName, byte[] array)[source]¶
Appends to the
toStringabytearray.- Parameters:
fieldName – The field name
array – The array to add to the
toString
- Returns:
thisinstance.
- public ToStringBuilder append(String fieldName, byte[] array, boolean fullDetail)[source]¶
Appends 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 of the array.- Parameters:
fieldName – The field name
array – The array to add to the
toStringfullDetail –
truefor detail,falsefor summary info
- Returns:
thisinstance.
- public ToStringBuilder append(String fieldName, char value)[source]¶
Appends to the
toStringacharvalue.- Parameters:
fieldName – The field name
value – The value to add to the
toString
- Returns:
thisinstance.
- public ToStringBuilder append(String fieldName, char[] array)[source]¶
Appends to the
toStringachararray.- Parameters:
fieldName – The field name
array – The array to add to the
toString
- Returns:
thisinstance.
- public ToStringBuilder append(String fieldName, char[] array, boolean fullDetail)[source]¶
Appends 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 of the array.- Parameters:
fieldName – The field name
array – The array to add to the
toStringfullDetail –
truefor detail,falsefor summary info
- Returns:
thisinstance.
- public ToStringBuilder append(String fieldName, double value)[source]¶
Appends to the
toStringadoublevalue.- Parameters:
fieldName – The field name
value – The value to add to the
toString
- Returns:
thisinstance.
- public ToStringBuilder append(String fieldName, double[] array)[source]¶
Appends to the
toStringadoublearray.- Parameters:
fieldName – The field name
array – The array to add to the
toString
- Returns:
thisinstance.
- public ToStringBuilder append(String fieldName, double[] array, boolean fullDetail)[source]¶
Appends 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 of the array.- Parameters:
fieldName – The field name
array – The array to add to the
toStringfullDetail –
truefor detail,falsefor summary info
- Returns:
thisinstance.
- public ToStringBuilder append(String fieldName, float value)[source]¶
Appends to the
toStringanfloatvalue.- Parameters:
fieldName – The field name
value – The value to add to the
toString
- Returns:
thisinstance.
- public ToStringBuilder append(String fieldName, float[] array)[source]¶
Appends to the
toStringafloatarray.- Parameters:
fieldName – The field name
array – The array to add to the
toString
- Returns:
thisinstance.
- public ToStringBuilder append(String fieldName, float[] array, boolean fullDetail)[source]¶
Appends 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 of the array.- Parameters:
fieldName – The field name
array – The array to add to the
toStringfullDetail –
truefor detail,falsefor summary info
- Returns:
thisinstance.
- public ToStringBuilder append(String fieldName, int value)[source]¶
Appends to the
toStringanintvalue.- Parameters:
fieldName – The field name
value – The value to add to the
toString
- Returns:
thisinstance.
- public ToStringBuilder append(String fieldName, int[] array)[source]¶
Appends to the
toStringanintarray.- Parameters:
fieldName – The field name
array – The array to add to the
toString
- Returns:
thisinstance.
- public ToStringBuilder append(String fieldName, int[] array, boolean fullDetail)[source]¶
Appends 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 of the array.- Parameters:
fieldName – The field name
array – The array to add to the
toStringfullDetail –
truefor detail,falsefor summary info
- Returns:
thisinstance.
- public ToStringBuilder append(String fieldName, Object obj)[source]¶
Appends to the
toStringanObjectvalue.- Parameters:
fieldName – The field name
obj – The value to add to the
toString
- Returns:
thisinstance.
- public ToStringBuilder append(String fieldName, Object obj, boolean fullDetail)[source]¶
Appends to the
toStringanObjectvalue.- Parameters:
fieldName – The field name
obj – The value to add to the
toStringfullDetail –
truefor detail,falsefor summary info
- Returns:
thisinstance.
- public ToStringBuilder append(String fieldName, Object[] array)[source]¶
Appends to the
toStringanObjectarray.- Parameters:
fieldName – The field name
array – The array to add to the
toString
- Returns:
thisinstance.
- public ToStringBuilder append(String fieldName, Object[] array, boolean fullDetail)[source]¶
Appends 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 of the array.- Parameters:
fieldName – The field name
array – The array to add to the
toStringfullDetail –
truefor detail,falsefor summary info
- Returns:
thisinstance.
- public ToStringBuilder append(String fieldName, long value)[source]¶
Appends to the
toStringalongvalue.- Parameters:
fieldName – The field name
value – The value to add to the
toString
- Returns:
thisinstance.
- public ToStringBuilder append(String fieldName, long[] array)[source]¶
Appends to the
toStringalongarray.- Parameters:
fieldName – The field name
array – The array to add to the
toString
- Returns:
thisinstance.
- public ToStringBuilder append(String fieldName, long[] array, boolean fullDetail)[source]¶
Appends 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 of the array.- Parameters:
fieldName – The field name
array – The array to add to the
toStringfullDetail –
truefor detail,falsefor summary info
- Returns:
thisinstance.
- public ToStringBuilder append(String fieldName, short value)[source]¶
Appends to the
toStringanshortvalue.- Parameters:
fieldName – The field name
value – The value to add to the
toString
- Returns:
thisinstance.
- public ToStringBuilder append(String fieldName, short[] array)[source]¶
Appends to the
toStringashortarray.- Parameters:
fieldName – The field name
array – The array to add to the
toString
- Returns:
thisinstance.
- public ToStringBuilder append(String fieldName, short[] array, boolean fullDetail)[source]¶
Appends 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 of the array.- Parameters:
fieldName – The field name
array – The array to add to the
toStringfullDetail –
truefor detail,falsefor summary info
- Returns:
thisinstance.
- public ToStringBuilder append(long value)[source]¶
Appends to the
toStringalongvalue.- Parameters:
value – The value to add to the
toString- Returns:
thisinstance.
- public ToStringBuilder append(long[] array)[source]¶
Appends to the
toStringalongarray.- Parameters:
array – The array to add to the
toString- Returns:
thisinstance.
- public ToStringBuilder append(short value)[source]¶
Appends to the
toStringashortvalue.- Parameters:
value – The value to add to the
toString- Returns:
thisinstance.
- public ToStringBuilder append(short[] array)[source]¶
Appends to the
toStringashortarray.- Parameters:
array – The array to add to the
toString- Returns:
thisinstance.
- public ToStringBuilder appendAsObjectToString(Object srcObject)[source]¶
Appends with the same format as the default
Object toString()method. Appends the class name followed bySystem#identityHashCode(Object).- Parameters:
srcObject – The
Objectwhose class name and id to output- Returns:
thisinstance.- Throws:
java.lang.NullPointerException – Thrown if
srcObjectisnull.- Since:
2.0
- public ToStringBuilder appendSuper(String superToString)[source]¶
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 of
super.toString()- Returns:
thisinstance.- Since:
2.0
- public ToStringBuilder appendToString(String toString)[source]¶
Append the
toStringfrom 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
- @Override public String build()[source]¶
Returns the String that was build as an object representation. The default implementation utilizes the
#toString()implementation.- Returns:
The String
toString- See also:
- Since:
3.0
- public static ToStringBuilder.Builder builder()[source]¶
Constructs a new Builder.
- Returns:
A new Builder.
- Since:
3.21.0
- public static ToStringStyle getDefaultStyle()[source]¶
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 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 using#setDefaultStyleis 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
- public Object getObject()[source]¶
Gets the
Objectbeing output.- Returns:
The object being output.
- Since:
2.0
- public StringBuffer getStringBuffer()[source]¶
Gets the
StringBufferbeing populated.- Returns:
The
StringBufferbeing populated
- public ToStringStyle getStyle()[source]¶
Gets the
ToStringStylebeing used.- Returns:
The
ToStringStylebeing used- Since:
2.0
- public static String reflectionToString(Object object)[source]¶
Uses
ReflectionToStringBuilderto generate atoStringfor the specified object.- Parameters:
object – The Object to be output
- Returns:
The String result
- See also:
- public static String reflectionToString(Object object, ToStringStyle style)[source]¶
Uses
ReflectionToStringBuilderto generate atoStringfor the specified object.- Parameters:
object – The Object to be output
style – The style of the
toStringto create, may benull
- Returns:
The String result
- See also:
- public static String reflectionToString(Object object, ToStringStyle style, boolean outputTransients)[source]¶
Uses
ReflectionToStringBuilderto generate atoStringfor the specified object.- Parameters:
object – The Object to be output
style – The style of the
toStringto create, may benulloutputTransients – whether to include transient fields
- Returns:
The String result
- See also:
ReflectionToStringBuilder#toString(Object,ToStringStyle,boolean)
- public static <T> String reflectionToString(T object, ToStringStyle style, boolean outputTransients, Class<? super T> reflectUpToClass)[source]¶
Uses
ReflectionToStringBuilderto 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 benulloutputTransients – whether to include transient fields
reflectUpToClass – The superclass to reflect up to (inclusive), may be
null
- Returns:
The String result
- See also:
ReflectionToStringBuilder#toString(Object,ToStringStyle,boolean,boolean,Class)- Since:
2.0
- public static void setDefaultStyle(ToStringStyle style)[source]¶
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 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 from#getDefaultStyle.- Parameters:
style – The default
ToStringStyle- Throws:
java.lang.NullPointerException – Thrown if the style is
null.
- @Override public String toString()[source]¶
Returns the built
toString.This method appends the end of data indicator, and can only be called once. Use
#getStringBufferto get the current string state.If the object is
null, return the style’snullText- Returns:
The String
toString