ReflectionToStringBuilder
- public class org.apache.commons.lang3.builder.ReflectionToStringBuilder extends ToStringBuilder[source]
Assists in implementing
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.lang.reflect.AccessibleObject#setAccessible(java.lang.reflect.AccessibleObject[], boolean)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
passwordfield in the returnedString:public String toString() { return (new ReflectionToStringBuilder(this) { protected boolean accept(Field f) { return super.accept(f) && !f.getName().equals("password"); } }).toString(); }
Alternatively the
ToStringExcludeannotation can be used to exclude fields from being incorporated in the result.It is also possible to use the
ToStringSummaryannotation to output the summary information instead of the detailed information of a field.The exact format of the
toStringis determined by theToStringStylepassed into the constructor.Note: the default
ToStringStylewill only do a “shallow” formatting, i.e. composed objects are not further traversed. To get “deep” formatting, use an instance ofRecursiveToStringStyle.- Since:
2.0
Constructors
Constructs a new instance. |
|
Constructs a new instance. |
|
|
Constructs a new instance. |
|
Constructs a new instance. |
|
Constructs a new instance. |
- public ReflectionToStringBuilder(Object object)[source]
Constructs a new instance.
This constructor outputs using the default style set with
setDefaultStyle.- Parameters:
object – the Object to build a
toStringfor, must not benull
- public ReflectionToStringBuilder(Object object, ToStringStyle style)[source]
Constructs a new instance.
If the style is
null, the default style is used.- Parameters:
object – the Object to build a
toStringfor, must not benullstyle – the style of the
toStringto create, may benull
- public ReflectionToStringBuilder(Object object, ToStringStyle style, StringBuffer buffer)[source]
Constructs a new instance.
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
toStringforstyle – the style of the
toStringto create, may benullbuffer – the
StringBufferto populate, may benull
- public <T> ReflectionToStringBuilder(T object, ToStringStyle style, StringBuffer buffer, Class<? super T> reflectUpToClass, boolean outputTransients, boolean outputStatics)[source]
Constructs a new instance.
- Type parameters:
<T> – the type of the object
- Parameters:
object – the Object to build a
toStringforstyle – the style of the
toStringto create, may benullbuffer – the
StringBufferto populate, may benullreflectUpToClass – the superclass to reflect up to (inclusive), may be
nulloutputTransients – whether to include transient fields
outputStatics – whether to include static fields
- Since:
2.1
- public <T> ReflectionToStringBuilder(T object, ToStringStyle style, StringBuffer buffer, Class<? super T> reflectUpToClass, boolean outputTransients, boolean outputStatics, boolean excludeNullValues)[source]
Constructs a new instance.
- Type parameters:
<T> – the type of the object
- Parameters:
object – the Object to build a
toStringforstyle – the style of the
toStringto create, may benullbuffer – the
StringBufferto populate, may benullreflectUpToClass – the superclass to reflect up to (inclusive), may be
nulloutputTransients – whether to include transient fields
outputStatics – whether to include static fields
excludeNullValues – whether to exclude fields who value is null
- Since:
3.6
Fields and constants
Which field names to exclude from output. |
|
Field names that will be included in the output. |
Methods
Returns whether or not to append the given |
|
Appends the fields and values defined by the given object of the given Class. |
|
Gets the excludeFieldNames. |
|
Gets the includeFieldNames |
|
Gets the last super class to stop appending fields for. |
|
Gets the field value using |
|
Tests whether or not to append static fields. |
|
Tests whether or not to append transient fields. |
|
Tests whether or not to append fields whose values are null. |
|
Appends to the |
|
Sets whether or not to append static fields. |
|
Sets whether or not to append transient fields. |
|
Sets the field names to exclude. |
|
Sets whether or not to append fields whose values are null. |
|
Sets the field names to include. |
|
Sets the last super class to stop appending fields for. |
|
Gets the String built by this builder. |
|
Builds a |
|
Builds a |
|
Builds a |
|
Builds a |
|
|
Builds a |
Builds a |
|
Builds a String for a toString method excluding the given field names. |
|
Builds a String for a toString method excluding the given field names. |
|
Builds a String for a toString method including the given field names. |
|
Builds a String for a toString method including the given field names. |
- protected boolean accept(Field field)[source]
Returns whether or not to append the given
Field.Transient fields are appended only if
#isAppendTransients()returnstrue.Static fields are appended only if
#isAppendStatics()returnstrue.Inner class fields are not appended.
- Parameters:
field – The Field to test.
- Returns:
Whether or not to append the given
Field.
- protected void appendFieldsIn(Class<?> clazz)[source]
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
Object.toString()had been called and not implemented by the object.- Parameters:
clazz – The class of object parameter
- public String[] getExcludeFieldNames()[source]
Gets the excludeFieldNames.
- Returns:
The excludeFieldNames.
- public String[] getIncludeFieldNames()[source]
Gets the includeFieldNames
- Returns:
The includeFieldNames.
- Since:
3.13.0
- public Class<?> getUpToClass()[source]
Gets the last super class to stop appending fields for.
- Returns:
The last super class to stop appending fields for.
- protected Object getValue(Field field) throws IllegalAccessException[source]
Gets the field value using
java.lang.reflect.Field.get(Object).- Parameters:
field – The Field to query.
- Returns:
The Object from the given Field.
- Throws:
java.lang.IllegalArgumentException – Thrown as described in
java.lang.reflect.Field#get(Object).java.lang.IllegalAccessException – Thrown as described in
java.lang.reflect.Field#get(Object).
- See also:
- public boolean isAppendStatics()[source]
Tests whether or not to append static fields.
- Returns:
Whether or not to append static fields.
- Since:
2.1
- public boolean isAppendTransients()[source]
Tests whether or not to append transient fields.
- Returns:
Whether or not to append transient fields.
- public boolean isExcludeNullValues()[source]
Tests whether or not to append fields whose values are null.
- Returns:
Whether or not to append fields whose values are null.
- Since:
3.6
- public ReflectionToStringBuilder reflectionAppendArray(Object array)[source]
Appends to the
toStringanObjectarray.- Parameters:
array – the array to add to the
toString- Returns:
thisinstance.
- public void setAppendStatics(boolean appendStatics)[source]
Sets whether or not to append static fields.
- Parameters:
appendStatics – Whether or not to append static fields.
- Since:
2.1
- public void setAppendTransients(boolean appendTransients)[source]
Sets whether or not to append transient fields.
- Parameters:
appendTransients – Whether or not to append transient fields.
- public ReflectionToStringBuilder setExcludeFieldNames(String... excludeFieldNamesParam)[source]
Sets the field names to exclude.
- Parameters:
excludeFieldNamesParam – The excludeFieldNames to excluding from toString or
null.- Returns:
this
- public void setExcludeNullValues(boolean excludeNullValues)[source]
Sets whether or not to append fields whose values are null.
- Parameters:
excludeNullValues – Whether or not to append fields whose values are null.
- Since:
3.6
- public ReflectionToStringBuilder setIncludeFieldNames(String... includeFieldNamesParam)[source]
Sets the field names to include.
nullor empty means all fields are included. All fields are included by default. This method will override the default behavior.- Parameters:
includeFieldNamesParam – The includeFieldNames that must be on toString or
null.- Returns:
this- Since:
3.13.0
- public void setUpToClass(Class<?> clazz)[source]
Sets the last super class to stop appending fields for.
- Parameters:
clazz – The last super class to stop appending fields for.
- @Override public String toString()[source]
Gets the String built by this builder.
- Returns:
The built string
- public static String toString(Object object)[source]
Builds a
toStringvalue using the defaultToStringStylethrough reflection.It uses
AccessibleObject.setAccessibleto gain access to private fields. This means that it will throw a security exception if run under a security manager, if the permissions are not set up correctly. It is also not as efficient as testing explicitly.Transient members will be not be included, as they are likely derived. Static fields will not be included. Superclass fields will be appended.
- Parameters:
object – the Object to be output
- Returns:
The String result
- Throws:
java.lang.IllegalArgumentException – Thrown if the Object is
null.- See also:
- public static String toString(Object object, ToStringStyle style)[source]
Builds a
toStringvalue through reflection.It uses
AccessibleObject.setAccessibleto gain access to private fields. This means that it will throw a security exception if run under a security manager, if the permissions are not set up correctly. It is also not as efficient as testing explicitly.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
null, the defaultToStringStyleis used.- Parameters:
object – the Object to be output
style – the style of the
toStringto create, may benull
- Returns:
The String result
- Throws:
java.lang.IllegalArgumentException – Thrown if the Object or
ToStringStyleisnull.- See also:
- public static String toString(Object object, ToStringStyle style, boolean outputTransients)[source]
Builds a
toStringvalue through reflection.It uses
AccessibleObject.setAccessibleto gain access to private fields. This means that it will throw a security exception if run under a security manager, if the permissions are not set up correctly. It is also not as efficient as testing explicitly.If the
outputTransientsistrue, transient members will be output, otherwise they are ignored, as they are likely derived fields, and not part of the value of the Object.Static fields will not be included. Superclass fields will be appended.
If the style is
null, the defaultToStringStyleis used.- 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
- Throws:
java.lang.IllegalArgumentException – Thrown if the Object is
null.- See also:
- public static String toString(Object object, ToStringStyle style, boolean outputTransients, boolean outputStatics)[source]
Builds a
toStringvalue through reflection.It uses
AccessibleObject.setAccessibleto gain access to private fields. This means that it will throw a security exception if run under a security manager, if the permissions are not set up correctly. It is also not as efficient as testing explicitly.If the
outputTransientsistrue, transient fields will be output, otherwise they are ignored, as they are likely derived fields, and not part of the value of the Object.If the
outputStaticsistrue, static fields will be output, otherwise they are ignored.Static fields will not be included. Superclass fields will be appended.
If the style is
null, the defaultToStringStyleis used.- Parameters:
object – the Object to be output
style – the style of the
toStringto create, may benulloutputTransients – whether to include transient fields
outputStatics – whether to include static fields
- Returns:
The String result
- Throws:
java.lang.IllegalArgumentException – Thrown if the Object is
null.- See also:
- Since:
2.1
- public static <T> String toString(T object, ToStringStyle style, boolean outputTransients, boolean outputStatics, boolean excludeNullValues, Class<? super T> reflectUpToClass)[source]
Builds a
toStringvalue through reflection.It uses
AccessibleObject.setAccessibleto gain access to private fields. This means that it will throw a security exception if run under a security manager, if the permissions are not set up correctly. It is also not as efficient as testing explicitly.If the
outputTransientsistrue, transient fields will be output, otherwise they are ignored, as they are likely derived fields, and not part of the value of the Object.If the
outputStaticsistrue, static fields will be output, otherwise they are ignored.Superclass fields will be appended up to and including the specified superclass. A null superclass is treated as
Object.If the style is
null, the defaultToStringStyleis used.- 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
outputStatics – whether to include static fields
excludeNullValues – whether to exclude fields whose values are null
reflectUpToClass – the superclass to reflect up to (inclusive), may be
null
- Returns:
The String result
- Throws:
java.lang.IllegalArgumentException – Thrown if the Object is
null.- See also:
- Since:
3.6
- public static <T> String toString(T object, ToStringStyle style, boolean outputTransients, boolean outputStatics, Class<? super T> reflectUpToClass)[source]
Builds a
toStringvalue through reflection.It uses
AccessibleObject.setAccessibleto gain access to private fields. This means that it will throw a security exception if run under a security manager, if the permissions are not set up correctly. It is also not as efficient as testing explicitly.If the
outputTransientsistrue, transient fields will be output, otherwise they are ignored, as they are likely derived fields, and not part of the value of the Object.If the
outputStaticsistrue, static fields will be output, otherwise they are ignored.Superclass fields will be appended up to and including the specified superclass. A null superclass is treated as
Object.If the style is
null, the defaultToStringStyleis used.- 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
outputStatics – whether to include static fields
reflectUpToClass – the superclass to reflect up to (inclusive), may be
null
- Returns:
The String result
- Throws:
java.lang.IllegalArgumentException – Thrown if the Object is
null.- See also:
- Since:
2.1
- public static String toStringExclude(Object object, String... excludeFieldNames)[source]
Builds a String for a toString method excluding the given field names.
- Parameters:
object – The object to “toString”.
excludeFieldNames – The field names to exclude
- Returns:
The toString value.
- public static String toStringExclude(Object object, Collection<String> excludeFieldNames)[source]
Builds a String for a toString method excluding the given field names.
- Parameters:
object – The object to “toString”.
excludeFieldNames – The field names to exclude. Null excludes nothing.
- Returns:
The toString value.
- public static String toStringInclude(Object object, String... includeFieldNames)[source]
Builds a String for a toString method including the given field names.
- Parameters:
object – The object to “toString”.
includeFieldNames – The field names to include.
nullor empty means all fields are included. All fields are included by default. This method will override the default behavior.
- Returns:
The toString value.
- Since:
3.13.0
- public static String toStringInclude(Object object, Collection<String> includeFieldNames)[source]
Builds a String for a toString method including the given field names.
- Parameters:
object – The object to “toString”.
includeFieldNames –
nullor empty means all fields are included. All fields are included by default. This method will override the default behavior.
- Returns:
The toString value.
- Since:
3.13.0