diff --git a/src/java/org/apache/commons/lang/builder/ReflectionToStringBuilder.java b/src/java/org/apache/commons/lang/builder/ReflectionToStringBuilder.java index 17b10db08..da2aa1d0c 100644 --- a/src/java/org/apache/commons/lang/builder/ReflectionToStringBuilder.java +++ b/src/java/org/apache/commons/lang/builder/ReflectionToStringBuilder.java @@ -104,7 +104,7 @@ import org.apache.commons.lang.ClassUtils; * @author Stephen Colebourne * @author Pete Gieser * @since 2.0 - * @version $Id: ReflectionToStringBuilder.java,v 1.11 2003/09/07 14:32:34 psteitz Exp $ + * @version $Id: ReflectionToStringBuilder.java,v 1.12 2003/10/23 22:25:16 ggregory Exp $ */ public class ReflectionToStringBuilder extends ToStringBuilder { @@ -114,9 +114,9 @@ public class ReflectionToStringBuilder extends ToStringBuilder { */ private static ThreadLocal registry = new ThreadLocal() { protected synchronized Object initialValue() { - // The HashSet implementation is not synchronized, - // which is just what we need here. - return new HashSet(); + // The HashSet implementation is not synchronized, + // which is just what we need here. + return new HashSet(); } }; @@ -150,7 +150,7 @@ public class ReflectionToStringBuilder extends ToStringBuilder { static void register(Object value) { getRegistry().add(value); } - + /** *
This method uses reflection to build a suitable
* toString
using the default ToStringStyle
.
@@ -168,7 +168,7 @@ public class ReflectionToStringBuilder extends ToStringBuilder {
* @throws IllegalArgumentException if the Object is null
*/
public static String toString(Object object) {
- return toString(object, null, false, null);
+ return toString(object, null, false, false, null);
}
/**
@@ -194,7 +194,7 @@ public class ReflectionToStringBuilder extends ToStringBuilder {
* ToStringStyle
is null
*/
public static String toString(Object object, ToStringStyle style) {
- return toString(object, style, false, null);
+ return toString(object, style, false, false, null);
}
/**
@@ -224,7 +224,79 @@ public class ReflectionToStringBuilder extends ToStringBuilder {
* @throws IllegalArgumentException if the Object is null
*/
public static String toString(Object object, ToStringStyle style, boolean outputTransients) {
- return toString(object, style, outputTransients, null);
+ return toString(object, style, outputTransients, false, null);
+ }
+
+ /**
+ *
This method uses reflection to build a suitable
+ * toString
.
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.
toString
to create,
+ * may be null
+ * @param outputTransients whether to include transient fields
+ * @param outputStatics whether to include transient fields
+ * @return the String result
+ * @throws IllegalArgumentException if the Object is null
+ */
+ public static String toString(Object object, ToStringStyle style, boolean outputTransients, boolean outputStatics) {
+ return toString(object, style, outputTransients, outputStatics, null);
+ }
+
+ /**
+ * This method uses reflection to build a suitable
+ * toString
.
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.
toString
to create,
+ * may be null
+ * @param outputTransients whether to include transient fields
+ * @param outputStatics whether to include static fields
+ * @param reflectUpToClass the superclass to reflect up to (inclusive),
+ * may be null
+ * @return the String result
+ * @throws IllegalArgumentException if the Object is null
+ */
+ public static String toString(Object object, ToStringStyle style, boolean outputTransients, boolean outputStatics, Class reflectUpToClass) {
+ return new ReflectionToStringBuilder(object, style, null, reflectUpToClass, outputTransients, outputStatics).toString();
}
/**
@@ -247,6 +319,8 @@ public class ReflectionToStringBuilder extends ToStringBuilder {
*
* If the style is null
, the default
* ToStringStyle
is used.
toString
to create,
@@ -257,14 +331,97 @@ public class ReflectionToStringBuilder extends ToStringBuilder {
* @return the String result
* @throws IllegalArgumentException if the Object is null
*/
- public static String toString(
- Object object,
- ToStringStyle style,
- boolean outputTransients,
- Class reflectUpToClass) {
+ public static String toString(Object object, ToStringStyle style, boolean outputTransients, Class reflectUpToClass) {
return new ReflectionToStringBuilder(object, style, null, reflectUpToClass, outputTransients).toString();
}
+ /**
+ * TODO: Is this convenience API really needed?
+ *
+ * This method uses reflection to build a suitable
+ * toString
value which includes static fields.
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 fields are not output.
+ * + *Superclass fields will be appended up to and including
+ * java.lang.Object
.
The default ToStringStyle
is used.
null
+ * @return the String result
+ * @throws IllegalArgumentException if the Object is null
+ */
+ public static Object toStringWithStatics(Object object) {
+ return toString(object, null, false, true, null);
+ }
+
+ /**
+ * TODO: Is this convenience API really needed?
+ *
+ * This method uses reflection to build a suitable
+ * toString
value which includes static fields.
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 fields are not output.
+ * + *Superclass fields will be appended up to and including the specified superclass.
+ * A null superclass is treated as java.lang.Object
.
The default ToStringStyle
is used.
null
+ * @return the String result
+ * @throws IllegalArgumentException if the Object is null
+ */
+ public static Object toStringWithStatics(Object object, Class reflectUpToClass) {
+ return toString(object, null, false, true, reflectUpToClass);
+ }
+
+ /**
+ * TODO: Is this convenience API really needed?
+ *
+ * This method uses reflection to build a suitable
+ * toString
value which includes static fields.
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 fields are not output.
+ * + *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.
toString
to create,
+ * may be null
+ * @param reflectUpToClass the superclass to reflect up to (inclusive),
+ * may be null
+ * @return the String result
+ * @throws IllegalArgumentException if the Object is null
+ */
+ public static Object toStringWithStatics(Object object, ToStringStyle toStringStyle, Class reflectUpToClass) {
+ return toString(object, toStringStyle, false, true, reflectUpToClass);
+ }
+
/**
* Unregisters the given object.
* @@ -276,6 +433,11 @@ public class ReflectionToStringBuilder extends ToStringBuilder { getRegistry().remove(value); } + /** + * Whether or not to append static fields. + */ + private boolean appendStatics = false; + /** * Whether or not to append transient fields. */ @@ -340,6 +502,8 @@ public class ReflectionToStringBuilder extends ToStringBuilder { /** * Constructor. * + * @deprecated Use {@link #ReflectionToStringBuilder(Object,ToStringStyle,StringBuffer,Class,boolean,boolean)}. + * * @param object the Object to build atoString
for,
* must not be null
* @param style the style of the toString
to create,
@@ -361,21 +525,57 @@ public class ReflectionToStringBuilder extends ToStringBuilder {
this.setAppendTransients(outputTransients);
}
+ /**
+ * Constructor.
+ *
+ * @param object the Object to build a toString
for,
+ * must not be null
+ * @param style the style of the toString
to create,
+ * may be null
+ * @param buffer the StringBuffer
to populate, may be
+ * null
+ * @param reflectUpToClass the superclass to reflect up to (inclusive),
+ * may be null
+ * @param outputTransients whether to include transient fields
+ * @param outputStatics whether to include static 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);
+ }
+
/**
* Returns whether or not to append the given Field
.
* true
.
+ * true
.
* Field
.
*/
protected boolean accept(Field field) {
- String fieldName = field.getName();
- return (fieldName.indexOf(ClassUtils.INNER_CLASS_SEPARATOR_CHAR) == -1)
- && (this.isAppendTransients() || !Modifier.isTransient(field.getModifiers()))
- && (!Modifier.isStatic(field.getModifiers()));
+ if (field.getName().indexOf(ClassUtils.INNER_CLASS_SEPARATOR_CHAR) != -1) {
+ // Reject field from inner class.
+ return false;
+ }
+ if (Modifier.isTransient(field.getModifiers()) && !this.isAppendTransients()) {
+ // transients.
+ return false;
+ }
+ if (Modifier.isStatic(field.getModifiers()) && !this.isAppendStatics()) {
+ // transients.
+ return false;
+ }
+ return true;
}
/**
@@ -438,7 +638,7 @@ public class ReflectionToStringBuilder extends ToStringBuilder {
this.unregisterObject();
}
}
-
+
/**
* Gets the last super class to stop appending fields for.
* @@ -459,6 +659,15 @@ public class ReflectionToStringBuilder extends ToStringBuilder { return field.get(this.getObject()); } + /** + *Gets whether or not to append static fields.
+ * + * @return Whether or not to append static fields. + */ + public boolean isAppendStatics() { + return this.appendStatics; + } + /** *Gets whether or not to append transient fields.
* @@ -467,7 +676,7 @@ public class ReflectionToStringBuilder extends ToStringBuilder { public boolean isAppendTransients() { return this.appendTransients; } - + /** *Append to the toString
an Object
* array.
Sets whether or not to append static fields.
+ * + * @param appendStatics Whether or not to append static fields. + */ + public void setAppendStatics(boolean appendStatics) { + this.appendStatics = appendStatics; + } + /** *Sets whether or not to append transient fields.
* diff --git a/src/java/org/apache/commons/lang/builder/ToStringBuilder.java b/src/java/org/apache/commons/lang/builder/ToStringBuilder.java index 2d7de946e..d94a6e7f9 100644 --- a/src/java/org/apache/commons/lang/builder/ToStringBuilder.java +++ b/src/java/org/apache/commons/lang/builder/ToStringBuilder.java @@ -125,7 +125,7 @@ import org.apache.commons.lang.ObjectUtils; * @author Gary Gregory * @author Pete Gieser * @since 1.0 - * @version $Id: ToStringBuilder.java,v 1.29 2003/08/23 00:21:49 ggregory Exp $ + * @version $Id: ToStringBuilder.java,v 1.30 2003/10/23 22:25:16 ggregory Exp $ */ public class ToStringBuilder { @@ -176,7 +176,7 @@ public class ToStringBuilder { * @see ReflectionToStringBuilder#toString(Object,ToStringStyle,boolean) */ public static String reflectionToString(Object object, ToStringStyle style, boolean outputTransients) { - return ReflectionToStringBuilder.toString(object, style, outputTransients, null); + return ReflectionToStringBuilder.toString(object, style, outputTransients, false, null); } /** @@ -190,7 +190,7 @@ public class ToStringBuilder { ToStringStyle style, boolean outputTransients, Class reflectUpToClass) { - return ReflectionToStringBuilder.toString(object, style, outputTransients, reflectUpToClass); + return ReflectionToStringBuilder.toString(object, style, outputTransients, false, reflectUpToClass); } /** @@ -1035,6 +1035,16 @@ public class ToStringBuilder { return this; } + /** + *Returns the Object
being output.
Gets the StringBuffer
being populated.
Returns the Object
being output.