Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/langtools/tools/jdeps/modules/GenModuleInfo.java
41149 views
1
/*
2
* Copyright (c) 2016, 2017, 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 8193192
27
* @library ../lib
28
* @build CompilerUtils JdepsUtil JdepsRunner
29
* @modules jdk.jdeps/com.sun.tools.jdeps
30
* @run testng GenModuleInfo
31
* @summary Tests jdeps --generate-module-info option
32
*/
33
34
import java.io.File;
35
import java.io.IOException;
36
import java.io.InputStream;
37
import java.io.UncheckedIOException;
38
import java.lang.module.ModuleDescriptor;
39
40
import java.nio.file.Files;
41
import java.nio.file.Path;
42
import java.nio.file.Paths;
43
44
import java.util.List;
45
import java.util.Set;
46
import java.util.stream.Collectors;
47
import java.util.stream.Stream;
48
49
import org.testng.annotations.BeforeTest;
50
import org.testng.annotations.Test;
51
52
import static org.testng.Assert.assertEquals;
53
import static org.testng.Assert.assertTrue;
54
55
public class GenModuleInfo {
56
private static final String MODULE_INFO = "module-info.class";
57
private static final String TEST_SRC = System.getProperty("test.src");
58
59
private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
60
private static final Path MODS_DIR = Paths.get("mods");
61
private static final Path LIBS_DIR = Paths.get("libs");
62
private static final Path MLIBS_DIR = Paths.get("mlibs");
63
private static final Path DEST_DIR = Paths.get("moduleinfosrc");
64
private static final Path NEW_MODS_DIR = Paths.get("new_mods");
65
66
// the names of the modules in this test
67
public static final String UNSUPPORTED = "unsupported";
68
public static final Set<String> MODULES = Set.of(
69
"mI", "mII", "mIII", "provider", "test", UNSUPPORTED
70
);
71
72
@BeforeTest
73
public void setup() throws Exception {
74
compileAndCreateJars();
75
}
76
77
/**
78
* Compile modules
79
*/
80
public static void compileModules(Path dest) {
81
assertTrue(CompilerUtils.compileModule(SRC_DIR, dest, UNSUPPORTED,
82
"--add-exports", "java.base/jdk.internal.perf=" + UNSUPPORTED));
83
MODULES.stream()
84
.filter(mn -> !mn.equals(UNSUPPORTED))
85
.forEach(mn -> assertTrue(CompilerUtils.compileModule(SRC_DIR, dest, mn)));
86
}
87
88
/**
89
* Create JAR files with no module-info.class
90
*/
91
public static void createModularJARs(Path mods, Path dest, String... modules) throws IOException {
92
Files.createDirectory(dest);
93
// create modular JAR
94
for (String mn : modules) {
95
Path root = mods.resolve(mn);
96
try (Stream<Path> stream = Files.find(root, Integer.MAX_VALUE,
97
(p, attr) -> { return attr.isRegularFile(); })) {
98
JdepsUtil.createJar(dest.resolve(mn + ".jar"), root, stream);
99
}
100
}
101
}
102
103
/**
104
* Create JAR files with no module-info.class
105
*/
106
public static List<Path> createJARFiles(Path mods, Path libs) throws IOException {
107
Files.createDirectory(libs);
108
109
for (String mn : MODULES) {
110
Path root = mods.resolve(mn);
111
Path msrc = SRC_DIR.resolve(mn);
112
Path metaInf = msrc.resolve("META-INF");
113
if (Files.exists(metaInf)) {
114
try (Stream<Path> resources = Files.find(metaInf, Integer.MAX_VALUE,
115
(p, attr) -> { return attr.isRegularFile();})) {
116
resources.forEach(file -> {
117
try {
118
Path path = msrc.relativize(file);
119
Files.createDirectories(root.resolve(path).getParent());
120
Files.copy(file, root.resolve(path));
121
} catch (IOException e) {
122
throw new UncheckedIOException(e);
123
}
124
});
125
}
126
}
127
// copy all entries except module-info.class
128
try (Stream<Path> stream = Files.find(root, Integer.MAX_VALUE,
129
(p, attr) -> { return attr.isRegularFile();})) {
130
Stream<Path> entries = stream.filter(f -> {
131
String fn = f.getFileName().toString();
132
if (fn.endsWith(".class")) {
133
return !fn.equals("module-info.class");
134
} else {
135
return true;
136
}
137
});
138
139
JdepsUtil.createJar(libs.resolve(mn + ".jar"), root, entries);
140
}
141
}
142
143
return MODULES.stream()
144
.map(mn -> LIBS_DIR.resolve(mn + ".jar"))
145
.collect(Collectors.toList());
146
}
147
148
/**
149
* compile the generated module-info.java
150
*/
151
public static void compileNewGenModuleInfo(Path source, Path dest) {
152
153
assertTrue(CompilerUtils.compileModule(source, dest, UNSUPPORTED,
154
"-p", dest.toString(),
155
"--add-exports", "java.base/jdk.internal.perf=" + UNSUPPORTED));
156
157
MODULES.stream()
158
.filter(mn -> !mn.equals(UNSUPPORTED))
159
.forEach(mn -> assertTrue(
160
CompilerUtils.compileModule(source, dest,
161
mn, "-p", dest.toString()))
162
);
163
164
}
165
166
public static void compileAndCreateJars() throws Exception {
167
CompilerUtils.cleanDir(MODS_DIR);
168
CompilerUtils.cleanDir(LIBS_DIR);
169
170
compileModules(MODS_DIR);
171
172
// create modular JARs except test
173
createModularJARs(MODS_DIR, MLIBS_DIR, MODULES.stream().filter(mn -> !mn.equals("test"))
174
.toArray(String[]::new));
175
// create non-modular JARs
176
createJARFiles(MODS_DIR, LIBS_DIR);
177
}
178
179
@Test
180
public void automaticModules() throws IOException {
181
Stream<String> files = MODULES.stream()
182
.map(mn -> LIBS_DIR.resolve(mn + ".jar"))
183
.map(Path::toString);
184
JdepsRunner.run(Stream.concat(Stream.of("-cp"), files).toArray(String[]::new));
185
}
186
187
@Test
188
public void test() throws IOException {
189
Path dest = DEST_DIR.resolve("case1");
190
Path classes = NEW_MODS_DIR.resolve("case1");
191
Files.createDirectories(dest);
192
Files.createDirectories(classes);
193
194
Stream<String> files = MODULES.stream()
195
.map(mn -> LIBS_DIR.resolve(mn + ".jar"))
196
.map(Path::toString);
197
198
Stream<String> options = Stream.concat(
199
Stream.of("--generate-module-info", dest.toString()), files);
200
JdepsRunner.run(options.toArray(String[]::new));
201
202
// check file exists
203
MODULES.stream()
204
.map(mn -> dest.resolve(mn).resolve("module-info.java"))
205
.forEach(f -> assertTrue(Files.exists(f)));
206
207
// copy classes to a temporary directory
208
// and then compile new module-info.java
209
copyClasses(MODS_DIR, classes);
210
compileNewGenModuleInfo(dest, classes);
211
212
for (String mn : MODULES) {
213
verify(mn, classes, MODS_DIR);
214
}
215
}
216
217
@Test
218
public void withModulePath() throws IOException {
219
Path dest = DEST_DIR.resolve("case2");
220
Path classes = NEW_MODS_DIR.resolve("case2");
221
Files.createDirectories(dest);
222
Files.createDirectories(classes);
223
224
JdepsRunner.run("--module-path", MLIBS_DIR.toString(),
225
"--generate-module-info", dest.toString(),
226
LIBS_DIR.resolve("test.jar").toString());
227
228
String name = "test";
229
Path gensrc = dest.resolve(name).resolve("module-info.java");
230
assertTrue(Files.exists(gensrc));
231
232
// copy classes to a temporary directory
233
// and then compile new module-info.java
234
copyClasses(MODS_DIR.resolve(name), classes.resolve(name));
235
assertTrue(CompilerUtils.compileModule(dest, classes, name, "-p", MLIBS_DIR.toString()));
236
237
verify(name, classes, MODS_DIR);
238
}
239
240
/**
241
* Verify the dependences from the given module-info.class files
242
*/
243
public void verify(String mn, Path mdir1, Path mdir2) throws IOException {
244
Path p1 = mdir1.resolve(mn).resolve(MODULE_INFO);
245
Path p2 = mdir2.resolve(mn).resolve(MODULE_INFO);
246
try (InputStream in1 = Files.newInputStream(p1);
247
InputStream in2 = Files.newInputStream(p2)) {
248
verify(ModuleDescriptor.read(in1),
249
ModuleDescriptor.read(in2, () -> packages(mdir2.resolve(mn))));
250
}
251
}
252
253
/**
254
* Copy classes except the module-info.class to the destination directory
255
*/
256
public static void copyClasses(Path from, Path dest) throws IOException {
257
try (Stream<Path> stream = Files.walk(from, Integer.MAX_VALUE)) {
258
stream.filter(path -> !path.getFileName().toString().equals(MODULE_INFO) &&
259
path.getFileName().toString().endsWith(".class"))
260
.map(path -> from.relativize(path))
261
.forEach(path -> {
262
try {
263
Path newFile = dest.resolve(path);
264
Files.createDirectories(newFile.getParent());
265
Files.copy(from.resolve(path), newFile);
266
} catch (IOException e) {
267
throw new UncheckedIOException(e);
268
}
269
});
270
}
271
}
272
273
/**
274
* Verify the generated module-info.java is equivalent to module-info.class
275
* compiled from source.
276
*/
277
private void verify(ModuleDescriptor md1, ModuleDescriptor md2) {
278
System.out.println("verifying: " + md1.name());
279
assertEquals(md1.name(), md2.name());
280
assertEquals(md1.requires(), md2.requires());
281
// all packages are exported
282
assertEquals(md1.exports().stream()
283
.map(ModuleDescriptor.Exports::source)
284
.collect(Collectors.toSet()), md2.packages());
285
if (!md1.opens().isEmpty()) {
286
throw new RuntimeException("unexpected opens: " +
287
md1.opens().stream()
288
.map(o -> o.toString())
289
.collect(Collectors.joining(",")));
290
}
291
292
assertEquals(md1.provides(), md2.provides());
293
}
294
295
private Set<String> packages(Path dir) {
296
try (Stream<Path> stream = Files.find(dir, Integer.MAX_VALUE,
297
((path, attrs) -> attrs.isRegularFile() &&
298
path.toString().endsWith(".class")))) {
299
return stream.map(path -> toPackageName(dir.relativize(path)))
300
.filter(pkg -> pkg.length() > 0) // module-info
301
.distinct()
302
.collect(Collectors.toSet());
303
} catch (IOException x) {
304
throw new UncheckedIOException(x);
305
}
306
}
307
308
private String toPackageName(Path path) {
309
String name = path.toString();
310
assert name.endsWith(".class");
311
int index = name.lastIndexOf(File.separatorChar);
312
if (index != -1) {
313
return name.substring(0, index).replace(File.separatorChar, '.');
314
} else {
315
return "";
316
}
317
}
318
319
}
320
321