| java.lang.Object org.apache.commons.lang.builder.ToStringBuilder
All known Subclasses: org.apache.commons.lang.builder.ReflectionToStringBuilder,
ToStringBuilder | public class ToStringBuilder (Code) | | 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:
- allowing field names
- handling all types consistently
- handling nulls consistently
- outputting arrays and multi-dimensional arrays
- enabling the detail level to be controlled for Objects and Collections
- handling class hierarchies
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
ToStringBuilder.appendSuper .
To append the toString from an object that is delegated
to (or any other object), use
ToStringBuilder.appendToString .
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.
author: Stephen Colebourne author: Gary Gregory author: Pete Gieser since: 1.0 version: $Id: ToStringBuilder.java 492354 2007-01-03 23:48:10Z scolebourne $ |
Method Summary | |
public ToStringBuilder | append(boolean value) | public ToStringBuilder | append(boolean[] array) | public ToStringBuilder | append(byte value) | public ToStringBuilder | append(byte[] array) | public ToStringBuilder | append(char value) | public ToStringBuilder | append(char[] array) | public ToStringBuilder | append(double value) | public ToStringBuilder | append(double[] array) | public ToStringBuilder | append(float value) | public ToStringBuilder | append(float[] array) | public ToStringBuilder | append(int value) | public ToStringBuilder | append(int[] array) | public ToStringBuilder | append(long value) | public ToStringBuilder | append(long[] array) | public ToStringBuilder | append(Object obj) | public ToStringBuilder | append(Object[] array) | public ToStringBuilder | append(short value) | public ToStringBuilder | append(short[] array) | public ToStringBuilder | append(String fieldName, boolean value) | public ToStringBuilder | append(String fieldName, boolean[] array) | public ToStringBuilder | append(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. | public ToStringBuilder | append(String fieldName, byte value) | public ToStringBuilder | append(String fieldName, byte[] array) | public ToStringBuilder | append(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. | public ToStringBuilder | append(String fieldName, char value) | public ToStringBuilder | append(String fieldName, char[] array) | public ToStringBuilder | append(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. | public ToStringBuilder | append(String fieldName, double value) | public ToStringBuilder | append(String fieldName, double[] array) | public ToStringBuilder | append(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. | public ToStringBuilder | append(String fieldName, float value) | public ToStringBuilder | append(String fieldName, float[] array) | public ToStringBuilder | append(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. | public ToStringBuilder | append(String fieldName, int value) | public ToStringBuilder | append(String fieldName, int[] array) | public ToStringBuilder | append(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. | public ToStringBuilder | append(String fieldName, long value) | public ToStringBuilder | append(String fieldName, long[] array) | public ToStringBuilder | append(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. | public ToStringBuilder | append(String fieldName, Object obj) | public ToStringBuilder | append(String fieldName, Object obj, boolean fullDetail) | public ToStringBuilder | append(String fieldName, Object[] array) | public ToStringBuilder | append(String fieldName, 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. | public ToStringBuilder | append(String fieldName, short value) | public ToStringBuilder | append(String fieldName, short[] array) | public ToStringBuilder | append(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. | public ToStringBuilder | appendAsObjectToString(Object object) Appends with the same format as the default Object toString()
method. | public ToStringBuilder | appendSuper(String superToString) | public ToStringBuilder | appendToString(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. | public static ToStringStyle | getDefaultStyle() | public Object | getObject() | public StringBuffer | getStringBuffer() | public ToStringStyle | getStyle() | public static String | reflectionToString(Object object) | public static String | reflectionToString(Object object, ToStringStyle style) | public static String | reflectionToString(Object object, ToStringStyle style, boolean outputTransients) | public static String | reflectionToString(Object object, ToStringStyle style, boolean outputTransients, Class reflectUpToClass) | public static void | setDefaultStyle(ToStringStyle style) | public String | toString() Returns the built toString .
This method appends the end of data indicator, and can only be called once. |
ToStringBuilder | public ToStringBuilder(Object object)(Code) | | Constructor for ToStringBuilder .
This constructor outputs using the default style set with
setDefaultStyle .
Parameters: object - the Object to build a toString for throws: IllegalArgumentException - if the Object passed in isnull |
ToStringBuilder | public ToStringBuilder(Object object, ToStringStyle style)(Code) | | Constructor for ToStringBuilder specifying the
output style.
If the style is null , the default style is used.
Parameters: object - the Object to build a toString for Parameters: style - the style of the toString to create,may be null throws: IllegalArgumentException - if the Object passed in isnull |
ToStringBuilder | public ToStringBuilder(Object object, ToStringStyle style, StringBuffer buffer)(Code) | | Constructor for ToStringBuilder .
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 a toString for Parameters: style - the style of the toString to create,may be null Parameters: buffer - the StringBuffer to populate, may benull |
append | public ToStringBuilder append(boolean value)(Code) | | Append to the toString a boolean
value.
Parameters: value - the value to add to the toString this |
append | public ToStringBuilder append(boolean[] array)(Code) | | Append to the toString a boolean
array.
Parameters: array - the array to add to the toString this |
append | public ToStringBuilder append(byte value)(Code) | | Append to the toString a byte
value.
Parameters: value - the value to add to the toString this |
append | public ToStringBuilder append(byte[] array)(Code) | | Append to the toString a byte
array.
Parameters: array - the array to add to the toString this |
append | public ToStringBuilder append(char value)(Code) | | Append to the toString a char
value.
Parameters: value - the value to add to the toString this |
append | public ToStringBuilder append(char[] array)(Code) | | Append to the toString a char
array.
Parameters: array - the array to add to the toString this |
append | public ToStringBuilder append(double value)(Code) | | Append to the toString a double
value.
Parameters: value - the value to add to the toString this |
append | public ToStringBuilder append(double[] array)(Code) | | Append to the toString a double
array.
Parameters: array - the array to add to the toString this |
append | public ToStringBuilder append(float value)(Code) | | Append to the toString a float
value.
Parameters: value - the value to add to the toString this |
append | public ToStringBuilder append(float[] array)(Code) | | Append to the toString a float
array.
Parameters: array - the array to add to the toString this |
append | public ToStringBuilder append(int value)(Code) | | Append to the toString an int
value.
Parameters: value - the value to add to the toString this |
append | public ToStringBuilder append(int[] array)(Code) | | Append to the toString an int
array.
Parameters: array - the array to add to the toString this |
append | public ToStringBuilder append(long value)(Code) | | Append to the toString a long
value.
Parameters: value - the value to add to the toString this |
append | public ToStringBuilder append(long[] array)(Code) | | Append to the toString a long
array.
Parameters: array - the array to add to the toString this |
append | public ToStringBuilder append(Object obj)(Code) | | Append to the toString an Object
value.
Parameters: obj - the value to add to the toString this |
append | public ToStringBuilder append(Object[] array)(Code) | | Append to the toString an Object
array.
Parameters: array - the array to add to the toString this |
append | public ToStringBuilder append(short value)(Code) | | Append to the toString a short
value.
Parameters: value - the value to add to the toString this |
append | public ToStringBuilder append(short[] array)(Code) | | Append to the toString a short
array.
Parameters: array - the array to add to the toString this |
append | public ToStringBuilder append(String fieldName, boolean value)(Code) | | Append to the toString a boolean
value.
Parameters: fieldName - the field name Parameters: value - the value to add to the toString this |
append | public ToStringBuilder append(String fieldName, boolean[] array)(Code) | | Append to the toString a boolean
array.
Parameters: fieldName - the field name Parameters: array - the array to add to the hashCode this |
append | public ToStringBuilder append(String fieldName, boolean[] array, boolean fullDetail)(Code) | | 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.
Parameters: fieldName - the field name Parameters: array - the array to add to the toString Parameters: fullDetail - true for detail, false for summary info this |
append | public ToStringBuilder append(String fieldName, byte value)(Code) | | Append to the toString an byte
value.
Parameters: fieldName - the field name Parameters: value - the value to add to the toString this |
append | public ToStringBuilder append(String fieldName, byte[] array)(Code) | | Append to the toString a byte array.
Parameters: fieldName - the field name Parameters: array - the array to add to the toString this |
append | public ToStringBuilder append(String fieldName, byte[] array, boolean fullDetail)(Code) | | 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.
Parameters: fieldName - the field name Parameters: array - the array to add to the toString Parameters: fullDetail - true for detail, false for summary info this |
append | public ToStringBuilder append(String fieldName, char value)(Code) | | Append to the toString a char
value.
Parameters: fieldName - the field name Parameters: value - the value to add to the toString this |
append | public ToStringBuilder append(String fieldName, char[] array)(Code) | | Append to the toString a char
array.
Parameters: fieldName - the field name Parameters: array - the array to add to the toString this |
append | public ToStringBuilder append(String fieldName, char[] array, boolean fullDetail)(Code) | | 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.
Parameters: fieldName - the field name Parameters: array - the array to add to the toString Parameters: fullDetail - true for detail, false for summary info this |
append | public ToStringBuilder append(String fieldName, double value)(Code) | | Append to the toString a double
value.
Parameters: fieldName - the field name Parameters: value - the value to add to the toString this |
append | public ToStringBuilder append(String fieldName, double[] array)(Code) | | Append to the toString a double
array.
Parameters: fieldName - the field name Parameters: array - the array to add to the toString this |
append | public ToStringBuilder append(String fieldName, double[] array, boolean fullDetail)(Code) | | 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.
Parameters: fieldName - the field name Parameters: array - the array to add to the toString Parameters: fullDetail - true for detail, false for summary info this |
append | public ToStringBuilder append(String fieldName, float value)(Code) | | Append to the toString an float
value.
Parameters: fieldName - the field name Parameters: value - the value to add to the toString this |
append | public ToStringBuilder append(String fieldName, float[] array)(Code) | | Append to the toString a float
array.
Parameters: fieldName - the field name Parameters: array - the array to add to the toString this |
append | public ToStringBuilder append(String fieldName, float[] array, boolean fullDetail)(Code) | | 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.
Parameters: fieldName - the field name Parameters: array - the array to add to the toString Parameters: fullDetail - true for detail, false for summary info this |
append | public ToStringBuilder append(String fieldName, int value)(Code) | | Append to the toString an int
value.
Parameters: fieldName - the field name Parameters: value - the value to add to the toString this |
append | public ToStringBuilder append(String fieldName, int[] array)(Code) | | Append to the toString an int
array.
Parameters: fieldName - the field name Parameters: array - the array to add to the toString this |
append | public ToStringBuilder append(String fieldName, int[] array, boolean fullDetail)(Code) | | 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.
Parameters: fieldName - the field name Parameters: array - the array to add to the toString Parameters: fullDetail - true for detail, false for summary info this |
append | public ToStringBuilder append(String fieldName, long value)(Code) | | Append to the toString a long
value.
Parameters: fieldName - the field name Parameters: value - the value to add to the toString this |
append | public ToStringBuilder append(String fieldName, long[] array)(Code) | | Append to the toString a long
array.
Parameters: fieldName - the field name Parameters: array - the array to add to the toString this |
append | public ToStringBuilder append(String fieldName, long[] array, boolean fullDetail)(Code) | | 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.
Parameters: fieldName - the field name Parameters: array - the array to add to the toString Parameters: fullDetail - true for detail, false for summary info this |
append | public ToStringBuilder append(String fieldName, Object obj)(Code) | | Append to the toString an Object
value.
Parameters: fieldName - the field name Parameters: obj - the value to add to the toString this |
append | public ToStringBuilder append(String fieldName, Object obj, boolean fullDetail)(Code) | | Append to the toString an Object
value.
Parameters: fieldName - the field name Parameters: obj - the value to add to the toString Parameters: fullDetail - true for detail,false for summary info this |
append | public ToStringBuilder append(String fieldName, Object[] array)(Code) | | Append to the toString an Object
array.
Parameters: fieldName - the field name Parameters: array - the array to add to the toString this |
append | public ToStringBuilder append(String fieldName, Object[] array, boolean fullDetail)(Code) | | 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.
Parameters: fieldName - the field name Parameters: array - the array to add to the toString Parameters: fullDetail - true for detail, false for summary info this |
append | public ToStringBuilder append(String fieldName, short value)(Code) | | Append to the toString an short
value.
Parameters: fieldName - the field name Parameters: value - the value to add to the toString this |
append | public ToStringBuilder append(String fieldName, short[] array)(Code) | | Append to the toString a short
array.
Parameters: fieldName - the field name Parameters: array - the array to add to the toString this |
append | public ToStringBuilder append(String fieldName, short[] array, boolean fullDetail)(Code) | | 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.
Parameters: fieldName - the field name Parameters: array - the array to add to the toString Parameters: fullDetail - true for detail, false for summary info this |
appendSuper | public ToStringBuilder appendSuper(String superToString)(Code) | | 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.
Parameters: superToString - the result of super.toString() this since: 2.0 |
appendToString | public ToStringBuilder appendToString(String toString)(Code) | | 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.
Parameters: toString - the result of toString() on another object this since: 2.0 |
getDefaultStyle | public static ToStringStyle getDefaultStyle()(Code) | | 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.
the default ToStringStyle |
getObject | public Object getObject()(Code) | | Returns the Object being output.
The object being output. since: 2.0 |
getStringBuffer | public StringBuffer getStringBuffer()(Code) | | Gets the StringBuffer being populated.
the StringBuffer being populated |
getStyle | public ToStringStyle getStyle()(Code) | | Gets the ToStringStyle being used.
the ToStringStyle being used since: 2.0 |
reflectionToString | public static String reflectionToString(Object object, ToStringStyle style, boolean outputTransients, Class reflectUpToClass)(Code) | | Forwards to ReflectionToStringBuilder .
Parameters: object - the Object to be output Parameters: style - the style of the toString to create, may be null Parameters: outputTransients - whether to include transient fields Parameters: reflectUpToClass - the superclass to reflect up to (inclusive), may be null the String result See Also: ReflectionToStringBuilder.toString(ObjectToStringStylebooleanbooleanClass) since: 2.0 |
setDefaultStyle | public static void setDefaultStyle(ToStringStyle style)(Code) | | Sets the default ToStringStyle to use.
Parameters: style - the default ToStringStyle throws: IllegalArgumentException - if the style is null |
toString | public String toString()(Code) | | Returns the built toString .
This method appends the end of data indicator, and can only be called once.
Use
ToStringBuilder.getStringBuffer to get the current string state.
If the object is null , return the style's nullText
the String toString |
|
|