Skip to content

Commit cd52f99

Browse files
Sonia Zaldana CallesAndrew Haley
authored andcommitted
8275509: ModuleDescriptor.hashCode isn't reproducible across builds
Backport-of: 396132f
1 parent 410b893 commit cd52f99

File tree

2 files changed

+92
-4
lines changed

2 files changed

+92
-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 = h * 43 + Objects.hashCode(e.name());
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) {
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import org.testng.annotations.Test;
25+
26+
import java.io.IOException;
27+
import java.io.InputStream;
28+
import java.lang.module.ModuleDescriptor;
29+
import java.util.Set;
30+
31+
import static org.testng.Assert.assertEquals;
32+
import static org.testng.Assert.assertNotSame;
33+
34+
/**
35+
* @test
36+
* @bug 8275509
37+
* @run testng ModuleDescriptorHashCodeTest
38+
* @run testng/othervm -Xshare:off ModuleDescriptorHashCodeTest
39+
* @summary Tests the ModuleDescriptor.hashCode() for boot layer modules
40+
*/
41+
public class ModuleDescriptorHashCodeTest {
42+
43+
/**
44+
* Verifies that the ModuleDescriptor.hashCode() returned by a boot layer module is
45+
* the same as that returned by a ModuleDescriptor constructed from the ModuleDescriptor.Builder
46+
* for the same module.
47+
*/
48+
@Test
49+
public void testBootModuleDescriptor() throws Exception {
50+
Set<Module> bootModules = ModuleLayer.boot().modules();
51+
for (Module bootModule : bootModules) {
52+
System.out.println("Testing module descriptor of boot module " + bootModule);
53+
ModuleDescriptor bootMD = bootModule.getDescriptor();
54+
ModuleDescriptor mdFromBuilder = fromModuleInfoClass(bootModule);
55+
// verify that this object is indeed a different object instance than the boot module descriptor
56+
// to prevent any artificial passing of the test
57+
assertNotSame(mdFromBuilder, bootMD, "ModuleDescriptor loaded from boot layer and " +
58+
"one created from module-info.class unexpectedly returned the same instance");
59+
assertEquals(mdFromBuilder.hashCode(), bootMD.hashCode(),
60+
"Unexpected ModuleDescriptor.hashCode() for " + mdFromBuilder);
61+
assertEquals(mdFromBuilder.compareTo(bootMD), 0,
62+
"Unexpected ModuleDescriptor.compareTo() for " + mdFromBuilder);
63+
}
64+
}
65+
66+
// Returns a ModuleDescriptor parsed out of the module-info.class of the passed Module
67+
private static ModuleDescriptor fromModuleInfoClass(Module module) throws IOException {
68+
try (InputStream moduleInfo = module.getResourceAsStream("module-info.class")) {
69+
if (moduleInfo == null) {
70+
throw new RuntimeException("Could not locate module-info.class in " + module);
71+
}
72+
// internally calls ModuleDescriptor.Builder
73+
return ModuleDescriptor.read(moduleInfo);
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)