This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-lang.git
The following commit(s) were added to refs/heads/master by this push: new bc8a3b9 sort fields in HashCodeBuilder for deterministic order (#480) bc8a3b9 is described below commit bc8a3b9d8428bfcdfbce67e9010cf0aa27e6c8a6 Author: contextshuffling <55522232+contextshuffl...@users.noreply.github.com> AuthorDate: Fri Nov 22 11:15:10 2019 -0600 sort fields in HashCodeBuilder for deterministic order (#480) --- .../java/org/apache/commons/lang3/builder/HashCodeBuilder.java | 3 +++ .../org/apache/commons/lang3/builder/HashCodeBuilderTest.java | 8 ++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/builder/HashCodeBuilder.java b/src/main/java/org/apache/commons/lang3/builder/HashCodeBuilder.java index 1c2d722..a1569da 100644 --- a/src/main/java/org/apache/commons/lang3/builder/HashCodeBuilder.java +++ b/src/main/java/org/apache/commons/lang3/builder/HashCodeBuilder.java @@ -20,7 +20,9 @@ package org.apache.commons.lang3.builder; import java.lang.reflect.AccessibleObject; import java.lang.reflect.Field; import java.lang.reflect.Modifier; +import java.util.Arrays; import java.util.Collection; +import java.util.Comparator; import java.util.HashSet; import java.util.Set; @@ -189,6 +191,7 @@ public class HashCodeBuilder implements Builder<Integer> { try { register(object); final Field[] fields = clazz.getDeclaredFields(); + Arrays.sort(fields, Comparator.comparing(Field::getName)); AccessibleObject.setAccessible(fields, true); for (final Field field : fields) { if (!ArrayUtils.contains(excludeFields, field.getName()) diff --git a/src/test/java/org/apache/commons/lang3/builder/HashCodeBuilderTest.java b/src/test/java/org/apache/commons/lang3/builder/HashCodeBuilderTest.java index 148c533..598f899 100644 --- a/src/test/java/org/apache/commons/lang3/builder/HashCodeBuilderTest.java +++ b/src/test/java/org/apache/commons/lang3/builder/HashCodeBuilderTest.java @@ -490,11 +490,11 @@ public class HashCodeBuilderTest { public void testReflectionHashCodeExcludeFields() { final TestObjectWithMultipleFields x = new TestObjectWithMultipleFields(1, 2, 3); - assertEquals(((17 * 37 + 1) * 37 + 2) * 37 + 3, HashCodeBuilder.reflectionHashCode(x)); + assertEquals(((17 * 37 + 1) * 37 + 3) * 37 + 2, HashCodeBuilder.reflectionHashCode(x)); - assertEquals(((17 * 37 + 1) * 37 + 2) * 37 + 3, HashCodeBuilder.reflectionHashCode(x, (String[]) null)); - assertEquals(((17 * 37 + 1) * 37 + 2) * 37 + 3, HashCodeBuilder.reflectionHashCode(x)); - assertEquals(((17 * 37 + 1) * 37 + 2) * 37 + 3, HashCodeBuilder.reflectionHashCode(x, "xxx")); + assertEquals(((17 * 37 + 1) * 37 + 3) * 37 + 2, HashCodeBuilder.reflectionHashCode(x, (String[]) null)); + assertEquals(((17 * 37 + 1) * 37 + 3) * 37 + 2, HashCodeBuilder.reflectionHashCode(x)); + assertEquals(((17 * 37 + 1) * 37 + 3) * 37 + 2, HashCodeBuilder.reflectionHashCode(x, "xxx")); assertEquals((17 * 37 + 1) * 37 + 3, HashCodeBuilder.reflectionHashCode(x, "two")); assertEquals((17 * 37 + 1) * 37 + 2, HashCodeBuilder.reflectionHashCode(x, "three"));