toString()
方法。
@author Michael Zhou
Assists in implementing {@link 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 isSmoker; ... 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 {@link #appendSuper}. To append the toString
from an object that is delegated to (or any other object), use {@link #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 {@link ToStringStyle} passed into the constructor.
Assists in implementing {@link 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 {@link #appendSuper}. To append the toString
from an object that is delegated to (or any other object), use {@link #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 {@link ToStringStyle} passed into the constructor.
toString()
method. The code was pasted from the Hivemind container written by Howard Lewis Ship.
@author Siegfried Goeschl
toString()
method.
@author Howard Lewis Ship
You can customize the output using the builder-style methods {@link ToStringBuilder#singleLine()} {@link ToStringBuilder#skipNulls()} and {@link ToStringBuilder#hideFieldNames()}.
You can either directly list fields to include via {@link ToStringBuilder#add(String,Object)} and {@link ToStringBuilder#add(Object)}or you can let the builder do it automatically using reflection, either including the fields declared in this class or including all superclasses.
The builder will automatically handle cycles in the object tree. It also pretty prints arrays and Iterables.
This class is not thread safe. @since 2.7
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|