Class ToStringBuilder
- All Implemented Interfaces:
Serializable,Builder<String>
- Direct Known Subclasses:
ReflectionToStringBuilder
Object.toString()方法
这个类支持为任何类或对象构建良好且一致的toString()这个类的目的是通过
- 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();
}
}
- Since:
- Java 17+
- Author:
- Kimi Liu
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.aoju.bus.core.builder.Builder
Builder.HashKey -
Constructor Summary
ConstructorsConstructorDescriptionToStringBuilder(Object object) ToStringBuilder(Object object, ToStringStyle style) ToStringBuilder(Object object, ToStringStyle style, StringBuffer buffer) -
Method Summary
Modifier and TypeMethodDescriptionappend(boolean value) Append to thetoStringabooleanvalue.append(boolean[] array) Append to thetoStringabooleanarray.append(byte value) Append to thetoStringabytevalue.append(byte[] array) Append to thetoStringabytearray.append(char value) Append to thetoStringacharvalue.append(char[] array) Append to thetoStringachararray.append(double value) Append to thetoStringadoublevalue.append(double[] array) Append to thetoStringadoublearray.append(float value) Append to thetoStringafloatvalue.append(float[] array) Append to thetoStringafloatarray.append(int value) Append to thetoStringanintvalue.append(int[] array) Append to thetoStringanintarray.append(long value) Append to thetoStringalongvalue.append(long[] array) Append to thetoStringalongarray.append(short value) Append to thetoStringashortvalue.append(short[] array) Append to thetoStringashortarray.Append to thetoStringanObjectvalue.Append to thetoStringanObjectarray.Append to thetoStringabooleanvalue.Append to thetoStringabooleanarray.Append to thetoStringabooleanarray.Append to thetoStringanbytevalue.Append to thetoStringabytearray.Append to thetoStringabytearray.Append to thetoStringacharvalue.Append to thetoStringachararray.Append to thetoStringachararray.Append to thetoStringadoublevalue.Append to thetoStringadoublearray.Append to thetoStringadoublearray.Append to thetoStringanfloatvalue.Append to thetoStringafloatarray.Append to thetoStringafloatarray.Append to thetoStringanintvalue.Append to thetoStringanintarray.Append to thetoStringanintarray.Append to thetoStringalongvalue.Append to thetoStringalongarray.Append to thetoStringalongarray.Append to thetoStringanshortvalue.Append to thetoStringashortarray.Append to thetoStringashortarray.Append to thetoStringanObjectvalue.Append to thetoStringanObjectarray.Append to thetoStringanObjectarray.Append 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 ToStringStyleGets the defaultToStringStyleto use.Returns 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.
-
Constructor Details
-
ToStringBuilder
-
ToStringBuilder
-
ToStringBuilder
-
-
Method Details
-
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 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(org.aoju.bus.core.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
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 fromgetDefaultStyle().- Parameters:
style- the defaultToStringStyle- Throws:
IllegalArgumentException- if the style isnull
-
reflectionToString
-
reflectionToString
Uses
ReflectionToStringBuilderto 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) 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- Returns:
- the String result
- See Also:
-
reflectionToString
public static <T> String reflectionToString(T object, ToStringStyle style, boolean outputTransients, Class<? super T> reflectUpToClass) Uses
ReflectionToStringBuilderto 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
- See Also:
-
append
Append to the
toStringabooleanvalue.- Parameters:
value- the value to add to thetoString- Returns:
- this
-
append
Append to the
toStringabooleanarray.- Parameters:
array- the array to add to thetoString- Returns:
- this
-
append
Append to the
toStringabytevalue.- Parameters:
value- the value to add to thetoString- Returns:
- this
-
append
Append to the
toStringabytearray.- Parameters:
array- the array to add to thetoString- Returns:
- this
-
append
Append to the
toStringacharvalue.- Parameters:
value- the value to add to thetoString- Returns:
- this
-
append
Append to the
toStringachararray.- Parameters:
array- the array to add to thetoString- Returns:
- this
-
append
Append to the
toStringadoublevalue.- Parameters:
value- the value to add to thetoString- Returns:
- this
-
append
Append to the
toStringadoublearray.- Parameters:
array- the array to add to thetoString- Returns:
- this
-
append
Append to the
toStringafloatvalue.- Parameters:
value- the value to add to thetoString- Returns:
- this
-
append
Append to the
toStringafloatarray.- Parameters:
array- the array to add to thetoString- Returns:
- this
-
append
Append to the
toStringanintvalue.- Parameters:
value- the value to add to thetoString- Returns:
- this
-
append
Append to the
toStringanintarray.- Parameters:
array- the array to add to thetoString- Returns:
- this
-
append
Append to the
toStringalongvalue.- Parameters:
value- the value to add to thetoString- Returns:
- this
-
append
Append to the
toStringalongarray.- Parameters:
array- the array to add to thetoString- Returns:
- this
-
append
Append to the
toStringanObjectvalue.- Parameters:
object- the value to add to thetoString- Returns:
- this
-
append
Append to the
toStringanObjectarray.- Parameters:
array- the array to add to thetoString- Returns:
- this
-
append
Append to the
toStringashortvalue.- Parameters:
value- the value to add to thetoString- Returns:
- this
-
append
Append to the
toStringashortarray.- Parameters:
array- the array to add to thetoString- Returns:
- this
-
append
Append to the
toStringabooleanvalue.- Parameters:
fieldName- the field namevalue- the value to add to thetoString- Returns:
- this
-
append
Append to the
toStringabooleanarray.- Parameters:
fieldName- the field namearray- the array to add to thehashCode- Returns:
- this
-
append
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 of the array.- Parameters:
fieldName- the field namearray- the array to add to thetoStringfullDetail-truefor detail,falsefor summary info- Returns:
- this
-
append
Append to the
toStringanbytevalue.- Parameters:
fieldName- the field namevalue- the value to add to thetoString- Returns:
- this
-
append
Append to the
toStringabytearray.- Parameters:
fieldName- the field namearray- the array to add to thetoString- Returns:
- this
-
append
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 of the array.- Parameters:
fieldName- the field namearray- the array to add to thetoStringfullDetail-truefor detail,falsefor summary info- Returns:
- this
-
append
Append to the
toStringacharvalue.- Parameters:
fieldName- the field namevalue- the value to add to thetoString- Returns:
- this
-
append
Append to the
toStringachararray.- Parameters:
fieldName- the field namearray- the array to add to thetoString- Returns:
- this
-
append
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 of the array.- Parameters:
fieldName- the field namearray- the array to add to thetoStringfullDetail-truefor detail,falsefor summary info- Returns:
- this
-
append
Append to the
toStringadoublevalue.- Parameters:
fieldName- the field namevalue- the value to add to thetoString- Returns:
- this
-
append
Append to the
toStringadoublearray.- Parameters:
fieldName- the field namearray- the array to add to thetoString- Returns:
- this
-
append
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 of the array.- Parameters:
fieldName- the field namearray- the array to add to thetoStringfullDetail-truefor detail,falsefor summary info- Returns:
- this
-
append
Append to the
toStringanfloatvalue.- Parameters:
fieldName- the field namevalue- the value to add to thetoString- Returns:
- this
-
append
Append to the
toStringafloatarray.- Parameters:
fieldName- the field namearray- the array to add to thetoString- Returns:
- this
-
append
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 of the array.- Parameters:
fieldName- the field namearray- the array to add to thetoStringfullDetail-truefor detail,falsefor summary info- Returns:
- this
-
append
Append to the
toStringanintvalue.- Parameters:
fieldName- the field namevalue- the value to add to thetoString- Returns:
- this
-
append
Append to the
toStringanintarray.- Parameters:
fieldName- the field namearray- the array to add to thetoString- Returns:
- this
-
append
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 of the array.- Parameters:
fieldName- the field namearray- the array to add to thetoStringfullDetail-truefor detail,falsefor summary info- Returns:
- this
-
append
Append to the
toStringalongvalue.- Parameters:
fieldName- the field namevalue- the value to add to thetoString- Returns:
- this
-
append
Append to the
toStringalongarray.- Parameters:
fieldName- the field namearray- the array to add to thetoString- Returns:
- this
-
append
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 of the array.- Parameters:
fieldName- the field namearray- the array to add to thetoStringfullDetail-truefor detail,falsefor summary info- Returns:
- this
-
append
Append to the
toStringanObjectvalue.- Parameters:
fieldName- the field nameobject- the value to add to thetoString- Returns:
- this
-
append
Append to the
toStringanObjectvalue.- Parameters:
fieldName- the field nameobject- the value to add to thetoStringfullDetail-truefor detail,falsefor summary info- Returns:
- this
-
append
Append to the
toStringanObjectarray.- Parameters:
fieldName- the field namearray- the array to add to thetoString- Returns:
- this
-
append
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 of the array.- Parameters:
fieldName- the field namearray- the array to add to thetoStringfullDetail-truefor detail,falsefor summary info- Returns:
- this
-
append
Append to the
toStringanshortvalue.- Parameters:
fieldName- the field namevalue- the value to add to thetoString- Returns:
- this
-
append
Append to the
toStringashortarray.- Parameters:
fieldName- the field namearray- the array to add to thetoString- Returns:
- this
-
append
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 of the array.- Parameters:
fieldName- the field namearray- the array to add to thetoStringfullDetail-truefor detail,falsefor summary info- Returns:
- this
-
appendAsObjectToString
Appends with the same format as the default
Object toString()method. Appends the class name followed bySystem.identityHashCode(Object).- Parameters:
srcObject- theObjectwhose class name and id to output- Returns:
- this
-
appendSuper
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()- Returns:
- this
-
appendToString
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 oftoString()on another object- Returns:
- this
-
getObject
-
getStringBuffer
Gets the
StringBufferbeing populated.- Returns:
- the
StringBufferbeing populated
-
getStyle
Gets the
ToStringStylebeing used.- Returns:
- the
ToStringStylebeing used
-
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 -
build
Returns the String that was build as an object representation. The default implementation utilizes thetoString()implementation.
-