Class ReflectionToStringBuilder
- java.lang.Object
-
- org.apache.commons.lang.builder.ToStringBuilder
-
- org.apache.commons.lang.builder.ReflectionToStringBuilder
-
@Deprecated(since="2021-04-30") public class ReflectionToStringBuilder extends ToStringBuilder
Deprecated.Commons Lang 2 is in maintenance mode. Commons Lang 3 should be used instead.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.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 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
toString
is determined by theToStringStyle
passed into the constructor.- Since:
- 2.0
-
-
Constructor Summary
Constructors Constructor Description ReflectionToStringBuilder(java.lang.Object object)
Deprecated.Constructor.ReflectionToStringBuilder(java.lang.Object object, ToStringStyle style)
Deprecated.Constructor.ReflectionToStringBuilder(java.lang.Object object, ToStringStyle style, java.lang.StringBuffer buffer)
Deprecated.Constructor.ReflectionToStringBuilder(java.lang.Object object, ToStringStyle style, java.lang.StringBuffer buffer, java.lang.Class reflectUpToClass, boolean outputTransients)
ReflectionToStringBuilder(java.lang.Object object, ToStringStyle style, java.lang.StringBuffer buffer, java.lang.Class reflectUpToClass, boolean outputTransients, boolean outputStatics)
Deprecated.Constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description java.lang.String[]
getExcludeFieldNames()
Deprecated.java.lang.Class
getUpToClass()
Deprecated.Gets the last super class to stop appending fields for.boolean
isAppendStatics()
Deprecated.Gets whether or not to append static fields.boolean
isAppendTransients()
Deprecated.Gets whether or not to append transient fields.ToStringBuilder
reflectionAppendArray(java.lang.Object array)
Deprecated.Append to thetoString
anObject
array.void
setAppendStatics(boolean appendStatics)
Deprecated.Sets whether or not to append static fields.void
setAppendTransients(boolean appendTransients)
Deprecated.Sets whether or not to append transient fields.ReflectionToStringBuilder
setExcludeFieldNames(java.lang.String[] excludeFieldNamesParam)
Deprecated.Sets the field names to exclude.void
setUpToClass(java.lang.Class clazz)
Deprecated.Sets the last super class to stop appending fields for.java.lang.String
toString()
Deprecated.Gets the String built by this builder.static java.lang.String
toString(java.lang.Object object)
Deprecated.Builds atoString
value using the defaultToStringStyle
through reflection.static java.lang.String
toString(java.lang.Object object, ToStringStyle style)
Deprecated.Builds atoString
value through reflection.static java.lang.String
toString(java.lang.Object object, ToStringStyle style, boolean outputTransients)
Deprecated.Builds atoString
value through reflection.static java.lang.String
toString(java.lang.Object object, ToStringStyle style, boolean outputTransients, boolean outputStatics)
Deprecated.Builds atoString
value through reflection.static java.lang.String
toString(java.lang.Object object, ToStringStyle style, boolean outputTransients, boolean outputStatics, java.lang.Class reflectUpToClass)
Deprecated.Builds atoString
value through reflection.static java.lang.String
toString(java.lang.Object object, ToStringStyle style, boolean outputTransients, java.lang.Class reflectUpToClass)
Deprecated.static java.lang.String
toStringExclude(java.lang.Object object, java.lang.String excludeFieldName)
Deprecated.Builds a String for a toString method excluding the given field name.static java.lang.String
toStringExclude(java.lang.Object object, java.lang.String[] excludeFieldNames)
Deprecated.Builds a String for a toString method excluding the given field names.static java.lang.String
toStringExclude(java.lang.Object object, java.util.Collection excludeFieldNames)
Deprecated.Builds a String for a toString method excluding the given field names.-
Methods inherited from class org.apache.commons.lang.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, getDefaultStyle, getObject, getStringBuffer, getStyle, reflectionToString, reflectionToString, reflectionToString, reflectionToString, setDefaultStyle
-
-
-
-
Constructor Detail
-
ReflectionToStringBuilder
public ReflectionToStringBuilder(java.lang.Object object)
Deprecated.Constructor.
This constructor outputs using the default style set with
setDefaultStyle
.- Parameters:
object
- the Object to build atoString
for, must not benull
- Throws:
java.lang.IllegalArgumentException
- if the Object passed in isnull
-
ReflectionToStringBuilder
public ReflectionToStringBuilder(java.lang.Object object, ToStringStyle style)
Deprecated.Constructor.
If the style is
null
, the default style is used.- Parameters:
object
- the Object to build atoString
for, must not benull
style
- the style of thetoString
to 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)
Deprecated.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 atoString
forstyle
- the style of thetoString
to create, may benull
buffer
- theStringBuffer
to populate, 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, java.lang.Class reflectUpToClass, boolean outputTransients)
Constructor.- Parameters:
object
- the Object to build atoString
forstyle
- the style of thetoString
to create, may benull
buffer
- theStringBuffer
to populate, may benull
reflectUpToClass
- the superclass to reflect up to (inclusive), may benull
outputTransients
- whether to include transient fields
-
ReflectionToStringBuilder
public ReflectionToStringBuilder(java.lang.Object object, ToStringStyle style, java.lang.StringBuffer buffer, java.lang.Class reflectUpToClass, boolean outputTransients, boolean outputStatics)
Deprecated.Constructor.- Parameters:
object
- the Object to build atoString
forstyle
- the style of thetoString
to create, may benull
buffer
- theStringBuffer
to populate, may benull
reflectUpToClass
- the superclass to reflect up to (inclusive), may benull
outputTransients
- whether to include transient fieldsoutputStatics
- whether to include static fields- Since:
- 2.1
-
-
Method Detail
-
toString
public static java.lang.String toString(java.lang.Object object)
Deprecated.Builds a
toString
value using the defaultToStringStyle
through reflection.It uses
AccessibleObject.setAccessible
to 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
-
toString
public static java.lang.String toString(java.lang.Object object, ToStringStyle style)
Deprecated.Builds a
toString
value through reflection.It uses
AccessibleObject.setAccessible
to 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 defaultToStringStyle
is used.- Parameters:
object
- the Object to be outputstyle
- the style of thetoString
to create, may benull
- Returns:
- the String result
- Throws:
java.lang.IllegalArgumentException
- if the Object orToStringStyle
isnull
-
toString
public static java.lang.String toString(java.lang.Object object, ToStringStyle style, boolean outputTransients)
Deprecated.Builds a
toString
value through reflection.It uses
AccessibleObject.setAccessible
to 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
outputTransients
istrue
, 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 defaultToStringStyle
is used.- Parameters:
object
- the Object to be outputstyle
- the style of thetoString
to create, may benull
outputTransients
- whether to include transient fields- Returns:
- the String result
- Throws:
java.lang.IllegalArgumentException
- if the Object isnull
-
toString
public static java.lang.String toString(java.lang.Object object, ToStringStyle style, boolean outputTransients, boolean outputStatics)
Deprecated.Builds a
toString
value through reflection.It uses
AccessibleObject.setAccessible
to 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
outputTransients
istrue
, 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
outputStatics
istrue
, 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 defaultToStringStyle
is used.- Parameters:
object
- the Object to be outputstyle
- the style of thetoString
to create, may benull
outputTransients
- whether to include transient fieldsoutputStatics
- whether to include transient fields- Returns:
- the String result
- Throws:
java.lang.IllegalArgumentException
- if the Object isnull
- Since:
- 2.1
-
toString
public static java.lang.String toString(java.lang.Object object, ToStringStyle style, boolean outputTransients, boolean outputStatics, java.lang.Class reflectUpToClass)
Deprecated.Builds a
toString
value through reflection.It uses
AccessibleObject.setAccessible
to 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
outputTransients
istrue
, 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
outputStatics
istrue
, 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 defaultToStringStyle
is used.- Parameters:
object
- the Object to be outputstyle
- the style of thetoString
to create, may benull
outputTransients
- 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
-
toString
public static java.lang.String toString(java.lang.Object object, ToStringStyle style, boolean outputTransients, java.lang.Class reflectUpToClass)
Deprecated.Builds a
toString
value through reflection.It uses
AccessibleObject.setAccessible
to 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
outputTransients
istrue
, 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 up to and including the specified superclass. A null superclass is treated as
java.lang.Object
.If the style is
null
, the defaultToStringStyle
is used.- Parameters:
object
- the Object to be outputstyle
- the style of thetoString
to create, may benull
outputTransients
- whether to include transient fieldsreflectUpToClass
- the superclass to reflect up to (inclusive), may benull
- Returns:
- the String result
- Throws:
java.lang.IllegalArgumentException
- if the Object isnull
- Since:
- 2.0
-
toStringExclude
public static java.lang.String toStringExclude(java.lang.Object object, java.lang.String excludeFieldName)
Deprecated.Builds a String for a toString method excluding the given field name.- Parameters:
object
- The object to "toString".excludeFieldName
- The field name to exclude- Returns:
- The toString value.
-
toStringExclude
public static java.lang.String toStringExclude(java.lang.Object object, java.util.Collection excludeFieldNames)
Deprecated.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)
Deprecated.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.
-
getExcludeFieldNames
public java.lang.String[] getExcludeFieldNames()
Deprecated.- Returns:
- Returns the excludeFieldNames.
-
getUpToClass
public java.lang.Class getUpToClass()
Deprecated.Gets the last super class to stop appending fields for.
- Returns:
- The last super class to stop appending fields for.
-
isAppendStatics
public boolean isAppendStatics()
Deprecated.Gets whether or not to append static fields.
- Returns:
- Whether or not to append static fields.
- Since:
- 2.1
-
isAppendTransients
public boolean isAppendTransients()
Deprecated.Gets whether or not to append transient fields.
- Returns:
- Whether or not to append transient fields.
-
reflectionAppendArray
public ToStringBuilder reflectionAppendArray(java.lang.Object array)
Deprecated.Append to the
toString
anObject
array.- Parameters:
array
- the array to add to thetoString
- Returns:
- this
-
setAppendStatics
public void setAppendStatics(boolean appendStatics)
Deprecated.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)
Deprecated.Sets whether or not to append transient fields.
- Parameters:
appendTransients
- Whether or not to append transient fields.
-
setExcludeFieldNames
public ReflectionToStringBuilder setExcludeFieldNames(java.lang.String[] excludeFieldNamesParam)
Deprecated.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)
Deprecated.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()
Deprecated.Gets the String built by this builder.
- Overrides:
toString
in classToStringBuilder
- Returns:
- the built string
-
-