| Constructor: |
public ReflectionToStringBuilder(Object object) {
super(object);
}
Parameters:
object -
the Object to build a toString for, must not be null
Throws:
IllegalArgumentException -
if the Object passed in is null
|
public ReflectionToStringBuilder(Object object,
ToStringStyle style) {
super(object, style);
}
Constructor.
If the style is null, the default style is used.
Parameters:
object -
the Object to build a toString for, must not be null
style -
the style of the toString to create, may be null
Throws:
IllegalArgumentException -
if the Object passed in is null
|
public ReflectionToStringBuilder(Object object,
ToStringStyle style,
StringBuffer buffer) {
super(object, style, buffer);
}
Constructor.
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
style -
the style of the toString to create, may be null
buffer -
the StringBuffer to populate, may be null
Throws:
IllegalArgumentException -
if the Object passed in is null
|
public ReflectionToStringBuilder(Object object,
ToStringStyle style,
StringBuffer buffer,
Class reflectUpToClass,
boolean outputTransients) {
super(object, style, buffer);
this.setUpToClass(reflectUpToClass);
this.setAppendTransients(outputTransients);
}
Parameters:
object -
the Object to build a toString for
style -
the style of the toString to create, may be null
buffer -
the StringBuffer to populate, may be null
reflectUpToClass -
the superclass to reflect up to (inclusive), may be null
outputTransients -
whether to include transient fields
|
public ReflectionToStringBuilder(Object object,
ToStringStyle style,
StringBuffer buffer,
Class reflectUpToClass,
boolean outputTransients,
boolean outputStatics) {
super(object, style, buffer);
this.setUpToClass(reflectUpToClass);
this.setAppendTransients(outputTransients);
this.setAppendStatics(outputStatics);
}
Parameters:
object -
the Object to build a toString for
style -
the style of the toString to create, may be null
buffer -
the StringBuffer to populate, may be null
reflectUpToClass -
the superclass to reflect up to (inclusive), may be null
outputTransients -
whether to include transient fields
outputStatics -
whether to include static fields
- since:
2.1 -
|
| Method from org.apache.commons.lang.builder.ReflectionToStringBuilder Detail: |
protected boolean accept(Field field) {
if (field.getName().indexOf(ClassUtils.INNER_CLASS_SEPARATOR_CHAR) != -1) {
// Reject field from inner class.
return false;
}
if (Modifier.isTransient(field.getModifiers()) && !this.isAppendTransients()) {
// Reject transient fields.
return false;
}
if (Modifier.isStatic(field.getModifiers()) && !this.isAppendStatics()) {
// Rject static fields.
return false;
}
if (this.getExcludeFieldNames() != null
&& Arrays.binarySearch(this.getExcludeFieldNames(), field.getName()) >= 0) {
// Reject fields from the getExcludeFieldNames list.
return false;
}
return true;
}
Returns whether or not to append the given Field.
|
protected void appendFieldsIn(Class clazz) {
if (clazz.isArray()) {
this.reflectionAppendArray(this.getObject());
return;
}
Field[] fields = clazz.getDeclaredFields();
AccessibleObject.setAccessible(fields, true);
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
String fieldName = field.getName();
if (this.accept(field)) {
try {
// Warning: Field.get(Object) creates wrappers objects
// for primitive types.
Object fieldValue = this.getValue(field);
this.append(fieldName, fieldValue);
} catch (IllegalAccessException ex) {
//this can't happen. Would get a Security exception
// instead
//throw a runtime exception in case the impossible
// happens.
throw new InternalError("Unexpected IllegalAccessException: " + ex.getMessage());
}
}
}
}
Appends the fields and values defined by the given object of the given Class.
If a cycle is detected as an object is "toString()'ed", such an object is rendered as if
Object.toString() had been called and not implemented by the object.
|
public String[] getExcludeFieldNames() {
return this.excludeFieldNames;
}
|
public Class getUpToClass() {
return this.upToClass;
}
|
protected Object getValue(Field field) throws IllegalArgumentException, IllegalAccessException {
return field.get(this.getObject());
}
|
public boolean isAppendStatics() {
return this.appendStatics;
}
|
public boolean isAppendTransients() {
return this.appendTransients;
}
|
public ToStringBuilder reflectionAppendArray(Object array) {
this.getStyle().reflectionAppendArrayDetail(this.getStringBuffer(), null, array);
return this;
}
|
public void setAppendStatics(boolean appendStatics) {
this.appendStatics = appendStatics;
}
|
public void setAppendTransients(boolean appendTransients) {
this.appendTransients = appendTransients;
}
|
public ReflectionToStringBuilder setExcludeFieldNames(String[] excludeFieldNamesParam) {
if (excludeFieldNamesParam == null) {
this.excludeFieldNames = null;
} else {
this.excludeFieldNames = toNoNullStringArray(excludeFieldNamesParam);
Arrays.sort(this.excludeFieldNames);
}
return this;
}
Sets the field names to exclude. |
public void setUpToClass(Class clazz) {
this.upToClass = clazz;
}
|
static String[] toNoNullStringArray(Collection collection) {
if (collection == null) {
return ArrayUtils.EMPTY_STRING_ARRAY;
}
return toNoNullStringArray(collection.toArray());
}
|
static String[] toNoNullStringArray(Object[] array) {
ArrayList list = new ArrayList(array.length);
for (int i = 0; i < array.length; i++) {
Object e = array[i];
if (e != null) {
list.add(e.toString());
}
}
return (String[]) list.toArray(ArrayUtils.EMPTY_STRING_ARRAY);
}
Returns a new array of Strings without null elements. Internal method used to normalize exclude lists
(arrays and collections). Note that Arrays#sort(Object[]) will throw an NullPointerException
if an array element is null. |
public String toString() {
if (this.getObject() == null) {
return this.getStyle().getNullText();
}
Class clazz = this.getObject().getClass();
this.appendFieldsIn(clazz);
while (clazz.getSuperclass() != null && clazz != this.getUpToClass()) {
clazz = clazz.getSuperclass();
this.appendFieldsIn(clazz);
}
return super.toString();
}
|
public static String toString(Object object) {
return toString(object, null, false, false, null);
}
Builds a toString value using the default ToStringStyle through reflection.
It uses AccessibleObject.setAccessible to gain access to private fields. This means that it will
throw a security exception if run under a security manager, if the permissions are not set up correctly. It is
also not as efficient as testing explicitly.
Transient members will be not be included, as they are likely derived. Static fields will not be included.
Superclass fields will be appended.
|
public static String toString(Object object,
ToStringStyle style) {
return toString(object, style, false, false, null);
}
Builds a toString value through reflection.
It uses AccessibleObject.setAccessible to gain access to private fields. This means that it will
throw a security exception if run under a security manager, if the permissions are not set up correctly. It is
also not as efficient as testing explicitly.
Transient members will be not be included, as they are likely derived. Static fields will not be included.
Superclass fields will be appended.
If the style is null, the default ToStringStyle is used.
|
public static String toString(Object object,
ToStringStyle style,
boolean outputTransients) {
return toString(object, style, outputTransients, false, null);
}
Builds a toString value through reflection.
It uses AccessibleObject.setAccessible to gain access to private fields. This means that it will
throw a security exception if run under a security manager, if the permissions are not set up correctly. It is
also not as efficient as testing explicitly.
If the outputTransients is 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 included. Superclass fields will be appended.
If the style is null, the default ToStringStyle is used.
|
public static String toString(Object object,
ToStringStyle style,
boolean outputTransients,
boolean outputStatics) {
return toString(object, style, outputTransients, outputStatics, null);
}
Builds a toString value through reflection.
It uses AccessibleObject.setAccessible to gain access to private fields. This means that it will
throw a security exception if run under a security manager, if the permissions are not set up correctly. It is
also not as efficient as testing explicitly.
If the outputTransients is true, transient fields will be output, otherwise they
are ignored, as they are likely derived fields, and not part of the value of the Object.
If the outputStatics is true, static fields will be output, otherwise they are
ignored.
Static fields will not be included. Superclass fields will be appended.
If the style is null, the default ToStringStyle is used.
|
public static String toString(Object object,
ToStringStyle style,
boolean outputTransients,
Class reflectUpToClass) {
return new ReflectionToStringBuilder(object, style, null, reflectUpToClass, outputTransients).toString();
} Deprecated! Use - #toString(Object,ToStringStyle,boolean,boolean,Class)
Builds a toString value through reflection.
It uses AccessibleObject.setAccessible to gain access to private fields. This means that it will
throw a security exception if run under a security manager, if the permissions are not set up correctly. It is
also not as efficient as testing explicitly.
If the outputTransients is 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 included. Superclass fields will be appended up to and including the specified
superclass. A null superclass is treated as java.lang.Object.
If the style is null, the default ToStringStyle is used.
|
public static String toString(Object object,
ToStringStyle style,
boolean outputTransients,
boolean outputStatics,
Class reflectUpToClass) {
return new ReflectionToStringBuilder(object, style, null, reflectUpToClass, outputTransients, outputStatics)
.toString();
}
Builds a toString value through reflection.
It uses AccessibleObject.setAccessible to gain access to private fields. This means that it will
throw a security exception if run under a security manager, if the permissions are not set up correctly. It is
also not as efficient as testing explicitly.
If the outputTransients is true, transient fields will be output, otherwise they
are ignored, as they are likely derived fields, and not part of the value of the Object.
If the outputStatics is true, static fields will be output, otherwise they are
ignored.
Superclass fields will be appended up to and including the specified superclass. A null superclass is treated as
java.lang.Object.
If the style is null, the default ToStringStyle is used.
|
public static String toStringExclude(Object object,
String excludeFieldName) {
return toStringExclude(object, new String[]{excludeFieldName});
}
Builds a String for a toString method excluding the given field name. |
public static String toStringExclude(Object object,
Collection excludeFieldNames) {
return toStringExclude(object, toNoNullStringArray(excludeFieldNames));
}
Builds a String for a toString method excluding the given field names. |
public static String toStringExclude(Object object,
String[] excludeFieldNames) {
return new ReflectionToStringBuilder(object).setExcludeFieldNames(excludeFieldNames).toString();
}
Builds a String for a toString method excluding the given field names. |