Skip to content

Commit d383ba5

Browse files
committedDec 2, 2024
8322809: SystemModulesMap::classNames and moduleNames arrays do not match the order
Reviewed-by: sgehwolf, mbaesken Backport-of: f3be138eb80c9e7f6cc21afb75cda9e49b667c8a
1 parent e569997 commit d383ba5

File tree

6 files changed

+312
-5
lines changed

6 files changed

+312
-5
lines changed
 

‎src/jdk.jlink/share/classes/jdk/tools/jlink/internal/plugins/SystemModulesPlugin.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -1811,6 +1811,9 @@ private String genSystemModulesMapClass(ClassDesc allSystemModules,
18111811

18121812
// write the class file to the pool as a resource
18131813
String rn = "/java.base/" + SYSTEM_MODULES_MAP_CLASSNAME + ".class";
1814+
// sort the map of module name to the class name of the generated SystemModules class
1815+
List<Map.Entry<String, String>> systemModulesMap = map.entrySet()
1816+
.stream().sorted(Map.Entry.comparingByKey()).toList();
18141817
ResourcePoolEntry e = ResourcePoolEntry.create(rn, Classfile.build(
18151818
CD_SYSTEM_MODULES_MAP,
18161819
clb -> clb.withFlags(ACC_FINAL + ACC_SUPER)
@@ -1861,10 +1864,10 @@ private String genSystemModulesMapClass(ClassDesc allSystemModules,
18611864
cob.anewarray(CD_String);
18621865

18631866
int index = 0;
1864-
for (String moduleName : sorted(map.keySet())) {
1867+
for (Map.Entry<String,String> entry : systemModulesMap) {
18651868
cob.dup() // arrayref
18661869
.constantInstruction(index)
1867-
.constantInstruction(moduleName)
1870+
.constantInstruction(entry.getKey())
18681871
.aastore();
18691872
index++;
18701873
}
@@ -1882,10 +1885,10 @@ private String genSystemModulesMapClass(ClassDesc allSystemModules,
18821885
.anewarray(CD_String);
18831886

18841887
int index = 0;
1885-
for (String className : sorted(map.values())) {
1888+
for (Map.Entry<String,String> entry : systemModulesMap) {
18861889
cob.dup() // arrayref
18871890
.constantInstruction(index)
1888-
.constantInstruction(className.replace('/', '.'))
1891+
.constantInstruction(entry.getValue().replace('/', '.'))
18891892
.aastore();
18901893
index++;
18911894
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*
2+
* Copyright (c) 2024, 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 java.nio.file.Files;
25+
import java.nio.file.Path;
26+
import java.nio.file.Paths;
27+
import java.util.Arrays;
28+
import java.util.spi.ToolProvider;
29+
import java.util.stream.Collectors;
30+
import java.util.stream.Stream;
31+
32+
import jdk.test.lib.compiler.CompilerUtils;
33+
import jdk.test.lib.util.FileUtils;
34+
35+
import static jdk.test.lib.process.ProcessTools.*;
36+
37+
import org.junit.jupiter.api.Test;
38+
import org.junit.jupiter.api.BeforeAll;
39+
import static org.junit.jupiter.api.Assertions.*;
40+
41+
/**
42+
* @test
43+
* @bug 8322809
44+
* @library /test/lib
45+
* @modules jdk.compiler jdk.jlink
46+
* @build jdk.test.lib.compiler.CompilerUtils
47+
* jdk.test.lib.process.ProcessTools
48+
* jdk.test.lib.util.FileUtils
49+
* ModuleMainClassTest
50+
* @run junit ModuleMainClassTest
51+
*/
52+
53+
public class ModuleMainClassTest {
54+
private static final String JAVA_HOME = System.getProperty("java.home");
55+
private static final String TEST_SRC = System.getProperty("test.src");
56+
57+
private static final Path SRC_DIR = Path.of(TEST_SRC, "src");
58+
private static final Path MODS_DIR = Path.of("mods");
59+
private static final Path JMODS_DIR = Path.of("jmods");
60+
61+
private static final Path IMAGE = Path.of("image");
62+
63+
// the module names are sorted by the plugin and so these names cover
64+
// the cases that are before and after the elements of `jdk.*` modules
65+
// with main classes
66+
private static String[] modules = new String[] {"com.foo", "net.foo"};
67+
68+
private static boolean hasJmods() {
69+
if (!Files.exists(Paths.get(JAVA_HOME, "jmods"))) {
70+
System.err.println("Test skipped. NO jmods directory");
71+
return false;
72+
}
73+
return true;
74+
}
75+
76+
@BeforeAll
77+
public static void compileAll() throws Throwable {
78+
if (!hasJmods()) return;
79+
80+
for (String mn : modules) {
81+
Path msrc = SRC_DIR.resolve(mn);
82+
assertTrue(CompilerUtils.compile(msrc, MODS_DIR,
83+
"--module-source-path", SRC_DIR.toString(),
84+
"--add-exports", "java.base/jdk.internal.module=" + mn));
85+
}
86+
87+
if (Files.exists(IMAGE)) {
88+
FileUtils.deleteFileTreeUnchecked(IMAGE);
89+
}
90+
91+
// create JMOD files
92+
Files.createDirectories(JMODS_DIR);
93+
Stream.of(modules).forEach(mn ->
94+
assertTrue(jmod("create",
95+
"--class-path", MODS_DIR.resolve(mn).toString(),
96+
"--main-class", mn + ".Main",
97+
JMODS_DIR.resolve(mn + ".jmod").toString()) == 0)
98+
);
99+
100+
// the run-time image created will have 4 modules with main classes
101+
createImage(IMAGE, "com.foo");
102+
}
103+
104+
@Test
105+
public void testComFoo() throws Exception {
106+
if (!hasJmods()) return;
107+
108+
Path java = IMAGE.resolve("bin").resolve("java");
109+
assertTrue(executeProcess(java.toString(),
110+
"-m", "com.foo")
111+
.outputTo(System.out)
112+
.errorTo(System.out)
113+
.getExitValue() == 0);
114+
}
115+
116+
@Test
117+
public void testNetFoo() throws Exception {
118+
if (!hasJmods()) return;
119+
120+
Path java = IMAGE.resolve("bin").resolve("java");
121+
assertTrue(executeProcess(java.toString(),
122+
"-m", "net.foo")
123+
.outputTo(System.out)
124+
.errorTo(System.out)
125+
.getExitValue() == 0);
126+
}
127+
128+
static final ToolProvider JLINK_TOOL = ToolProvider.findFirst("jlink")
129+
.orElseThrow(() -> new RuntimeException("jlink tool not found"));
130+
131+
static final ToolProvider JMOD_TOOL = ToolProvider.findFirst("jmod")
132+
.orElseThrow(() -> new RuntimeException("jmod tool not found"));
133+
134+
private static void createImage(Path outputDir, String... modules) throws Throwable {
135+
assertTrue(JLINK_TOOL.run(System.out, System.out,
136+
"--output", outputDir.toString(),
137+
"--add-modules", Arrays.stream(modules).collect(Collectors.joining(",")),
138+
"--module-path", JMODS_DIR.toString()) == 0);
139+
}
140+
141+
private static int jmod(String... options) {
142+
System.out.println("jmod " + Arrays.asList(options));
143+
return JMOD_TOOL.run(System.out, System.out, options);
144+
}
145+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (c) 2024, 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+
package com.foo;
25+
26+
import java.lang.module.ModuleDescriptor;
27+
import java.lang.module.ModuleFinder;
28+
import java.util.stream.Stream;
29+
30+
/**
31+
* Sanity test if SystemModules pre-resolved at link-time for com.foo
32+
* with main class is loaded properly.
33+
*/
34+
public class Main {
35+
public static void main(String... args) throws Exception {
36+
ModuleDescriptor md = Main.class.getModule().getDescriptor();
37+
System.out.println(md);
38+
39+
checkMainClass("com.foo", "com.foo.Main");
40+
checkMainClass("net.foo", "net.foo.Main");
41+
Stream.of("jdk.httpserver").forEach(mn ->
42+
ModuleFinder.ofSystem().find(mn).get().descriptor().mainClass()
43+
.orElseThrow(() -> new RuntimeException(mn + " no main class"))
44+
);
45+
}
46+
47+
static void checkMainClass(String mn, String mainClass) {
48+
String cn = ModuleFinder.ofSystem().find(mn).get().descriptor().mainClass().get();
49+
if (!cn.equals(mainClass)) {
50+
throw new RuntimeException("Mismatched main class of module " + mn + ": " + cn + " expected: " + mainClass);
51+
}
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (c) 2024, 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+
module com.foo {
25+
requires jdk.httpserver;
26+
requires net.foo;
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright (c) 2024, 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+
module net.foo {
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) 2024, 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+
package net.foo;
25+
26+
import java.lang.module.ModuleDescriptor;
27+
import java.lang.module.ModuleFinder;
28+
import java.util.stream.Stream;
29+
30+
/**
31+
* Sanity test if SystemModules pre-resolved at link-time for net.foo
32+
* with main class is loaded properly.
33+
*/
34+
public class Main {
35+
public static void main(String... args) throws Exception {
36+
ModuleDescriptor md = Main.class.getModule().getDescriptor();
37+
System.out.println(md);
38+
39+
checkMainClass("com.foo", "com.foo.Main");
40+
checkMainClass("net.foo", "net.foo.Main");
41+
Stream.of("jdk.httpserver").forEach(mn ->
42+
ModuleFinder.ofSystem().find(mn).get().descriptor().mainClass()
43+
.orElseThrow(() -> new RuntimeException(mn + " no main class"))
44+
);
45+
}
46+
47+
static void checkMainClass(String mn, String mainClass) {
48+
String cn = ModuleFinder.ofSystem().find(mn).get().descriptor().mainClass().get();
49+
if (!cn.equals(mainClass)) {
50+
throw new RuntimeException("Mismatched main class of module " + mn + ": " + cn + " expected: " + mainClass);
51+
}
52+
}
53+
54+
}

1 commit comments

Comments
 (1)

openjdk-notifier[bot] commented on Dec 2, 2024

@openjdk-notifier[bot]
Please sign in to comment.