scolebourne 2002/09/17 15:06:38 Modified: lang/src/java/org/apache/commons/lang/builder CompareToBuilder.java EqualsBuilder.java HashCodeBuilder.java Log: Exclude static fields from reflection based builder Revision Changes Path 1.2 +19 -14 jakarta-commons/lang/src/java/org/apache/commons/lang/builder/CompareToBuilder.java Index: CompareToBuilder.java =================================================================== RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/builder/CompareToBuilder.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- CompareToBuilder.java 15 Sep 2002 10:25:22 -0000 1.1 +++ CompareToBuilder.java 17 Sep 2002 22:06:37 -0000 1.2 @@ -128,9 +128,10 @@ * 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. - * @param lhs - Left Hand Side - * @param rhs - Right Hand Side - * @return int - a negative integer, zero, or a positive integer as this + * Static fields will not be tested. + * @param lhs Left Hand Side + * @param rhs Right Hand Side + * @return int a negative integer, zero, or a positive integer as this * object is less than, equal to, or greater than the specified object. * @throws ClassCastException - if the specified object's type prevents it * from being compared to this Object. @@ -149,16 +150,18 @@ * If the TestTransients parameter is set to true, transient members will be * tested, otherwise they are ignored, as they are likely derived fields, and * not part of the value of the object. + * Static fields will not be tested. * - * @param lhs - Left Hand Side - * @param rhs - Right Hand Side - * @param testTransients - whether to include transient fields + * @param lhs Left Hand Side + * @param rhs Right Hand Side + * @param testTransients whether to include transient fields * @return int - a negative integer, zero, or a positive integer as this * object is less than, equal to, or greater than the specified object. * @throws ClassCastException - if the specified object's type prevents it * from being compared to this Object. */ - public static int reflectionCompare(Object lhs, Object rhs, boolean testTransients) { + public static int reflectionCompare(Object lhs, Object rhs, + boolean testTransients) { if (lhs == rhs) { return 0; } @@ -175,12 +178,14 @@ for (int i = 0; i < fields.length && compareToBuilder.comparison == 0; ++i) { Field f = fields[i]; if (testTransients || !Modifier.isTransient(f.getModifiers())) { - try { - compareToBuilder.append(f.get(lhs), f.get(rhs)); - } catch (IllegalAccessException e) { - //this can't happen. Would get a Security exception instead - //throw a runtime exception in case the impossible happens. - throw new InternalError("Unexpected IllegalAccessException"); + if ( !Modifier.isStatic(f.getModifiers())) { + try { + compareToBuilder.append(f.get(lhs), f.get(rhs)); + } catch (IllegalAccessException e) { + //this can't happen. Would get a Security exception instead + //throw a runtime exception in case the impossible happens. + throw new InternalError("Unexpected IllegalAccessException"); + } } } } 1.2 +20 -15 jakarta-commons/lang/src/java/org/apache/commons/lang/builder/EqualsBuilder.java Index: EqualsBuilder.java =================================================================== RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/builder/EqualsBuilder.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- EqualsBuilder.java 12 Sep 2002 22:00:00 -0000 1.1 +++ EqualsBuilder.java 17 Sep 2002 22:06:38 -0000 1.2 @@ -129,11 +129,12 @@ * that it will throw a security exception if run under a security manger, if * the permissions are not set up. * 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. + * 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. * - * @param lhs - Left Hand Side - * @param rhs - Right Hand Side + * @param lhs Left Hand Side + * @param rhs Right Hand Side * @return boolean - if the two objects have tested equals. */ public static boolean reflectionEquals(Object lhs, Object rhs) { @@ -150,13 +151,15 @@ * If the TestTransients parameter is set to true, transient members will be * tested, otherwise they are ignored, as they are likely derived fields, and * not part of the value of the object. + * Static fields will not be tested. * - * @param lhs - Left Hand Side - * @param rhs - Right Hand Side - * @param testTransients - whether to include transient fields + * @param lhs Left Hand Side + * @param rhs Right Hand Side + * @param testTransients whether to include transient fields * @return boolean - if the two objects have tested equals. */ - public static boolean reflectionEquals(Object lhs, Object rhs, boolean testTransients) { + public static boolean reflectionEquals(Object lhs, Object rhs, + boolean testTransients) { if (lhs == rhs) { return true; } @@ -173,12 +176,14 @@ for (int i = 0; i < fields.length && equalsBuilder.isEquals; ++i) { Field f = fields[i]; if (testTransients || !Modifier.isTransient(f.getModifiers())) { - try { - equalsBuilder.append(f.get(lhs), f.get(rhs)); - } catch (IllegalAccessException e) { - //this can't happen. Would get a Security exception instead - //throw a runtime exception in case the impossible happens. - throw new InternalError("Unexpected IllegalAccessException"); + if (!Modifier.isStatic(f.getModifiers())) { + try { + equalsBuilder.append(f.get(lhs), f.get(rhs)); + } catch (IllegalAccessException e) { + //this can't happen. Would get a Security exception instead + //throw a runtime exception in case the impossible happens. + throw new InternalError("Unexpected IllegalAccessException"); + } } } } 1.2 +36 -30 jakarta-commons/lang/src/java/org/apache/commons/lang/builder/HashCodeBuilder.java Index: HashCodeBuilder.java =================================================================== RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/builder/HashCodeBuilder.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- HashCodeBuilder.java 12 Sep 2002 21:59:01 -0000 1.1 +++ HashCodeBuilder.java 17 Sep 2002 22:06:38 -0000 1.2 @@ -165,6 +165,7 @@ * 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. * This constructor uses two hard coded choices for the constants needed * to build a hash code. * @@ -183,26 +184,22 @@ * that it will throw a security exception if run under a security manger, if * the permissions are not set up. * 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. - * <p> - * Two randomly chosen, non-zero, odd numbers must be passed in. Ideally - * these should be different for each class, however this is not vital. - * Prime numbers are preferred, especially for the multiplier. + * If the TestTransients parameter is set to true, transient members will be + * tested, otherwise they are ignored, as they are likely derived fields, and + * not part of the value of the object. + * Static fields will not be tested. + * This constructor uses two hard coded choices for the constants needed + * to build a hash code. * - * @param initialNonZeroOddNumber - * @param multiplierNonZeroOddNumber * @param object the object to create a hash code for + * @param testTransients whether to include transient fields * @return int hash code * @throws IllegalArgumentException if the object is null - * @throws IllegalArgumentException if the number is zero or even */ - public static int reflectionHashCode( - int initialNonZeroOddNumber, int multiplierNonZeroOddNumber, - Object object) { - return reflectionHashCode(initialNonZeroOddNumber, multiplierNonZeroOddNumber, object); + public static int reflectionHashCode(Object object, boolean testTransients) { + return reflectionHashCode(17, 37, object, testTransients); } - + /** * This method uses reflection to build a valid hash code. * <p> @@ -210,21 +207,27 @@ * that it will throw a security exception if run under a security manger, if * the permissions are not set up. * It is also not as efficient as testing explicitly. - * If the TestTransients parameter is set to true, transient members will be - * tested, otherwise they are ignored, as they are likely derived fields, and - * not part of the value of the object. - * This constructor uses two hard coded choices for the constants needed - * to build a hash code. + * 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. + * <p> + * Two randomly chosen, non-zero, odd numbers must be passed in. Ideally + * these should be different for each class, however this is not vital. + * Prime numbers are preferred, especially for the multiplier. * + * @param initialNonZeroOddNumber + * @param multiplierNonZeroOddNumber * @param object the object to create a hash code for - * @param testTransients whether to include transient fields * @return int hash code * @throws IllegalArgumentException if the object is null + * @throws IllegalArgumentException if the number is zero or even */ - public static int reflectionHashCode(Object object, boolean testTransients) { - return reflectionHashCode(17, 37, object, testTransients); + public static int reflectionHashCode( + int initialNonZeroOddNumber, int multiplierNonZeroOddNumber, + Object object) { + return reflectionHashCode(initialNonZeroOddNumber, multiplierNonZeroOddNumber, object, false); } - + /** * This method uses reflection to build a valid hash code. * <p> @@ -235,6 +238,7 @@ * If the TestTransients parameter is set to true, transient members will be * tested, otherwise they are ignored, as they are likely derived fields, and * not part of the value of the object. + * Static fields will not be tested. * <p> * Two randomly chosen, non-zero, odd numbers must be passed in. Ideally * these should be different for each class, however this is not vital. @@ -261,12 +265,14 @@ for (int i = 0; i < fields.length; ++i) { Field f = fields[i]; if (testTransients || !Modifier.isTransient(f.getModifiers())) { - try { - hashCodeBuilder.append(f.get(object)); - } catch (IllegalAccessException e) { - //this can't happen. Would get a Security exception instead - //throw a runtime exception in case the impossible happens. - throw new InternalError("Unexpected IllegalAccessException"); + if (!Modifier.isStatic(f.getModifiers())) { + try { + hashCodeBuilder.append(f.get(object)); + } catch (IllegalAccessException e) { + //this can't happen. Would get a Security exception instead + //throw a runtime exception in case the impossible happens. + throw new InternalError("Unexpected IllegalAccessException"); + } } } }
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>