|
||||||||||
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
ToString
generation routine.
This class enables a good toString to be built for any class. This class aims to simplify the process by:
To use this class write code as follows:
public class Person { String name; int age; boolean isSmoker; ... 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]
Alternatively, there is a method that uses reflection to determine
the fields to test. Because these fields are usually private, the method,
reflectionToString
, uses Field.setAccessible
to change
the visibility of the fields. This will fail under a security manager,
unless the appropriate permissions are set. It is also slower than testing
explicitly.
A typical invocation for this method would look like:
public String toString() { return ToStringBuilder.reflectionToString(this); }
The exact format of the toString is determined by the ToStringStyle
passed into the constructor.
Field Summary | |
private StringBuffer |
buffer
Current toString buffer |
private static ToStringStyle |
defaultStyle
The default style of output to use |
private Object |
object
The object being output |
private ToStringStyle |
style
The style of output to use |
Constructor Summary | |
ToStringBuilder(Object object)
Constructor for ToStringBuilder. |
|
ToStringBuilder(Object object,
ToStringStyle style)
Constructor for ToStringBuilder specifying the output style. |
|
ToStringBuilder(Object object,
ToStringStyle style,
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 a 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(Object object)
Append to the toString an Object value. |
ToStringBuilder |
append(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(String fieldName,
boolean value)
Append to the toString a boolean value. |
ToStringBuilder |
append(String fieldName,
boolean[] array)
Append a hashCode for a boolean array. |
ToStringBuilder |
append(String fieldName,
boolean[] array,
boolean fullDetail)
Append to the toString a boolean array. |
ToStringBuilder |
append(String fieldName,
byte value)
Append to the toString a byte value. |
ToStringBuilder |
append(String fieldName,
byte[] array)
Append a hashCode for a byte array. |
ToStringBuilder |
append(String fieldName,
byte[] array,
boolean fullDetail)
Append to the toString a byte array. |
ToStringBuilder |
append(String fieldName,
char value)
Append to the toString a char value. |
ToStringBuilder |
append(String fieldName,
char[] array)
Append a hashCode for a char array. |
ToStringBuilder |
append(String fieldName,
char[] array,
boolean fullDetail)
Append to the toString a char array. |
ToStringBuilder |
append(String fieldName,
double value)
Append to the toString a double value. |
ToStringBuilder |
append(String fieldName,
double[] array)
Append a hashCode for a double array. |
ToStringBuilder |
append(String fieldName,
double[] array,
boolean fullDetail)
Append to the toString a double array. |
ToStringBuilder |
append(String fieldName,
float value)
Append to the toString a float value. |
ToStringBuilder |
append(String fieldName,
float[] array)
Append a hashCode for a float array. |
ToStringBuilder |
append(String fieldName,
float[] array,
boolean fullDetail)
Append to the toString a float array. |
ToStringBuilder |
append(String fieldName,
int value)
Append to the toString an int value. |
ToStringBuilder |
append(String fieldName,
int[] array)
Append a hashCode for an int array. |
ToStringBuilder |
append(String fieldName,
int[] array,
boolean fullDetail)
Append to the toString an int array. |
ToStringBuilder |
append(String fieldName,
long value)
Append to the toString a long value. |
ToStringBuilder |
append(String fieldName,
long[] array)
Append a hashCode for a long array. |
ToStringBuilder |
append(String fieldName,
long[] array,
boolean fullDetail)
Append to the toString a long array. |
ToStringBuilder |
append(String fieldName,
Object object)
Append to the toString an Object value. |
ToStringBuilder |
append(String fieldName,
Object[] array)
Append to the toString an Object array. |
ToStringBuilder |
append(String fieldName,
Object[] array,
boolean fullDetail)
Append to the toString an Object array. |
ToStringBuilder |
append(String fieldName,
Object object,
boolean fullDetail)
Append to the toString an Object value. |
ToStringBuilder |
append(String fieldName,
short value)
Append to the toString a short value. |
ToStringBuilder |
append(String fieldName,
short[] array)
Append a hashCode for a short array. |
ToStringBuilder |
append(String fieldName,
short[] array,
boolean fullDetail)
Append to the toString a short array. |
static ToStringStyle |
getDefaultStyle()
Gets the default style to use. |
StringBuffer |
getStringBuffer()
Gets the buffer being populated |
static String |
reflectionToString(Object object)
This method uses reflection to build a suitable toString using the default style. |
static String |
reflectionToString(Object object,
ToStringStyle style)
This method uses reflection to build a suitable toString. |
static String |
reflectionToString(Object object,
ToStringStyle style,
boolean outputTransients)
This method uses reflection to build a suitable toString. |
static void |
setDefaultStyle(ToStringStyle style)
Sets the default style to use. |
String |
toString()
Returns the built toString |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
private static ToStringStyle defaultStyle
private final StringBuffer buffer
private final ToStringStyle style
private final Object object
Constructor Detail |
public ToStringBuilder(Object object)
setDefaultStyle
.
object
- the object to build a toString for, must not be null
IllegalArgumentException
- if the object passed in is nullpublic ToStringBuilder(Object object, ToStringStyle style)
If the style is null, the default style is used.
object
- the object to build a toString for, must not be nullstyle
- the style of the toString to create, may be null
IllegalArgumentException
- if the object passed in is nullpublic ToStringBuilder(Object object, ToStringStyle style, StringBuffer buffer)
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 for, must not be nullstyle
- the style of the toString to create, may be nullbuffer
- the string buffer to populate, may be null
IllegalArgumentException
- if the object passed in is nullMethod Detail |
public static ToStringStyle getDefaultStyle()
This could allow the toString style to be controlled for an entire application with one call. This might be used to have a verbose toString during development and a compact toString in production.
public static void setDefaultStyle(ToStringStyle style)
style
- the default toString style
IllegalArgumentException
- if the style is nullpublic static String reflectionToString(Object object)
It uses Field.setAccessible to gain access to private fields. This means that it will throw a security exception if run under a security manger, if the permissions are not set up. It is also not as efficient as testing explicitly. Transient members will be not be included, as they are likely derived. Static fields will be not be included. fields, and not part of the value of the object.
object
- the object to be output
IllegalArgumentException
- if the object is nullpublic static String reflectionToString(Object object, ToStringStyle style)
It uses Field.setAccessible to gain access to private fields. This means that it will throw a security exception if run under a security manger, if the permissions are not set up. It is also not as efficient as testing explicitly. Transient members will be not be included, as they are likely derived. Static fields will be not be included. fields, and not part of the value of the object.
If the style is null, the default style is used.
object
- the object to be outputstyle
- the style of the toString to create, may be null
IllegalArgumentException
- if the object or style is nullpublic static String reflectionToString(Object object, ToStringStyle style, boolean outputTransients)
It uses Field.setAccessible to gain access to private fields. This means that it will throw a security exception if run under a security manger, if the permissions are not set up. It is also not as efficient as testing explicitly. If the outputTransients parameter is set to true, 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 tested.
If the style is null, the default style is used.
object
- the object to be outputstyle
- the style of the toString to create, may be nulloutputTransients
- whether to include transient fields
IllegalArgumentException
- if the object is nullpublic ToStringBuilder append(Object object)
object
- the value to add to the toString
public ToStringBuilder append(String fieldName, Object object)
object
- the value to add to the toStringfieldName
- the field name
public ToStringBuilder append(String fieldName, Object object, boolean fullDetail)
object
- the value to add to the toStringfieldName
- the field namefullDetail
- true for detail, false for summary info
public ToStringBuilder append(long value)
value
- the value to add to the toString
public ToStringBuilder append(String fieldName, long value)
value
- the value to add to the toStringfieldName
- the field name
public ToStringBuilder append(int value)
value
- the value to add to the toString
public ToStringBuilder append(String fieldName, int value)
value
- the value to add to the toStringfieldName
- the field name
public ToStringBuilder append(short value)
value
- the value to add to the toString
public ToStringBuilder append(String fieldName, short value)
value
- the value to add to the toStringfieldName
- the field name
public ToStringBuilder append(char value)
value
- the value to add to the toString
public ToStringBuilder append(String fieldName, char value)
value
- the value to add to the toStringfieldName
- the field name
public ToStringBuilder append(byte value)
value
- the value to add to the toString
public ToStringBuilder append(String fieldName, byte value)
value
- the value to add to the toStringfieldName
- the field name
public ToStringBuilder append(double value)
value
- the value to add to the toString
public ToStringBuilder append(String fieldName, double value)
value
- the value to add to the toStringfieldName
- the field name
public ToStringBuilder append(float value)
value
- the value to add to the toString
public ToStringBuilder append(String fieldName, float value)
value
- the value to add to the toStringfieldName
- the field name
public ToStringBuilder append(boolean value)
value
- the value to add to the toString
public ToStringBuilder append(String fieldName, boolean value)
value
- the value to add to the toStringfieldName
- the field name
public ToStringBuilder append(Object[] array)
array
- the array to add to the toString
public ToStringBuilder append(String fieldName, Object[] array)
fieldName
- the field namearray
- the array to add to the toString
public ToStringBuilder append(String fieldName, Object[] array, boolean fullDetail)
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 toStringfullDetail
- true for detail, false for summary info
public ToStringBuilder append(long[] array)
array
- the array to add to the toString
public ToStringBuilder append(String fieldName, long[] array)
fieldName
- the field namearray
- the array to add to the hashCode
public ToStringBuilder append(String fieldName, long[] array, boolean fullDetail)
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 toStringfullDetail
- true for detail, false for summary info
public ToStringBuilder append(int[] array)
array
- the array to add to the toString
public ToStringBuilder append(String fieldName, int[] array)
fieldName
- the field namearray
- the array to add to the hashCode
public ToStringBuilder append(String fieldName, int[] array, boolean fullDetail)
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 toStringfullDetail
- true for detail, false for summary info
public ToStringBuilder append(short[] array)
array
- the array to add to the toString
public ToStringBuilder append(String fieldName, short[] array)
fieldName
- the field namearray
- the array to add to the hashCode
public ToStringBuilder append(String fieldName, short[] array, boolean fullDetail)
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 toStringfullDetail
- true for detail, false for summary info
public ToStringBuilder append(char[] array)
array
- the array to add to the toString
public ToStringBuilder append(String fieldName, char[] array)
fieldName
- the field namearray
- the array to add to the hashCode
public ToStringBuilder append(String fieldName, char[] array, boolean fullDetail)
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 toStringfullDetail
- true for detail, false for summary info
public ToStringBuilder append(byte[] array)
array
- the array to add to the toString
public ToStringBuilder append(String fieldName, byte[] array)
fieldName
- the field namearray
- the array to add to the hashCode
public ToStringBuilder append(String fieldName, byte[] array, boolean fullDetail)
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 toStringfullDetail
- true for detail, false for summary info
public ToStringBuilder append(double[] array)
array
- the array to add to the toString
public ToStringBuilder append(String fieldName, double[] array)
fieldName
- the field namearray
- the array to add to the hashCode
public ToStringBuilder append(String fieldName, double[] array, boolean fullDetail)
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 toStringfullDetail
- true for detail, false for summary info
public ToStringBuilder append(float[] array)
array
- the array to add to the toString
public ToStringBuilder append(String fieldName, float[] array)
fieldName
- the field namearray
- the array to add to the hashCode
public ToStringBuilder append(String fieldName, float[] array, boolean fullDetail)
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 toStringfullDetail
- true for detail, false for summary info
public ToStringBuilder append(boolean[] array)
array
- the array to add to the toString
public ToStringBuilder append(String fieldName, boolean[] array)
fieldName
- the field namearray
- the array to add to the hashCode
public ToStringBuilder append(String fieldName, boolean[] array, boolean fullDetail)
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 toStringfullDetail
- true for detail, false for summary info
public StringBuffer getStringBuffer()
public String toString()
toString
in class Object
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |