A cut at http://issues.apache.org/bugzilla/show_bug.cgi?id=36925: Using ReflectionToStringBuilder and excluding secure fields.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@294949 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary D. Gregory 2005-10-05 00:05:11 +00:00
parent 0c46c2e50a
commit 0c36e28cd1
2 changed files with 205 additions and 183 deletions

View File

@ -13,11 +13,13 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.apache.commons.lang.builder; package org.apache.commons.lang.builder;
import java.lang.reflect.AccessibleObject; import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
@ -29,11 +31,10 @@
* </p> * </p>
* *
* <p> * <p>
* This class uses reflection to determine the fields to append. Because these * This class uses reflection to determine the fields to append. Because these fields are usually private, the class
* fields are usually private, the class uses * uses {@link java.lang.reflect.AccessibleObject#setAccessible(java.lang.reflect.AccessibleObject[], boolean)} to
* {@link java.lang.reflect.AccessibleObject#setAccessible(java.lang.reflect.AccessibleObject[], boolean)} * change the visibility of the fields. This will fail under a security manager, unless the appropriate permissions are
* to change the visibility of the fields. This will fail under a security * set up correctly.
* manager, unless the appropriate permissions are set up correctly.
* </p> * </p>
* *
* <p> * <p>
@ -64,8 +65,8 @@
* </ul> * </ul>
* </p> * </p>
* <p> * <p>
* For example, this method does <i>not</i> include the <code>password</code> * For example, this method does <i>not</i> include the <code>password</code> field in the returned
* field in the returned <code>String</code>: * <code>String</code>:
* </p> * </p>
* *
* <pre> * <pre>
@ -80,22 +81,21 @@
* *
* *
* <p> * <p>
* The exact format of the <code>toString</code> is determined by the * The exact format of the <code>toString</code> is determined by the {@link ToStringStyle} passed into the
* {@link ToStringStyle} passed into the constructor. * constructor.
* </p> * </p>
* *
* @author Gary Gregory * @author Gary Gregory
* @author Stephen Colebourne * @author Stephen Colebourne
* @author Pete Gieser * @author Pete Gieser
* @since 2.0 * @since 2.0
* @version $Id: ReflectionToStringBuilder.java,v 1.15 2003/12/02 19:11:58 * @version $Id$
* ggregory Exp $
*/ */
public class ReflectionToStringBuilder extends ToStringBuilder { public class ReflectionToStringBuilder extends ToStringBuilder {
/** /**
* <p> * <p>
* A registry of objects used by <code>reflectionToString</code> methods * A registry of objects used by <code>reflectionToString</code> methods to detect cyclical object references and
* to detect cyclical object references and avoid infinite loops. * avoid infinite loops.
* </p> * </p>
*/ */
private static ThreadLocal registry = new ThreadLocal() { private static ThreadLocal registry = new ThreadLocal() {
@ -108,8 +108,8 @@ protected synchronized Object initialValue() {
/** /**
* <p> * <p>
* Returns the registry of objects being traversed by the <code>reflectionToString</code> * Returns the registry of objects being traversed by the <code>reflectionToString</code> methods in the current
* methods in the current thread. * thread.
* </p> * </p>
* *
* @return Set the registry of objects being traversed * @return Set the registry of objects being traversed
@ -120,14 +120,13 @@ static Set getRegistry() {
/** /**
* <p> * <p>
* Returns <code>true</code> if the registry contains the given object. * Returns <code>true</code> if the registry contains the given object. Used by the reflection methods to avoid
* Used by the reflection methods to avoid infinite loops. * infinite loops.
* </p> * </p>
* *
* @param value * @param value
* The object to lookup in the registry. * The object to lookup in the registry.
* @return boolean <code>true</code> if the registry contains the given * @return boolean <code>true</code> if the registry contains the given object.
* object.
*/ */
static boolean isRegistered(Object value) { static boolean isRegistered(Object value) {
return getRegistry().contains(value); return getRegistry().contains(value);
@ -135,12 +134,11 @@ static boolean isRegistered(Object value) {
/** /**
* <p> * <p>
* Registers the given object. Used by the reflection methods to avoid * Registers the given object. Used by the reflection methods to avoid infinite loops.
* infinite loops.
* </p> * </p>
* *
* @param value * @param value
* The object to register. * The object to register.
*/ */
static void register(Object value) { static void register(Object value) {
getRegistry().add(value); getRegistry().add(value);
@ -152,22 +150,21 @@ static void register(Object value) {
* </p> * </p>
* *
* <p> * <p>
* It uses <code>AccessibleObject.setAccessible</code> to gain access to * It uses <code>AccessibleObject.setAccessible</code> to gain access to private fields. This means that it will
* private fields. This means that it will throw a security exception if * throw a security exception if run under a security manager, if the permissions are not set up correctly. It is
* run under a security manager, if the permissions are not set up * also not as efficient as testing explicitly.
* correctly. It is also not as efficient as testing explicitly.
* </p> * </p>
* *
* <p> * <p>
* Transient members will be not be included, as they are likely derived. * Transient members will be not be included, as they are likely derived. Static fields will not be included.
* Static fields will not be included. Superclass fields will be appended. * Superclass fields will be appended.
* </p> * </p>
* *
* @param object * @param object
* the Object to be output * the Object to be output
* @return the String result * @return the String result
* @throws IllegalArgumentException * @throws IllegalArgumentException
* if the Object is <code>null</code> * if the Object is <code>null</code>
*/ */
public static String toString(Object object) { public static String toString(Object object) {
return toString(object, null, false, false, null); return toString(object, null, false, false, null);
@ -179,30 +176,27 @@ public static String toString(Object object) {
* </p> * </p>
* *
* <p> * <p>
* It uses <code>AccessibleObject.setAccessible</code> to gain access to * It uses <code>AccessibleObject.setAccessible</code> to gain access to private fields. This means that it will
* private fields. This means that it will throw a security exception if * throw a security exception if run under a security manager, if the permissions are not set up correctly. It is
* run under a security manager, if the permissions are not set up * also not as efficient as testing explicitly.
* correctly. It is also not as efficient as testing explicitly.
* </p> * </p>
* *
* <p> * <p>
* Transient members will be not be included, as they are likely derived. * Transient members will be not be included, as they are likely derived. Static fields will not be included.
* Static fields will not be included. Superclass fields will be appended. * Superclass fields will be appended.
* </p> * </p>
* *
* <p> * <p>
* If the style is <code>null</code>, the default <code>ToStringStyle</code> * If the style is <code>null</code>, the default <code>ToStringStyle</code> is used.
* is used.
* </p> * </p>
* *
* @param object * @param object
* the Object to be output * the Object to be output
* @param style * @param style
* the style of the <code>toString</code> to create, may be * the style of the <code>toString</code> to create, may be <code>null</code>
* <code>null</code>
* @return the String result * @return the String result
* @throws IllegalArgumentException * @throws IllegalArgumentException
* if the Object or <code>ToStringStyle</code> is <code>null</code> * if the Object or <code>ToStringStyle</code> is <code>null</code>
*/ */
public static String toString(Object object, ToStringStyle style) { public static String toString(Object object, ToStringStyle style) {
return toString(object, style, false, false, null); return toString(object, style, false, false, null);
@ -214,16 +208,14 @@ public static String toString(Object object, ToStringStyle style) {
* </p> * </p>
* *
* <p> * <p>
* It uses <code>AccessibleObject.setAccessible</code> to gain access to * It uses <code>AccessibleObject.setAccessible</code> to gain access to private fields. This means that it will
* private fields. This means that it will throw a security exception if * throw a security exception if run under a security manager, if the permissions are not set up correctly. It is
* run under a security manager, if the permissions are not set up * also not as efficient as testing explicitly.
* correctly. It is also not as efficient as testing explicitly.
* </p> * </p>
* *
* <p> * <p>
* If the <code>outputTransients</code> is <code>true</code>, * If the <code>outputTransients</code> is <code>true</code>, transient members will be output, otherwise they
* transient members will be output, otherwise they are ignored, as they * are ignored, as they are likely derived fields, and not part of the value of the Object.
* are likely derived fields, and not part of the value of the Object.
* </p> * </p>
* *
* <p> * <p>
@ -231,20 +223,18 @@ public static String toString(Object object, ToStringStyle style) {
* </p> * </p>
* *
* <p> * <p>
* If the style is <code>null</code>, the default <code>ToStringStyle</code> * If the style is <code>null</code>, the default <code>ToStringStyle</code> is used.
* is used.
* </p> * </p>
* *
* @param object * @param object
* the Object to be output * the Object to be output
* @param style * @param style
* the style of the <code>toString</code> to create, may be * the style of the <code>toString</code> to create, may be <code>null</code>
* <code>null</code>
* @param outputTransients * @param outputTransients
* whether to include transient fields * whether to include transient fields
* @return the String result * @return the String result
* @throws IllegalArgumentException * @throws IllegalArgumentException
* if the Object is <code>null</code> * if the Object is <code>null</code>
*/ */
public static String toString(Object object, ToStringStyle style, boolean outputTransients) { public static String toString(Object object, ToStringStyle style, boolean outputTransients) {
return toString(object, style, outputTransients, false, null); return toString(object, style, outputTransients, false, null);
@ -256,21 +246,19 @@ public static String toString(Object object, ToStringStyle style, boolean output
* </p> * </p>
* *
* <p> * <p>
* It uses <code>AccessibleObject.setAccessible</code> to gain access to * It uses <code>AccessibleObject.setAccessible</code> to gain access to private fields. This means that it will
* private fields. This means that it will throw a security exception if * throw a security exception if run under a security manager, if the permissions are not set up correctly. It is
* run under a security manager, if the permissions are not set up * also not as efficient as testing explicitly.
* correctly. It is also not as efficient as testing explicitly.
* </p> * </p>
* *
* <p> * <p>
* If the <code>outputTransients</code> is <code>true</code>, * If the <code>outputTransients</code> is <code>true</code>, transient fields will be output, otherwise they
* transient fields will be output, otherwise they are ignored, as they are * are ignored, as they are likely derived fields, and not part of the value of the Object.
* likely derived fields, and not part of the value of the Object.
* </p> * </p>
* *
* <p> * <p>
* If the <code>outputStatics</code> is <code>true</code>, static * If the <code>outputStatics</code> is <code>true</code>, static fields will be output, otherwise they are
* fields will be output, otherwise they are ignored. * ignored.
* </p> * </p>
* *
* <p> * <p>
@ -278,22 +266,20 @@ public static String toString(Object object, ToStringStyle style, boolean output
* </p> * </p>
* *
* <p> * <p>
* If the style is <code>null</code>, the default <code>ToStringStyle</code> * If the style is <code>null</code>, the default <code>ToStringStyle</code> is used.
* is used.
* </p> * </p>
* *
* @param object * @param object
* the Object to be output * the Object to be output
* @param style * @param style
* the style of the <code>toString</code> to create, may be * the style of the <code>toString</code> to create, may be <code>null</code>
* <code>null</code>
* @param outputTransients * @param outputTransients
* whether to include transient fields * whether to include transient fields
* @param outputStatics * @param outputStatics
* whether to include transient fields * whether to include transient fields
* @return the String result * @return the String result
* @throws IllegalArgumentException * @throws IllegalArgumentException
* if the Object is <code>null</code> * if the Object is <code>null</code>
* @since 2.1 * @since 2.1
*/ */
public static String toString(Object object, ToStringStyle style, boolean outputTransients, boolean outputStatics) { public static String toString(Object object, ToStringStyle style, boolean outputTransients, boolean outputStatics) {
@ -306,47 +292,43 @@ public static String toString(Object object, ToStringStyle style, boolean output
* </p> * </p>
* *
* <p> * <p>
* It uses <code>AccessibleObject.setAccessible</code> to gain access to * It uses <code>AccessibleObject.setAccessible</code> to gain access to private fields. This means that it will
* private fields. This means that it will throw a security exception if * throw a security exception if run under a security manager, if the permissions are not set up correctly. It is
* run under a security manager, if the permissions are not set up * also not as efficient as testing explicitly.
* correctly. It is also not as efficient as testing explicitly.
* </p> * </p>
* *
* <p> * <p>
* If the <code>outputTransients</code> is <code>true</code>, * If the <code>outputTransients</code> is <code>true</code>, transient fields will be output, otherwise they
* transient fields will be output, otherwise they are ignored, as they are * are ignored, as they are likely derived fields, and not part of the value of the Object.
* likely derived fields, and not part of the value of the Object.
* </p> * </p>
* *
* <p> * <p>
* If the <code>outputStatics</code> is <code>true</code>, static * If the <code>outputStatics</code> is <code>true</code>, static fields will be output, otherwise they are
* fields will be output, otherwise they are ignored. * ignored.
* </p> * </p>
* *
* <p> * <p>
* Superclass fields will be appended up to and including the specified * Superclass fields will be appended up to and including the specified superclass. A null superclass is treated as
* superclass. A null superclass is treated as <code>java.lang.Object</code>. * <code>java.lang.Object</code>.
* </p> * </p>
* *
* <p> * <p>
* If the style is <code>null</code>, the default <code>ToStringStyle</code> * If the style is <code>null</code>, the default <code>ToStringStyle</code> is used.
* is used.
* </p> * </p>
* *
* @param object * @param object
* the Object to be output * the Object to be output
* @param style * @param style
* the style of the <code>toString</code> to create, may be * the style of the <code>toString</code> to create, may be <code>null</code>
* <code>null</code>
* @param outputTransients * @param outputTransients
* whether to include transient fields * whether to include transient fields
* @param outputStatics * @param outputStatics
* whether to include static fields * whether to include static fields
* @param reflectUpToClass * @param reflectUpToClass
* the superclass to reflect up to (inclusive), may be <code>null</code> * the superclass to reflect up to (inclusive), may be <code>null</code>
* @return the String result * @return the String result
* @throws IllegalArgumentException * @throws IllegalArgumentException
* if the Object is <code>null</code> * if the Object is <code>null</code>
* @since 2.1 * @since 2.1
*/ */
public static String toString(Object object, ToStringStyle style, boolean outputTransients, boolean outputStatics, public static String toString(Object object, ToStringStyle style, boolean outputTransients, boolean outputStatics,
@ -361,51 +343,70 @@ public static String toString(Object object, ToStringStyle style, boolean output
* </p> * </p>
* *
* <p> * <p>
* It uses <code>AccessibleObject.setAccessible</code> to gain access to * It uses <code>AccessibleObject.setAccessible</code> to gain access to private fields. This means that it will
* private fields. This means that it will throw a security exception if * throw a security exception if run under a security manager, if the permissions are not set up correctly. It is
* run under a security manager, if the permissions are not set up * also not as efficient as testing explicitly.
* correctly. It is also not as efficient as testing explicitly.
* </p> * </p>
* *
* <p> * <p>
* If the <code>outputTransients</code> is <code>true</code>, * If the <code>outputTransients</code> is <code>true</code>, transient members will be output, otherwise they
* transient members will be output, otherwise they are ignored, as they * are ignored, as they are likely derived fields, and not part of the value of the Object.
* are likely derived fields, and not part of the value of the Object.
* </p> * </p>
* *
* <p> * <p>
* Static fields will not be included. Superclass fields will be appended * Static fields will not be included. Superclass fields will be appended up to and including the specified
* up to and including the specified superclass. A null superclass is * superclass. A null superclass is treated as <code>java.lang.Object</code>.
* treated as <code>java.lang.Object</code>.
* </p> * </p>
* *
* <p> * <p>
* If the style is <code>null</code>, the default <code>ToStringStyle</code> * If the style is <code>null</code>, the default <code>ToStringStyle</code> is used.
* is used.
* </p> * </p>
* *
* @deprecated Use * @deprecated Use {@link #toString(Object,ToStringStyle,boolean,boolean,Class)}
* {@link #toString(Object,ToStringStyle,boolean,boolean,Class)}
* *
* @param object * @param object
* the Object to be output * the Object to be output
* @param style * @param style
* the style of the <code>toString</code> to create, may be * the style of the <code>toString</code> to create, may be <code>null</code>
* <code>null</code>
* @param outputTransients * @param outputTransients
* whether to include transient fields * whether to include transient fields
* @param reflectUpToClass * @param reflectUpToClass
* the superclass to reflect up to (inclusive), may be <code>null</code> * the superclass to reflect up to (inclusive), may be <code>null</code>
* @return the String result * @return the String result
* @throws IllegalArgumentException * @throws IllegalArgumentException
* if the Object is <code>null</code> * if the Object is <code>null</code>
* @since 2.0 * @since 2.0
*/ */
public static String toString(Object object, ToStringStyle style, boolean outputTransients, public static String toString(Object object, ToStringStyle style, boolean outputTransients, Class reflectUpToClass) {
Class reflectUpToClass) {
return new ReflectionToStringBuilder(object, style, null, reflectUpToClass, outputTransients).toString(); return new ReflectionToStringBuilder(object, style, null, reflectUpToClass, outputTransients).toString();
} }
/**
* Builds a String for a toString method excluding the given field name.
*
* @param object
* The object to "toString".
* @param excludeFieldName
* The field name to exclude
* @return The toString value.
*/
public static String toStringExclude(Object object, final String excludeFieldName) {
return toStringExclude(object, new String[]{excludeFieldName});
}
/**
* Builds a String for a toString method excluding the given field name.
*
* @param object
* The object to "toString".
* @param excludeFieldNames
* The field names to exclude
* @return The toString value.
*/
public static String toStringExclude(Object object, String[] excludeFieldNames) {
return new ReflectionToStringBuilder(object).setExcludeFieldNames(excludeFieldNames).toString();
}
/** /**
* <p> * <p>
* Unregisters the given object. * Unregisters the given object.
@ -416,7 +417,7 @@ public static String toString(Object object, ToStringStyle style, boolean output
* </p> * </p>
* *
* @param value * @param value
* The object to unregister. * The object to unregister.
*/ */
static void unregister(Object value) { static void unregister(Object value) {
getRegistry().remove(value); getRegistry().remove(value);
@ -432,6 +433,11 @@ static void unregister(Object value) {
*/ */
private boolean appendTransients = false; private boolean appendTransients = false;
/**
* Which field names to exclude from output. Intended for fields like <code>"password"</code>.
*/
private String[] excludeFieldNames;
/** /**
* The last super class to stop appending fields for. * The last super class to stop appending fields for.
*/ */
@ -447,10 +453,9 @@ static void unregister(Object value) {
* </p> * </p>
* *
* @param object * @param object
* the Object to build a <code>toString</code> for, must not * the Object to build a <code>toString</code> for, must not be <code>null</code>
* be <code>null</code>
* @throws IllegalArgumentException * @throws IllegalArgumentException
* if the Object passed in is <code>null</code> * if the Object passed in is <code>null</code>
*/ */
public ReflectionToStringBuilder(Object object) { public ReflectionToStringBuilder(Object object) {
super(object); super(object);
@ -466,13 +471,11 @@ public ReflectionToStringBuilder(Object object) {
* </p> * </p>
* *
* @param object * @param object
* the Object to build a <code>toString</code> for, must not * the Object to build a <code>toString</code> for, must not be <code>null</code>
* be <code>null</code>
* @param style * @param style
* the style of the <code>toString</code> to create, may be * the style of the <code>toString</code> to create, may be <code>null</code>
* <code>null</code>
* @throws IllegalArgumentException * @throws IllegalArgumentException
* if the Object passed in is <code>null</code> * if the Object passed in is <code>null</code>
*/ */
public ReflectionToStringBuilder(Object object, ToStringStyle style) { public ReflectionToStringBuilder(Object object, ToStringStyle style) {
super(object, style); super(object, style);
@ -492,14 +495,13 @@ public ReflectionToStringBuilder(Object object, ToStringStyle style) {
* </p> * </p>
* *
* @param object * @param object
* the Object to build a <code>toString</code> for * the Object to build a <code>toString</code> for
* @param style * @param style
* the style of the <code>toString</code> to create, may be * the style of the <code>toString</code> to create, may be <code>null</code>
* <code>null</code>
* @param buffer * @param buffer
* the <code>StringBuffer</code> to populate, may be <code>null</code> * the <code>StringBuffer</code> to populate, may be <code>null</code>
* @throws IllegalArgumentException * @throws IllegalArgumentException
* if the Object passed in is <code>null</code> * if the Object passed in is <code>null</code>
*/ */
public ReflectionToStringBuilder(Object object, ToStringStyle style, StringBuffer buffer) { public ReflectionToStringBuilder(Object object, ToStringStyle style, StringBuffer buffer) {
super(object, style, buffer); super(object, style, buffer);
@ -508,20 +510,18 @@ public ReflectionToStringBuilder(Object object, ToStringStyle style, StringBuffe
/** /**
* Constructor. * Constructor.
* *
* @deprecated Use * @deprecated Use {@link #ReflectionToStringBuilder(Object,ToStringStyle,StringBuffer,Class,boolean,boolean)}.
* {@link #ReflectionToStringBuilder(Object,ToStringStyle,StringBuffer,Class,boolean,boolean)}.
* *
* @param object * @param object
* the Object to build a <code>toString</code> for * the Object to build a <code>toString</code> for
* @param style * @param style
* the style of the <code>toString</code> to create, may be * the style of the <code>toString</code> to create, may be <code>null</code>
* <code>null</code>
* @param buffer * @param buffer
* the <code>StringBuffer</code> to populate, may be <code>null</code> * the <code>StringBuffer</code> to populate, may be <code>null</code>
* @param reflectUpToClass * @param reflectUpToClass
* the superclass to reflect up to (inclusive), may be <code>null</code> * the superclass to reflect up to (inclusive), may be <code>null</code>
* @param outputTransients * @param outputTransients
* whether to include transient fields * whether to include transient fields
*/ */
public ReflectionToStringBuilder(Object object, ToStringStyle style, StringBuffer buffer, Class reflectUpToClass, public ReflectionToStringBuilder(Object object, ToStringStyle style, StringBuffer buffer, Class reflectUpToClass,
boolean outputTransients) { boolean outputTransients) {
@ -534,18 +534,17 @@ public ReflectionToStringBuilder(Object object, ToStringStyle style, StringBuffe
* Constructor. * Constructor.
* *
* @param object * @param object
* the Object to build a <code>toString</code> for * the Object to build a <code>toString</code> for
* @param style * @param style
* the style of the <code>toString</code> to create, may be * the style of the <code>toString</code> to create, may be <code>null</code>
* <code>null</code>
* @param buffer * @param buffer
* the <code>StringBuffer</code> to populate, may be <code>null</code> * the <code>StringBuffer</code> to populate, may be <code>null</code>
* @param reflectUpToClass * @param reflectUpToClass
* the superclass to reflect up to (inclusive), may be <code>null</code> * the superclass to reflect up to (inclusive), may be <code>null</code>
* @param outputTransients * @param outputTransients
* whether to include transient fields * whether to include transient fields
* @param outputStatics * @param outputStatics
* whether to include static fields * whether to include static fields
* @since 2.1 * @since 2.1
*/ */
public ReflectionToStringBuilder(Object object, ToStringStyle style, StringBuffer buffer, Class reflectUpToClass, public ReflectionToStringBuilder(Object object, ToStringStyle style, StringBuffer buffer, Class reflectUpToClass,
@ -559,15 +558,13 @@ public ReflectionToStringBuilder(Object object, ToStringStyle style, StringBuffe
/** /**
* Returns whether or not to append the given <code>Field</code>. * Returns whether or not to append the given <code>Field</code>.
* <ul> * <ul>
* <li>Transient fields are appended only if {@link #isAppendTransients()} * <li>Transient fields are appended only if {@link #isAppendTransients()} returns <code>true</code>.
* returns <code>true</code>. * <li>Static fields are appended only if {@link #isAppendStatics()} returns <code>true</code>.
* <li>Static fields are appended only if {@link #isAppendStatics()}
* returns <code>true</code>.
* <li>Inner class fields are not appened.</li> * <li>Inner class fields are not appened.</li>
* </ul> * </ul>
* *
* @param field * @param field
* The Field to test. * The Field to test.
* @return Whether or not to append the given <code>Field</code>. * @return Whether or not to append the given <code>Field</code>.
*/ */
protected boolean accept(Field field) { protected boolean accept(Field field) {
@ -576,11 +573,16 @@ protected boolean accept(Field field) {
return false; return false;
} }
if (Modifier.isTransient(field.getModifiers()) && !this.isAppendTransients()) { if (Modifier.isTransient(field.getModifiers()) && !this.isAppendTransients()) {
// transients. // Reject transient fields.
return false; return false;
} }
if (Modifier.isStatic(field.getModifiers()) && !this.isAppendStatics()) { if (Modifier.isStatic(field.getModifiers()) && !this.isAppendStatics()) {
// transients. // 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 false;
} }
return true; return true;
@ -588,18 +590,16 @@ protected boolean accept(Field field) {
/** /**
* <p> * <p>
* Appends the fields and values defined by the given object of the given * Appends the fields and values defined by the given object of the given Class.
* Class.
* </p> * </p>
* *
* <p> * <p>
* If a cycle is detected as an object is &quot;toString()'ed&quot;, such * If a cycle is detected as an object is &quot;toString()'ed&quot;, such an object is rendered as if
* an object is rendered as if <code>Object.toString()</code> had been * <code>Object.toString()</code> had been called and not implemented by the object.
* called and not implemented by the object.
* </p> * </p>
* *
* @param clazz * @param clazz
* The class of object parameter * The class of object parameter
*/ */
protected void appendFieldsIn(Class clazz) { protected void appendFieldsIn(Class clazz) {
if (isRegistered(this.getObject())) { if (isRegistered(this.getObject())) {
@ -634,7 +634,7 @@ protected void appendFieldsIn(Class clazz) {
this.appendAsObjectToString(fieldValue); this.appendAsObjectToString(fieldValue);
this.getStyle().appendFieldEnd(this.getStringBuffer(), fieldName); this.getStyle().appendFieldEnd(this.getStringBuffer(), fieldName);
// The recursion out of // The recursion out of
// builder.append(fieldName, fieldValue); // builder.append(fieldName, fieldValue);
// below will append the field // below will append the field
// end marker. // end marker.
} else { } else {
@ -646,9 +646,9 @@ protected void appendFieldsIn(Class clazz) {
} }
} }
} catch (IllegalAccessException ex) { } catch (IllegalAccessException ex) {
//this can't happen. Would get a Security exception // this can't happen. Would get a Security exception
// instead // instead
//throw a runtime exception in case the impossible // throw a runtime exception in case the impossible
// happens. // happens.
throw new InternalError("Unexpected IllegalAccessException: " + ex.getMessage()); throw new InternalError("Unexpected IllegalAccessException: " + ex.getMessage());
} }
@ -659,6 +659,13 @@ protected void appendFieldsIn(Class clazz) {
} }
} }
/**
* @return Returns the excludeFieldNames.
*/
public String[] getExcludeFieldNames() {
return this.excludeFieldNames;
}
/** /**
* <p> * <p>
* Gets the last super class to stop appending fields for. * Gets the last super class to stop appending fields for.
@ -676,13 +683,13 @@ public Class getUpToClass() {
* </p> * </p>
* *
* @param field * @param field
* The Field to query. * The Field to query.
* @return The Object from the given Field. * @return The Object from the given Field.
* *
* @throws IllegalArgumentException * @throws IllegalArgumentException
* see {@link java.lang.reflect.Field#get(Object)} * see {@link java.lang.reflect.Field#get(Object)}
* @throws IllegalAccessException * @throws IllegalAccessException
* see {@link java.lang.reflect.Field#get(Object)} * see {@link java.lang.reflect.Field#get(Object)}
* *
* @see java.lang.reflect.Field#get(Object) * @see java.lang.reflect.Field#get(Object)
*/ */
@ -719,7 +726,7 @@ public boolean isAppendTransients() {
* </p> * </p>
* *
* @param array * @param array
* the array to add to the <code>toString</code> * the array to add to the <code>toString</code>
* @return this * @return this
*/ */
public ToStringBuilder reflectionAppendArray(Object array) { public ToStringBuilder reflectionAppendArray(Object array) {
@ -729,8 +736,7 @@ public ToStringBuilder reflectionAppendArray(Object array) {
/** /**
* <p> * <p>
* Registers this builder's source object to avoid infinite loops when * Registers this builder's source object to avoid infinite loops when processing circular object references.
* processing circular object references.
* </p> * </p>
*/ */
void registerObject() { void registerObject() {
@ -743,7 +749,7 @@ void registerObject() {
* </p> * </p>
* *
* @param appendStatics * @param appendStatics
* Whether or not to append static fields. * Whether or not to append static fields.
* @since 2.1 * @since 2.1
*/ */
public void setAppendStatics(boolean appendStatics) { public void setAppendStatics(boolean appendStatics) {
@ -756,19 +762,35 @@ public void setAppendStatics(boolean appendStatics) {
* </p> * </p>
* *
* @param appendTransients * @param appendTransients
* Whether or not to append transient fields. * Whether or not to append transient fields.
*/ */
public void setAppendTransients(boolean appendTransients) { public void setAppendTransients(boolean appendTransients) {
this.appendTransients = appendTransients; this.appendTransients = appendTransients;
} }
/**
* Sets the field names to exclude.
*
* @param excludeFieldNamesParam
* The excludeFieldNames to set.
* @return <code>this</code>
*/
public ReflectionToStringBuilder setExcludeFieldNames(String[] excludeFieldNamesParam) {
if (excludeFieldNamesParam == null) {
this.excludeFieldNames = null;
}
this.excludeFieldNames = (String[]) excludeFieldNamesParam.clone();
Arrays.sort(this.excludeFieldNames);
return this;
}
/** /**
* <p> * <p>
* Sets the last super class to stop appending fields for. * Sets the last super class to stop appending fields for.
* </p> * </p>
* *
* @param clazz * @param clazz
* The last super class to stop appending fields for. * The last super class to stop appending fields for.
*/ */
public void setUpToClass(Class clazz) { public void setUpToClass(Class clazz) {
this.upToClass = clazz; this.upToClass = clazz;
@ -796,8 +818,7 @@ public String toString() {
/** /**
* <p> * <p>
* Unregisters this builder's source object to avoid infinite loops when * Unregisters this builder's source object to avoid infinite loops when processing circular object references.
* processing circular object references.
* </p> * </p>
*/ */
void unregisterObject() { void unregisterObject() {

View File

@ -47,17 +47,18 @@ public static void main(String[] args) {
public static Test suite() { public static Test suite() {
TestSuite suite = new TestSuite(); TestSuite suite = new TestSuite();
suite.setName("Commons-Lang-Builder Tests"); suite.setName("Commons-Lang-Builder Tests");
suite.addTest(CompareToBuilderTest.suite()); suite.addTestSuite(CompareToBuilderTest.class);
suite.addTest(EqualsBuilderTest.suite()); suite.addTestSuite(EqualsBuilderTest.class);
suite.addTest(HashCodeBuilderTest.suite()); suite.addTestSuite(HashCodeBuilderTest.class);
suite.addTest(HashCodeBuilderAndEqualsBuilderTest.suite()); suite.addTestSuite(HashCodeBuilderAndEqualsBuilderTest.class);
suite.addTest(ToStringBuilderTest.suite()); suite.addTestSuite(ToStringBuilderTest.class);
suite.addTest(DefaultToStringStyleTest.suite()); suite.addTestSuite(DefaultToStringStyleTest.class);
suite.addTest(NoFieldNamesToStringStyleTest.suite()); suite.addTestSuite(NoFieldNamesToStringStyleTest.class);
suite.addTest(MultiLineToStringStyleTest.suite()); suite.addTestSuite(MultiLineToStringStyleTest.class);
suite.addTest(SimpleToStringStyleTest.suite()); suite.addTestSuite(ReflectionToStringBuilderExcludeTest.class);
suite.addTest(StandardToStringStyleTest.suite()); suite.addTestSuite(SimpleToStringStyleTest.class);
suite.addTest(ToStringStyleTest.suite()); suite.addTestSuite(StandardToStringStyleTest.class);
suite.addTestSuite(ToStringStyleTest.class);
return suite; return suite;
} }
} }