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.identityToString(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 method gets a singleton default value, typically for the whole JVM.
Changing this default should generally only be done during application startup.
It is recommended to pass a ToStringStyle to the constructor instead
of using this global default.
This method can be used from multiple threads.
Internally, a volatile variable is used to provide the guarantee
that the latest value set using #setDefaultStyle is the value returned.
It is strongly recommended that the default style is only changed during application startup.
One reason for changing the default could be to have a verbose style during
development and a compact style 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;
}
Sets the default ToStringStyle to use.
This method sets a singleton default value, typically for the whole JVM.
Changing this default should generally only be done during application startup.
It is recommended to pass a ToStringStyle to the constructor instead
of changing this global default.
This method is not intended for use from multiple threads.
Internally, a volatile variable is used to provide the guarantee
that the latest value set is the value returned from #getDefaultStyle .
|
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
|