1 /* |
|
2 * Copyright (c) 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 package org.graalvm.compiler.test; |
|
26 |
|
27 import java.io.IOException; |
|
28 import java.lang.module.ModuleDescriptor.Requires; |
|
29 import java.net.URI; |
|
30 import java.nio.file.FileSystem; |
|
31 import java.nio.file.FileSystems; |
|
32 import java.nio.file.Files; |
|
33 import java.nio.file.Path; |
|
34 import java.util.ArrayList; |
|
35 import java.util.Collections; |
|
36 import java.util.HashSet; |
|
37 import java.util.List; |
|
38 import java.util.Set; |
|
39 |
|
40 import org.graalvm.compiler.debug.DebugOptions; |
|
41 |
|
42 import jdk.internal.module.Modules; |
|
43 |
|
44 public class ModuleSupport { |
|
45 |
|
46 public static void exportPackageTo(Class<?> moduleMember, String packageName, Class<?> requestor) { |
|
47 Module moduleToExport = moduleMember.getModule(); |
|
48 Module requestorModule = requestor.getModule(); |
|
49 if (moduleToExport != requestorModule) { |
|
50 Modules.addExports(moduleToExport, packageName, requestorModule); |
|
51 } |
|
52 } |
|
53 |
|
54 public static void exportAllPackagesTo(Class<?> moduleMember, Class<?> requestor) { |
|
55 Module moduleToExport = moduleMember.getModule(); |
|
56 Module requestorModule = requestor.getModule(); |
|
57 if (moduleToExport != requestorModule) { |
|
58 for (String packageName : moduleToExport.getPackages()) { |
|
59 Modules.addExports(moduleToExport, packageName, requestorModule); |
|
60 } |
|
61 } |
|
62 } |
|
63 |
|
64 public static void exportAllPackagesTo(Class<?> moduleMember, ClassLoader cl) { |
|
65 Module moduleToExport = moduleMember.getModule(); |
|
66 Module unnamedModule = cl.getUnnamedModule(); |
|
67 for (String packageName : moduleToExport.getPackages()) { |
|
68 Modules.addExports(moduleToExport, packageName, unnamedModule); |
|
69 } |
|
70 } |
|
71 |
|
72 @SuppressWarnings("unused") |
|
73 public static void exportAndOpenAllPackagesToUnnamed(String name) { |
|
74 Module module = ModuleLayer.boot().findModule(name).orElseThrow(); |
|
75 Set<String> packages = module.getPackages(); |
|
76 for (String pkg : packages) { |
|
77 Modules.addExportsToAllUnnamed(module, pkg); |
|
78 Modules.addOpensToAllUnnamed(module, pkg); |
|
79 } |
|
80 } |
|
81 |
|
82 public static List<String> getJRTGraalClassNames() throws IOException { |
|
83 List<String> classNames = new ArrayList<>(); |
|
84 FileSystem fs = FileSystems.newFileSystem(URI.create("jrt:/"), Collections.emptyMap()); |
|
85 Module graalModule = DebugOptions.class.getModule(); |
|
86 Set<String> graalModuleSet = new HashSet<>(); |
|
87 graalModuleSet.add(graalModule.getName()); |
|
88 for (Module module : graalModule.getLayer().modules()) { |
|
89 if (requires(module, graalModule)) { |
|
90 graalModuleSet.add(module.getName()); |
|
91 } |
|
92 } |
|
93 |
|
94 Path top = fs.getPath("/modules/"); |
|
95 Files.find(top, Integer.MAX_VALUE, |
|
96 (path, attrs) -> attrs.isRegularFile()).forEach(p -> { |
|
97 int nameCount = p.getNameCount(); |
|
98 if (nameCount > 2) { |
|
99 String base = p.getName(nameCount - 1).toString(); |
|
100 if (base.endsWith(".class") && !base.equals("module-info.class")) { |
|
101 String module = p.getName(1).toString(); |
|
102 if (graalModuleSet.contains(module)) { |
|
103 // Strip module prefix and convert to dotted |
|
104 // form |
|
105 String className = p.subpath(2, nameCount).toString().replace('/', '.'); |
|
106 // Strip ".class" suffix |
|
107 className = className.replace('/', '.').substring(0, className.length() - ".class".length()); |
|
108 classNames.add(className); |
|
109 } |
|
110 } |
|
111 } |
|
112 }); |
|
113 return classNames; |
|
114 } |
|
115 |
|
116 private static boolean requires(Module module, Module graalModule) { |
|
117 ModuleLayer graalLayer = graalModule.getLayer(); |
|
118 for (Requires r : module.getDescriptor().requires()) { |
|
119 if (r.name().equals(graalModule.getName())) { |
|
120 return true; |
|
121 } |
|
122 Module dep = graalLayer.findModule(r.name()).get(); |
|
123 if (requires(dep, graalModule)) { |
|
124 return true; |
|
125 } |
|
126 } |
|
127 return false; |
|
128 } |
|
129 } |
|