From ff010a98f11931f2a4a4fb4bf5393a62428868a5 Mon Sep 17 00:00:00 2001
From: "Gary D. Gregory"
* 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:
*
* You can also use the builder to debug 3rd party objects:
*
* A subclass can control field output by overriding the methods:
*
* public String toString() {
- * return ReflectionToStringBuilder.toString(this);
- * }
- *
- *
- *
+ * return ReflectionToStringBuilder.toString(this);
+ * }
+ *
*
- * System.out.println("An object: " + ReflectionToStringBuilder.toString(anObject));
- *
- *
- *
+ * System.out.println("An object: " + ReflectionToStringBuilder.toString(anObject));
+ *
*
@@ -68,26 +67,21 @@ import org.apache.commons.lang3.ClassUtils;
*
*
- * 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.
*