author | mchung |
Mon, 19 Dec 2016 12:30:39 -0800 | |
changeset 42840 | dfe1a03d4db4 |
parent 41860 | 906670ff49c7 |
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; |
|
58 |
private final Pattern includePattern; |
|
38524 | 59 |
|
60 |
private final Set<String> requires; |
|
36526 | 61 |
|
62 |
private JdepsFilter(Dependency.Filter filter, |
|
63 |
Pattern filterPattern, |
|
64 |
boolean filterSamePackage, |
|
65 |
boolean filterSameArchive, |
|
66 |
boolean findJDKInternals, |
|
67 |
Pattern includePattern, |
|
38524 | 68 |
Set<String> requires) { |
36526 | 69 |
this.filter = filter; |
70 |
this.filterPattern = filterPattern; |
|
71 |
this.filterSamePackage = filterSamePackage; |
|
72 |
this.filterSameArchive = filterSameArchive; |
|
73 |
this.findJDKInternals = findJDKInternals; |
|
74 |
this.includePattern = includePattern; |
|
38524 | 75 |
this.requires = requires; |
36526 | 76 |
} |
77 |
||
78 |
/** |
|
79 |
* Tests if the given class matches the pattern given in the -include option |
|
80 |
* |
|
81 |
* @param cn fully-qualified name |
|
82 |
*/ |
|
83 |
public boolean matches(String cn) { |
|
38524 | 84 |
if (includePattern == null) |
36526 | 85 |
return true; |
86 |
||
87 |
if (includePattern != null) |
|
88 |
return includePattern.matcher(cn).matches(); |
|
89 |
||
90 |
return false; |
|
91 |
} |
|
92 |
||
93 |
/** |
|
38524 | 94 |
* Tests if the given source includes classes specified in -include option |
36526 | 95 |
* |
96 |
* This method can be used to determine if the given source should eagerly |
|
97 |
* be processed. |
|
98 |
*/ |
|
99 |
public boolean matches(Archive source) { |
|
38524 | 100 |
if (includePattern != null) { |
101 |
return source.reader().entries().stream() |
|
102 |
.map(name -> name.replace('/', '.')) |
|
103 |
.filter(name -> !name.equals("module-info.class")) |
|
104 |
.anyMatch(this::matches); |
|
36526 | 105 |
} |
38524 | 106 |
return hasTargetFilter(); |
107 |
} |
|
108 |
||
109 |
public boolean hasIncludePattern() { |
|
42840
dfe1a03d4db4
8171418: Remove jdeps internal --include-system-modules option
mchung
parents:
41860
diff
changeset
|
110 |
return includePattern != null; |
38524 | 111 |
} |
112 |
||
113 |
public boolean hasTargetFilter() { |
|
114 |
return filter != null; |
|
115 |
} |
|
116 |
||
117 |
public Set<String> requiresFilter() { |
|
118 |
return requires; |
|
36526 | 119 |
} |
120 |
||
121 |
// ----- Dependency.Filter ----- |
|
122 |
||
123 |
@Override |
|
124 |
public boolean accepts(Dependency d) { |
|
125 |
if (d.getOrigin().equals(d.getTarget())) |
|
126 |
return false; |
|
127 |
||
128 |
// filter same package dependency |
|
129 |
String pn = d.getTarget().getPackageName(); |
|
130 |
if (filterSamePackage && d.getOrigin().getPackageName().equals(pn)) { |
|
131 |
return false; |
|
132 |
} |
|
133 |
||
134 |
// filter if the target package matches the given filter |
|
135 |
if (filterPattern != null && filterPattern.matcher(pn).matches()) { |
|
136 |
return false; |
|
137 |
} |
|
138 |
||
139 |
// filter if the target matches the given filtered package name or regex |
|
140 |
return filter != null ? filter.accepts(d) : true; |
|
141 |
} |
|
142 |
||
143 |
// ----- Analyzer.Filter ------ |
|
144 |
||
145 |
/** |
|
146 |
* Filter depending on the containing archive or module |
|
147 |
*/ |
|
148 |
@Override |
|
149 |
public boolean accepts(Location origin, Archive originArchive, |
|
150 |
Location target, Archive targetArchive) { |
|
151 |
if (findJDKInternals) { |
|
152 |
// accepts target that is JDK class but not exported |
|
153 |
Module module = targetArchive.getModule(); |
|
154 |
return originArchive != targetArchive && |
|
41860
906670ff49c7
8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
38524
diff
changeset
|
155 |
isJDKInternalPackage(module, target.getPackageName()); |
36526 | 156 |
} else if (filterSameArchive) { |
157 |
// accepts origin and target that from different archive |
|
158 |
return originArchive != targetArchive; |
|
159 |
} |
|
160 |
return true; |
|
161 |
} |
|
162 |
||
41860
906670ff49c7
8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
38524
diff
changeset
|
163 |
/** |
906670ff49c7
8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
38524
diff
changeset
|
164 |
* 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
|
165 |
*/ |
906670ff49c7
8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
38524
diff
changeset
|
166 |
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
|
167 |
if (module.isJDKUnsupported()) { |
906670ff49c7
8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
38524
diff
changeset
|
168 |
// 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
|
169 |
return true; |
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 |
|
906670ff49c7
8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
38524
diff
changeset
|
172 |
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
|
173 |
} |
906670ff49c7
8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
38524
diff
changeset
|
174 |
|
36526 | 175 |
@Override |
176 |
public String toString() { |
|
177 |
StringBuilder sb = new StringBuilder(); |
|
38524 | 178 |
sb.append("include pattern: ").append(includePattern).append("\n"); |
36526 | 179 |
sb.append("filter same archive: ").append(filterSameArchive).append("\n"); |
180 |
sb.append("filter same package: ").append(filterSamePackage).append("\n"); |
|
38524 | 181 |
sb.append("requires: ").append(requires).append("\n"); |
36526 | 182 |
return sb.toString(); |
183 |
} |
|
184 |
||
38524 | 185 |
public static class Builder { |
36526 | 186 |
Pattern filterPattern; |
38524 | 187 |
Pattern regex; |
36526 | 188 |
boolean filterSamePackage; |
189 |
boolean filterSameArchive; |
|
190 |
boolean findJDKInterals; |
|
191 |
// source filters |
|
192 |
Pattern includePattern; |
|
38524 | 193 |
Set<String> requires = new HashSet<>(); |
194 |
Set<String> targetPackages = new HashSet<>(); |
|
36526 | 195 |
|
42840
dfe1a03d4db4
8171418: Remove jdeps internal --include-system-modules option
mchung
parents:
41860
diff
changeset
|
196 |
public Builder() {}; |
dfe1a03d4db4
8171418: Remove jdeps internal --include-system-modules option
mchung
parents:
41860
diff
changeset
|
197 |
|
36526 | 198 |
public Builder packages(Set<String> packageNames) { |
38524 | 199 |
this.targetPackages.addAll(packageNames); |
36526 | 200 |
return this; |
201 |
} |
|
202 |
public Builder regex(Pattern regex) { |
|
38524 | 203 |
this.regex = regex; |
36526 | 204 |
return this; |
205 |
} |
|
206 |
public Builder filter(Pattern regex) { |
|
207 |
this.filterPattern = regex; |
|
208 |
return this; |
|
209 |
} |
|
210 |
public Builder filter(boolean samePackage, boolean sameArchive) { |
|
211 |
this.filterSamePackage = samePackage; |
|
212 |
this.filterSameArchive = sameArchive; |
|
213 |
return this; |
|
214 |
} |
|
38524 | 215 |
public Builder requires(String name, Set<String> packageNames) { |
216 |
this.requires.add(name); |
|
217 |
this.targetPackages.addAll(packageNames); |
|
218 |
return this; |
|
219 |
} |
|
36526 | 220 |
public Builder findJDKInternals(boolean value) { |
221 |
this.findJDKInterals = value; |
|
222 |
return this; |
|
223 |
} |
|
224 |
public Builder includePattern(Pattern regex) { |
|
225 |
this.includePattern = regex; |
|
226 |
return this; |
|
227 |
} |
|
228 |
||
38524 | 229 |
public JdepsFilter build() { |
230 |
Dependency.Filter filter = null; |
|
231 |
if (regex != null) |
|
232 |
filter = Dependencies.getRegexFilter(regex); |
|
233 |
else if (!targetPackages.isEmpty()) { |
|
234 |
filter = Dependencies.getPackageFilter(targetPackages, false); |
|
235 |
} |
|
36526 | 236 |
return new JdepsFilter(filter, |
237 |
filterPattern, |
|
238 |
filterSamePackage, |
|
239 |
filterSameArchive, |
|
240 |
findJDKInterals, |
|
241 |
includePattern, |
|
38524 | 242 |
requires); |
36526 | 243 |
} |
244 |
||
245 |
} |
|
246 |
} |