Skip to content

Commit b9b17af

Browse files
committed
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 <[email protected]>
1 parent bf3d2d7 commit b9b17af

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/java.base/share/classes/java/lang/module/ModuleDescriptor.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ public boolean equals(Object ob) {
327327
*/
328328
@Override
329329
public int hashCode() {
330-
int hash = name.hashCode() * 43 + mods.hashCode();
330+
int hash = name.hashCode() * 43 + modsHashCode(mods);
331331
if (compiledVersion != null)
332332
hash = hash * 43 + compiledVersion.hashCode();
333333
if (rawCompiledVersion != null)
@@ -505,7 +505,7 @@ public int compareTo(Exports that) {
505505
*/
506506
@Override
507507
public int hashCode() {
508-
int hash = mods.hashCode();
508+
int hash = modsHashCode(mods);
509509
hash = hash * 43 + source.hashCode();
510510
return hash * 43 + targets.hashCode();
511511
}
@@ -708,7 +708,7 @@ public int compareTo(Opens that) {
708708
*/
709709
@Override
710710
public int hashCode() {
711-
int hash = mods.hashCode();
711+
int hash = modsHashCode(mods);
712712
hash = hash * 43 + source.hashCode();
713713
return hash * 43 + targets.hashCode();
714714
}
@@ -2261,7 +2261,7 @@ public int hashCode() {
22612261
int hc = hash;
22622262
if (hc == 0) {
22632263
hc = name.hashCode();
2264-
hc = hc * 43 + Objects.hashCode(modifiers);
2264+
hc = hc * 43 + modsHashCode(modifiers);
22652265
hc = hc * 43 + requires.hashCode();
22662266
hc = hc * 43 + Objects.hashCode(packages);
22672267
hc = hc * 43 + exports.hashCode();
@@ -2546,6 +2546,18 @@ private static <M> String toString(Set<M> mods, String what) {
25462546
.collect(Collectors.joining(" "));
25472547
}
25482548

2549+
/**
2550+
* Generates and returns a hashcode for the enum instances. The returned hashcode
2551+
* is a value based on the {@link Enum#name() name} of each enum instance.
2552+
*/
2553+
private static int modsHashCode(Iterable<? extends Enum<?>> enums) {
2554+
int h = 0;
2555+
for (Enum<?> e : enums) {
2556+
h += e.name().hashCode();
2557+
}
2558+
return h;
2559+
}
2560+
25492561
private static <T extends Object & Comparable<? super T>>
25502562
int compare(T obj1, T obj2) {
25512563
if (obj1 != null) {

0 commit comments

Comments
 (0)