author | mchung |
Wed, 21 Nov 2018 22:34:01 -0800 | |
changeset 52650 | c16b6cc93272 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
36526 | 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 |
package com.sun.tools.jdeps; |
|
26 |
||
27 |
import com.sun.tools.classfile.Dependencies; |
|
28 |
import com.sun.tools.classfile.Dependency; |
|
29 |
import com.sun.tools.classfile.Dependency.Location; |
|
30 |
||
31 |
import java.util.HashSet; |
|
32 |
import java.util.Set; |
|
33 |
import java.util.regex.Pattern; |
|
34 |
||
35 |
/* |
|
36 |
* Filter configured based on the input jdeps option |
|
37 |
* 1. -p and -regex to match target dependencies |
|
38 |
* 2. -filter:package to filter out same-package dependencies |
|
39 |
* This filter is applied when jdeps parses the class files |
|
40 |
* and filtered dependencies are not stored in the Analyzer. |
|
41860
906670ff49c7
8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
38524
diff
changeset
|
41 |
* 3. --require specifies to match target dependence from the given module |
36526 | 42 |
* This gets expanded into package lists to be filtered. |
43 |
* 4. -filter:archive to filter out same-archive dependencies |
|
44 |
* This filter is applied later in the Analyzer as the |
|
45 |
* containing archive of a target class may not be known until |
|
46 |
* the entire archive |
|
47 |
*/ |
|
38524 | 48 |
public class JdepsFilter implements Dependency.Filter, Analyzer.Filter { |
49 |
||
50 |
public static final JdepsFilter DEFAULT_FILTER = |
|
51 |
new JdepsFilter.Builder().filter(true, true).build(); |
|
52 |
||
36526 | 53 |
private final Dependency.Filter filter; |
54 |
private final Pattern filterPattern; |
|
55 |
private final boolean filterSamePackage; |
|
56 |
private final boolean filterSameArchive; |
|
57 |
private final boolean findJDKInternals; |
|
52650
c16b6cc93272
8213909: jdeps --print-module-deps should report missing dependences
mchung
parents:
47216
diff
changeset
|
58 |
private final boolean findMissingDeps; |
36526 | 59 |
private final Pattern includePattern; |
38524 | 60 |
|
61 |
private final Set<String> requires; |
|
36526 | 62 |
|
63 |
private JdepsFilter(Dependency.Filter filter, |
|
64 |
Pattern filterPattern, |
|
65 |
boolean filterSamePackage, |
|
66 |
boolean filterSameArchive, |
|
67 |
boolean findJDKInternals, |
|
52650
c16b6cc93272
8213909: jdeps --print-module-deps should report missing dependences
mchung
parents:
47216
diff
changeset
|
68 |
boolean findMissingDeps, |
36526 | 69 |
Pattern includePattern, |
38524 | 70 |
Set<String> requires) { |
36526 | 71 |
this.filter = filter; |
72 |
this.filterPattern = filterPattern; |
|
73 |
this.filterSamePackage = filterSamePackage; |
|
74 |
this.filterSameArchive = filterSameArchive; |
|
75 |
this.findJDKInternals = findJDKInternals; |
|
52650
c16b6cc93272
8213909: jdeps --print-module-deps should report missing dependences
mchung
parents:
47216
diff
changeset
|
76 |
this.findMissingDeps = findMissingDeps; |
36526 | 77 |
this.includePattern = includePattern; |
38524 | 78 |
this.requires = requires; |
36526 | 79 |
} |
80 |
||
81 |
/** |
|
82 |
* Tests if the given class matches the pattern given in the -include option |
|
83 |
* |
|
84 |
* @param cn fully-qualified name |
|
85 |
*/ |
|
86 |
public boolean matches(String cn) { |
|
38524 | 87 |
if (includePattern == null) |
36526 | 88 |
return true; |
89 |
||
90 |
if (includePattern != null) |
|
91 |
return includePattern.matcher(cn).matches(); |
|
92 |
||
93 |
return false; |
|
94 |
} |
|
95 |
||
96 |
/** |
|
38524 | 97 |
* Tests if the given source includes classes specified in -include option |
36526 | 98 |
* |
99 |
* This method can be used to determine if the given source should eagerly |
|
100 |
* be processed. |
|
101 |
*/ |
|
102 |
public boolean matches(Archive source) { |
|
38524 | 103 |
if (includePattern != null) { |
104 |
return source.reader().entries().stream() |
|
105 |
.map(name -> name.replace('/', '.')) |
|
106 |
.filter(name -> !name.equals("module-info.class")) |
|
107 |
.anyMatch(this::matches); |
|
36526 | 108 |
} |
38524 | 109 |
return hasTargetFilter(); |
110 |
} |
|
111 |
||
112 |
public boolean hasIncludePattern() { |
|
42840
dfe1a03d4db4
8171418: Remove jdeps internal --include-system-modules option
mchung
parents:
41860
diff
changeset
|
113 |
return includePattern != null; |
38524 | 114 |
} |
115 |
||
116 |
public boolean hasTargetFilter() { |
|
117 |
return filter != null; |
|
118 |
} |
|
119 |
||
120 |
public Set<String> requiresFilter() { |
|
121 |
return requires; |
|
36526 | 122 |
} |
123 |
||
124 |
// ----- Dependency.Filter ----- |
|
125 |
||
126 |
@Override |
|
127 |
public boolean accepts(Dependency d) { |
|
128 |
if (d.getOrigin().equals(d.getTarget())) |
|
129 |
return false; |
|
130 |
||
131 |
// filter same package dependency |
|
132 |
String pn = d.getTarget().getPackageName(); |
|
133 |
if (filterSamePackage && d.getOrigin().getPackageName().equals(pn)) { |
|
134 |
return false; |
|
135 |
} |
|
136 |
||
137 |
// filter if the target package matches the given filter |
|
138 |
if (filterPattern != null && filterPattern.matcher(pn).matches()) { |
|
139 |
return false; |
|
140 |
} |
|
141 |
||
142 |
// filter if the target matches the given filtered package name or regex |
|
143 |
return filter != null ? filter.accepts(d) : true; |
|
144 |
} |
|
145 |
||
146 |
// ----- Analyzer.Filter ------ |
|
147 |
||
148 |
/** |
|
149 |
* Filter depending on the containing archive or module |
|
150 |
*/ |
|
151 |
@Override |
|
152 |
public boolean accepts(Location origin, Archive originArchive, |
|
153 |
Location target, Archive targetArchive) { |
|
154 |
if (findJDKInternals) { |
|
155 |
// accepts target that is JDK class but not exported |
|
156 |
Module module = targetArchive.getModule(); |
|
157 |
return originArchive != targetArchive && |
|
41860
906670ff49c7
8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
38524
diff
changeset
|
158 |
isJDKInternalPackage(module, target.getPackageName()); |
52650
c16b6cc93272
8213909: jdeps --print-module-deps should report missing dependences
mchung
parents:
47216
diff
changeset
|
159 |
} else if (findMissingDeps) { |
c16b6cc93272
8213909: jdeps --print-module-deps should report missing dependences
mchung
parents:
47216
diff
changeset
|
160 |
return Analyzer.notFound(targetArchive); |
36526 | 161 |
} else if (filterSameArchive) { |
162 |
// accepts origin and target that from different archive |
|
163 |
return originArchive != targetArchive; |
|
164 |
} |
|
165 |
return true; |
|
166 |
} |
|
167 |
||
41860
906670ff49c7
8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
38524
diff
changeset
|
168 |
/** |
906670ff49c7
8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
38524
diff
changeset
|
169 |
* Tests if the package is an internal package of the given module. |
906670ff49c7
8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
38524
diff
changeset
|
170 |
*/ |
906670ff49c7
8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
38524
diff
changeset
|
171 |
public boolean isJDKInternalPackage(Module module, String pn) { |
906670ff49c7
8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
38524
diff
changeset
|
172 |
if (module.isJDKUnsupported()) { |
906670ff49c7
8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
38524
diff
changeset
|
173 |
// its exported APIs are unsupported |
906670ff49c7
8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
38524
diff
changeset
|
174 |
return true; |
906670ff49c7
8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
38524
diff
changeset
|
175 |
} |
906670ff49c7
8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
38524
diff
changeset
|
176 |
|
906670ff49c7
8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
38524
diff
changeset
|
177 |
return module.isJDK() && !module.isExported(pn); |
906670ff49c7
8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
38524
diff
changeset
|
178 |
} |
906670ff49c7
8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
38524
diff
changeset
|
179 |
|
36526 | 180 |
@Override |
181 |
public String toString() { |
|
182 |
StringBuilder sb = new StringBuilder(); |
|
38524 | 183 |
sb.append("include pattern: ").append(includePattern).append("\n"); |
36526 | 184 |
sb.append("filter same archive: ").append(filterSameArchive).append("\n"); |
185 |
sb.append("filter same package: ").append(filterSamePackage).append("\n"); |
|
38524 | 186 |
sb.append("requires: ").append(requires).append("\n"); |
36526 | 187 |
return sb.toString(); |
188 |
} |
|
189 |
||
38524 | 190 |
public static class Builder { |
36526 | 191 |
Pattern filterPattern; |
38524 | 192 |
Pattern regex; |
36526 | 193 |
boolean filterSamePackage; |
194 |
boolean filterSameArchive; |
|
195 |
boolean findJDKInterals; |
|
52650
c16b6cc93272
8213909: jdeps --print-module-deps should report missing dependences
mchung
parents:
47216
diff
changeset
|
196 |
boolean findMissingDeps; |
36526 | 197 |
// source filters |
198 |
Pattern includePattern; |
|
38524 | 199 |
Set<String> requires = new HashSet<>(); |
200 |
Set<String> targetPackages = new HashSet<>(); |
|
36526 | 201 |
|
42840
dfe1a03d4db4
8171418: Remove jdeps internal --include-system-modules option
mchung
parents:
41860
diff
changeset
|
202 |
public Builder() {}; |
dfe1a03d4db4
8171418: Remove jdeps internal --include-system-modules option
mchung
parents:
41860
diff
changeset
|
203 |
|
36526 | 204 |
public Builder packages(Set<String> packageNames) { |
38524 | 205 |
this.targetPackages.addAll(packageNames); |
36526 | 206 |
return this; |
207 |
} |
|
208 |
public Builder regex(Pattern regex) { |
|
38524 | 209 |
this.regex = regex; |
36526 | 210 |
return this; |
211 |
} |
|
212 |
public Builder filter(Pattern regex) { |
|
213 |
this.filterPattern = regex; |
|
214 |
return this; |
|
215 |
} |
|
216 |
public Builder filter(boolean samePackage, boolean sameArchive) { |
|
217 |
this.filterSamePackage = samePackage; |
|
218 |
this.filterSameArchive = sameArchive; |
|
219 |
return this; |
|
220 |
} |
|
38524 | 221 |
public Builder requires(String name, Set<String> packageNames) { |
222 |
this.requires.add(name); |
|
223 |
this.targetPackages.addAll(packageNames); |
|
224 |
return this; |
|
225 |
} |
|
36526 | 226 |
public Builder findJDKInternals(boolean value) { |
227 |
this.findJDKInterals = value; |
|
228 |
return this; |
|
229 |
} |
|
52650
c16b6cc93272
8213909: jdeps --print-module-deps should report missing dependences
mchung
parents:
47216
diff
changeset
|
230 |
public Builder findMissingDeps(boolean value) { |
c16b6cc93272
8213909: jdeps --print-module-deps should report missing dependences
mchung
parents:
47216
diff
changeset
|
231 |
this.findMissingDeps = value; |
c16b6cc93272
8213909: jdeps --print-module-deps should report missing dependences
mchung
parents:
47216
diff
changeset
|
232 |
return this; |
c16b6cc93272
8213909: jdeps --print-module-deps should report missing dependences
mchung
parents:
47216
diff
changeset
|
233 |
} |
36526 | 234 |
public Builder includePattern(Pattern regex) { |
235 |
this.includePattern = regex; |
|
236 |
return this; |
|
237 |
} |
|
238 |
||
38524 | 239 |
public JdepsFilter build() { |
240 |
Dependency.Filter filter = null; |
|
241 |
if (regex != null) |
|
242 |
filter = Dependencies.getRegexFilter(regex); |
|
243 |
else if (!targetPackages.isEmpty()) { |
|
244 |
filter = Dependencies.getPackageFilter(targetPackages, false); |
|
245 |
} |
|
36526 | 246 |
return new JdepsFilter(filter, |
247 |
filterPattern, |
|
248 |
filterSamePackage, |
|
249 |
filterSameArchive, |
|
250 |
findJDKInterals, |
|
52650
c16b6cc93272
8213909: jdeps --print-module-deps should report missing dependences
mchung
parents:
47216
diff
changeset
|
251 |
findMissingDeps, |
36526 | 252 |
includePattern, |
38524 | 253 |
requires); |
36526 | 254 |
} |
255 |
||
256 |
} |
|
257 |
} |