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