author | serb |
Sat, 09 Jun 2018 13:33:35 -0700 | |
changeset 50647 | a98ff7c2103d |
parent 47216 | 71c04702a3d5 |
child 52650 | c16b6cc93272 |
permissions | -rw-r--r-- |
36526 | 1 |
/* |
2 |
* Copyright (c) 2015, 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 |
package com.sun.tools.jdeps; |
|
26 |
||
38524 | 27 |
import static com.sun.tools.jdeps.Module.*; |
28 |
import static com.sun.tools.jdeps.Analyzer.NOT_FOUND; |
|
29 |
import static java.util.stream.Collectors.*; |
|
30 |
||
36526 | 31 |
import com.sun.tools.classfile.AccessFlags; |
32 |
import com.sun.tools.classfile.ClassFile; |
|
33 |
import com.sun.tools.classfile.ConstantPoolException; |
|
34 |
import com.sun.tools.classfile.Dependencies; |
|
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42407
diff
changeset
|
35 |
import com.sun.tools.classfile.Dependencies.ClassFileError; |
36526 | 36 |
import com.sun.tools.classfile.Dependency; |
38524 | 37 |
import com.sun.tools.classfile.Dependency.Location; |
36526 | 38 |
|
39 |
import java.io.IOException; |
|
38524 | 40 |
import java.io.UncheckedIOException; |
42407
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38524
diff
changeset
|
41 |
import java.util.Collections; |
36526 | 42 |
import java.util.Deque; |
43 |
import java.util.HashMap; |
|
38524 | 44 |
import java.util.HashSet; |
36526 | 45 |
import java.util.Map; |
46 |
import java.util.Optional; |
|
47 |
import java.util.Set; |
|
48 |
import java.util.concurrent.Callable; |
|
38524 | 49 |
import java.util.concurrent.ConcurrentHashMap; |
36526 | 50 |
import java.util.concurrent.ConcurrentLinkedDeque; |
51 |
import java.util.concurrent.ExecutionException; |
|
52 |
import java.util.concurrent.ExecutorService; |
|
53 |
import java.util.concurrent.Executors; |
|
54 |
import java.util.concurrent.FutureTask; |
|
55 |
import java.util.stream.Stream; |
|
56 |
||
38524 | 57 |
/** |
58 |
* Parses class files and finds dependences |
|
59 |
*/ |
|
60 |
class DependencyFinder { |
|
61 |
private static Finder API_FINDER = new Finder(true); |
|
62 |
private static Finder CLASS_FINDER = new Finder(false); |
|
63 |
||
64 |
private final JdepsConfiguration configuration; |
|
65 |
private final JdepsFilter filter; |
|
36526 | 66 |
|
38524 | 67 |
private final Map<Finder, Deque<Archive>> parsedArchives = new ConcurrentHashMap<>(); |
68 |
private final Map<Location, Archive> parsedClasses = new ConcurrentHashMap<>(); |
|
69 |
||
70 |
private final ExecutorService pool = Executors.newFixedThreadPool(2); |
|
71 |
private final Deque<FutureTask<Set<Location>>> tasks = new ConcurrentLinkedDeque<>(); |
|
36526 | 72 |
|
38524 | 73 |
DependencyFinder(JdepsConfiguration configuration, |
74 |
JdepsFilter filter) { |
|
75 |
this.configuration = configuration; |
|
76 |
this.filter = filter; |
|
77 |
this.parsedArchives.put(API_FINDER, new ConcurrentLinkedDeque<>()); |
|
78 |
this.parsedArchives.put(CLASS_FINDER, new ConcurrentLinkedDeque<>()); |
|
36526 | 79 |
} |
80 |
||
38524 | 81 |
Map<Location, Archive> locationToArchive() { |
82 |
return parsedClasses; |
|
36526 | 83 |
} |
84 |
||
38524 | 85 |
/** |
86 |
* Returns the modules of all dependencies found |
|
36526 | 87 |
*/ |
38524 | 88 |
Stream<Archive> getDependences(Archive source) { |
89 |
return source.getDependencies() |
|
90 |
.map(this::locationToArchive) |
|
91 |
.filter(a -> a != source); |
|
36526 | 92 |
} |
93 |
||
94 |
/** |
|
38524 | 95 |
* Returns the location to archive map; or NOT_FOUND. |
96 |
* |
|
97 |
* Location represents a parsed class. |
|
36526 | 98 |
*/ |
38524 | 99 |
Archive locationToArchive(Location location) { |
100 |
return parsedClasses.containsKey(location) |
|
101 |
? parsedClasses.get(location) |
|
102 |
: configuration.findClass(location).orElse(NOT_FOUND); |
|
36526 | 103 |
} |
104 |
||
105 |
/** |
|
38524 | 106 |
* Returns a map from an archive to its required archives |
36526 | 107 |
*/ |
38524 | 108 |
Map<Archive, Set<Archive>> dependences() { |
109 |
Map<Archive, Set<Archive>> map = new HashMap<>(); |
|
110 |
parsedArchives.values().stream() |
|
111 |
.flatMap(Deque::stream) |
|
112 |
.filter(a -> !a.isEmpty()) |
|
113 |
.forEach(source -> { |
|
114 |
Set<Archive> deps = getDependences(source).collect(toSet()); |
|
115 |
if (!deps.isEmpty()) { |
|
116 |
map.put(source, deps); |
|
117 |
} |
|
118 |
}); |
|
119 |
return map; |
|
120 |
} |
|
121 |
||
122 |
boolean isParsed(Location location) { |
|
123 |
return parsedClasses.containsKey(location); |
|
36526 | 124 |
} |
125 |
||
126 |
/** |
|
38524 | 127 |
* Parses all class files from the given archive stream and returns |
128 |
* all target locations. |
|
36526 | 129 |
*/ |
38524 | 130 |
public Set<Location> parse(Stream<? extends Archive> archiveStream) { |
131 |
archiveStream.forEach(archive -> parse(archive, CLASS_FINDER)); |
|
132 |
return waitForTasksCompleted(); |
|
36526 | 133 |
} |
134 |
||
135 |
/** |
|
38524 | 136 |
* Parses the exported API class files from the given archive stream and |
137 |
* returns all target locations. |
|
36526 | 138 |
*/ |
38524 | 139 |
public Set<Location> parseExportedAPIs(Stream<? extends Archive> archiveStream) { |
140 |
archiveStream.forEach(archive -> parse(archive, API_FINDER)); |
|
141 |
return waitForTasksCompleted(); |
|
142 |
} |
|
143 |
||
144 |
/** |
|
145 |
* Parses the named class from the given archive and |
|
146 |
* returns all target locations the named class references. |
|
147 |
*/ |
|
148 |
public Set<Location> parse(Archive archive, String name) { |
|
149 |
try { |
|
150 |
return parse(archive, CLASS_FINDER, name); |
|
151 |
} catch (IOException e) { |
|
152 |
throw new UncheckedIOException(e); |
|
153 |
} |
|
36526 | 154 |
} |
155 |
||
156 |
/** |
|
38524 | 157 |
* Parses the exported API of the named class from the given archive and |
158 |
* returns all target locations the named class references. |
|
36526 | 159 |
*/ |
38524 | 160 |
public Set<Location> parseExportedAPIs(Archive archive, String name) |
36526 | 161 |
{ |
38524 | 162 |
try { |
163 |
return parse(archive, API_FINDER, name); |
|
164 |
} catch (IOException e) { |
|
165 |
throw new UncheckedIOException(e); |
|
166 |
} |
|
167 |
} |
|
36526 | 168 |
|
38524 | 169 |
private Optional<FutureTask<Set<Location>>> parse(Archive archive, Finder finder) { |
170 |
if (parsedArchives.get(finder).contains(archive)) |
|
171 |
return Optional.empty(); |
|
36526 | 172 |
|
38524 | 173 |
parsedArchives.get(finder).add(archive); |
36526 | 174 |
|
38524 | 175 |
trace("parsing %s %s%n", archive.getName(), archive.path()); |
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42407
diff
changeset
|
176 |
FutureTask<Set<Location>> task = new FutureTask<>(() -> { |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42407
diff
changeset
|
177 |
Set<Location> targets = new HashSet<>(); |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42407
diff
changeset
|
178 |
for (ClassFile cf : archive.reader().getClassFiles()) { |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42407
diff
changeset
|
179 |
if (cf.access_flags.is(AccessFlags.ACC_MODULE)) |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42407
diff
changeset
|
180 |
continue; |
36526 | 181 |
|
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42407
diff
changeset
|
182 |
String classFileName; |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42407
diff
changeset
|
183 |
try { |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42407
diff
changeset
|
184 |
classFileName = cf.getName(); |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42407
diff
changeset
|
185 |
} catch (ConstantPoolException e) { |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42407
diff
changeset
|
186 |
throw new ClassFileError(e); |
36526 | 187 |
} |
38524 | 188 |
|
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42407
diff
changeset
|
189 |
// filter source class/archive |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42407
diff
changeset
|
190 |
String cn = classFileName.replace('/', '.'); |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42407
diff
changeset
|
191 |
if (!finder.accept(archive, cn, cf.access_flags)) |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42407
diff
changeset
|
192 |
continue; |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42407
diff
changeset
|
193 |
|
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42407
diff
changeset
|
194 |
// tests if this class matches the -include |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42407
diff
changeset
|
195 |
if (!filter.matches(cn)) |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42407
diff
changeset
|
196 |
continue; |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42407
diff
changeset
|
197 |
|
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42407
diff
changeset
|
198 |
for (Dependency d : finder.findDependencies(cf)) { |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42407
diff
changeset
|
199 |
if (filter.accepts(d)) { |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42407
diff
changeset
|
200 |
archive.addClass(d.getOrigin(), d.getTarget()); |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42407
diff
changeset
|
201 |
targets.add(d.getTarget()); |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42407
diff
changeset
|
202 |
} else { |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42407
diff
changeset
|
203 |
// ensure that the parsed class is added the archive |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42407
diff
changeset
|
204 |
archive.addClass(d.getOrigin()); |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42407
diff
changeset
|
205 |
} |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42407
diff
changeset
|
206 |
parsedClasses.putIfAbsent(d.getOrigin(), archive); |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42407
diff
changeset
|
207 |
} |
38524 | 208 |
} |
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42407
diff
changeset
|
209 |
|
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42407
diff
changeset
|
210 |
return targets; |
38524 | 211 |
}); |
212 |
tasks.add(task); |
|
213 |
pool.submit(task); |
|
214 |
return Optional.of(task); |
|
215 |
} |
|
36526 | 216 |
|
38524 | 217 |
private Set<Location> parse(Archive archive, Finder finder, String name) |
218 |
throws IOException |
|
219 |
{ |
|
220 |
ClassFile cf = archive.reader().getClassFile(name); |
|
221 |
if (cf == null) { |
|
42407
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38524
diff
changeset
|
222 |
throw new IllegalArgumentException(archive.getName() + |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38524
diff
changeset
|
223 |
" does not contain " + name); |
38524 | 224 |
} |
36526 | 225 |
|
42407
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38524
diff
changeset
|
226 |
if (cf.access_flags.is(AccessFlags.ACC_MODULE)) |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38524
diff
changeset
|
227 |
return Collections.emptySet(); |
f3702cff2933
8169069: Module system implementation refresh (11/2016)
alanb
parents:
38524
diff
changeset
|
228 |
|
38524 | 229 |
Set<Location> targets = new HashSet<>(); |
230 |
String cn; |
|
231 |
try { |
|
232 |
cn = cf.getName().replace('/', '.'); |
|
233 |
} catch (ConstantPoolException e) { |
|
234 |
throw new Dependencies.ClassFileError(e); |
|
36526 | 235 |
} |
236 |
||
38524 | 237 |
if (!finder.accept(archive, cn, cf.access_flags)) |
238 |
return targets; |
|
239 |
||
240 |
// tests if this class matches the -include |
|
241 |
if (!filter.matches(cn)) |
|
242 |
return targets; |
|
36526 | 243 |
|
38524 | 244 |
// skip checking filter.matches |
245 |
for (Dependency d : finder.findDependencies(cf)) { |
|
246 |
if (filter.accepts(d)) { |
|
247 |
targets.add(d.getTarget()); |
|
248 |
archive.addClass(d.getOrigin(), d.getTarget()); |
|
249 |
} else { |
|
250 |
// ensure that the parsed class is added the archive |
|
251 |
archive.addClass(d.getOrigin()); |
|
252 |
} |
|
253 |
parsedClasses.putIfAbsent(d.getOrigin(), archive); |
|
254 |
} |
|
255 |
return targets; |
|
256 |
} |
|
36526 | 257 |
|
38524 | 258 |
/* |
259 |
* Waits until all submitted tasks are completed. |
|
260 |
*/ |
|
261 |
private Set<Location> waitForTasksCompleted() { |
|
262 |
try { |
|
263 |
Set<Location> targets = new HashSet<>(); |
|
264 |
FutureTask<Set<Location>> task; |
|
265 |
while ((task = tasks.poll()) != null) { |
|
266 |
// wait for completion |
|
267 |
if (!task.isDone()) |
|
268 |
targets.addAll(task.get()); |
|
269 |
} |
|
270 |
return targets; |
|
271 |
} catch (InterruptedException|ExecutionException e) { |
|
272 |
throw new Error(e); |
|
273 |
} |
|
274 |
} |
|
36526 | 275 |
|
38524 | 276 |
/* |
277 |
* Shutdown the executor service. |
|
278 |
*/ |
|
279 |
void shutdown() { |
|
280 |
pool.shutdown(); |
|
281 |
} |
|
282 |
||
283 |
private interface SourceFilter { |
|
284 |
boolean accept(Archive archive, String cn, AccessFlags accessFlags); |
|
285 |
} |
|
286 |
||
287 |
private static class Finder implements Dependency.Finder, SourceFilter { |
|
288 |
private final Dependency.Finder finder; |
|
289 |
private final boolean apiOnly; |
|
290 |
Finder(boolean apiOnly) { |
|
291 |
this.apiOnly = apiOnly; |
|
292 |
this.finder = apiOnly |
|
293 |
? Dependencies.getAPIFinder(AccessFlags.ACC_PROTECTED) |
|
294 |
: Dependencies.getClassDependencyFinder(); |
|
295 |
||
36526 | 296 |
} |
297 |
||
38524 | 298 |
@Override |
299 |
public boolean accept(Archive archive, String cn, AccessFlags accessFlags) { |
|
300 |
int i = cn.lastIndexOf('.'); |
|
301 |
String pn = i > 0 ? cn.substring(0, i) : ""; |
|
36526 | 302 |
|
38524 | 303 |
// if -apionly is specified, analyze only exported and public types |
304 |
// All packages are exported in unnamed module. |
|
305 |
return apiOnly ? archive.getModule().isExported(pn) && |
|
306 |
accessFlags.is(AccessFlags.ACC_PUBLIC) |
|
307 |
: true; |
|
36526 | 308 |
} |
309 |
||
38524 | 310 |
@Override |
311 |
public Iterable<? extends Dependency> findDependencies(ClassFile classfile) { |
|
312 |
return finder.findDependencies(classfile); |
|
36526 | 313 |
} |
314 |
} |
|
315 |
} |