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:
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 the toString from 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, 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.
| Modifier and Type | Field and Description |
|---|---|
| private final StringBuffer | buffer
Current toString buffer, not null. |
| private static volatile ToStringStyle | defaultStyle
The default style of output to use, not null. |
| private final Object | object
The object being output, may be null. |
| private final ToStringStyle | style
The style of output to use, not null. |
| Access | Constructor and Description |
|---|---|
| public | ToStringBuilder(final Object
the Object to build a object)toString for, not recommended to be nullConstructs a builder for the specified object using the default output style. |
| public | ToStringBuilder(final Object
the Object to build a object, final ToStringStyle toString for, not recommended to be nullthe style of the style)toString to create, null uses the default styleConstructs a builder for the specified object using the defined output style. |
| public | ToStringBuilder(final Object
the Object to build a object, ToStringStyle toString for, not recommended to be nullthe style of the style, StringBuffer toString to create, null uses the default stylethe buffer)StringBuffer to populate, may be nullConstructs a builder for the specified object. |
| Modifier and Type | Method and Description |
|---|---|
| public ToStringBuilder | Returns: this instance.the value to add to the value)toStringAppend to the |
| public ToStringBuilder | Returns: this instance.the array to add to the array)toStringAppend to the |
| public ToStringBuilder | Returns: this instance.the value to add to the value)toStringAppend to the |
| public ToStringBuilder | Returns: this instance.the array to add to the array)toStringAppend to the |
| public ToStringBuilder | Returns: this instance.the value to add to the value)toStringAppend to the |
| public ToStringBuilder | Returns: this instance.the array to add to the array)toStringAppend to the |
| public ToStringBuilder | Returns: this instance.the value to add to the value)toStringAppend to the |
| public ToStringBuilder | Returns: this instance.the array to add to the array)toStringAppend to the |
| public ToStringBuilder | Returns: this instance.the value to add to the value)toStringAppend to the |
| public ToStringBuilder | Returns: this instance.the array to add to the array)toStringAppend to the |
| public ToStringBuilder | Returns: this instance.the value to add to the value)toStringAppend to the |
| public ToStringBuilder | Returns: this instance.the array to add to the array)toStringAppend to the |
| public ToStringBuilder | Returns: this instance.the value to add to the value)toStringAppend to the |
| public ToStringBuilder | Returns: this instance.the array to add to the array)toStringAppend to the |
| public ToStringBuilder | |
| public ToStringBuilder | |
| public ToStringBuilder | Returns: this instance.the value to add to the value)toStringAppend to the |
| public ToStringBuilder | Returns: this instance.the array to add to the array)toStringAppend to the |
| public ToStringBuilder | |
| public ToStringBuilder | |
| public ToStringBuilder | |
| public ToStringBuilder | |
| public ToStringBuilder | |
| public ToStringBuilder | |
| public ToStringBuilder | |
| public ToStringBuilder | |
| public ToStringBuilder | |
| public ToStringBuilder | |
| public ToStringBuilder | |
| public ToStringBuilder | |
| public ToStringBuilder | |
| public ToStringBuilder | |
| public ToStringBuilder | |
| public ToStringBuilder | |
| public ToStringBuilder | |
| public ToStringBuilder | |
| public ToStringBuilder | |
| public ToStringBuilder | |
| public ToStringBuilder | |
| public ToStringBuilder | |
| public ToStringBuilder | |
| public ToStringBuilder | |
| public ToStringBuilder | |
| public ToStringBuilder | |
| public ToStringBuilder | |
| public ToStringBuilder | |
| public ToStringBuilder | Returns: this instance.the srcObject)Object whose class name and id to outputAppends with the same format as the default |
| public ToStringBuilder | Returns: this instance.the result of superToString)super.toString()Append the |
| public ToStringBuilder | Returns: this instance.the result of toString)toString() on another objectAppend the |
| public String | Returns: the StringtoStringImplements org. Returns the String that was build as an object representation. |
| public static ToStringStyle | Returns: the defaultToStringStyle, never nullGets the default |
| public Object | |
| public StringBuffer | |
| public ToStringStyle | |
| public static String | Returns: the String resultthe Object to be output object)Uses |
| public static String | Returns: the String resultthe Object to be output object, final ToStringStyle the style of the style)toString to create, may be nullUses |
| public static String | Returns: the String resultthe Object to be output object, final ToStringStyle the style of the style, final boolean toString to create, may be nullwhether to include transient fields outputTransients)Uses |
| public static < the type of the object T> String | Returns: the String resultthe Object to be output object, final ToStringStyle the style of the style, final boolean toString to create, may be nullwhether to include transient fields outputTransients, final Class<? super T> the superclass to reflect up to (inclusive), may be reflectUpToClass)nullUses |
| public static void | setDefaultStyle(final ToStringStyle
the default style)ToStringStyleSets the default |
| public String | Returns: the StringtoStringOverrides java. Returns the built |