-
- All Implemented Interfaces:
-
external.org.apache.commons.lang3.builder.Builder
public class ReflectionToStringBuilder extends ToStringBuilder
Assists in implementing toString methods using reflection.
This class uses reflection to determine the fields to append. Because these fields are usually private, the class uses setAccessible 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:
- accept
- getValue
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(); }The exact format of the
toStringis determined by the ToStringStyle passed into the constructor.
-
-
Field Summary
Fields Modifier and Type Field Description private booleanappendStaticsprivate booleanappendTransientsprotected Array<String>excludeFieldNamesprivate Class<out Object>upToClass
-
Constructor Summary
Constructors Constructor Description ReflectionToStringBuilder(Object object)Constructor. ReflectionToStringBuilder(Object object, ToStringStyle style)Constructor. ReflectionToStringBuilder(Object object, ToStringStyle style, StringBuffer buffer)Constructor. ReflectionToStringBuilder(T object, ToStringStyle style, StringBuffer buffer, Class<out Object> reflectUpToClass, boolean outputTransients, boolean outputStatics)Constructor.
-
Method Summary
Modifier and Type Method Description voidsetAppendStatics(boolean appendStatics)Sets whether or not to append static fields. voidsetAppendTransients(boolean appendTransients)Sets whether or not to append transient fields. Array<String>getExcludeFieldNames()ReflectionToStringBuildersetExcludeFieldNames(Array<String> excludeFieldNamesParam)Sets the field names to exclude. Class<out Object>getUpToClass()Gets the last super class to stop appending fields for. voidsetUpToClass(Class<out Object> clazz)Sets the last super class to stop appending fields for. static StringtoString(Object object)Builds a toStringvalue using the defaultToStringStylethrough reflection.static StringtoString(Object object, ToStringStyle style)Builds a toStringvalue through reflection.static StringtoString(Object object, ToStringStyle style, boolean outputTransients)Builds a toStringvalue through reflection.static StringtoString(Object object, ToStringStyle style, boolean outputTransients, boolean outputStatics)Builds a toStringvalue through reflection.static <T> StringtoString(T object, ToStringStyle style, boolean outputTransients, boolean outputStatics, Class<out Object> reflectUpToClass)Builds a toStringvalue through reflection.static StringtoStringExclude(Object object, Collection<String> excludeFieldNames)Builds a String for a toString method excluding the given field names. static StringtoStringExclude(Object object, Array<String> excludeFieldNames)Builds a String for a toString method excluding the given field names. booleanisAppendStatics()Gets whether or not to append static fields. booleanisAppendTransients()Gets whether or not to append transient fields. ReflectionToStringBuilderreflectionAppendArray(Object array)Append to the toStringanObjectarray.StringtoString()Gets the String built by this builder. -
Methods inherited from class external.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 -
Methods inherited from class external.org.apache.commons.lang3.builder.Builder
build -
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
-
Constructor Detail
-
ReflectionToStringBuilder
ReflectionToStringBuilder(Object object)
Constructor.- Parameters:
object- the Object to build atoStringfor, must not benull
-
ReflectionToStringBuilder
ReflectionToStringBuilder(Object object, ToStringStyle style)
Constructor.- Parameters:
object- the Object to build atoStringfor, must not benullstyle- the style of thetoStringto create, may benull
-
ReflectionToStringBuilder
ReflectionToStringBuilder(Object object, ToStringStyle style, StringBuffer buffer)
Constructor.- Parameters:
object- the Object to build atoStringforstyle- the style of thetoStringto create, may benullbuffer- theStringBufferto populate, may benull
-
ReflectionToStringBuilder
ReflectionToStringBuilder(T object, ToStringStyle style, StringBuffer buffer, Class<out Object> reflectUpToClass, boolean outputTransients, boolean outputStatics)
Constructor.- 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
-
-
Method Detail
-
setAppendStatics
void setAppendStatics(boolean appendStatics)
Sets whether or not to append static fields.
- Parameters:
appendStatics- Whether or not to append static fields.
-
setAppendTransients
void setAppendTransients(boolean appendTransients)
Sets whether or not to append transient fields.
- Parameters:
appendTransients- Whether or not to append transient fields.
-
getExcludeFieldNames
Array<String> getExcludeFieldNames()
-
setExcludeFieldNames
ReflectionToStringBuilder setExcludeFieldNames(Array<String> excludeFieldNamesParam)
Sets the field names to exclude.
- Parameters:
excludeFieldNamesParam- The excludeFieldNames to excluding from toString ornull.
-
getUpToClass
Class<out Object> getUpToClass()
Gets the last super class to stop appending fields for.
-
setUpToClass
void setUpToClass(Class<out Object> clazz)
Sets the last super class to stop appending fields for.
- Parameters:
clazz- The last super class to stop appending fields for.
-
toString
static String toString(Object object)
Builds a
toStringvalue using the defaultToStringStylethrough reflection.It uses
AccessibleObject.setAccessibleto gain access to private fields. This means that it willthrow a security exception if run under a security manager, if the permissions are not set up correctly. It isalso 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
-
toString
static String toString(Object object, ToStringStyle style)
Builds a
toStringvalue through reflection.It uses
AccessibleObject.setAccessibleto gain access to private fields. This means that it willthrow a security exception if run under a security manager, if the permissions are not set up correctly. It isalso 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
-
toString
static String toString(Object object, ToStringStyle style, boolean outputTransients)
Builds a
toStringvalue through reflection.It uses
AccessibleObject.setAccessibleto gain access to private fields. This means that it willthrow a security exception if run under a security manager, if the permissions are not set up correctly. It isalso not as efficient as testing explicitly.If the
outputTransientsistrue, transient members will be output, otherwise theyare 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
-
toString
static String toString(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 willthrow a security exception if run under a security manager, if the permissions are not set up correctly. It isalso not as efficient as testing explicitly.If the
outputTransientsistrue, transient fields will be output, otherwise theyare 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 areignored.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 transient fields
-
toString
static <T> String toString(T object, ToStringStyle style, boolean outputTransients, boolean outputStatics, Class<out Object> reflectUpToClass)
Builds a
toStringvalue through reflection.It uses
AccessibleObject.setAccessibleto gain access to private fields. This means that it willthrow a security exception if run under a security manager, if the permissions are not set up correctly. It isalso not as efficient as testing explicitly.If the
outputTransientsistrue, transient fields will be output, otherwise theyare 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 areignored.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.- 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
-
toStringExclude
static String toStringExclude(Object object, Collection<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.
-
toStringExclude
static String toStringExclude(Object object, Array<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
-
isAppendStatics
boolean isAppendStatics()
Gets whether or not to append static fields.
-
isAppendTransients
boolean isAppendTransients()
Gets whether or not to append transient fields.
-
reflectionAppendArray
ReflectionToStringBuilder reflectionAppendArray(Object array)
Append to the
toStringanObjectarray.- Parameters:
array- the array to add to thetoString
-
-
-
-