Skip to content

Commit

Permalink
Back-port fix for ModuleDescriptor.hashCode()
Browse files Browse the repository at this point in the history
* 8275509: ModuleDescriptor.hashCode isn't reproducible across builds
* 8290041: ModuleDescriptor.hashCode is inconsistent

Signed-off-by: Keith W. Campbell <keithc@ca.ibm.com>
  • Loading branch information
keithc-ca committed Jul 26, 2023
1 parent 7ef5802 commit 317681b
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/java.base/share/classes/java/lang/module/ModuleDescriptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -2568,6 +2568,18 @@ private static <M> String toString(Set<M> 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<? extends Enum<?>> enums) {
int h = 0;
for (Enum<?> e : enums) {
h += e.name().hashCode();
}
return h;
}

private static <T extends Object & Comparable<? super T>>
int compare(T obj1, T obj2) {
if (obj1 != null) {
Expand Down

0 comments on commit 317681b

Please sign in to comment.