Object#toString() methods using reflection.
This class uses reflection to determine the fields to append. Because these fields are usually private, the class
uses java. to
change the visibility of the fields. This will fail under a security manager, unless the appropriate permissions are
set up correctly.
Using reflection to access (private) fields circumvents any synchronization protection guarding access to these fields. If a toString method cannot safely read a field, you should exclude it from the toString method, or use synchronization consistent with the class' lock management around the invocation of the method. Take special care to exclude non-thread-safe collection classes, because these classes may throw ConcurrentModificationException if modified while the toString method is executing.
A typical invocation for this method would look like:
public String toString() {
return ReflectionToStringBuilder.toString(this);
}
You can also use the builder to debug 3rd party objects:
System.out.println("An object: " + ReflectionToStringBuilder.toString(anObject));
A subclass can control field output by overriding the methods:
For example, this method does not include the password field in the returned String:
public String toString() {
return (new ReflectionToStringBuilder(this) {
protected boolean accept(Field f) {
return super.accept(f) && !f.getName().equals("password");
}
}).toString();
}
Alternatively the ToStringExclude annotation can be used to exclude fields from being incorporated in the
result.
It is also possible to use the ToStringSummary annotation to output the summary information instead of the
detailed information of a field.
The exact format of the toString is determined by the ToStringStyle passed into the constructor.
Note
the default ToStringStyle will only do a "shallow" formatting, i.e. composed objects are not
further traversed. To get "deep" formatting, use an instance of RecursiveToStringStyle.
| Modifier and Type | Field and Description |
|---|---|
| private boolean | appendStatics
Whether or not to append static fields. |
| private boolean | appendTransients
Whether or not to append transient fields. |
| protected String[] | excludeFieldNames
Which field names to exclude from output. |
| private boolean | excludeNullValues
Whether or not to append fields that are null. |
| protected String[] | includeFieldNames
Field names that will be included in the output. |
| private Class | upToClass
The last super class to stop appending fields for. |
| Access | Constructor and Description |
|---|---|
| public | ReflectionToStringBuilder(final Object
the Object to build a object)toString for, must not be nullConstructs a new instance. |
| public | ReflectionToStringBuilder(final Object
the Object to build a object, final ToStringStyle toString for, must not be nullthe style of the style)toString to create, may be nullConstructs a new instance. |
| public | ReflectionToStringBuilder(final Object
the Object to build a object, final ToStringStyle toString forthe style of the style, final StringBuffer toString to create, may be nullthe buffer)StringBuffer to populate, may be nullConstructs a new instance. |
| public < the type of the object T> | ReflectionToStringBuilder(final T
the Object to build a object, final ToStringStyle toString forthe style of the style, final StringBuffer toString to create, may be nullthe buffer, final Class<? super T> StringBuffer to populate, may be nullthe superclass to reflect up to (inclusive), may be reflectUpToClass, final boolean nullwhether to include transient fields outputTransients, final boolean whether to include static fields outputStatics)Constructs a new instance. |
| public < the type of the object T> | ReflectionToStringBuilder(final T
the Object to build a object, final ToStringStyle toString forthe style of the style, final StringBuffer toString to create, may be nullthe buffer, final Class<? super T> StringBuffer to populate, may be nullthe superclass to reflect up to (inclusive), may be reflectUpToClass, final boolean nullwhether to include transient fields outputTransients, final boolean whether to include static fields outputStatics, final boolean whether to exclude fields who value is null excludeNullValues)Constructs a new instance. |
| Modifier and Type | Method and Description |
|---|---|
| protected boolean | |
| protected void | appendFieldsIn(final Class<?>
The class of object parameter clazz)Appends the fields and values defined by the given object of the given Class. |
| public String[] | |
| public String[] | |
| public Class | Returns: The last super class to stop appending fields for.Gets the last super class to stop appending fields for. |
| protected Object | |
| public boolean | Returns: Whether or not to append static fields.Gets whether or not to append static fields. |
| public boolean | Returns: Whether or not to append transient fields.Gets whether or not to append transient fields. |
| public boolean | Returns: Whether or not to append fields whose values are null.Gets whether or not to append fields whose values are null. |
| public ReflectionToStringBuilder | Returns: this instance.the array to add to the array)toStringAppends to the |
| public void | setAppendStatics(final boolean
Whether or not to append static fields. appendStatics)Sets whether or not to append static fields. |
| public void | setAppendTransients(final boolean
Whether or not to append transient fields. appendTransients)Sets whether or not to append transient fields. |
| public ReflectionToStringBuilder | Returns: thisThe excludeFieldNames to excluding from toString or excludeFieldNamesParam)null.Sets the field names to exclude. |
| public void | setExcludeNullValues(final boolean
Whether or not to append fields whose values are null. excludeNullValues)Sets whether or not to append fields whose values are null. |
| public ReflectionToStringBuilder | Returns: thisThe includeFieldNames that must be on toString or includeFieldNamesParam)null.Sets the field names to include. |
| public void | setUpToClass(final Class<?>
The last super class to stop appending fields for. clazz)Sets the last super class to stop appending fields for. |
| pack-priv static String[] | Returns: A new array of Strings.The collection to convert collection)Converts the given Collection into an array of Strings. |
| pack-priv static String[] | Returns: The given array or a new array without null.The array to check array)Returns a new array of Strings without null elements. |
| public static String | Returns: the String resultthe Object to be output object)Builds a |
| public static String | Returns: the String resultthe Object to be output object, final ToStringStyle the style of the style)toString to create, may be nullBuilds a |
| 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)Builds a |
| 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, final boolean whether to include static fields outputStatics)Builds a |
| 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 boolean whether to include static fields outputStatics, final boolean whether to exclude fields whose values are null excludeNullValues, final Class<? super T> the superclass to reflect up to (inclusive), may be reflectUpToClass)nullBuilds a |
| 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 boolean whether to include static fields outputStatics, final Class<? super T> the superclass to reflect up to (inclusive), may be reflectUpToClass)nullBuilds a |
| public String | Returns: the built stringOverrides org. Gets the String built by this builder. |
| public static String | Returns: The toString value.The object to "toString". object, final Collection<String> The field names to exclude. Null excludes nothing. excludeFieldNames)Builds a String for a toString method excluding the given field names. |
| public static String | Returns: The toString value.The object to "toString". object, final String... The field names to exclude excludeFieldNames)Builds a String for a toString method excluding the given field names. |
| public static String | Returns: The toString value.The object to "toString". object, final Collection<String> null or empty means all fields are included. All fields are included by default. This method will override the default behavior.Builds a String for a toString method including the given field names. |
| public static String | Returns: The toString value.The object to "toString". object, final String... The field names to include. includeFieldNames)null or empty means all fields are included. All fields are included by default. This method will override the default
behavior.Builds a String for a toString method including the given field names. |
| private void |
| appendStatics | back to summary |
|---|---|
| private boolean appendStatics Whether or not to append static fields. | |
| appendTransients | back to summary |
|---|---|
| private boolean appendTransients Whether or not to append transient fields. | |
| excludeFieldNames | back to summary |
|---|---|
| protected String[] excludeFieldNames Which field names to exclude from output. Intended for fields like
| |
| excludeNullValues | back to summary |
|---|---|
| private boolean excludeNullValues Whether or not to append fields that are null. | |
| includeFieldNames | back to summary |
|---|---|
| protected String[] includeFieldNames Field names that will be included in the output. All fields are included by default.
| |
| upToClass | back to summary |
|---|---|
| private Class<?> upToClass The last super class to stop appending fields for. | |
| ReflectionToStringBuilder | back to summary |
|---|---|
| public ReflectionToStringBuilder(final Object object) Constructs a new instance.
This constructor outputs using the default style set with
| |
| ReflectionToStringBuilder | back to summary |
|---|---|
| public ReflectionToStringBuilder(final Object object, final ToStringStyle style) Constructs a new instance.
If the style is
| |
| ReflectionToStringBuilder | back to summary |
|---|---|
| public ReflectionToStringBuilder(final Object object, final ToStringStyle style, final StringBuffer buffer) Constructs a new instance.
If the style is
If the buffer is
| |
| ReflectionToStringBuilder | back to summary |
|---|---|
| public <T> ReflectionToStringBuilder(final T object, final ToStringStyle style, final StringBuffer buffer, final Class<? super T> reflectUpToClass, final boolean outputTransients, final boolean outputStatics) Constructs a new instance.
| |
| ReflectionToStringBuilder | back to summary |
|---|---|
| public <T> ReflectionToStringBuilder(final T object, final ToStringStyle style, final StringBuffer buffer, final Class<? super T> reflectUpToClass, final boolean outputTransients, final boolean outputStatics, final boolean excludeNullValues) Constructs a new instance.
| |
| accept | back to summary |
|---|---|
| protected boolean accept(final Field field) Returns whether or not to append the given
| |
| appendFieldsIn | back to summary |
|---|---|
| protected void appendFieldsIn(final Class<?> clazz) Appends the fields and values defined by the given object of the given Class.
If a cycle is detected as an object is "toString()'ed", such an object is rendered as if
| |
| getExcludeFieldNames | back to summary |
|---|---|
| public String[] getExcludeFieldNames() Gets the excludeFieldNames.
| |
| getIncludeFieldNames | back to summary |
|---|---|
| public String[] getIncludeFieldNames() Gets the includeFieldNames
| |
| getUpToClass | back to summary |
|---|---|
| public Class Gets the last super class to stop appending fields for.
| |
| getValue | back to summary |
|---|---|
| protected Object getValue(final Field field) throws IllegalAccessException Calls
| |
| isAppendStatics | back to summary |
|---|---|
| public boolean isAppendStatics() Gets whether or not to append static fields.
| |
| isAppendTransients | back to summary |
|---|---|
| public boolean isAppendTransients() Gets whether or not to append transient fields.
| |
| isExcludeNullValues | back to summary |
|---|---|
| public boolean isExcludeNullValues() Gets whether or not to append fields whose values are null.
| |
| reflectionAppendArray | back to summary |
|---|---|
| public ReflectionToStringBuilder reflectionAppendArray(final Object array) Appends to the
| |
| setAppendStatics | back to summary |
|---|---|
| public void setAppendStatics(final boolean appendStatics) Sets whether or not to append static fields.
| |
| setAppendTransients | back to summary |
|---|---|
| public void setAppendTransients(final boolean appendTransients) Sets whether or not to append transient fields.
| |
| setExcludeFieldNames | back to summary |
|---|---|
| public ReflectionToStringBuilder setExcludeFieldNames(final String... excludeFieldNamesParam) Sets the field names to exclude.
| |
| setExcludeNullValues | back to summary |
|---|---|
| public void setExcludeNullValues(final boolean excludeNullValues) Sets whether or not to append fields whose values are null.
| |
| setIncludeFieldNames | back to summary |
|---|---|
| public ReflectionToStringBuilder setIncludeFieldNames(final String... includeFieldNamesParam) Sets the field names to include.
| |
| setUpToClass | back to summary |
|---|---|
| public void setUpToClass(final Class<?> clazz) Sets the last super class to stop appending fields for.
| |
| toNoNullStringArray | back to summary |
|---|---|
| pack-priv static String[] toNoNullStringArray(final Collection<String> collection) Converts the given Collection into an array of Strings. The returned array does not contain
| |
| toNoNullStringArray | back to summary |
|---|---|
| pack-priv static String[] toNoNullStringArray(final Object[] array) Returns a new array of Strings without null elements. Internal method used to normalize exclude lists
(arrays and collections). Note that | |
| toString | back to summary |
|---|---|
| public static String toString(final Object object) Builds a
It uses Transient members will be not be included, as they are likely derived. Static fields will not be included. Superclass fields will be appended.
| |
| toString | back to summary |
|---|---|
| public static String toString(final Object object, final ToStringStyle style) Builds a
It uses Transient members will be not be included, as they are likely derived. Static fields will not be included. Superclass fields will be appended.
If the style is
| |
| toString | back to summary |
|---|---|
| public static String toString(final Object object, final ToStringStyle style, final boolean outputTransients) Builds a
It uses
If the Static fields will not be included. Superclass fields will be appended.
If the style is
| |
| toString | back to summary |
|---|---|
| public static String toString(final Object object, final ToStringStyle style, final boolean outputTransients, final boolean outputStatics) Builds a
It uses
If the
If the Static fields will not be included. Superclass fields will be appended.
If the style is
| |
| toString | back to summary |
|---|---|
| public static <T> String toString(final T object, final ToStringStyle style, final boolean outputTransients, final boolean outputStatics, final boolean excludeNullValues, final Class<? super T> reflectUpToClass) Builds a
It uses
If the
If the
Superclass fields will be appended up to and including the specified superclass. A null superclass is treated as
If the style is
| |
| toString | back to summary |
|---|---|
| public static <T> String toString(final T object, final ToStringStyle style, final boolean outputTransients, final boolean outputStatics, final Class<? super T> reflectUpToClass) Builds a
It uses
If the
If the
Superclass fields will be appended up to and including the specified superclass. A null superclass is treated as
If the style is
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides org. Gets the String built by this builder. | |
| toStringExclude | back to summary |
|---|---|
| public static String toStringExclude(final Object object, final Collection<String> excludeFieldNames) Builds a String for a toString method excluding the given field names.
| |
| toStringExclude | back to summary |
|---|---|
| public static String toStringExclude(final Object object, final String... excludeFieldNames) Builds a String for a toString method excluding the given field names. | |
| toStringInclude | back to summary |
|---|---|
| public static String toStringInclude(final Object object, final Collection<String> includeFieldNames) Builds a String for a toString method including the given field names.
| |
| toStringInclude | back to summary |
|---|---|
| public static String toStringInclude(final Object object, final String... includeFieldNames) Builds a String for a toString method including the given field names. | |
| validate | back to summary |
|---|---|
| private void validate() Validates that include and exclude names do not intersect. | |