8275509: ModuleDescriptor.hashCode isn't reproducible across builds

Reviewed-by: alanb, ihse
This commit is contained in:
Jaikiran Pai 2021-11-05 03:44:45 +00:00
parent 9ad4d3d06b
commit 396132ff1e
2 changed files with 92 additions and 4 deletions

View file

@ -327,7 +327,7 @@ public class ModuleDescriptor
*/
@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)
@ -505,7 +505,7 @@ public class ModuleDescriptor
*/
@Override
public int hashCode() {
int hash = mods.hashCode();
int hash = modsHashCode(mods);
hash = hash * 43 + source.hashCode();
return hash * 43 + targets.hashCode();
}
@ -708,7 +708,7 @@ public class ModuleDescriptor
*/
@Override
public int hashCode() {
int hash = mods.hashCode();
int hash = modsHashCode(mods);
hash = hash * 43 + source.hashCode();
return hash * 43 + targets.hashCode();
}
@ -2261,7 +2261,7 @@ public class ModuleDescriptor
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();
@ -2546,6 +2546,18 @@ public class ModuleDescriptor
.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 = h * 43 + Objects.hashCode(e.name());
}
return h;
}
private static <T extends Object & Comparable<? super T>>
int compare(T obj1, T obj2) {
if (obj1 != null) {