Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/java/lang/module/customfs/ModulesInCustomFileSystem.java
66645 views
1
/*
2
* Copyright (c) 2017, 2022, 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 8178380 8282444
27
* @modules jdk.zipfs
28
* @library /test/lib
29
* @build ModulesInCustomFileSystem m1/* m2/*
30
* jdk.test.lib.util.JarUtils
31
* @run testng/othervm ModulesInCustomFileSystem
32
* @summary Test ModuleFinder to find modules in a custom file system
33
*/
34
35
import java.io.File;
36
import java.lang.module.Configuration;
37
import java.lang.module.ModuleFinder;
38
import java.lang.module.ModuleReader;
39
import java.lang.module.ModuleReference;
40
import java.lang.reflect.Method;
41
import java.nio.file.FileSystem;
42
import java.nio.file.FileSystems;
43
import java.nio.file.Files;
44
import java.nio.file.Path;
45
import java.nio.file.Paths;
46
import java.util.Set;
47
48
import jdk.test.lib.util.JarUtils;
49
50
import org.testng.annotations.Test;
51
import static org.testng.Assert.*;
52
53
@Test
54
public class ModulesInCustomFileSystem {
55
private static final Path HERE = Paths.get("");
56
57
/**
58
* Test exploded modules in a Zip file system.
59
*/
60
public void testExplodedModulesInZipFileSystem() throws Exception {
61
Path m1 = findModuleDirectory("m1");
62
Path m2 = findModuleDirectory("m2");
63
Path mlib = m1.getParent();
64
assertEquals(mlib, m2.getParent());
65
66
// create JAR file containing m1/** and m2/**
67
Path jar = Files.createTempDirectory(HERE, "mlib").resolve("modules.jar");
68
JarUtils.createJarFile(jar, mlib);
69
testZipFileSystem(jar);
70
}
71
72
/**
73
* Test modular JARs in a Zip file system.
74
*/
75
public void testModularJARsInZipFileSystem() throws Exception {
76
Path m1 = findModuleDirectory("m1");
77
Path m2 = findModuleDirectory("m2");
78
Path contents = Files.createTempDirectory(HERE, "contents");
79
JarUtils.createJarFile(contents.resolve("m1.jar"), m1);
80
JarUtils.createJarFile(contents.resolve("m2.jar"), m2);
81
82
// create JAR file containing m1.jar and m2.jar
83
Path jar = Files.createTempDirectory(HERE, "mlib").resolve("modules.jar");
84
JarUtils.createJarFile(jar, contents);
85
testZipFileSystem(jar);
86
}
87
88
/**
89
* Opens a JAR file as a file system
90
*/
91
private void testZipFileSystem(Path zip) throws Exception {
92
try (FileSystem fs = FileSystems.newFileSystem(zip)) {
93
// ModuleFinder to find modules in top-level directory
94
Path top = fs.getPath("/");
95
ModuleFinder finder = ModuleFinder.of(top);
96
97
// list the modules
98
listAllModules(finder);
99
100
// load modules into child layer, invoking m1/p.Main
101
loadAndRunModule(finder);
102
}
103
}
104
105
/**
106
* List all modules that the finder finds and the resources in the module.
107
*/
108
private void listAllModules(ModuleFinder finder) throws Exception {
109
for (ModuleReference mref : finder.findAll()) {
110
System.out.println(mref.descriptor());
111
try (ModuleReader reader = mref.open()) {
112
reader.list().forEach(name -> System.out.format(" %s%n", name));
113
}
114
}
115
}
116
117
/**
118
* Creates a child layer with m1 and m2, invokes m1/p.Main to ensure that
119
* classes can be loaded.
120
*/
121
private void loadAndRunModule(ModuleFinder finder) throws Exception {
122
ModuleLayer bootLayer = ModuleLayer.boot();
123
Configuration cf = bootLayer.configuration()
124
.resolve(finder, ModuleFinder.of(), Set.of("m1"));
125
ClassLoader scl = ClassLoader.getSystemClassLoader();
126
ModuleLayer layer = bootLayer.defineModulesWithOneLoader(cf, scl);
127
Class<?> c = layer.findLoader("m1").loadClass("p.Main");
128
Method m = c.getMethod("main", String[].class);
129
m.invoke(null, (Object)new String[0]);
130
}
131
132
/**
133
* Find the directory for a module on the module path
134
*/
135
private Path findModuleDirectory(String name) {
136
String mp = System.getProperty("jdk.module.path");
137
for (String element : mp.split(File.pathSeparator)) {
138
Path dir = Paths.get(element).resolve(name);
139
if (Files.exists(dir)) {
140
return dir;
141
}
142
}
143
assertFalse(true);
144
return null;
145
}
146
}
147
148