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