From d3c425d6f1281d9387f5b80836ce855bc168453d Mon Sep 17 00:00:00 2001 From: Henri Yandell Date: Thu, 22 Jun 2006 22:20:44 +0000 Subject: [PATCH] Added Collection APIs for the excludeFieldNames as suggested at the end of LANG-226 git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@416487 13f79535-47bb-0310-9956-ffa450edef68 --- .../lang/builder/CompareToBuilder.java | 35 ++++++++++++++++++- .../commons/lang/builder/EqualsBuilder.java | 24 +++++++++++++ .../commons/lang/builder/HashCodeBuilder.java | 26 ++++++++++++++ .../builder/ReflectionToStringBuilder.java | 6 ++-- 4 files changed, 87 insertions(+), 4 deletions(-) diff --git a/src/java/org/apache/commons/lang/builder/CompareToBuilder.java b/src/java/org/apache/commons/lang/builder/CompareToBuilder.java index 5a398fcea..c2be8186a 100644 --- a/src/java/org/apache/commons/lang/builder/CompareToBuilder.java +++ b/src/java/org/apache/commons/lang/builder/CompareToBuilder.java @@ -20,6 +20,7 @@ import java.lang.reflect.Modifier; import java.util.Arrays; import java.util.Collections; +import java.util.Collection; import java.util.Comparator; import java.util.List; @@ -192,7 +193,39 @@ public static int reflectionCompare(Object lhs, Object rhs, boolean compareTrans * * @param lhs left-hand object * @param rhs right-hand object - * @param excludeFields fields to exclude + * @param excludeFields Collection of String fields to exclude + * @return a negative integer, zero, or a positive integer as lhs + * is less than, equal to, or greater than rhs + * @throws NullPointerException if either lhs or rhs + * (but not both) is null + * @throws ClassCastException if rhs is not assignment-compatible + * with lhs + */ + public static int reflectionCompare(Object lhs, Object rhs, Collection /*String*/ excludeFields) { + return reflectionCompare(lhs, rhs, ReflectionToStringBuilder.toNoNullStringArray(excludeFields)); + } + + /** + *

Compares two Objects via reflection.

+ * + *

Fields can be private, thus AccessibleObject.setAccessible + * is used to bypass normal access control checks. This will fail under a + * security manager unless the appropriate permissions are set.

+ * + * + * + *

If both lhs and rhs are null, + * they are considered equal.

+ * + * @param lhs left-hand object + * @param rhs right-hand object + * @param excludeFields array of fields to exclude * @return a negative integer, zero, or a positive integer as lhs * is less than, equal to, or greater than rhs * @throws NullPointerException if either lhs or rhs diff --git a/src/java/org/apache/commons/lang/builder/EqualsBuilder.java b/src/java/org/apache/commons/lang/builder/EqualsBuilder.java index fe0d4bcbf..a59764ea8 100644 --- a/src/java/org/apache/commons/lang/builder/EqualsBuilder.java +++ b/src/java/org/apache/commons/lang/builder/EqualsBuilder.java @@ -20,6 +20,7 @@ import java.lang.reflect.Modifier; import java.util.Arrays; import java.util.Collections; +import java.util.Collection; import java.util.List; /** @@ -124,6 +125,29 @@ public static boolean reflectionEquals(Object lhs, Object rhs) { return reflectionEquals(lhs, rhs, false, null, null); } + /** + *

This method uses reflection to determine if the two Objects + * are equal.

+ * + *

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 tested, as they are likely derived + * fields, and not part of the value of the Object.

+ * + *

Static fields will not be tested. Superclass fields will be included.

+ * + * @param lhs this object + * @param rhs the other object + * @param excludeFields Collection of String field names to exclude from testing + * @return true if the two Objects have tested equals. + */ + public static boolean reflectionEquals(Object lhs, Object rhs, Collection /*String*/ excludeFields) { + return reflectionEquals(lhs, rhs, ReflectionToStringBuilder.toNoNullStringArray(excludeFields)); + } + /** *

This method uses reflection to determine if the two Objects * are equal.

diff --git a/src/java/org/apache/commons/lang/builder/HashCodeBuilder.java b/src/java/org/apache/commons/lang/builder/HashCodeBuilder.java index 06afc9dfd..c3c60d695 100644 --- a/src/java/org/apache/commons/lang/builder/HashCodeBuilder.java +++ b/src/java/org/apache/commons/lang/builder/HashCodeBuilder.java @@ -20,6 +20,7 @@ import java.lang.reflect.Modifier; import java.util.Arrays; import java.util.Collections; +import java.util.Collection; import java.util.List; /** @@ -153,6 +154,31 @@ public static int reflectionHashCode(Object object) { return reflectionHashCode(17, 37, object, false, null, null); } + /** + *

This method uses reflection to build a valid hash code.

+ * + *

This constructor uses two hard coded choices for the constants + * needed to build a hash code.

+ * + *

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 used, as they are likely derived + * fields, and not part of the value of the Object.

+ * + *

Static fields will not be tested. Superclass fields will be included.

+ * + * @param object the Object to create a hashCode for + * @param excludeFields Collection of String field names to exclude from use in calculation of hash code + * @return int hash code + * @throws IllegalArgumentException if the object is null + */ + public static int reflectionHashCode(Object object, Collection /*String*/ excludeFields) { + return reflectionHashCode(object, ReflectionToStringBuilder.toNoNullStringArray(excludeFields)); + } + /** *

This method uses reflection to build a valid hash code.

* diff --git a/src/java/org/apache/commons/lang/builder/ReflectionToStringBuilder.java b/src/java/org/apache/commons/lang/builder/ReflectionToStringBuilder.java index 9459e7c31..bc032f724 100644 --- a/src/java/org/apache/commons/lang/builder/ReflectionToStringBuilder.java +++ b/src/java/org/apache/commons/lang/builder/ReflectionToStringBuilder.java @@ -405,7 +405,7 @@ public static String toStringExclude(Object object, final String excludeFieldNam * The field names to exclude. Null excludes nothing. * @return The toString value. */ - public static String toStringExclude(Object object, Collection excludeFieldNames) { + public static String toStringExclude(Object object, Collection /*String*/ excludeFieldNames) { return toStringExclude(object, toNoNullStringArray(excludeFieldNames)); } @@ -418,7 +418,7 @@ public static String toStringExclude(Object object, Collection excludeFieldNames * The collection to convert * @return A new array of Strings. */ - private static String[] toNoNullStringArray(Collection collection) { + static String[] toNoNullStringArray(Collection collection) { if (collection == null) { return ArrayUtils.EMPTY_STRING_ARRAY; } @@ -434,7 +434,7 @@ private static String[] toNoNullStringArray(Collection collection) { * The array to check * @return The given array or a new array without null. */ - private static String[] toNoNullStringArray(Object[] array) { + static String[] toNoNullStringArray(Object[] array) { ArrayList list = new ArrayList(array.length); for (int i = 0; i < array.length; i++) { Object e = array[i];