Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/ModuleLayer/automatic/AutomaticModulesTest.java
41153 views
1
/*
2
* Copyright (c) 2018, 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
/**
25
* @test
26
* @bug 8211825
27
* @modules jdk.compiler
28
* @library /test/lib
29
* @build jdk.test.lib.compiler.CompilerUtils jdk.test.lib.util.JarUtils
30
* @run testng/othervm AutomaticModulesTest
31
* @summary Tests automatic modules in module layers
32
*/
33
34
import java.nio.file.Files;
35
import java.nio.file.Path;
36
import java.lang.ModuleLayer.Controller;
37
import java.lang.module.*;
38
import java.lang.reflect.Method;
39
import java.util.List;
40
import java.util.Set;
41
42
import jdk.test.lib.compiler.CompilerUtils;
43
import jdk.test.lib.util.JarUtils;
44
45
import org.testng.annotations.BeforeTest;
46
import org.testng.annotations.Test;
47
import static org.testng.Assert.*;
48
49
/**
50
* This test uses two modules:
51
* m requires alib and has an entry point p.Main
52
* alib is an automatic module
53
*/
54
55
@Test
56
public class AutomaticModulesTest {
57
58
private static final String TEST_SRC = System.getProperty("test.src");
59
60
private static final Path CLASSES = Path.of("classes");
61
private static final Path LIB = Path.of("lib");
62
private static final Path MODS = Path.of("mods");
63
64
@BeforeTest
65
public void setup() throws Exception {
66
// javac -d classes src/alib/**
67
// jar cf lib/alib.jar -C classes .
68
Files.createDirectory(CLASSES);
69
assertTrue(CompilerUtils.compile(Path.of(TEST_SRC, "src", "alib"), CLASSES));
70
JarUtils.createJarFile(LIB.resolve("alib.jar"), CLASSES);
71
72
// javac -p lib -d mods/m - src/m/**
73
Path src = Path.of(TEST_SRC, "src", "m");
74
Path output = Files.createDirectories(MODS.resolve("m"));
75
assertTrue(CompilerUtils.compile(src, output, "-p", LIB.toString()));
76
}
77
78
/**
79
* Create a module layer with modules m and alib mapped to the same class
80
* loader.
81
*/
82
public void testOneLoader() throws Exception {
83
Configuration cf = ModuleLayer.boot()
84
.configuration()
85
.resolve(ModuleFinder.of(), ModuleFinder.of(MODS, LIB), Set.of("m"));
86
ResolvedModule m = cf.findModule("m").orElseThrow();
87
ResolvedModule alib = cf.findModule("alib").orElseThrow();
88
assertTrue(m.reads().contains(alib));
89
assertTrue(alib.reference().descriptor().isAutomatic());
90
ModuleLayer bootLayer = ModuleLayer.boot();
91
ClassLoader scl = ClassLoader.getSystemClassLoader();
92
Controller controller = ModuleLayer.defineModulesWithOneLoader(cf, List.of(bootLayer), scl);
93
invokeMain(controller, "m/p.Main");
94
}
95
96
/**
97
* Create a module layer with modules m and alib mapped to different class
98
* loaders. This will test that L(m) delegates to L(alib) in the same layer.
99
*/
100
public void testManyLoaders() throws Exception {
101
Configuration cf = ModuleLayer.boot()
102
.configuration()
103
.resolve(ModuleFinder.of(), ModuleFinder.of(MODS, LIB), Set.of("m"));
104
ResolvedModule m = cf.findModule("m").orElseThrow();
105
ResolvedModule alib = cf.findModule("alib").orElseThrow();
106
assertTrue(m.reads().contains(alib));
107
assertTrue(alib.reference().descriptor().isAutomatic());
108
ModuleLayer bootLayer = ModuleLayer.boot();
109
ClassLoader scl = ClassLoader.getSystemClassLoader();
110
Controller controller = ModuleLayer.defineModulesWithManyLoaders(cf, List.of(bootLayer), scl);
111
invokeMain(controller, "m/p.Main");
112
}
113
114
/**
115
* Create a module layer with alib and another module layer with m.
116
* This will test that L(m) delegates to L(alib) in a parent layer.
117
*/
118
public void testAutomaticModuleInParent() throws Exception {
119
ModuleLayer bootLayer = ModuleLayer.boot();
120
ClassLoader scl = ClassLoader.getSystemClassLoader();
121
122
// configuration/layer containing alib
123
Configuration cf1 = bootLayer
124
.configuration()
125
.resolve(ModuleFinder.of(), ModuleFinder.of(LIB), Set.of("alib"));
126
ModuleLayer layer1 = bootLayer.defineModulesWithOneLoader(cf1, scl);
127
128
// configuration/layer containing m
129
Configuration cf2 = cf1.resolve(ModuleFinder.of(), ModuleFinder.of(MODS), Set.of("m"));
130
Controller controller = ModuleLayer.defineModulesWithOneLoader(cf2, List.of(layer1), scl);
131
132
invokeMain(controller, "m/p.Main");
133
}
134
135
/**
136
* Invokes the main method of the given entry point (module-name/class-name)
137
*/
138
private void invokeMain(Controller controller, String entry) throws Exception {
139
String[] s = entry.split("/");
140
String moduleName = s[0];
141
String className = s[1];
142
int pos = className.lastIndexOf('.');
143
String packageName = className.substring(0, pos);
144
ModuleLayer layer = controller.layer();
145
Module module = layer.findModule(moduleName).orElseThrow();
146
controller.addExports(module, packageName, this.getClass().getModule());
147
ClassLoader loader = layer.findLoader(moduleName);
148
Class<?> c = loader.loadClass(className);
149
Method m = c.getMethod("main", String[].class);
150
m.invoke(null, (Object)new String[0]);
151
}
152
}
153
154