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