From 317681b5638045e66f56307b55e10717bd889908 Mon Sep 17 00:00:00 2001 From: "Keith W. Campbell" Date: Tue, 25 Jul 2023 16:05:34 -0400 Subject: [PATCH] Back-port fix for ModuleDescriptor.hashCode() * 8275509: ModuleDescriptor.hashCode isn't reproducible across builds * 8290041: ModuleDescriptor.hashCode is inconsistent Signed-off-by: Keith W. Campbell --- .../java/lang/module/ModuleDescriptor.java | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/java.base/share/classes/java/lang/module/ModuleDescriptor.java b/src/java.base/share/classes/java/lang/module/ModuleDescriptor.java index fa359348f6c..f9ebfe1559d 100644 --- a/src/java.base/share/classes/java/lang/module/ModuleDescriptor.java +++ b/src/java.base/share/classes/java/lang/module/ModuleDescriptor.java @@ -333,7 +333,7 @@ public boolean equals(Object ob) { */ @Override public int hashCode() { - int hash = name.hashCode() * 43 + mods.hashCode(); + int hash = name.hashCode() * 43 + modsHashCode(mods); if (compiledVersion != null) hash = hash * 43 + compiledVersion.hashCode(); if (rawCompiledVersion != null) @@ -514,7 +514,7 @@ public int compareTo(Exports that) { */ @Override public int hashCode() { - int hash = mods.hashCode(); + int hash = modsHashCode(mods); hash = hash * 43 + source.hashCode(); return hash * 43 + targets.hashCode(); } @@ -721,7 +721,7 @@ public int compareTo(Opens that) { */ @Override public int hashCode() { - int hash = mods.hashCode(); + int hash = modsHashCode(mods); hash = hash * 43 + source.hashCode(); return hash * 43 + targets.hashCode(); } @@ -2283,7 +2283,7 @@ public int hashCode() { int hc = hash; if (hc == 0) { hc = name.hashCode(); - hc = hc * 43 + Objects.hashCode(modifiers); + hc = hc * 43 + modsHashCode(modifiers); hc = hc * 43 + requires.hashCode(); hc = hc * 43 + Objects.hashCode(packages); hc = hc * 43 + exports.hashCode(); @@ -2568,6 +2568,18 @@ private static String toString(Set mods, String what) { .collect(Collectors.joining(" ")); } + /** + * Generates and returns a hashcode for the enum instances. The returned hashcode + * is a value based on the {@link Enum#name() name} of each enum instance. + */ + private static int modsHashCode(Iterable> enums) { + int h = 0; + for (Enum e : enums) { + h += e.name().hashCode(); + } + return h; + } + private static > int compare(T obj1, T obj2) { if (obj1 != null) {