From ff010a98f11931f2a4a4fb4bf5393a62428868a5 Mon Sep 17 00:00:00 2001 From: "Gary D. Gregory" Date: Thu, 10 Nov 2011 06:14:33 +0000 Subject: [PATCH] [LANG-762] Handle or document ReflectionToStringBuilder and ToStringBuilder for collections that are not thread safe. Better Javadocs. See Phil's comments in the Jira. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1200177 13f79535-47bb-0310-9956-ffa450edef68 --- .../builder/ReflectionToStringBuilder.java | 42 ++++++++----------- 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/builder/ReflectionToStringBuilder.java b/src/main/java/org/apache/commons/lang3/builder/ReflectionToStringBuilder.java index 816206b6d..91759ef6b 100644 --- a/src/main/java/org/apache/commons/lang3/builder/ReflectionToStringBuilder.java +++ b/src/main/java/org/apache/commons/lang3/builder/ReflectionToStringBuilder.java @@ -32,34 +32,33 @@ import org.apache.commons.lang3.ClassUtils; *

* Assists in implementing {@link Object#toString()} methods using reflection. *

- * *

* This class uses reflection to determine the fields to append. Because these fields are usually private, the class * uses {@link java.lang.reflect.AccessibleObject#setAccessible(java.lang.reflect.AccessibleObject[], boolean)} to * change the visibility of the fields. This will fail under a security manager, unless the appropriate permissions are * set up correctly. *

- * + *

+ * Using reflection to access (private) fields circumvents any synchronization protection guarding access to these + * fields. If a toString method cannot safely read a field, you should exclude it from the toString method, or use + * synchronization consistent with the class' lock management around the invocation of the method. Take special care to + * exclude non-thread-safe collection classes, because these classes may throw ConcurrentModificationException if + * modified while the toString method is executing. + *

*

* A typical invocation for this method would look like: *

- * *
  * public String toString() {
- *   return ReflectionToStringBuilder.toString(this);
- * }
- * - * - * + * return ReflectionToStringBuilder.toString(this); + * } + * *

* You can also use the builder to debug 3rd party objects: *

- * *
- * System.out.println("An object: " + ReflectionToStringBuilder.toString(anObject));
- * - * - * + * System.out.println("An object: " + ReflectionToStringBuilder.toString(anObject)); + * *

* A subclass can control field output by overriding the methods: *

*

*

- * For example, this method does not include the password field in the returned - * String: + * For example, this method does not include the password field in the returned String: *

- * *
  * public String toString() {
  *     return (new ReflectionToStringBuilder(this) {
  *         protected boolean accept(Field f) {
- *             return super.accept(f) && !f.getName().equals("password");
+ *             return super.accept(f) && !f.getName().equals("password");
  *         }
  *     }).toString();
- * }
- * - * - * + * } + * *

- * The exact format of the toString is determined by the {@link ToStringStyle} passed into the - * constructor. + * The exact format of the toString is determined by the {@link ToStringStyle} passed into the constructor. *

- * + * * @since 2.0 * @version $Id$ */