Class ReflectionToStringBuilder
- java.lang.Object
-
- org.apache.commons.lang3.builder.ToStringBuilder
-
- org.apache.commons.lang3.builder.ReflectionToStringBuilder
-
- All Implemented Interfaces:
Builder<java.lang.String>
public class ReflectionToStringBuilder extends ToStringBuilder
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
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
-
-
Field Summary
Fields Modifier and Type Field Description protected java.lang.String[]excludeFieldNamesWhich field names to exclude from output.
-
Constructor Summary
Constructors Constructor Description ReflectionToStringBuilder(java.lang.Object object)Constructor.ReflectionToStringBuilder(java.lang.Object object, ToStringStyle style)Constructor.ReflectionToStringBuilder(java.lang.Object object, ToStringStyle style, java.lang.StringBuffer buffer)Constructor.ReflectionToStringBuilder(T object, ToStringStyle style, java.lang.StringBuffer buffer, java.lang.Class<? super T> reflectUpToClass, boolean outputTransients, boolean outputStatics)Constructor.ReflectionToStringBuilder(T object, ToStringStyle style, java.lang.StringBuffer buffer, java.lang.Class<? super T> reflectUpToClass, boolean outputTransients, boolean outputStatics, boolean excludeNullValues)Constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected booleanaccept(java.lang.reflect.Field field)Returns whether or not to append the givenField.protected voidappendFieldsIn(java.lang.Class<?> clazz)Appends the fields and values defined by the given object of the given Class.java.lang.String[]getExcludeFieldNames()java.lang.Class<?>getUpToClass()Gets the last super class to stop appending fields for.protected java.lang.ObjectgetValue(java.lang.reflect.Field field)Callsjava.lang.reflect.Field.get(Object).booleanisAppendStatics()Gets whether or not to append static fields.booleanisAppendTransients()Gets whether or not to append transient fields.booleanisExcludeNullValues()Gets whether or not to append fields whose values are null.ReflectionToStringBuilderreflectionAppendArray(java.lang.Object array)Append to thetoStringanObjectarray.voidsetAppendStatics(boolean appendStatics)Sets whether or not to append static fields.voidsetAppendTransients(boolean appendTransients)Sets whether or not to append transient fields.ReflectionToStringBuildersetExcludeFieldNames(java.lang.String... excludeFieldNamesParam)Sets the field names to exclude.voidsetExcludeNullValues(boolean excludeNullValues)Sets whether or not to append fields whose values are null.voidsetUpToClass(java.lang.Class<?> clazz)Sets the last super class to stop appending fields for.java.lang.StringtoString()Gets the String built by this builder.static java.lang.StringtoString(java.lang.Object object)Builds atoStringvalue using the defaultToStringStylethrough reflection.static java.lang.StringtoString(java.lang.Object object, ToStringStyle style)Builds atoStringvalue through reflection.static java.lang.StringtoString(java.lang.Object object, ToStringStyle style, boolean outputTransients)Builds atoStringvalue through reflection.static java.lang.StringtoString(java.lang.Object object, ToStringStyle style, boolean outputTransients, boolean outputStatics)Builds atoStringvalue through reflection.static <T> java.lang.StringtoString(T object, ToStringStyle style, boolean outputTransients, boolean outputStatics, boolean excludeNullValues, java.lang.Class<? super T> reflectUpToClass)Builds atoStringvalue through reflection.static <T> java.lang.StringtoString(T object, ToStringStyle style, boolean outputTransients, boolean outputStatics, java.lang.Class<? super T> reflectUpToClass)Builds atoStringvalue through reflection.static java.lang.StringtoStringExclude(java.lang.Object object, java.lang.String... excludeFieldNames)Builds a String for a toString method excluding the given field names.static java.lang.StringtoStringExclude(java.lang.Object object, java.util.Collection<java.lang.String> excludeFieldNames)Builds a String for a toString method excluding the given field names.-
Methods inherited from class org.apache.commons.lang3.builder.ToStringBuilder
append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, appendAsObjectToString, appendSuper, appendToString, build, getDefaultStyle, getObject, getStringBuffer, getStyle, reflectionToString, reflectionToString, reflectionToString, reflectionToString, setDefaultStyle
-
-
-
-
Constructor Detail
-
ReflectionToStringBuilder
public ReflectionToStringBuilder(java.lang.Object object)
Constructor.
This constructor outputs using the default style set with
setDefaultStyle.- Parameters:
object- the Object to build atoStringfor, must not benull- Throws:
java.lang.IllegalArgumentException- if the Object passed in isnull
-
ReflectionToStringBuilder
public ReflectionToStringBuilder(java.lang.Object object, ToStringStyle style)Constructor.
If the style is
null, the default style is used.- Parameters:
object- the Object to build atoStringfor, must not benullstyle- the style of thetoStringto create, may benull- Throws:
java.lang.IllegalArgumentException- if the Object passed in isnull
-
ReflectionToStringBuilder
public ReflectionToStringBuilder(java.lang.Object object, ToStringStyle style, java.lang.StringBuffer buffer)Constructor.
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 atoStringforstyle- the style of thetoStringto create, may benullbuffer- theStringBufferto populate, may benull- Throws:
java.lang.IllegalArgumentException- if the Object passed in isnull
-
ReflectionToStringBuilder
public ReflectionToStringBuilder(T object, ToStringStyle style, java.lang.StringBuffer buffer, java.lang.Class<? super T> reflectUpToClass, boolean outputTransients, boolean outputStatics)Constructor.- Type Parameters:
T- the type of the object- Parameters:
object- the Object to build atoStringforstyle- the style of thetoStringto create, may benullbuffer- theStringBufferto populate, may benullreflectUpToClass- the superclass to reflect up to (inclusive), may benulloutputTransients- whether to include transient fieldsoutputStatics- whether to include static fields- Since:
- 2.1
-
ReflectionToStringBuilder
public ReflectionToStringBuilder(T object, ToStringStyle style, java.lang.StringBuffer buffer, java.lang.Class<? super T> reflectUpToClass, boolean outputTransients, boolean outputStatics, boolean excludeNullValues)Constructor.- Type Parameters:
T- the type of the object- Parameters:
object- the Object to build atoStringforstyle- the style of thetoStringto create, may benullbuffer- theStringBufferto populate, may benullreflectUpToClass- the superclass to reflect up to (inclusive), may benulloutputTransients- whether to include transient fieldsoutputStatics- whether to include static fieldsexcludeNullValues- whether to exclude fields who value is null- Since:
- 3.6
-
-
Method Detail
-
toString
public static java.lang.String toString(java.lang.Object object)
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- if the Object isnull- See Also:
ToStringExclude,ToStringSummary
-
toString
public static java.lang.String toString(java.lang.Object object, ToStringStyle style)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 outputstyle- the style of thetoStringto create, may benull- Returns:
- the String result
- Throws:
java.lang.IllegalArgumentException- if the Object orToStringStyleisnull- See Also:
ToStringExclude,ToStringSummary
-
toString
public static java.lang.String toString(java.lang.Object object, ToStringStyle style, boolean outputTransients)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 outputstyle- the style of thetoStringto create, may benulloutputTransients- whether to include transient fields- Returns:
- the String result
- Throws:
java.lang.IllegalArgumentException- if the Object isnull- See Also:
ToStringExclude,ToStringSummary
-
toString
public static java.lang.String toString(java.lang.Object object, ToStringStyle style, boolean outputTransients, boolean outputStatics)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 outputstyle- the style of thetoStringto create, may benulloutputTransients- whether to include transient fieldsoutputStatics- whether to include static fields- Returns:
- the String result
- Throws:
java.lang.IllegalArgumentException- if the Object isnull- Since:
- 2.1
- See Also:
ToStringExclude,ToStringSummary
-
toString
public static <T> java.lang.String toString(T object, ToStringStyle style, boolean outputTransients, boolean outputStatics, java.lang.Class<? super T> reflectUpToClass)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
java.lang.Object.If the style is
null, the defaultToStringStyleis used.- 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 fieldsoutputStatics- whether to include static fieldsreflectUpToClass- the superclass to reflect up to (inclusive), may benull- Returns:
- the String result
- Throws:
java.lang.IllegalArgumentException- if the Object isnull- Since:
- 2.1
- See Also:
ToStringExclude,ToStringSummary
-
toString
public static <T> java.lang.String toString(T object, ToStringStyle style, boolean outputTransients, boolean outputStatics, boolean excludeNullValues, java.lang.Class<? super T> reflectUpToClass)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
java.lang.Object.If the style is
null, the defaultToStringStyleis used.- 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 fieldsoutputStatics- whether to include static fieldsexcludeNullValues- whether to exclude fields whose values are nullreflectUpToClass- the superclass to reflect up to (inclusive), may benull- Returns:
- the String result
- Throws:
java.lang.IllegalArgumentException- if the Object isnull- Since:
- 3.6
- See Also:
ToStringExclude,ToStringSummary
-
toStringExclude
public static java.lang.String toStringExclude(java.lang.Object object, java.util.Collection<java.lang.String> excludeFieldNames)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.
-
toStringExclude
public static java.lang.String toStringExclude(java.lang.Object object, java.lang.String... excludeFieldNames)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.
-
accept
protected boolean accept(java.lang.reflect.Field field)
Returns whether or not to append the givenField.- 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.
- Transient fields are appended only if
-
appendFieldsIn
protected void appendFieldsIn(java.lang.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
Object.toString()had been called and not implemented by the object.- Parameters:
clazz- The class of object parameter
-
getExcludeFieldNames
public java.lang.String[] getExcludeFieldNames()
- Returns:
- Returns the excludeFieldNames.
-
getUpToClass
public java.lang.Class<?> getUpToClass()
Gets the last super class to stop appending fields for.
- Returns:
- The last super class to stop appending fields for.
-
getValue
protected java.lang.Object getValue(java.lang.reflect.Field field) throws java.lang.IllegalAccessExceptionCalls
java.lang.reflect.Field.get(Object).- Parameters:
field- The Field to query.- Returns:
- The Object from the given Field.
- Throws:
java.lang.IllegalArgumentException- seeField.get(Object)java.lang.IllegalAccessException- seeField.get(Object)- See Also:
Field.get(Object)
-
isAppendStatics
public boolean isAppendStatics()
Gets whether or not to append static fields.
- Returns:
- Whether or not to append static fields.
- Since:
- 2.1
-
isAppendTransients
public boolean isAppendTransients()
Gets whether or not to append transient fields.
- Returns:
- Whether or not to append transient fields.
-
isExcludeNullValues
public boolean isExcludeNullValues()
Gets whether or not to append fields whose values are null.
- Returns:
- Whether or not to append fields whose values are null.
- Since:
- 3.6
-
reflectionAppendArray
public ReflectionToStringBuilder reflectionAppendArray(java.lang.Object array)
Append to the
toStringanObjectarray.- Parameters:
array- the array to add to thetoString- Returns:
- this
-
setAppendStatics
public void setAppendStatics(boolean appendStatics)
Sets whether or not to append static fields.
- Parameters:
appendStatics- Whether or not to append static fields.- Since:
- 2.1
-
setAppendTransients
public void setAppendTransients(boolean appendTransients)
Sets whether or not to append transient fields.
- Parameters:
appendTransients- Whether or not to append transient fields.
-
setExcludeNullValues
public void setExcludeNullValues(boolean excludeNullValues)
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
-
setExcludeFieldNames
public ReflectionToStringBuilder setExcludeFieldNames(java.lang.String... excludeFieldNamesParam)
Sets the field names to exclude.- Parameters:
excludeFieldNamesParam- The excludeFieldNames to excluding from toString ornull.- Returns:
this
-
setUpToClass
public void setUpToClass(java.lang.Class<?> clazz)
Sets the last super class to stop appending fields for.
- Parameters:
clazz- The last super class to stop appending fields for.
-
toString
public java.lang.String toString()
Gets the String built by this builder.
- Overrides:
toStringin classToStringBuilder- Returns:
- the built string
-
-