author | serb |
Sat, 09 Jun 2018 13:33:35 -0700 | |
changeset 50647 | a98ff7c2103d |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
38524 | 1 |
/* |
2 |
* Copyright (c) 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 |
||
28 |
import com.sun.tools.classfile.Dependency.Location; |
|
29 |
import java.io.IOException; |
|
30 |
import java.util.ArrayList; |
|
42840
dfe1a03d4db4
8171418: Remove jdeps internal --include-system-modules option
mchung
parents:
42407
diff
changeset
|
31 |
import java.util.Collection; |
38524 | 32 |
import java.util.Deque; |
33 |
import java.util.LinkedHashSet; |
|
34 |
import java.util.LinkedList; |
|
35 |
import java.util.List; |
|
36 |
import java.util.Optional; |
|
37 |
import java.util.Set; |
|
38 |
import java.util.concurrent.ConcurrentLinkedDeque; |
|
39 |
import java.util.stream.Collectors; |
|
40 |
import java.util.stream.Stream; |
|
41 |
||
42 |
import static com.sun.tools.jdeps.Analyzer.Type.CLASS; |
|
43 |
import static com.sun.tools.jdeps.Analyzer.Type.VERBOSE; |
|
44 |
import static com.sun.tools.jdeps.Module.trace; |
|
45 |
import static java.util.stream.Collectors.*; |
|
46 |
||
47 |
/** |
|
48 |
* Dependency Analyzer. |
|
49 |
* |
|
50 |
* Type of filters: |
|
51 |
* source filter: -include <pattern> |
|
41860
906670ff49c7
8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
40762
diff
changeset
|
52 |
* target filter: -package, -regex, --require |
38524 | 53 |
* |
54 |
* The initial archive set for analysis includes |
|
55 |
* 1. archives specified in the command line arguments |
|
56 |
* 2. observable modules matching the source filter |
|
57 |
* 3. classpath archives matching the source filter or target filter |
|
40762
f8883aa0053c
8160851: Remove old launcher module-related options
mchung
parents:
39101
diff
changeset
|
58 |
* 4. --add-modules and -m root modules |
38524 | 59 |
*/ |
60 |
public class DepsAnalyzer { |
|
61 |
final JdepsConfiguration configuration; |
|
62 |
final JdepsFilter filter; |
|
63 |
final JdepsWriter writer; |
|
64 |
final Analyzer.Type verbose; |
|
65 |
final boolean apiOnly; |
|
66 |
||
67 |
final DependencyFinder finder; |
|
68 |
final Analyzer analyzer; |
|
69 |
final List<Archive> rootArchives = new ArrayList<>(); |
|
70 |
||
71 |
// parsed archives |
|
72 |
final Set<Archive> archives = new LinkedHashSet<>(); |
|
73 |
||
74 |
public DepsAnalyzer(JdepsConfiguration config, |
|
75 |
JdepsFilter filter, |
|
76 |
JdepsWriter writer, |
|
77 |
Analyzer.Type verbose, |
|
78 |
boolean apiOnly) { |
|
79 |
this.configuration = config; |
|
80 |
this.filter = filter; |
|
81 |
this.writer = writer; |
|
82 |
this.verbose = verbose; |
|
83 |
this.apiOnly = apiOnly; |
|
84 |
||
85 |
this.finder = new DependencyFinder(config, filter); |
|
86 |
this.analyzer = new Analyzer(configuration, verbose, filter); |
|
87 |
||
88 |
// determine initial archives to be analyzed |
|
89 |
this.rootArchives.addAll(configuration.initialArchives()); |
|
90 |
||
91 |
// if -include pattern is specified, add the matching archives on |
|
92 |
// classpath to the root archives |
|
93 |
if (filter.hasIncludePattern() || filter.hasTargetFilter()) { |
|
94 |
configuration.getModules().values().stream() |
|
42840
dfe1a03d4db4
8171418: Remove jdeps internal --include-system-modules option
mchung
parents:
42407
diff
changeset
|
95 |
.filter(source -> include(source) && filter.matches(source)) |
38524 | 96 |
.forEach(this.rootArchives::add); |
97 |
} |
|
98 |
||
99 |
// class path archives |
|
100 |
configuration.classPathArchives().stream() |
|
101 |
.filter(filter::matches) |
|
102 |
.forEach(this.rootArchives::add); |
|
103 |
||
104 |
// Include the root modules for analysis |
|
105 |
this.rootArchives.addAll(configuration.rootModules()); |
|
106 |
||
107 |
trace("analyze root archives: %s%n", this.rootArchives); |
|
108 |
} |
|
109 |
||
110 |
/* |
|
111 |
* Perform runtime dependency analysis |
|
112 |
*/ |
|
113 |
public boolean run() throws IOException { |
|
114 |
return run(false, 1); |
|
115 |
} |
|
116 |
||
117 |
/** |
|
118 |
* Perform compile-time view or run-time view dependency analysis. |
|
119 |
* |
|
120 |
* @param compileTimeView |
|
121 |
* @param maxDepth depth of recursive analysis. depth == 0 if -R is set |
|
122 |
*/ |
|
123 |
public boolean run(boolean compileTimeView, int maxDepth) throws IOException { |
|
124 |
try { |
|
125 |
// parse each packaged module or classpath archive |
|
126 |
if (apiOnly) { |
|
127 |
finder.parseExportedAPIs(rootArchives.stream()); |
|
128 |
} else { |
|
129 |
finder.parse(rootArchives.stream()); |
|
130 |
} |
|
131 |
archives.addAll(rootArchives); |
|
132 |
||
133 |
int depth = maxDepth > 0 ? maxDepth : Integer.MAX_VALUE; |
|
134 |
||
135 |
// transitive analysis |
|
136 |
if (depth > 1) { |
|
137 |
if (compileTimeView) |
|
138 |
transitiveArchiveDeps(depth-1); |
|
139 |
else |
|
140 |
transitiveDeps(depth-1); |
|
141 |
} |
|
142 |
||
143 |
Set<Archive> archives = archives(); |
|
144 |
||
145 |
// analyze the dependencies collected |
|
146 |
analyzer.run(archives, finder.locationToArchive()); |
|
147 |
||
41860
906670ff49c7
8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
40762
diff
changeset
|
148 |
if (writer != null) { |
906670ff49c7
8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
40762
diff
changeset
|
149 |
writer.generateOutput(archives, analyzer); |
906670ff49c7
8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
40762
diff
changeset
|
150 |
} |
38524 | 151 |
} finally { |
152 |
finder.shutdown(); |
|
153 |
} |
|
154 |
return true; |
|
155 |
} |
|
156 |
||
157 |
/** |
|
158 |
* Returns the archives for reporting that has matching dependences. |
|
159 |
* |
|
41860
906670ff49c7
8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
40762
diff
changeset
|
160 |
* If --require is set, they should be excluded. |
38524 | 161 |
*/ |
162 |
Set<Archive> archives() { |
|
163 |
if (filter.requiresFilter().isEmpty()) { |
|
164 |
return archives.stream() |
|
42840
dfe1a03d4db4
8171418: Remove jdeps internal --include-system-modules option
mchung
parents:
42407
diff
changeset
|
165 |
.filter(this::include) |
38524 | 166 |
.filter(Archive::hasDependences) |
167 |
.collect(Collectors.toSet()); |
|
168 |
} else { |
|
41860
906670ff49c7
8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
40762
diff
changeset
|
169 |
// use the archives that have dependences and not specified in --require |
38524 | 170 |
return archives.stream() |
42840
dfe1a03d4db4
8171418: Remove jdeps internal --include-system-modules option
mchung
parents:
42407
diff
changeset
|
171 |
.filter(this::include) |
dfe1a03d4db4
8171418: Remove jdeps internal --include-system-modules option
mchung
parents:
42407
diff
changeset
|
172 |
.filter(source -> !filter.requiresFilter().contains(source.getName())) |
38524 | 173 |
.filter(source -> |
174 |
source.getDependencies() |
|
175 |
.map(finder::locationToArchive) |
|
176 |
.anyMatch(a -> a != source)) |
|
177 |
.collect(Collectors.toSet()); |
|
178 |
} |
|
179 |
} |
|
180 |
||
181 |
/** |
|
182 |
* Returns the dependences, either class name or package name |
|
183 |
* as specified in the given verbose level. |
|
184 |
*/ |
|
39101
fd8a6392b7ea
8159524: jdeps -jdkinternals throws NPE when no replacement is known
mchung
parents:
38532
diff
changeset
|
185 |
Set<String> dependences() { |
38524 | 186 |
return analyzer.archives().stream() |
187 |
.map(analyzer::dependences) |
|
188 |
.flatMap(Set::stream) |
|
39101
fd8a6392b7ea
8159524: jdeps -jdkinternals throws NPE when no replacement is known
mchung
parents:
38532
diff
changeset
|
189 |
.collect(Collectors.toSet()); |
38524 | 190 |
} |
191 |
||
192 |
/** |
|
193 |
* Returns the archives that contains the given locations and |
|
194 |
* not parsed and analyzed. |
|
195 |
*/ |
|
196 |
private Set<Archive> unresolvedArchives(Stream<Location> locations) { |
|
197 |
return locations.filter(l -> !finder.isParsed(l)) |
|
198 |
.distinct() |
|
199 |
.map(configuration::findClass) |
|
200 |
.flatMap(Optional::stream) |
|
201 |
.collect(toSet()); |
|
202 |
} |
|
203 |
||
204 |
/* |
|
205 |
* Recursively analyzes entire module/archives. |
|
206 |
*/ |
|
207 |
private void transitiveArchiveDeps(int depth) throws IOException { |
|
208 |
Stream<Location> deps = archives.stream() |
|
209 |
.flatMap(Archive::getDependencies); |
|
210 |
||
211 |
// start with the unresolved archives |
|
212 |
Set<Archive> unresolved = unresolvedArchives(deps); |
|
213 |
do { |
|
214 |
// parse all unresolved archives |
|
215 |
Set<Location> targets = apiOnly |
|
216 |
? finder.parseExportedAPIs(unresolved.stream()) |
|
217 |
: finder.parse(unresolved.stream()); |
|
218 |
archives.addAll(unresolved); |
|
219 |
||
220 |
// Add dependencies to the next batch for analysis |
|
221 |
unresolved = unresolvedArchives(targets.stream()); |
|
222 |
} while (!unresolved.isEmpty() && depth-- > 0); |
|
223 |
} |
|
224 |
||
225 |
/* |
|
226 |
* Recursively analyze the class dependences |
|
227 |
*/ |
|
228 |
private void transitiveDeps(int depth) throws IOException { |
|
229 |
Stream<Location> deps = archives.stream() |
|
230 |
.flatMap(Archive::getDependencies); |
|
231 |
||
232 |
Deque<Location> unresolved = deps.collect(Collectors.toCollection(LinkedList::new)); |
|
233 |
ConcurrentLinkedDeque<Location> deque = new ConcurrentLinkedDeque<>(); |
|
234 |
do { |
|
235 |
Location target; |
|
236 |
while ((target = unresolved.poll()) != null) { |
|
237 |
if (finder.isParsed(target)) |
|
238 |
continue; |
|
239 |
||
240 |
Archive archive = configuration.findClass(target).orElse(null); |
|
42840
dfe1a03d4db4
8171418: Remove jdeps internal --include-system-modules option
mchung
parents:
42407
diff
changeset
|
241 |
if (archive != null) { |
38524 | 242 |
archives.add(archive); |
243 |
||
244 |
String name = target.getName(); |
|
245 |
Set<Location> targets = apiOnly |
|
246 |
? finder.parseExportedAPIs(archive, name) |
|
247 |
: finder.parse(archive, name); |
|
248 |
||
249 |
// build unresolved dependencies |
|
250 |
targets.stream() |
|
251 |
.filter(t -> !finder.isParsed(t)) |
|
252 |
.forEach(deque::add); |
|
253 |
} |
|
254 |
} |
|
255 |
unresolved = deque; |
|
256 |
deque = new ConcurrentLinkedDeque<>(); |
|
257 |
} while (!unresolved.isEmpty() && depth-- > 0); |
|
258 |
} |
|
259 |
||
42840
dfe1a03d4db4
8171418: Remove jdeps internal --include-system-modules option
mchung
parents:
42407
diff
changeset
|
260 |
/* |
dfe1a03d4db4
8171418: Remove jdeps internal --include-system-modules option
mchung
parents:
42407
diff
changeset
|
261 |
* Tests if the given archive is requested for analysis. |
dfe1a03d4db4
8171418: Remove jdeps internal --include-system-modules option
mchung
parents:
42407
diff
changeset
|
262 |
* It includes the root modules specified in --module, --add-modules |
dfe1a03d4db4
8171418: Remove jdeps internal --include-system-modules option
mchung
parents:
42407
diff
changeset
|
263 |
* or modules specified on the command line |
dfe1a03d4db4
8171418: Remove jdeps internal --include-system-modules option
mchung
parents:
42407
diff
changeset
|
264 |
* |
dfe1a03d4db4
8171418: Remove jdeps internal --include-system-modules option
mchung
parents:
42407
diff
changeset
|
265 |
* This filters system module by default unless they are explicitly |
dfe1a03d4db4
8171418: Remove jdeps internal --include-system-modules option
mchung
parents:
42407
diff
changeset
|
266 |
* requested. |
dfe1a03d4db4
8171418: Remove jdeps internal --include-system-modules option
mchung
parents:
42407
diff
changeset
|
267 |
*/ |
dfe1a03d4db4
8171418: Remove jdeps internal --include-system-modules option
mchung
parents:
42407
diff
changeset
|
268 |
public boolean include(Archive source) { |
dfe1a03d4db4
8171418: Remove jdeps internal --include-system-modules option
mchung
parents:
42407
diff
changeset
|
269 |
Module module = source.getModule(); |
dfe1a03d4db4
8171418: Remove jdeps internal --include-system-modules option
mchung
parents:
42407
diff
changeset
|
270 |
// skip system module by default |
dfe1a03d4db4
8171418: Remove jdeps internal --include-system-modules option
mchung
parents:
42407
diff
changeset
|
271 |
return !module.isSystem() |
dfe1a03d4db4
8171418: Remove jdeps internal --include-system-modules option
mchung
parents:
42407
diff
changeset
|
272 |
|| configuration.rootModules().contains(source); |
dfe1a03d4db4
8171418: Remove jdeps internal --include-system-modules option
mchung
parents:
42407
diff
changeset
|
273 |
} |
dfe1a03d4db4
8171418: Remove jdeps internal --include-system-modules option
mchung
parents:
42407
diff
changeset
|
274 |
|
38524 | 275 |
// ----- for testing purpose ----- |
276 |
||
277 |
public static enum Info { |
|
278 |
REQUIRES, |
|
42407
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41860
diff
changeset
|
279 |
REQUIRES_TRANSITIVE, |
38524 | 280 |
EXPORTED_API, |
281 |
MODULE_PRIVATE, |
|
282 |
QUALIFIED_EXPORTED_API, |
|
283 |
INTERNAL_API, |
|
38532
24f77d64bb1f
8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents:
38524
diff
changeset
|
284 |
JDK_INTERNAL_API, |
24f77d64bb1f
8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents:
38524
diff
changeset
|
285 |
JDK_REMOVED_INTERNAL_API |
38524 | 286 |
} |
287 |
||
288 |
public static class Node { |
|
289 |
public final String name; |
|
290 |
public final String source; |
|
291 |
public final Info info; |
|
292 |
Node(String name, Info info) { |
|
293 |
this(name, name, info); |
|
294 |
} |
|
295 |
Node(String name, String source, Info info) { |
|
296 |
this.name = name; |
|
297 |
this.source = source; |
|
298 |
this.info = info; |
|
299 |
} |
|
300 |
||
301 |
@Override |
|
302 |
public String toString() { |
|
303 |
StringBuilder sb = new StringBuilder(); |
|
42407
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41860
diff
changeset
|
304 |
if (info != Info.REQUIRES && info != Info.REQUIRES_TRANSITIVE) |
38524 | 305 |
sb.append(source).append("/"); |
306 |
||
307 |
sb.append(name); |
|
308 |
if (info == Info.QUALIFIED_EXPORTED_API) |
|
309 |
sb.append(" (qualified)"); |
|
310 |
else if (info == Info.JDK_INTERNAL_API) |
|
311 |
sb.append(" (JDK internal)"); |
|
312 |
else if (info == Info.INTERNAL_API) |
|
313 |
sb.append(" (internal)"); |
|
314 |
return sb.toString(); |
|
315 |
} |
|
316 |
||
317 |
@Override |
|
318 |
public boolean equals(Object o) { |
|
319 |
if (!(o instanceof Node)) |
|
320 |
return false; |
|
321 |
||
322 |
Node other = (Node)o; |
|
323 |
return this.name.equals(other.name) && |
|
324 |
this.source.equals(other.source) && |
|
325 |
this.info.equals(other.info); |
|
326 |
} |
|
327 |
||
328 |
@Override |
|
329 |
public int hashCode() { |
|
330 |
int result = name.hashCode(); |
|
331 |
result = 31 * result + source.hashCode(); |
|
332 |
result = 31 * result + info.hashCode(); |
|
333 |
return result; |
|
334 |
} |
|
335 |
} |
|
336 |
||
337 |
/** |
|
338 |
* Returns a graph of module dependences. |
|
339 |
* |
|
340 |
* Each Node represents a module and each edge is a dependence. |
|
42407
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41860
diff
changeset
|
341 |
* No analysis on "requires transitive". |
38524 | 342 |
*/ |
343 |
public Graph<Node> moduleGraph() { |
|
344 |
Graph.Builder<Node> builder = new Graph.Builder<>(); |
|
345 |
||
346 |
archives().stream() |
|
347 |
.forEach(m -> { |
|
348 |
Node u = new Node(m.getName(), Info.REQUIRES); |
|
349 |
builder.addNode(u); |
|
350 |
analyzer.requires(m) |
|
351 |
.map(req -> new Node(req.getName(), Info.REQUIRES)) |
|
352 |
.forEach(v -> builder.addEdge(u, v)); |
|
353 |
}); |
|
354 |
return builder.build(); |
|
355 |
} |
|
356 |
||
357 |
/** |
|
358 |
* Returns a graph of dependences. |
|
359 |
* |
|
360 |
* Each Node represents a class or package per the specified verbose level. |
|
361 |
* Each edge indicates |
|
362 |
*/ |
|
363 |
public Graph<Node> dependenceGraph() { |
|
364 |
Graph.Builder<Node> builder = new Graph.Builder<>(); |
|
365 |
||
366 |
archives().stream() |
|
367 |
.map(analyzer.results::get) |
|
368 |
.filter(deps -> !deps.dependencies().isEmpty()) |
|
369 |
.flatMap(deps -> deps.dependencies().stream()) |
|
370 |
.forEach(d -> addEdge(builder, d)); |
|
371 |
return builder.build(); |
|
372 |
} |
|
373 |
||
374 |
private void addEdge(Graph.Builder<Node> builder, Analyzer.Dep dep) { |
|
375 |
Archive source = dep.originArchive(); |
|
376 |
Archive target = dep.targetArchive(); |
|
377 |
String pn = dep.target(); |
|
41860
906670ff49c7
8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
40762
diff
changeset
|
378 |
if (verbose == CLASS || verbose == VERBOSE) { |
38524 | 379 |
int i = dep.target().lastIndexOf('.'); |
380 |
pn = i > 0 ? dep.target().substring(0, i) : ""; |
|
381 |
} |
|
382 |
final Info info; |
|
41860
906670ff49c7
8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
40762
diff
changeset
|
383 |
Module targetModule = target.getModule(); |
38524 | 384 |
if (source == target) { |
385 |
info = Info.MODULE_PRIVATE; |
|
41860
906670ff49c7
8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
40762
diff
changeset
|
386 |
} else if (!targetModule.isNamed()) { |
38524 | 387 |
info = Info.EXPORTED_API; |
41860
906670ff49c7
8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
40762
diff
changeset
|
388 |
} else if (targetModule.isExported(pn) && !targetModule.isJDKUnsupported()) { |
38524 | 389 |
info = Info.EXPORTED_API; |
390 |
} else { |
|
391 |
Module module = target.getModule(); |
|
38532
24f77d64bb1f
8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents:
38524
diff
changeset
|
392 |
if (module == Analyzer.REMOVED_JDK_INTERNALS) { |
24f77d64bb1f
8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents:
38524
diff
changeset
|
393 |
info = Info.JDK_REMOVED_INTERNAL_API; |
24f77d64bb1f
8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents:
38524
diff
changeset
|
394 |
} else if (!source.getModule().isJDK() && module.isJDK()) |
38524 | 395 |
info = Info.JDK_INTERNAL_API; |
396 |
// qualified exports or inaccessible |
|
397 |
else if (module.isExported(pn, source.getModule().name())) |
|
398 |
info = Info.QUALIFIED_EXPORTED_API; |
|
399 |
else |
|
400 |
info = Info.INTERNAL_API; |
|
401 |
} |
|
402 |
||
403 |
Node u = new Node(dep.origin(), source.getName(), info); |
|
404 |
Node v = new Node(dep.target(), target.getName(), info); |
|
405 |
builder.addEdge(u, v); |
|
406 |
} |
|
407 |
||
408 |
} |