|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.commons.lang.builder.ToStringBuilder
Assists in implementing Object.toString()
methods.
This class enables a good and consistent toString()
to be built for any
class or object. This class aims to simplify the process by:
To use this class write code as follows:
public class Person { String name; int age; boolean smoker; ... public String toString() { return new ToStringBuilder(this). append("name", name). append("age", age). append("smoker", smoker). toString(); } }
This will produce a toString of the format:
Person@7f54[name=Stephen,age=29,smoker=false]
To add the superclass toString
, use appendSuper(java.lang.String)
.
To append the toString
from an object that is delegated
to (or any other object), use appendToString(java.lang.String)
.
Alternatively, there is a method that uses reflection to determine
the fields to test. Because these fields are usually private, the method,
reflectionToString
, uses AccessibleObject.setAccessible
to
change the visibility of the fields. This will fail under a security manager,
unless the appropriate permissions are set up correctly. It is also
slower than testing explicitly.
A typical invocation for this method would look like:
public String toString() { return ToStringBuilder.reflectionToString(this); }
You can also use the builder to debug 3rd party objects:
System.out.println("An object: " + ToStringBuilder.reflectionToString(anObject));
The exact format of the toString
is determined by
the ToStringStyle
passed into the constructor.
Constructor Summary | |
ToStringBuilder(java.lang.Object object)
Constructor for ToStringBuilder . |
|
ToStringBuilder(java.lang.Object object,
ToStringStyle style)
Constructor for ToStringBuilder specifying the
output style. |
|
ToStringBuilder(java.lang.Object object,
ToStringStyle style,
java.lang.StringBuffer buffer)
Constructor for ToStringBuilder . |
Method Summary | |
ToStringBuilder |
append(boolean value)
Append to the toString a boolean
value. |
ToStringBuilder |
append(boolean[] array)
Append to the toString a boolean
array. |
ToStringBuilder |
append(byte value)
Append to the toString a byte
value. |
ToStringBuilder |
append(byte[] array)
Append to the toString a byte
array. |
ToStringBuilder |
append(char value)
Append to the toString a char
value. |
ToStringBuilder |
append(char[] array)
Append to the toString a char
array. |
ToStringBuilder |
append(double value)
Append to the toString a double
value. |
ToStringBuilder |
append(double[] array)
Append to the toString a double
array. |
ToStringBuilder |
append(float value)
Append to the toString a float
value. |
ToStringBuilder |
append(float[] array)
Append to the toString a float
array. |
ToStringBuilder |
append(int value)
Append to the toString an int
value. |
ToStringBuilder |
append(int[] array)
Append to the toString an int
array. |
ToStringBuilder |
append(long value)
Append to the toString a long
value. |
ToStringBuilder |
append(long[] array)
Append to the toString a long
array. |
ToStringBuilder |
append(java.lang.Object obj)
Append to the toString an Object
value. |
ToStringBuilder |
append(java.lang.Object[] array)
Append to the toString an Object
array. |
ToStringBuilder |
append(short value)
Append to the toString a short
value. |
ToStringBuilder |
append(short[] array)
Append to the toString a short
array. |
ToStringBuilder |
append(java.lang.String fieldName,
boolean value)
Append to the toString a boolean
value. |
ToStringBuilder |
append(java.lang.String fieldName,
boolean[] array)
Append to the toString a boolean
array. |
ToStringBuilder |
append(java.lang.String fieldName,
boolean[] array,
boolean fullDetail)
Append to the toString a boolean
array. |
ToStringBuilder |
append(java.lang.String fieldName,
byte value)
Append to the toString an byte
value. |
ToStringBuilder |
append(java.lang.String fieldName,
byte[] array)
Append to the toString a byte array. |
ToStringBuilder |
append(java.lang.String fieldName,
byte[] array,
boolean fullDetail)
Append to the toString a byte
array. |
ToStringBuilder |
append(java.lang.String fieldName,
char value)
Append to the toString a char
value. |
ToStringBuilder |
append(java.lang.String fieldName,
char[] array)
Append to the toString a char
array. |
ToStringBuilder |
append(java.lang.String fieldName,
char[] array,
boolean fullDetail)
Append to the toString a char
array. |
ToStringBuilder |
append(java.lang.String fieldName,
double value)
Append to the toString a double
value. |
ToStringBuilder |
append(java.lang.String fieldName,
double[] array)
Append to the toString a double
array. |
ToStringBuilder |
append(java.lang.String fieldName,
double[] array,
boolean fullDetail)
Append to the toString a double
array. |
ToStringBuilder |
append(java.lang.String fieldName,
float value)
Append to the toString an float
value. |
ToStringBuilder |
append(java.lang.String fieldName,
float[] array)
Append to the toString a float
array. |
ToStringBuilder |
append(java.lang.String fieldName,
float[] array,
boolean fullDetail)
Append to the toString a float
array. |
ToStringBuilder |
append(java.lang.String fieldName,
int value)
Append to the toString an int
value. |
ToStringBuilder |
append(java.lang.String fieldName,
int[] array)
Append to the toString an int
array. |
ToStringBuilder |
append(java.lang.String fieldName,
int[] array,
boolean fullDetail)
Append to the toString an int
array. |
ToStringBuilder |
append(java.lang.String fieldName,
long value)
Append to the toString a long
value. |
ToStringBuilder |
append(java.lang.String fieldName,
long[] array)
Append to the toString a long
array. |
ToStringBuilder |
append(java.lang.String fieldName,
long[] array,
boolean fullDetail)
Append to the toString a long
array. |
ToStringBuilder |
append(java.lang.String fieldName,
java.lang.Object obj)
Append to the toString an Object
value. |
ToStringBuilder |
append(java.lang.String fieldName,
java.lang.Object[] array)
Append to the toString an Object
array. |
ToStringBuilder |
append(java.lang.String fieldName,
java.lang.Object[] array,
boolean fullDetail)
Append to the toString an Object
array. |
ToStringBuilder |
append(java.lang.String fieldName,
java.lang.Object obj,
boolean fullDetail)
Append to the toString an Object
value. |
ToStringBuilder |
append(java.lang.String fieldName,
short value)
Append to the toString an short
value. |
ToStringBuilder |
append(java.lang.String fieldName,
short[] array)
Append to the toString a short
array. |
ToStringBuilder |
append(java.lang.String fieldName,
short[] array,
boolean fullDetail)
Append to the toString a short
array. |
ToStringBuilder |
appendAsObjectToString(java.lang.Object object)
Appends with the same format as the default Object toString()
method. |
ToStringBuilder |
appendSuper(java.lang.String superToString)
Append the toString from the superclass. |
ToStringBuilder |
appendToString(java.lang.String toString)
Append the toString from another object. |
static ToStringStyle |
getDefaultStyle()
Gets the default ToStringStyle to use. |
java.lang.Object |
getObject()
Returns the Object being output. |
java.lang.StringBuffer |
getStringBuffer()
Gets the StringBuffer being populated. |
ToStringStyle |
getStyle()
Gets the ToStringStyle being used. |
static java.lang.String |
reflectionToString(java.lang.Object object)
Forwards to ReflectionToStringBuilder . |
static java.lang.String |
reflectionToString(java.lang.Object object,
ToStringStyle style)
Forwards to ReflectionToStringBuilder . |
static java.lang.String |
reflectionToString(java.lang.Object object,
ToStringStyle style,
boolean outputTransients)
Forwards to ReflectionToStringBuilder . |
static java.lang.String |
reflectionToString(java.lang.Object object,
ToStringStyle style,
boolean outputTransients,
java.lang.Class reflectUpToClass)
Forwards to ReflectionToStringBuilder . |
static void |
setDefaultStyle(ToStringStyle style)
Sets the default ToStringStyle to use. |
java.lang.String |
toString()
Returns the built toString . |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
public ToStringBuilder(java.lang.Object object)
Constructor for ToStringBuilder
.
This constructor outputs using the default style set with
setDefaultStyle
.
object
- the Object to build a toString
for
java.lang.IllegalArgumentException
- if the Object passed in is
null
public ToStringBuilder(java.lang.Object object, ToStringStyle style)
Constructor for ToStringBuilder
specifying the
output style.
If the style is null
, the default style is used.
object
- the Object to build a toString
forstyle
- the style of the toString
to create,
may be null
java.lang.IllegalArgumentException
- if the Object passed in is
null
public ToStringBuilder(java.lang.Object object, ToStringStyle style, java.lang.StringBuffer buffer)
Constructor for ToStringBuilder
.
If the style is null
, the default style is used.
If the buffer is null
, a new one is created.
object
- the Object to build a toString
forstyle
- the style of the toString
to create,
may be null
buffer
- the StringBuffer
to populate, may be
null
Method Detail |
public static ToStringStyle getDefaultStyle()
Gets the default ToStringStyle
to use.
This could allow the ToStringStyle
to be
controlled for an entire application with one call.
This might be used to have a verbose
ToStringStyle
during development and a compact
ToStringStyle
in production.
ToStringStyle
public static java.lang.String reflectionToString(java.lang.Object object)
Forwards to ReflectionToStringBuilder
.
object
- the Object to be output
ReflectionToStringBuilder.toString(Object)
public static java.lang.String reflectionToString(java.lang.Object object, ToStringStyle style)
Forwards to ReflectionToStringBuilder
.
object
- the Object to be outputstyle
- the style of the toString
to create, may be null
ReflectionToStringBuilder.toString(Object,ToStringStyle)
public static java.lang.String reflectionToString(java.lang.Object object, ToStringStyle style, boolean outputTransients)
Forwards to ReflectionToStringBuilder
.
object
- the Object to be outputstyle
- the style of the toString
to create, may be null
outputTransients
- whether to include transient fields
ReflectionToStringBuilder.toString(Object,ToStringStyle,boolean)
public static java.lang.String reflectionToString(java.lang.Object object, ToStringStyle style, boolean outputTransients, java.lang.Class reflectUpToClass)
Forwards to ReflectionToStringBuilder
.
object
- the Object to be outputstyle
- the style of the toString
to create, may be null
outputTransients
- whether to include transient fieldsreflectUpToClass
- the superclass to reflect up to (inclusive), may be null
ReflectionToStringBuilder.toString(Object,ToStringStyle,boolean,boolean,Class)
public static void setDefaultStyle(ToStringStyle style)
Sets the default ToStringStyle
to use.
style
- the default ToStringStyle
java.lang.IllegalArgumentException
- if the style is null
public ToStringBuilder append(boolean value)
Append to the toString
a boolean
value.
value
- the value to add to the toString
public ToStringBuilder append(boolean[] array)
Append to the toString
a boolean
array.
array
- the array to add to the toString
public ToStringBuilder append(byte value)
Append to the toString
a byte
value.
value
- the value to add to the toString
public ToStringBuilder append(byte[] array)
Append to the toString
a byte
array.
array
- the array to add to the toString
public ToStringBuilder append(char value)
Append to the toString
a char
value.
value
- the value to add to the toString
public ToStringBuilder append(char[] array)
Append to the toString
a char
array.
array
- the array to add to the toString
public ToStringBuilder append(double value)
Append to the toString
a double
value.
value
- the value to add to the toString
public ToStringBuilder append(double[] array)
Append to the toString
a double
array.
array
- the array to add to the toString
public ToStringBuilder append(float value)
Append to the toString
a float
value.
value
- the value to add to the toString
public ToStringBuilder append(float[] array)
Append to the toString
a float
array.
array
- the array to add to the toString
public ToStringBuilder append(int value)
Append to the toString
an int
value.
value
- the value to add to the toString
public ToStringBuilder append(int[] array)
Append to the toString
an int
array.
array
- the array to add to the toString
public ToStringBuilder append(long value)
Append to the toString
a long
value.
value
- the value to add to the toString
public ToStringBuilder append(long[] array)
Append to the toString
a long
array.
array
- the array to add to the toString
public ToStringBuilder append(java.lang.Object obj)
Append to the toString
an Object
value.
obj
- the value to add to the toString
public ToStringBuilder append(java.lang.Object[] array)
Append to the toString
an Object
array.
array
- the array to add to the toString
public ToStringBuilder append(short value)
Append to the toString
a short
value.
value
- the value to add to the toString
public ToStringBuilder append(short[] array)
Append to the toString
a short
array.
array
- the array to add to the toString
public ToStringBuilder append(java.lang.String fieldName, boolean value)
Append to the toString
a boolean
value.
fieldName
- the field namevalue
- the value to add to the toString
public ToStringBuilder append(java.lang.String fieldName, boolean[] array)
Append to the toString
a boolean
array.
fieldName
- the field namearray
- the array to add to the hashCode
public ToStringBuilder append(java.lang.String fieldName, boolean[] array, boolean fullDetail)
Append to the toString
a boolean
array.
A boolean parameter controls the level of detail to show.
Setting true
will output the array in full. Setting
false
will output a summary, typically the size of
the array.
fieldName
- the field namearray
- the array to add to the toString
fullDetail
- true
for detail, false
for summary info
public ToStringBuilder append(java.lang.String fieldName, byte value)
Append to the toString
an byte
value.
fieldName
- the field namevalue
- the value to add to the toString
public ToStringBuilder append(java.lang.String fieldName, byte[] array)
Append to the toString
a byte
array.
fieldName
- the field namearray
- the array to add to the toString
public ToStringBuilder append(java.lang.String fieldName, byte[] array, boolean fullDetail)
Append to the toString
a byte
array.
A boolean parameter controls the level of detail to show.
Setting true
will output the array in full. Setting
false
will output a summary, typically the size of
the array.
fieldName
- the field namearray
- the array to add to the toString
fullDetail
- true
for detail, false
for summary info
public ToStringBuilder append(java.lang.String fieldName, char value)
Append to the toString
a char
value.
fieldName
- the field namevalue
- the value to add to the toString
public ToStringBuilder append(java.lang.String fieldName, char[] array)
Append to the toString
a char
array.
fieldName
- the field namearray
- the array to add to the toString
public ToStringBuilder append(java.lang.String fieldName, char[] array, boolean fullDetail)
Append to the toString
a char
array.
A boolean parameter controls the level of detail to show.
Setting true
will output the array in full. Setting
false
will output a summary, typically the size of
the array.
fieldName
- the field namearray
- the array to add to the toString
fullDetail
- true
for detail, false
for summary info
public ToStringBuilder append(java.lang.String fieldName, double value)
Append to the toString
a double
value.
fieldName
- the field namevalue
- the value to add to the toString
public ToStringBuilder append(java.lang.String fieldName, double[] array)
Append to the toString
a double
array.
fieldName
- the field namearray
- the array to add to the toString
public ToStringBuilder append(java.lang.String fieldName, double[] array, boolean fullDetail)
Append to the toString
a double
array.
A boolean parameter controls the level of detail to show.
Setting true
will output the array in full. Setting
false
will output a summary, typically the size of
the array.
fieldName
- the field namearray
- the array to add to the toString
fullDetail
- true
for detail, false
for summary info
public ToStringBuilder append(java.lang.String fieldName, float value)
Append to the toString
an float
value.
fieldName
- the field namevalue
- the value to add to the toString
public ToStringBuilder append(java.lang.String fieldName, float[] array)
Append to the toString
a float
array.
fieldName
- the field namearray
- the array to add to the toString
public ToStringBuilder append(java.lang.String fieldName, float[] array, boolean fullDetail)
Append to the toString
a float
array.
A boolean parameter controls the level of detail to show.
Setting true
will output the array in full. Setting
false
will output a summary, typically the size of
the array.
fieldName
- the field namearray
- the array to add to the toString
fullDetail
- true
for detail, false
for summary info
public ToStringBuilder append(java.lang.String fieldName, int value)
Append to the toString
an int
value.
fieldName
- the field namevalue
- the value to add to the toString
public ToStringBuilder append(java.lang.String fieldName, int[] array)
Append to the toString
an int
array.
fieldName
- the field namearray
- the array to add to the toString
public ToStringBuilder append(java.lang.String fieldName, int[] array, boolean fullDetail)
Append to the toString
an int
array.
A boolean parameter controls the level of detail to show.
Setting true
will output the array in full. Setting
false
will output a summary, typically the size of
the array.
fieldName
- the field namearray
- the array to add to the toString
fullDetail
- true
for detail, false
for summary info
public ToStringBuilder append(java.lang.String fieldName, long value)
Append to the toString
a long
value.
fieldName
- the field namevalue
- the value to add to the toString
public ToStringBuilder append(java.lang.String fieldName, long[] array)
Append to the toString
a long
array.
fieldName
- the field namearray
- the array to add to the toString
public ToStringBuilder append(java.lang.String fieldName, long[] array, boolean fullDetail)
Append to the toString
a long
array.
A boolean parameter controls the level of detail to show.
Setting true
will output the array in full. Setting
false
will output a summary, typically the size of
the array.
fieldName
- the field namearray
- the array to add to the toString
fullDetail
- true
for detail, false
for summary info
public ToStringBuilder append(java.lang.String fieldName, java.lang.Object obj)
Append to the toString
an Object
value.
fieldName
- the field nameobj
- the value to add to the toString
public ToStringBuilder append(java.lang.String fieldName, java.lang.Object obj, boolean fullDetail)
Append to the toString
an Object
value.
fieldName
- the field nameobj
- the value to add to the toString
fullDetail
- true
for detail,
false
for summary info
public ToStringBuilder append(java.lang.String fieldName, java.lang.Object[] array)
Append to the toString
an Object
array.
fieldName
- the field namearray
- the array to add to the toString
public ToStringBuilder append(java.lang.String fieldName, java.lang.Object[] array, boolean fullDetail)
Append to the toString
an Object
array.
A boolean parameter controls the level of detail to show.
Setting true
will output the array in full. Setting
false
will output a summary, typically the size of
the array.
fieldName
- the field namearray
- the array to add to the toString
fullDetail
- true
for detail, false
for summary info
public ToStringBuilder append(java.lang.String fieldName, short value)
Append to the toString
an short
value.
fieldName
- the field namevalue
- the value to add to the toString
public ToStringBuilder append(java.lang.String fieldName, short[] array)
Append to the toString
a short
array.
fieldName
- the field namearray
- the array to add to the toString
public ToStringBuilder append(java.lang.String fieldName, short[] array, boolean fullDetail)
Append to the toString
a short
array.
A boolean parameter controls the level of detail to show.
Setting true
will output the array in full. Setting
false
will output a summary, typically the size of
the array.
fieldName
- the field namearray
- the array to add to the toString
fullDetail
- true
for detail, false
for summary info
public ToStringBuilder appendAsObjectToString(java.lang.Object object)
Appends with the same format as the default Object toString()
method. Appends the class name followed by
System.identityHashCode(java.lang.Object)
.
object
- the Object
whose class name and id to output
public ToStringBuilder appendSuper(java.lang.String superToString)
Append the toString
from the superclass.
This method assumes that the superclass uses the same ToStringStyle
as this one.
If superToString
is null
, no change is made.
superToString
- the result of super.toString()
public ToStringBuilder appendToString(java.lang.String toString)
Append the toString
from another object.
This method is useful where a class delegates most of the implementation of
its properties to another class. You can then call toString()
on
the other class and pass the result into this method.
private AnotherObject delegate; private String fieldInThisClass; public String toString() { return new ToStringBuilder(this). appendToString(delegate.toString()). append(fieldInThisClass). toString(); }
This method assumes that the other object uses the same ToStringStyle
as this one.
If the toString
is null
, no change is made.
toString
- the result of toString()
on another object
public java.lang.Object getObject()
Returns the Object
being output.
public java.lang.StringBuffer getStringBuffer()
Gets the StringBuffer
being populated.
StringBuffer
being populatedpublic ToStringStyle getStyle()
Gets the ToStringStyle
being used.
ToStringStyle
being usedpublic java.lang.String toString()
Returns the built toString
.
This method appends the end of data indicator, and can only be called once.
Use getStringBuffer()
to get the current string state.
If the object is null
, return the style's nullText
toString
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |