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.
| Method from org.apache.commons.lang.builder.ToStringBuilder Detail: |
public ToStringBuilder append(boolean value) {
style.append(buffer, null, value);
return this;
}
|
public ToStringBuilder append(boolean[] array) {
style.append(buffer, null, array, null);
return this;
}
|
public ToStringBuilder append(byte value) {
style.append(buffer, null, value);
return this;
}
|
public ToStringBuilder append(byte[] array) {
style.append(buffer, null, array, null);
return this;
}
|
public ToStringBuilder append(char value) {
style.append(buffer, null, value);
return this;
}
|
public ToStringBuilder append(char[] array) {
style.append(buffer, null, array, null);
return this;
}
|
public ToStringBuilder append(double value) {
style.append(buffer, null, value);
return this;
}
|
public ToStringBuilder append(double[] array) {
style.append(buffer, null, array, null);
return this;
}
|
public ToStringBuilder append(float value) {
style.append(buffer, null, value);
return this;
}
|
public ToStringBuilder append(float[] array) {
style.append(buffer, null, array, null);
return this;
}
|
public ToStringBuilder append(int value) {
style.append(buffer, null, value);
return this;
}
|
public ToStringBuilder append(int[] array) {
style.append(buffer, null, array, null);
return this;
}
|
public ToStringBuilder append(long value) {
style.append(buffer, null, value);
return this;
}
|
public ToStringBuilder append(long[] array) {
style.append(buffer, null, array, null);
return this;
}
|
public ToStringBuilder append(Object obj) {
style.append(buffer, null, obj, null);
return this;
}
|
public ToStringBuilder append(Object[] array) {
style.append(buffer, null, array, null);
return this;
}
|
public ToStringBuilder append(short value) {
style.append(buffer, null, value);
return this;
}
|
public ToStringBuilder append(short[] array) {
style.append(buffer, null, array, null);
return this;
}
|
public ToStringBuilder append(String fieldName,
boolean value) {
style.append(buffer, fieldName, value);
return this;
}
|
public ToStringBuilder append(String fieldName,
boolean[] array) {
style.append(buffer, fieldName, array, null);
return this;
}
|
public ToStringBuilder append(String fieldName,
byte value) {
style.append(buffer, fieldName, value);
return this;
}
|
public ToStringBuilder append(String fieldName,
byte[] array) {
style.append(buffer, fieldName, array, null);
return this;
}
|
public ToStringBuilder append(String fieldName,
char value) {
style.append(buffer, fieldName, value);
return this;
}
|
public ToStringBuilder append(String fieldName,
char[] array) {
style.append(buffer, fieldName, array, null);
return this;
}
|
public ToStringBuilder append(String fieldName,
double value) {
style.append(buffer, fieldName, value);
return this;
}
|
public ToStringBuilder append(String fieldName,
double[] array) {
style.append(buffer, fieldName, array, null);
return this;
}
|
public ToStringBuilder append(String fieldName,
float value) {
style.append(buffer, fieldName, value);
return this;
}
|
public ToStringBuilder append(String fieldName,
float[] array) {
style.append(buffer, fieldName, array, null);
return this;
}
|
public ToStringBuilder append(String fieldName,
int value) {
style.append(buffer, fieldName, value);
return this;
}
|
public ToStringBuilder append(String fieldName,
int[] array) {
style.append(buffer, fieldName, array, null);
return this;
}
|
public ToStringBuilder append(String fieldName,
long value) {
style.append(buffer, fieldName, value);
return this;
}
|
public ToStringBuilder append(String fieldName,
long[] array) {
style.append(buffer, fieldName, array, null);
return this;
}
|
public ToStringBuilder append(String fieldName,
Object obj) {
style.append(buffer, fieldName, obj, null);
return this;
}
|
public ToStringBuilder append(String fieldName,
Object[] array) {
style.append(buffer, fieldName, array, null);
return this;
}
|
public ToStringBuilder append(String fieldName,
short value) {
style.append(buffer, fieldName, value);
return this;
}
|
public ToStringBuilder append(String fieldName,
short[] array) {
style.append(buffer, fieldName, array, null);
return this;
}
|
public ToStringBuilder append(String fieldName,
boolean[] array,
boolean fullDetail) {
style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail));
return this;
}
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.
|
public ToStringBuilder append(String fieldName,
byte[] array,
boolean fullDetail) {
style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail));
return this;
}
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.
|
public ToStringBuilder append(String fieldName,
char[] array,
boolean fullDetail) {
style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail));
return this;
}
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.
|
public ToStringBuilder append(String fieldName,
double[] array,
boolean fullDetail) {
style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail));
return this;
}
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.
|
public ToStringBuilder append(String fieldName,
float[] array,
boolean fullDetail) {
style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail));
return this;
}
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.
|
public ToStringBuilder append(String fieldName,
int[] array,
boolean fullDetail) {
style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail));
return this;
}
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.
|
public ToStringBuilder append(String fieldName,
long[] array,
boolean fullDetail) {
style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail));
return this;
}
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.
|
public ToStringBuilder append(String fieldName,
Object obj,
boolean fullDetail) {
style.append(buffer, fieldName, obj, BooleanUtils.toBooleanObject(fullDetail));
return this;
}
|
public ToStringBuilder append(String fieldName,
Object[] array,
boolean fullDetail) {
style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail));
return this;
}
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.
|
public ToStringBuilder append(String fieldName,
short[] array,
boolean fullDetail) {
style.append(buffer, fieldName, array, BooleanUtils.toBooleanObject(fullDetail));
return this;
}
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.
|
public ToStringBuilder appendAsObjectToString(Object object) {
ObjectUtils.appendIdentityToString(this.getStringBuffer(), object);
return this;
}
|
public ToStringBuilder appendSuper(String superToString) {
if (superToString != null) {
style.appendSuper(buffer, superToString);
}
return this;
}
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.
|
public ToStringBuilder appendToString(String toString) {
if (toString != null) {
style.appendToString(buffer, toString);
}
return this;
}
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. |
public static ToStringStyle getDefaultStyle() {
//----------------------------------------------------------------------------
return defaultStyle;
}
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.
|
public Object getObject() {
return object;
}
|
public StringBuffer getStringBuffer() {
return buffer;
}
|
public ToStringStyle getStyle() {
return style;
}
|
public static String reflectionToString(Object object) {
return ReflectionToStringBuilder.toString(object);
}
|
public static String reflectionToString(Object object,
ToStringStyle style) {
return ReflectionToStringBuilder.toString(object, style);
}
|
public static String reflectionToString(Object object,
ToStringStyle style,
boolean outputTransients) {
return ReflectionToStringBuilder.toString(object, style, outputTransients, false, null);
}
|
public static String reflectionToString(Object object,
ToStringStyle style,
boolean outputTransients,
Class reflectUpToClass) {
return ReflectionToStringBuilder.toString(object, style, outputTransients, false, reflectUpToClass);
}
|
public static void setDefaultStyle(ToStringStyle style) {
if (style == null) {
throw new IllegalArgumentException("The style must not be null");
}
defaultStyle = style;
}
|
public String toString() {
if (this.getObject() == null) {
this.getStringBuffer().append(this.getStyle().getNullText());
} else {
style.appendEnd(this.getStringBuffer(), this.getObject());
}
return this.getStringBuffer().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
|