27 import com.sun.tools.classfile.Dependencies; |
27 import com.sun.tools.classfile.Dependencies; |
28 import com.sun.tools.classfile.Dependency; |
28 import com.sun.tools.classfile.Dependency; |
29 import com.sun.tools.classfile.Dependency.Location; |
29 import com.sun.tools.classfile.Dependency.Location; |
30 |
30 |
31 import java.util.HashSet; |
31 import java.util.HashSet; |
32 import java.util.Optional; |
|
33 import java.util.Set; |
32 import java.util.Set; |
34 import java.util.regex.Pattern; |
33 import java.util.regex.Pattern; |
35 import java.util.stream.Collectors; |
|
36 import java.util.stream.Stream; |
|
37 |
34 |
38 /* |
35 /* |
39 * Filter configured based on the input jdeps option |
36 * Filter configured based on the input jdeps option |
40 * 1. -p and -regex to match target dependencies |
37 * 1. -p and -regex to match target dependencies |
41 * 2. -filter:package to filter out same-package dependencies |
38 * 2. -filter:package to filter out same-package dependencies |
57 private final Pattern filterPattern; |
54 private final Pattern filterPattern; |
58 private final boolean filterSamePackage; |
55 private final boolean filterSamePackage; |
59 private final boolean filterSameArchive; |
56 private final boolean filterSameArchive; |
60 private final boolean findJDKInternals; |
57 private final boolean findJDKInternals; |
61 private final Pattern includePattern; |
58 private final Pattern includePattern; |
62 private final Pattern includeSystemModules; |
|
63 |
59 |
64 private final Set<String> requires; |
60 private final Set<String> requires; |
65 |
61 |
66 private JdepsFilter(Dependency.Filter filter, |
62 private JdepsFilter(Dependency.Filter filter, |
67 Pattern filterPattern, |
63 Pattern filterPattern, |
68 boolean filterSamePackage, |
64 boolean filterSamePackage, |
69 boolean filterSameArchive, |
65 boolean filterSameArchive, |
70 boolean findJDKInternals, |
66 boolean findJDKInternals, |
71 Pattern includePattern, |
67 Pattern includePattern, |
72 Pattern includeSystemModules, |
|
73 Set<String> requires) { |
68 Set<String> requires) { |
74 this.filter = filter; |
69 this.filter = filter; |
75 this.filterPattern = filterPattern; |
70 this.filterPattern = filterPattern; |
76 this.filterSamePackage = filterSamePackage; |
71 this.filterSamePackage = filterSamePackage; |
77 this.filterSameArchive = filterSameArchive; |
72 this.filterSameArchive = filterSameArchive; |
78 this.findJDKInternals = findJDKInternals; |
73 this.findJDKInternals = findJDKInternals; |
79 this.includePattern = includePattern; |
74 this.includePattern = includePattern; |
80 this.includeSystemModules = includeSystemModules; |
|
81 this.requires = requires; |
75 this.requires = requires; |
82 } |
76 } |
83 |
77 |
84 /** |
78 /** |
85 * Tests if the given class matches the pattern given in the -include option |
79 * Tests if the given class matches the pattern given in the -include option |
110 .anyMatch(this::matches); |
104 .anyMatch(this::matches); |
111 } |
105 } |
112 return hasTargetFilter(); |
106 return hasTargetFilter(); |
113 } |
107 } |
114 |
108 |
115 public boolean include(Archive source) { |
|
116 Module module = source.getModule(); |
|
117 // skip system module by default; or if includeSystemModules is set |
|
118 // only include the ones matching the pattern |
|
119 return !module.isSystem() || (includeSystemModules != null && |
|
120 includeSystemModules.matcher(module.name()).matches()); |
|
121 } |
|
122 |
|
123 public boolean hasIncludePattern() { |
109 public boolean hasIncludePattern() { |
124 return includePattern != null || includeSystemModules != null; |
110 return includePattern != null; |
125 } |
111 } |
126 |
112 |
127 public boolean hasTargetFilter() { |
113 public boolean hasTargetFilter() { |
128 return filter != null; |
114 return filter != null; |
129 } |
115 } |
195 sb.append("requires: ").append(requires).append("\n"); |
181 sb.append("requires: ").append(requires).append("\n"); |
196 return sb.toString(); |
182 return sb.toString(); |
197 } |
183 } |
198 |
184 |
199 public static class Builder { |
185 public static class Builder { |
200 static Pattern SYSTEM_MODULE_PATTERN = Pattern.compile("java\\..*|jdk\\..*|javafx\\..*"); |
|
201 Pattern filterPattern; |
186 Pattern filterPattern; |
202 Pattern regex; |
187 Pattern regex; |
203 boolean filterSamePackage; |
188 boolean filterSamePackage; |
204 boolean filterSameArchive; |
189 boolean filterSameArchive; |
205 boolean findJDKInterals; |
190 boolean findJDKInterals; |
206 // source filters |
191 // source filters |
207 Pattern includePattern; |
192 Pattern includePattern; |
208 Pattern includeSystemModules; |
|
209 Set<String> requires = new HashSet<>(); |
193 Set<String> requires = new HashSet<>(); |
210 Set<String> targetPackages = new HashSet<>(); |
194 Set<String> targetPackages = new HashSet<>(); |
|
195 |
|
196 public Builder() {}; |
211 |
197 |
212 public Builder packages(Set<String> packageNames) { |
198 public Builder packages(Set<String> packageNames) { |
213 this.targetPackages.addAll(packageNames); |
199 this.targetPackages.addAll(packageNames); |
214 return this; |
200 return this; |
215 } |
201 } |
227 return this; |
213 return this; |
228 } |
214 } |
229 public Builder requires(String name, Set<String> packageNames) { |
215 public Builder requires(String name, Set<String> packageNames) { |
230 this.requires.add(name); |
216 this.requires.add(name); |
231 this.targetPackages.addAll(packageNames); |
217 this.targetPackages.addAll(packageNames); |
232 |
|
233 includeIfSystemModule(name); |
|
234 return this; |
218 return this; |
235 } |
219 } |
236 public Builder findJDKInternals(boolean value) { |
220 public Builder findJDKInternals(boolean value) { |
237 this.findJDKInterals = value; |
221 this.findJDKInterals = value; |
238 return this; |
222 return this; |
239 } |
223 } |
240 public Builder includePattern(Pattern regex) { |
224 public Builder includePattern(Pattern regex) { |
241 this.includePattern = regex; |
225 this.includePattern = regex; |
242 return this; |
|
243 } |
|
244 public Builder includeSystemModules(Pattern regex) { |
|
245 this.includeSystemModules = regex; |
|
246 return this; |
|
247 } |
|
248 public Builder includeIfSystemModule(String name) { |
|
249 if (includeSystemModules == null && |
|
250 SYSTEM_MODULE_PATTERN.matcher(name).matches()) { |
|
251 this.includeSystemModules = SYSTEM_MODULE_PATTERN; |
|
252 } |
|
253 return this; |
226 return this; |
254 } |
227 } |
255 |
228 |
256 public JdepsFilter build() { |
229 public JdepsFilter build() { |
257 Dependency.Filter filter = null; |
230 Dependency.Filter filter = null; |