author | serb |
Sat, 09 Jun 2018 13:33:35 -0700 | |
changeset 50647 | a98ff7c2103d |
parent 48253 | 82767203606e |
child 50598 | 8d9d4d91be7f |
permissions | -rw-r--r-- |
36526 | 1 |
/* |
43873
705d732d3715
8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
43767
diff
changeset
|
2 |
* Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved. |
36526 | 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. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package com.sun.tools.jdeps; |
|
27 |
||
38524 | 28 |
import static com.sun.tools.jdeps.Module.trace; |
29 |
import static java.util.stream.Collectors.*; |
|
30 |
||
31 |
import com.sun.tools.classfile.Dependency; |
|
32 |
||
33 |
import java.io.BufferedInputStream; |
|
36526 | 34 |
import java.io.File; |
35 |
import java.io.FileNotFoundException; |
|
36 |
import java.io.IOException; |
|
38524 | 37 |
import java.io.InputStream; |
36526 | 38 |
import java.io.UncheckedIOException; |
39 |
import java.lang.module.Configuration; |
|
40 |
import java.lang.module.ModuleDescriptor; |
|
41 |
import java.lang.module.ModuleFinder; |
|
38524 | 42 |
import java.lang.module.ModuleReader; |
36526 | 43 |
import java.lang.module.ModuleReference; |
44 |
import java.lang.module.ResolvedModule; |
|
45 |
import java.net.URI; |
|
38524 | 46 |
import java.nio.file.DirectoryStream; |
36526 | 47 |
import java.nio.file.FileSystem; |
48 |
import java.nio.file.FileSystems; |
|
49 |
import java.nio.file.Files; |
|
50 |
import java.nio.file.Path; |
|
51 |
import java.nio.file.Paths; |
|
38524 | 52 |
import java.util.ArrayList; |
53 |
import java.util.Collections; |
|
54 |
import java.util.HashMap; |
|
55 |
import java.util.HashSet; |
|
56 |
import java.util.LinkedHashMap; |
|
57 |
import java.util.LinkedHashSet; |
|
58 |
import java.util.List; |
|
59 |
import java.util.Map; |
|
60 |
import java.util.Objects; |
|
61 |
import java.util.Optional; |
|
62 |
import java.util.Set; |
|
63 |
import java.util.function.Function; |
|
64 |
import java.util.function.Supplier; |
|
65 |
import java.util.stream.Stream; |
|
66 |
||
38530 | 67 |
public class JdepsConfiguration implements AutoCloseable { |
38525
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
68 |
// the token for "all modules on the module path" |
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
69 |
public static final String ALL_MODULE_PATH = "ALL-MODULE-PATH"; |
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
70 |
public static final String ALL_DEFAULT = "ALL-DEFAULT"; |
42840
dfe1a03d4db4
8171418: Remove jdeps internal --include-system-modules option
mchung
parents:
42827
diff
changeset
|
71 |
public static final String ALL_SYSTEM = "ALL-SYSTEM"; |
48253
82767203606e
8193192: jdeps --generate-module-info does not look at module path
mchung
parents:
47239
diff
changeset
|
72 |
|
38525
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
73 |
public static final String MODULE_INFO = "module-info.class"; |
38524 | 74 |
|
75 |
private final SystemModuleFinder system; |
|
76 |
private final ModuleFinder finder; |
|
36526 | 77 |
|
38524 | 78 |
private final Map<String, Module> nameToModule = new LinkedHashMap<>(); |
79 |
private final Map<String, Module> packageToModule = new HashMap<>(); |
|
80 |
private final Map<String, List<Archive>> packageToUnnamedModule = new HashMap<>(); |
|
81 |
||
82 |
private final List<Archive> classpathArchives = new ArrayList<>(); |
|
83 |
private final List<Archive> initialArchives = new ArrayList<>(); |
|
84 |
private final Set<Module> rootModules = new HashSet<>(); |
|
85 |
private final Configuration configuration; |
|
41164
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
40762
diff
changeset
|
86 |
private final Runtime.Version version; |
38524 | 87 |
|
88 |
private JdepsConfiguration(SystemModuleFinder systemModulePath, |
|
89 |
ModuleFinder finder, |
|
90 |
Set<String> roots, |
|
91 |
List<Path> classpaths, |
|
38525
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
92 |
List<Archive> initialArchives, |
48253
82767203606e
8193192: jdeps --generate-module-info does not look at module path
mchung
parents:
47239
diff
changeset
|
93 |
Set<String> tokens, |
41164
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
40762
diff
changeset
|
94 |
Runtime.Version version) |
38524 | 95 |
throws IOException |
96 |
{ |
|
97 |
trace("root: %s%n", roots); |
|
98 |
||
99 |
this.system = systemModulePath; |
|
100 |
this.finder = finder; |
|
41164
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
40762
diff
changeset
|
101 |
this.version = version; |
36526 | 102 |
|
38525
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
103 |
// build root set for resolution |
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
104 |
Set<String> mods = new HashSet<>(roots); |
48253
82767203606e
8193192: jdeps --generate-module-info does not look at module path
mchung
parents:
47239
diff
changeset
|
105 |
if (tokens.contains(ALL_SYSTEM)) { |
41998
feae61dc2280
8169606: jdeps --list-reduced-deps should not show java.base as all modules require it
mchung
parents:
41859
diff
changeset
|
106 |
systemModulePath.findAll().stream() |
feae61dc2280
8169606: jdeps --list-reduced-deps should not show java.base as all modules require it
mchung
parents:
41859
diff
changeset
|
107 |
.map(mref -> mref.descriptor().name()) |
feae61dc2280
8169606: jdeps --list-reduced-deps should not show java.base as all modules require it
mchung
parents:
41859
diff
changeset
|
108 |
.forEach(mods::add); |
feae61dc2280
8169606: jdeps --list-reduced-deps should not show java.base as all modules require it
mchung
parents:
41859
diff
changeset
|
109 |
} |
38524 | 110 |
|
48253
82767203606e
8193192: jdeps --generate-module-info does not look at module path
mchung
parents:
47239
diff
changeset
|
111 |
if (tokens.contains(ALL_DEFAULT)) { |
43026
8e8b50c7491d
8172212: jdeps --require and --check should detect the specified module in the image
mchung
parents:
42840
diff
changeset
|
112 |
mods.addAll(systemModulePath.defaultSystemRoots()); |
8e8b50c7491d
8172212: jdeps --require and --check should detect the specified module in the image
mchung
parents:
42840
diff
changeset
|
113 |
} |
8e8b50c7491d
8172212: jdeps --require and --check should detect the specified module in the image
mchung
parents:
42840
diff
changeset
|
114 |
|
38524 | 115 |
this.configuration = Configuration.empty() |
43767
9cff98a149cb
8173393: Module system implementation refresh (2/2017)
alanb
parents:
43026
diff
changeset
|
116 |
.resolve(finder, ModuleFinder.of(), mods); |
38524 | 117 |
|
118 |
this.configuration.modules().stream() |
|
119 |
.map(ResolvedModule::reference) |
|
120 |
.forEach(this::addModuleReference); |
|
36526 | 121 |
|
38524 | 122 |
// packages in unnamed module |
123 |
initialArchives.forEach(archive -> { |
|
124 |
addPackagesInUnnamedModule(archive); |
|
125 |
this.initialArchives.add(archive); |
|
126 |
}); |
|
127 |
||
128 |
// classpath archives |
|
129 |
for (Path p : classpaths) { |
|
130 |
if (Files.exists(p)) { |
|
41164
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
40762
diff
changeset
|
131 |
Archive archive = Archive.getInstance(p, version); |
38524 | 132 |
addPackagesInUnnamedModule(archive); |
133 |
classpathArchives.add(archive); |
|
134 |
} |
|
135 |
} |
|
136 |
||
40762
f8883aa0053c
8160851: Remove old launcher module-related options
mchung
parents:
40308
diff
changeset
|
137 |
// all roots specified in --add-modules or -m are included |
38524 | 138 |
// as the initial set for analysis. |
139 |
roots.stream() |
|
140 |
.map(nameToModule::get) |
|
141 |
.forEach(this.rootModules::add); |
|
142 |
||
143 |
initProfiles(); |
|
144 |
||
145 |
trace("resolved modules: %s%n", nameToModule.keySet().stream() |
|
146 |
.sorted().collect(joining("\n", "\n", ""))); |
|
36526 | 147 |
} |
148 |
||
38524 | 149 |
private void initProfiles() { |
150 |
// other system modules are not observed and not added in nameToModule map |
|
151 |
Map<String, Module> systemModules = |
|
152 |
system.moduleNames() |
|
153 |
.collect(toMap(Function.identity(), (mn) -> { |
|
154 |
Module m = nameToModule.get(mn); |
|
155 |
if (m == null) { |
|
156 |
ModuleReference mref = finder.find(mn).get(); |
|
157 |
m = toModule(mref); |
|
158 |
} |
|
159 |
return m; |
|
160 |
})); |
|
161 |
Profile.init(systemModules); |
|
162 |
} |
|
163 |
||
164 |
private void addModuleReference(ModuleReference mref) { |
|
165 |
Module module = toModule(mref); |
|
166 |
nameToModule.put(mref.descriptor().name(), module); |
|
167 |
mref.descriptor().packages() |
|
168 |
.forEach(pn -> packageToModule.putIfAbsent(pn, module)); |
|
169 |
} |
|
170 |
||
171 |
private void addPackagesInUnnamedModule(Archive archive) { |
|
172 |
archive.reader().entries().stream() |
|
173 |
.filter(e -> e.endsWith(".class") && !e.equals(MODULE_INFO)) |
|
174 |
.map(this::toPackageName) |
|
175 |
.distinct() |
|
176 |
.forEach(pn -> packageToUnnamedModule |
|
177 |
.computeIfAbsent(pn, _n -> new ArrayList<>()).add(archive)); |
|
178 |
} |
|
179 |
||
180 |
private String toPackageName(String name) { |
|
181 |
int i = name.lastIndexOf('/'); |
|
182 |
return i > 0 ? name.replace('/', '.').substring(0, i) : ""; |
|
183 |
} |
|
184 |
||
185 |
public Optional<Module> findModule(String name) { |
|
186 |
Objects.requireNonNull(name); |
|
187 |
Module m = nameToModule.get(name); |
|
188 |
return m!= null ? Optional.of(m) : Optional.empty(); |
|
189 |
||
190 |
} |
|
191 |
||
192 |
public Optional<ModuleDescriptor> findModuleDescriptor(String name) { |
|
193 |
Objects.requireNonNull(name); |
|
194 |
Module m = nameToModule.get(name); |
|
195 |
return m!= null ? Optional.of(m.descriptor()) : Optional.empty(); |
|
196 |
} |
|
197 |
||
48253
82767203606e
8193192: jdeps --generate-module-info does not look at module path
mchung
parents:
47239
diff
changeset
|
198 |
public static boolean isToken(String name) { |
42840
dfe1a03d4db4
8171418: Remove jdeps internal --include-system-modules option
mchung
parents:
42827
diff
changeset
|
199 |
return ALL_MODULE_PATH.equals(name) || |
48253
82767203606e
8193192: jdeps --generate-module-info does not look at module path
mchung
parents:
47239
diff
changeset
|
200 |
ALL_DEFAULT.equals(name) || |
82767203606e
8193192: jdeps --generate-module-info does not look at module path
mchung
parents:
47239
diff
changeset
|
201 |
ALL_SYSTEM.equals(name); |
38525
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
202 |
} |
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
203 |
|
38524 | 204 |
/** |
205 |
* Returns the list of packages that split between resolved module and |
|
206 |
* unnamed module |
|
207 |
*/ |
|
208 |
public Map<String, Set<String>> splitPackages() { |
|
209 |
Set<String> splitPkgs = packageToModule.keySet().stream() |
|
210 |
.filter(packageToUnnamedModule::containsKey) |
|
211 |
.collect(toSet()); |
|
212 |
if (splitPkgs.isEmpty()) |
|
213 |
return Collections.emptyMap(); |
|
214 |
||
215 |
return splitPkgs.stream().collect(toMap(Function.identity(), (pn) -> { |
|
216 |
Set<String> sources = new LinkedHashSet<>(); |
|
217 |
sources.add(packageToModule.get(pn).getModule().location().toString()); |
|
218 |
packageToUnnamedModule.get(pn).stream() |
|
219 |
.map(Archive::getPathName) |
|
220 |
.forEach(sources::add); |
|
221 |
return sources; |
|
222 |
})); |
|
223 |
} |
|
224 |
||
225 |
/** |
|
226 |
* Returns an optional archive containing the given Location |
|
227 |
*/ |
|
228 |
public Optional<Archive> findClass(Dependency.Location location) { |
|
229 |
String name = location.getName(); |
|
230 |
int i = name.lastIndexOf('/'); |
|
231 |
String pn = i > 0 ? name.substring(0, i).replace('/', '.') : ""; |
|
232 |
Archive archive = packageToModule.get(pn); |
|
233 |
if (archive != null) { |
|
234 |
return archive.contains(name + ".class") |
|
235 |
? Optional.of(archive) |
|
236 |
: Optional.empty(); |
|
36526 | 237 |
} |
38524 | 238 |
|
239 |
if (packageToUnnamedModule.containsKey(pn)) { |
|
240 |
return packageToUnnamedModule.get(pn).stream() |
|
241 |
.filter(a -> a.contains(name + ".class")) |
|
242 |
.findFirst(); |
|
36526 | 243 |
} |
38524 | 244 |
return Optional.empty(); |
36526 | 245 |
} |
246 |
||
247 |
/** |
|
248 |
* Returns the list of Modules that can be found in the specified |
|
249 |
* module paths. |
|
250 |
*/ |
|
38524 | 251 |
public Map<String, Module> getModules() { |
252 |
return nameToModule; |
|
36526 | 253 |
} |
254 |
||
43873
705d732d3715
8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
43767
diff
changeset
|
255 |
/** |
705d732d3715
8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
43767
diff
changeset
|
256 |
* Returns Configuration with the given roots |
705d732d3715
8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
43767
diff
changeset
|
257 |
*/ |
705d732d3715
8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
43767
diff
changeset
|
258 |
public Configuration resolve(Set<String> roots) { |
705d732d3715
8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
43767
diff
changeset
|
259 |
if (roots.isEmpty()) |
705d732d3715
8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
43767
diff
changeset
|
260 |
throw new IllegalArgumentException("empty roots"); |
705d732d3715
8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
43767
diff
changeset
|
261 |
|
705d732d3715
8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
43767
diff
changeset
|
262 |
return Configuration.empty() |
705d732d3715
8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents:
43767
diff
changeset
|
263 |
.resolve(finder, ModuleFinder.of(), roots); |
36526 | 264 |
} |
265 |
||
38524 | 266 |
public List<Archive> classPathArchives() { |
267 |
return classpathArchives; |
|
268 |
} |
|
269 |
||
270 |
public List<Archive> initialArchives() { |
|
271 |
return initialArchives; |
|
272 |
} |
|
273 |
||
274 |
public Set<Module> rootModules() { |
|
275 |
return rootModules; |
|
36526 | 276 |
} |
277 |
||
38524 | 278 |
public Module toModule(ModuleReference mref) { |
279 |
try { |
|
280 |
String mn = mref.descriptor().name(); |
|
281 |
URI location = mref.location().orElseThrow(FileNotFoundException::new); |
|
282 |
ModuleDescriptor md = mref.descriptor(); |
|
47239
2088dbaa2282
8187449: jdeps fails when an upgradeable module is upgraded with an automatic module
mchung
parents:
47216
diff
changeset
|
283 |
// is this module from the system module path? |
2088dbaa2282
8187449: jdeps fails when an upgradeable module is upgraded with an automatic module
mchung
parents:
47216
diff
changeset
|
284 |
URI loc = system.find(mn).flatMap(ModuleReference::location).orElse(null); |
2088dbaa2282
8187449: jdeps fails when an upgradeable module is upgraded with an automatic module
mchung
parents:
47216
diff
changeset
|
285 |
boolean isSystem = location.equals(loc); |
36526 | 286 |
|
38524 | 287 |
final ClassFileReader reader; |
288 |
if (location.getScheme().equals("jrt")) { |
|
289 |
reader = system.getClassReader(mn); |
|
290 |
} else { |
|
41164
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
40762
diff
changeset
|
291 |
reader = ClassFileReader.newInstance(Paths.get(location), version); |
38524 | 292 |
} |
47239
2088dbaa2282
8187449: jdeps fails when an upgradeable module is upgraded with an automatic module
mchung
parents:
47216
diff
changeset
|
293 |
Module.Builder builder = new Module.Builder(md, isSystem); |
38524 | 294 |
builder.classes(reader); |
36526 | 295 |
builder.location(location); |
38524 | 296 |
|
297 |
return builder.build(); |
|
36526 | 298 |
} catch (IOException e) { |
299 |
throw new UncheckedIOException(e); |
|
300 |
} |
|
301 |
} |
|
302 |
||
41164
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
40762
diff
changeset
|
303 |
public Runtime.Version getVersion() { |
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
40762
diff
changeset
|
304 |
return version; |
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
40762
diff
changeset
|
305 |
} |
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
40762
diff
changeset
|
306 |
|
38530 | 307 |
/* |
308 |
* Close all archives e.g. JarFile |
|
309 |
*/ |
|
310 |
@Override |
|
311 |
public void close() throws IOException { |
|
312 |
for (Archive archive : initialArchives) |
|
313 |
archive.close(); |
|
314 |
for (Archive archive : classpathArchives) |
|
315 |
archive.close(); |
|
316 |
for (Module module : nameToModule.values()) |
|
317 |
module.close(); |
|
318 |
} |
|
319 |
||
38524 | 320 |
static class SystemModuleFinder implements ModuleFinder { |
38525
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
321 |
private static final String JAVA_HOME = System.getProperty("java.home"); |
38524 | 322 |
private static final String JAVA_SE = "java.se"; |
323 |
||
324 |
private final FileSystem fileSystem; |
|
325 |
private final Path root; |
|
326 |
private final Map<String, ModuleReference> systemModules; |
|
36526 | 327 |
|
38524 | 328 |
SystemModuleFinder() { |
38525
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
329 |
if (Files.isRegularFile(Paths.get(JAVA_HOME, "lib", "modules"))) { |
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
330 |
// jrt file system |
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
331 |
this.fileSystem = FileSystems.getFileSystem(URI.create("jrt:/")); |
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
332 |
this.root = fileSystem.getPath("/modules"); |
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
333 |
this.systemModules = walk(root); |
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
334 |
} else { |
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
335 |
// exploded image |
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
336 |
this.fileSystem = FileSystems.getDefault(); |
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
337 |
root = Paths.get(JAVA_HOME, "modules"); |
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
338 |
this.systemModules = ModuleFinder.ofSystem().findAll().stream() |
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
339 |
.collect(toMap(mref -> mref.descriptor().name(), Function.identity())); |
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
340 |
} |
38524 | 341 |
} |
38525
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
342 |
|
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
343 |
SystemModuleFinder(String javaHome) throws IOException { |
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
344 |
if (javaHome == null) { |
40762
f8883aa0053c
8160851: Remove old launcher module-related options
mchung
parents:
40308
diff
changeset
|
345 |
// --system none |
38525
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
346 |
this.fileSystem = null; |
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
347 |
this.root = null; |
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
348 |
this.systemModules = Collections.emptyMap(); |
36526 | 349 |
} else { |
38525
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
350 |
if (Files.isRegularFile(Paths.get(javaHome, "lib", "modules"))) |
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
351 |
throw new IllegalArgumentException("Invalid java.home: " + javaHome); |
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
352 |
|
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
353 |
// alternate java.home |
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
354 |
Map<String, String> env = new HashMap<>(); |
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
355 |
env.put("java.home", javaHome); |
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
356 |
// a remote run-time image |
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
357 |
this.fileSystem = FileSystems.newFileSystem(URI.create("jrt:/"), env); |
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
358 |
this.root = fileSystem.getPath("/modules"); |
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
359 |
this.systemModules = walk(root); |
36526 | 360 |
} |
38525
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
361 |
} |
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
362 |
|
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
363 |
private Map<String, ModuleReference> walk(Path root) { |
38529
58cf7e51c16d
8152502: tools/jdeps/modules/GenModuleInfo.java and TransitiveDeps fails on windows
mchung
parents:
38525
diff
changeset
|
364 |
try (Stream<Path> stream = Files.walk(root, 1)) { |
58cf7e51c16d
8152502: tools/jdeps/modules/GenModuleInfo.java and TransitiveDeps fails on windows
mchung
parents:
38525
diff
changeset
|
365 |
return stream.filter(path -> !path.equals(root)) |
58cf7e51c16d
8152502: tools/jdeps/modules/GenModuleInfo.java and TransitiveDeps fails on windows
mchung
parents:
38525
diff
changeset
|
366 |
.map(this::toModuleReference) |
58cf7e51c16d
8152502: tools/jdeps/modules/GenModuleInfo.java and TransitiveDeps fails on windows
mchung
parents:
38525
diff
changeset
|
367 |
.collect(toMap(mref -> mref.descriptor().name(), |
58cf7e51c16d
8152502: tools/jdeps/modules/GenModuleInfo.java and TransitiveDeps fails on windows
mchung
parents:
38525
diff
changeset
|
368 |
Function.identity())); |
38525
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
369 |
} catch (IOException e) { |
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
370 |
throw new UncheckedIOException(e); |
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
371 |
} |
36526 | 372 |
} |
373 |
||
38524 | 374 |
private ModuleReference toModuleReference(Path path) { |
375 |
Path minfo = path.resolve(MODULE_INFO); |
|
376 |
try (InputStream in = Files.newInputStream(minfo); |
|
377 |
BufferedInputStream bin = new BufferedInputStream(in)) { |
|
378 |
||
379 |
ModuleDescriptor descriptor = dropHashes(ModuleDescriptor.read(bin)); |
|
380 |
String mn = descriptor.name(); |
|
381 |
URI uri = URI.create("jrt:/" + path.getFileName().toString()); |
|
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42822
diff
changeset
|
382 |
Supplier<ModuleReader> readerSupplier = () -> new ModuleReader() { |
38524 | 383 |
@Override |
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42822
diff
changeset
|
384 |
public Optional<URI> find(String name) throws IOException { |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42822
diff
changeset
|
385 |
return name.equals(mn) |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42822
diff
changeset
|
386 |
? Optional.of(uri) : Optional.empty(); |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42822
diff
changeset
|
387 |
} |
38524 | 388 |
|
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42822
diff
changeset
|
389 |
@Override |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42822
diff
changeset
|
390 |
public Stream<String> list() { |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42822
diff
changeset
|
391 |
return Stream.empty(); |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42822
diff
changeset
|
392 |
} |
41859
85710a227743
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
41164
diff
changeset
|
393 |
|
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42822
diff
changeset
|
394 |
@Override |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42822
diff
changeset
|
395 |
public void close() { |
38524 | 396 |
} |
397 |
}; |
|
398 |
||
42822
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42407
diff
changeset
|
399 |
return new ModuleReference(descriptor, uri) { |
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42407
diff
changeset
|
400 |
@Override |
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42407
diff
changeset
|
401 |
public ModuleReader open() { |
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42407
diff
changeset
|
402 |
return readerSupplier.get(); |
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42407
diff
changeset
|
403 |
} |
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
42407
diff
changeset
|
404 |
}; |
38524 | 405 |
} catch (IOException e) { |
406 |
throw new UncheckedIOException(e); |
|
36526 | 407 |
} |
408 |
} |
|
409 |
||
38524 | 410 |
private ModuleDescriptor dropHashes(ModuleDescriptor md) { |
43767
9cff98a149cb
8173393: Module system implementation refresh (2/2017)
alanb
parents:
43026
diff
changeset
|
411 |
ModuleDescriptor.Builder builder = ModuleDescriptor.newModule(md.name()); |
38524 | 412 |
md.requires().forEach(builder::requires); |
413 |
md.exports().forEach(builder::exports); |
|
42407
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41998
diff
changeset
|
414 |
md.opens().forEach(builder::opens); |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41998
diff
changeset
|
415 |
md.provides().stream().forEach(builder::provides); |
38524 | 416 |
md.uses().stream().forEach(builder::uses); |
43767
9cff98a149cb
8173393: Module system implementation refresh (2/2017)
alanb
parents:
43026
diff
changeset
|
417 |
builder.packages(md.packages()); |
38524 | 418 |
return builder.build(); |
419 |
} |
|
420 |
||
421 |
@Override |
|
422 |
public Set<ModuleReference> findAll() { |
|
423 |
return systemModules.values().stream().collect(toSet()); |
|
36526 | 424 |
} |
425 |
||
38524 | 426 |
@Override |
427 |
public Optional<ModuleReference> find(String mn) { |
|
428 |
return systemModules.containsKey(mn) |
|
429 |
? Optional.of(systemModules.get(mn)) : Optional.empty(); |
|
36526 | 430 |
} |
431 |
||
38524 | 432 |
public Stream<String> moduleNames() { |
433 |
return systemModules.values().stream() |
|
434 |
.map(mref -> mref.descriptor().name()); |
|
435 |
} |
|
436 |
||
437 |
public ClassFileReader getClassReader(String modulename) throws IOException { |
|
36526 | 438 |
Path mp = root.resolve(modulename); |
439 |
if (Files.exists(mp) && Files.isDirectory(mp)) { |
|
38524 | 440 |
return ClassFileReader.newInstance(fileSystem, mp); |
36526 | 441 |
} else { |
442 |
throw new FileNotFoundException(mp.toString()); |
|
443 |
} |
|
444 |
} |
|
38524 | 445 |
|
446 |
public Set<String> defaultSystemRoots() { |
|
447 |
Set<String> roots = new HashSet<>(); |
|
448 |
boolean hasJava = false; |
|
449 |
if (systemModules.containsKey(JAVA_SE)) { |
|
450 |
// java.se is a system module |
|
451 |
hasJava = true; |
|
452 |
roots.add(JAVA_SE); |
|
453 |
} |
|
454 |
||
455 |
for (ModuleReference mref : systemModules.values()) { |
|
456 |
String mn = mref.descriptor().name(); |
|
457 |
if (hasJava && mn.startsWith("java.")) |
|
458 |
continue; |
|
459 |
||
460 |
// add as root if observable and exports at least one package |
|
461 |
ModuleDescriptor descriptor = mref.descriptor(); |
|
462 |
for (ModuleDescriptor.Exports e : descriptor.exports()) { |
|
463 |
if (!e.isQualified()) { |
|
464 |
roots.add(mn); |
|
465 |
break; |
|
466 |
} |
|
467 |
} |
|
468 |
} |
|
469 |
return roots; |
|
470 |
} |
|
36526 | 471 |
} |
472 |
||
38524 | 473 |
public static class Builder { |
474 |
||
475 |
final SystemModuleFinder systemModulePath; |
|
476 |
final Set<String> rootModules = new HashSet<>(); |
|
477 |
final List<Archive> initialArchives = new ArrayList<>(); |
|
478 |
final List<Path> paths = new ArrayList<>(); |
|
479 |
final List<Path> classPaths = new ArrayList<>(); |
|
48253
82767203606e
8193192: jdeps --generate-module-info does not look at module path
mchung
parents:
47239
diff
changeset
|
480 |
final Set<String> tokens = new HashSet<>(); |
38524 | 481 |
|
482 |
ModuleFinder upgradeModulePath; |
|
483 |
ModuleFinder appModulePath; |
|
41164
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
40762
diff
changeset
|
484 |
Runtime.Version version; |
38524 | 485 |
|
486 |
public Builder() { |
|
38525
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
487 |
this.systemModulePath = new SystemModuleFinder(); |
38524 | 488 |
} |
489 |
||
38525
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
490 |
public Builder(String javaHome) throws IOException { |
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
491 |
this.systemModulePath = SystemModuleFinder.JAVA_HOME.equals(javaHome) |
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
492 |
? new SystemModuleFinder() |
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
493 |
: new SystemModuleFinder(javaHome); |
38524 | 494 |
} |
495 |
||
496 |
public Builder upgradeModulePath(String upgradeModulePath) { |
|
497 |
this.upgradeModulePath = createModulePathFinder(upgradeModulePath); |
|
498 |
return this; |
|
499 |
} |
|
500 |
||
501 |
public Builder appModulePath(String modulePath) { |
|
502 |
this.appModulePath = createModulePathFinder(modulePath); |
|
503 |
return this; |
|
504 |
} |
|
505 |
||
506 |
public Builder addmods(Set<String> addmods) { |
|
507 |
for (String mn : addmods) { |
|
48253
82767203606e
8193192: jdeps --generate-module-info does not look at module path
mchung
parents:
47239
diff
changeset
|
508 |
if (isToken(mn)) { |
82767203606e
8193192: jdeps --generate-module-info does not look at module path
mchung
parents:
47239
diff
changeset
|
509 |
tokens.add(mn); |
82767203606e
8193192: jdeps --generate-module-info does not look at module path
mchung
parents:
47239
diff
changeset
|
510 |
} else { |
82767203606e
8193192: jdeps --generate-module-info does not look at module path
mchung
parents:
47239
diff
changeset
|
511 |
rootModules.add(mn); |
38524 | 512 |
} |
513 |
} |
|
514 |
return this; |
|
515 |
} |
|
516 |
||
41164
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
40762
diff
changeset
|
517 |
public Builder multiRelease(Runtime.Version version) { |
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
40762
diff
changeset
|
518 |
this.version = version; |
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
40762
diff
changeset
|
519 |
return this; |
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
40762
diff
changeset
|
520 |
} |
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
40762
diff
changeset
|
521 |
|
38524 | 522 |
public Builder addRoot(Path path) { |
41164
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
40762
diff
changeset
|
523 |
Archive archive = Archive.getInstance(path, version); |
38524 | 524 |
if (archive.contains(MODULE_INFO)) { |
525 |
paths.add(path); |
|
526 |
} else { |
|
527 |
initialArchives.add(archive); |
|
528 |
} |
|
529 |
return this; |
|
530 |
} |
|
531 |
||
532 |
public Builder addClassPath(String classPath) { |
|
533 |
this.classPaths.addAll(getClassPaths(classPath)); |
|
534 |
return this; |
|
535 |
} |
|
536 |
||
537 |
public JdepsConfiguration build() throws IOException { |
|
538 |
ModuleFinder finder = systemModulePath; |
|
539 |
if (upgradeModulePath != null) { |
|
540 |
finder = ModuleFinder.compose(upgradeModulePath, systemModulePath); |
|
541 |
} |
|
542 |
if (appModulePath != null) { |
|
543 |
finder = ModuleFinder.compose(finder, appModulePath); |
|
544 |
} |
|
545 |
if (!paths.isEmpty()) { |
|
546 |
ModuleFinder otherModulePath = ModuleFinder.of(paths.toArray(new Path[0])); |
|
547 |
||
548 |
finder = ModuleFinder.compose(finder, otherModulePath); |
|
549 |
// add modules specified on command-line (convenience) as root set |
|
550 |
otherModulePath.findAll().stream() |
|
551 |
.map(mref -> mref.descriptor().name()) |
|
552 |
.forEach(rootModules::add); |
|
553 |
} |
|
43026
8e8b50c7491d
8172212: jdeps --require and --check should detect the specified module in the image
mchung
parents:
42840
diff
changeset
|
554 |
|
48253
82767203606e
8193192: jdeps --generate-module-info does not look at module path
mchung
parents:
47239
diff
changeset
|
555 |
// add all modules to the root set for unnamed module or set explicitly |
82767203606e
8193192: jdeps --generate-module-info does not look at module path
mchung
parents:
47239
diff
changeset
|
556 |
boolean unnamed = !initialArchives.isEmpty() || !classPaths.isEmpty(); |
82767203606e
8193192: jdeps --generate-module-info does not look at module path
mchung
parents:
47239
diff
changeset
|
557 |
if ((unnamed || tokens.contains(ALL_MODULE_PATH)) && appModulePath != null) { |
38524 | 558 |
appModulePath.findAll().stream() |
559 |
.map(mref -> mref.descriptor().name()) |
|
560 |
.forEach(rootModules::add); |
|
561 |
} |
|
38525
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
562 |
|
43026
8e8b50c7491d
8172212: jdeps --require and --check should detect the specified module in the image
mchung
parents:
42840
diff
changeset
|
563 |
// no archive is specified for analysis |
8e8b50c7491d
8172212: jdeps --require and --check should detect the specified module in the image
mchung
parents:
42840
diff
changeset
|
564 |
// add all system modules as root if --add-modules ALL-SYSTEM is specified |
48253
82767203606e
8193192: jdeps --generate-module-info does not look at module path
mchung
parents:
47239
diff
changeset
|
565 |
if (tokens.contains(ALL_SYSTEM) && rootModules.isEmpty() && |
43026
8e8b50c7491d
8172212: jdeps --require and --check should detect the specified module in the image
mchung
parents:
42840
diff
changeset
|
566 |
initialArchives.isEmpty() && classPaths.isEmpty()) { |
8e8b50c7491d
8172212: jdeps --require and --check should detect the specified module in the image
mchung
parents:
42840
diff
changeset
|
567 |
systemModulePath.findAll() |
8e8b50c7491d
8172212: jdeps --require and --check should detect the specified module in the image
mchung
parents:
42840
diff
changeset
|
568 |
.stream() |
8e8b50c7491d
8172212: jdeps --require and --check should detect the specified module in the image
mchung
parents:
42840
diff
changeset
|
569 |
.map(mref -> mref.descriptor().name()) |
8e8b50c7491d
8172212: jdeps --require and --check should detect the specified module in the image
mchung
parents:
42840
diff
changeset
|
570 |
.forEach(rootModules::add); |
8e8b50c7491d
8172212: jdeps --require and --check should detect the specified module in the image
mchung
parents:
42840
diff
changeset
|
571 |
} |
8e8b50c7491d
8172212: jdeps --require and --check should detect the specified module in the image
mchung
parents:
42840
diff
changeset
|
572 |
|
48253
82767203606e
8193192: jdeps --generate-module-info does not look at module path
mchung
parents:
47239
diff
changeset
|
573 |
if (unnamed && !tokens.contains(ALL_DEFAULT)) { |
82767203606e
8193192: jdeps --generate-module-info does not look at module path
mchung
parents:
47239
diff
changeset
|
574 |
tokens.add(ALL_SYSTEM); |
82767203606e
8193192: jdeps --generate-module-info does not look at module path
mchung
parents:
47239
diff
changeset
|
575 |
} |
82767203606e
8193192: jdeps --generate-module-info does not look at module path
mchung
parents:
47239
diff
changeset
|
576 |
|
38524 | 577 |
return new JdepsConfiguration(systemModulePath, |
578 |
finder, |
|
579 |
rootModules, |
|
580 |
classPaths, |
|
38525
a2169f8fa712
8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents:
38524
diff
changeset
|
581 |
initialArchives, |
48253
82767203606e
8193192: jdeps --generate-module-info does not look at module path
mchung
parents:
47239
diff
changeset
|
582 |
tokens, |
41164
69167c89e68f
8153654: Update jdeps to be multi-release jar aware
sdrach
parents:
40762
diff
changeset
|
583 |
version); |
38524 | 584 |
} |
585 |
||
586 |
private static ModuleFinder createModulePathFinder(String mpaths) { |
|
587 |
if (mpaths == null) { |
|
588 |
return null; |
|
589 |
} else { |
|
590 |
String[] dirs = mpaths.split(File.pathSeparator); |
|
591 |
Path[] paths = new Path[dirs.length]; |
|
592 |
int i = 0; |
|
593 |
for (String dir : dirs) { |
|
594 |
paths[i++] = Paths.get(dir); |
|
595 |
} |
|
596 |
return ModuleFinder.of(paths); |
|
597 |
} |
|
598 |
} |
|
599 |
||
600 |
/* |
|
601 |
* Returns the list of Archive specified in cpaths and not included |
|
602 |
* initialArchives |
|
603 |
*/ |
|
604 |
private List<Path> getClassPaths(String cpaths) { |
|
605 |
if (cpaths.isEmpty()) { |
|
606 |
return Collections.emptyList(); |
|
607 |
} |
|
608 |
List<Path> paths = new ArrayList<>(); |
|
609 |
for (String p : cpaths.split(File.pathSeparator)) { |
|
610 |
if (p.length() > 0) { |
|
611 |
// wildcard to parse all JAR files e.g. -classpath dir/* |
|
612 |
int i = p.lastIndexOf(".*"); |
|
613 |
if (i > 0) { |
|
614 |
Path dir = Paths.get(p.substring(0, i)); |
|
615 |
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.jar")) { |
|
616 |
for (Path entry : stream) { |
|
617 |
paths.add(entry); |
|
618 |
} |
|
619 |
} catch (IOException e) { |
|
620 |
throw new UncheckedIOException(e); |
|
621 |
} |
|
622 |
} else { |
|
623 |
paths.add(Paths.get(p)); |
|
624 |
} |
|
625 |
} |
|
626 |
} |
|
627 |
return paths; |
|
36526 | 628 |
} |
629 |
} |
|
38524 | 630 |
|
36526 | 631 |
} |