4524
|
1 |
/*
|
|
2 |
* Copyright 2009 Sun Microsystems, Inc. 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.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any questions.
|
|
22 |
*/
|
|
23 |
package com.sun.classanalyzer;
|
|
24 |
|
|
25 |
import java.io.BufferedReader;
|
|
26 |
import java.io.FileInputStream;
|
|
27 |
import java.io.IOException;
|
|
28 |
import java.io.InputStreamReader;
|
|
29 |
import java.util.ArrayList;
|
|
30 |
import java.util.Collection;
|
|
31 |
import java.util.LinkedList;
|
|
32 |
import java.util.List;
|
|
33 |
import java.util.Set;
|
|
34 |
import java.util.TreeSet;
|
|
35 |
import java.util.regex.Pattern;
|
|
36 |
|
|
37 |
/**
|
|
38 |
*
|
|
39 |
* @author Mandy Chung
|
|
40 |
*/
|
|
41 |
public class ModuleConfig {
|
|
42 |
|
|
43 |
private static String baseModuleName = "base";
|
|
44 |
private final Set<String> roots;
|
|
45 |
private final Set<String> includes;
|
|
46 |
private final Filter filter;
|
|
47 |
private List<String> members;
|
|
48 |
final String module;
|
|
49 |
final boolean isBase;
|
|
50 |
|
|
51 |
private ModuleConfig(String name) throws IOException {
|
|
52 |
this.roots = new TreeSet<String>();
|
|
53 |
this.includes = new TreeSet<String>();
|
|
54 |
this.module = name;
|
|
55 |
this.isBase = name.equals(baseModuleName);
|
|
56 |
this.filter = new Filter(this);
|
|
57 |
}
|
|
58 |
|
|
59 |
List<String> members() {
|
|
60 |
if (members == null) {
|
|
61 |
members = new LinkedList<String>();
|
|
62 |
|
|
63 |
for (String s : includes) {
|
|
64 |
if (!s.contains("*") && Module.findModule(s) != null) {
|
|
65 |
// module member
|
|
66 |
members.add(s);
|
|
67 |
}
|
|
68 |
}
|
|
69 |
}
|
|
70 |
return members;
|
|
71 |
}
|
|
72 |
|
|
73 |
boolean matchesRoot(String name) {
|
|
74 |
for (String pattern : roots) {
|
|
75 |
if (matches(name, pattern)) {
|
|
76 |
return true;
|
|
77 |
}
|
|
78 |
}
|
|
79 |
return false;
|
|
80 |
}
|
|
81 |
|
|
82 |
boolean matchesIncludes(String name) {
|
|
83 |
for (String pattern : includes) {
|
|
84 |
if (matches(name, pattern)) {
|
|
85 |
return true;
|
|
86 |
}
|
|
87 |
}
|
|
88 |
return false;
|
|
89 |
}
|
|
90 |
|
|
91 |
boolean isExcluded(String name) {
|
|
92 |
return filter.isExcluded(name);
|
|
93 |
}
|
|
94 |
|
|
95 |
boolean matchesPackage(String packageName, String pattern) {
|
|
96 |
int pos = pattern.lastIndexOf('.');
|
|
97 |
String pkg = pos > 0 ? pattern.substring(0, pos) : "<unnamed>";
|
|
98 |
return packageName.equals(pkg);
|
|
99 |
}
|
|
100 |
|
|
101 |
|
|
102 |
boolean matches(String name, String pattern) {
|
|
103 |
if (pattern.contains("**") && !pattern.endsWith("**")) {
|
|
104 |
throw new UnsupportedOperationException("Not yet implemented");
|
|
105 |
}
|
|
106 |
|
|
107 |
String javaName = name;
|
|
108 |
|
|
109 |
boolean isResourceFile = name.indexOf('/') >= 0;
|
|
110 |
if (isResourceFile) {
|
|
111 |
// it's a resource file; convert the name as a java
|
|
112 |
javaName = name.replace('/', '.');
|
|
113 |
}
|
|
114 |
if (pattern.indexOf('/') < 0) {
|
|
115 |
// if the pattern doesn't contain '/
|
|
116 |
return matchesJavaName(javaName, pattern);
|
|
117 |
} else {
|
|
118 |
if (isResourceFile) {
|
|
119 |
// the pattern is for matching resource file
|
|
120 |
return matchesNameWithSlash(name, pattern);
|
|
121 |
} else {
|
|
122 |
return false;
|
|
123 |
}
|
|
124 |
}
|
|
125 |
}
|
|
126 |
|
|
127 |
boolean matchesJavaName(String name, String pattern) {
|
|
128 |
int pos = name.lastIndexOf('.');
|
|
129 |
String packageName = pos > 0 ? name.substring(0, pos) : "<unnamed>";
|
|
130 |
if (pattern.endsWith("**")) {
|
|
131 |
String p = pattern.substring(0, pattern.length() - 2);
|
|
132 |
return name.startsWith(p);
|
|
133 |
} else if (pattern.endsWith("*") && pattern.indexOf('*') == pattern.lastIndexOf('*')) {
|
|
134 |
if (matchesPackage(packageName, pattern)) {
|
|
135 |
// package name has to be exact match
|
|
136 |
String p = pattern.substring(0, pattern.length() - 1);
|
|
137 |
return name.startsWith(p);
|
|
138 |
} else {
|
|
139 |
return false;
|
|
140 |
}
|
|
141 |
} else if (pattern.contains("*")) {
|
|
142 |
String basename = pos > 0 ? name.substring(pos + 1, name.length()) : name;
|
|
143 |
pos = pattern.indexOf('*');
|
|
144 |
String prefix = pattern.substring(0, pos);
|
|
145 |
String suffix = pattern.substring(pos + 1, pattern.length());
|
|
146 |
if (name.startsWith(prefix) && matchesPackage(packageName, prefix)) {
|
|
147 |
// package name has to be exact match
|
|
148 |
if (suffix.contains("*")) {
|
|
149 |
return name.matches(convertToRegex(pattern));
|
|
150 |
} else {
|
|
151 |
return basename.endsWith(suffix);
|
|
152 |
}
|
|
153 |
} else {
|
|
154 |
// we don't support wildcard be used in the package name
|
|
155 |
return false;
|
|
156 |
}
|
|
157 |
} else {
|
|
158 |
// exact match or inner class
|
|
159 |
return name.equals(pattern) || name.startsWith(pattern + "$");
|
|
160 |
}
|
|
161 |
}
|
|
162 |
|
|
163 |
boolean matchesNameWithSlash(String name, String pattern) {
|
|
164 |
if (pattern.endsWith("**")) {
|
|
165 |
String p = pattern.substring(0, pattern.length() - 2);
|
|
166 |
return name.startsWith(p);
|
|
167 |
} else if (pattern.contains("*")) {
|
|
168 |
int pos = pattern.indexOf('*');
|
|
169 |
String prefix = pattern.substring(0, pos);
|
|
170 |
String suffix = pattern.substring(pos + 1, pattern.length());
|
|
171 |
String tail = name.substring(pos, name.length());
|
|
172 |
|
|
173 |
if (!name.startsWith(prefix)) {
|
|
174 |
// prefix has to exact match
|
|
175 |
return false;
|
|
176 |
}
|
|
177 |
|
|
178 |
if (pattern.indexOf('*') == pattern.lastIndexOf('*')) {
|
|
179 |
// exact match prefix with no '/' in the tail string
|
|
180 |
String wildcard = tail.substring(0, tail.length() - suffix.length());
|
|
181 |
return tail.indexOf('/') < 0 && tail.endsWith(suffix);
|
|
182 |
}
|
|
183 |
|
|
184 |
if (suffix.contains("*")) {
|
|
185 |
return matchesNameWithSlash(tail, suffix);
|
|
186 |
} else {
|
|
187 |
// tail ends with the suffix while no '/' in the wildcard matched string
|
|
188 |
String any = tail.substring(0, tail.length() - suffix.length());
|
|
189 |
return tail.endsWith(suffix) && any.indexOf('/') < 0;
|
|
190 |
}
|
|
191 |
} else {
|
|
192 |
// exact match
|
|
193 |
return name.equals(pattern);
|
|
194 |
}
|
|
195 |
}
|
|
196 |
|
|
197 |
private String convertToRegex(String pattern) {
|
|
198 |
StringBuilder sb = new StringBuilder();
|
|
199 |
int i = 0;
|
|
200 |
int index = 0;
|
|
201 |
int plen = pattern.length();
|
|
202 |
while (i < plen) {
|
|
203 |
char p = pattern.charAt(i);
|
|
204 |
if (p == '*') {
|
|
205 |
sb.append("(").append(pattern.substring(index, i)).append(")");
|
|
206 |
if (i + 1 < plen && pattern.charAt(i + 1) == '*') {
|
|
207 |
sb.append(".*");
|
|
208 |
index = i + 2;
|
|
209 |
} else {
|
|
210 |
sb.append("[^\\.]*");
|
|
211 |
index = i + 1;
|
|
212 |
}
|
|
213 |
}
|
|
214 |
i++;
|
|
215 |
}
|
|
216 |
if (index < plen) {
|
|
217 |
sb.append("(").append(pattern.substring(index, plen)).append(")");
|
|
218 |
}
|
|
219 |
return sb.toString();
|
|
220 |
}
|
|
221 |
|
|
222 |
static class Filter {
|
|
223 |
|
|
224 |
final ModuleConfig config;
|
|
225 |
final Set<String> exclude = new TreeSet<String>();
|
|
226 |
final Set<String> allow = new TreeSet<String>();
|
|
227 |
|
|
228 |
Filter(ModuleConfig config) {
|
|
229 |
this.config = config;
|
|
230 |
}
|
|
231 |
|
|
232 |
Filter exclude(String pattern) {
|
|
233 |
exclude.add(pattern);
|
|
234 |
return this;
|
|
235 |
}
|
|
236 |
|
|
237 |
Filter allow(String pattern) {
|
|
238 |
allow.add(pattern);
|
|
239 |
return this;
|
|
240 |
}
|
|
241 |
|
|
242 |
String allowedBy(String name) {
|
|
243 |
String allowedBy = null;
|
|
244 |
for (String pattern : allow) {
|
|
245 |
if (config.matches(name, pattern)) {
|
|
246 |
if (name.equals(pattern)) {
|
|
247 |
return pattern; // exact match
|
|
248 |
}
|
|
249 |
if (allowedBy == null) {
|
|
250 |
allowedBy = pattern;
|
|
251 |
} else {
|
|
252 |
if (pattern.length() > allowedBy.length()) {
|
|
253 |
allowedBy = pattern;
|
|
254 |
}
|
|
255 |
}
|
|
256 |
}
|
|
257 |
}
|
|
258 |
return allowedBy;
|
|
259 |
}
|
|
260 |
|
|
261 |
String excludedBy(String name) {
|
|
262 |
String allowedBy = allowedBy(name);
|
|
263 |
String excludedBy = null;
|
|
264 |
|
|
265 |
if (allowedBy != null && name.equals(allowedBy)) {
|
|
266 |
return null; // exact match
|
|
267 |
}
|
|
268 |
for (String pattern : exclude) {
|
|
269 |
if (config.matches(name, pattern)) {
|
|
270 |
// not matched by allowed rule or exact match
|
|
271 |
if (allowedBy == null || name.equals(pattern)) {
|
|
272 |
return pattern;
|
|
273 |
}
|
|
274 |
if (excludedBy == null) {
|
|
275 |
excludedBy = pattern;
|
|
276 |
} else {
|
|
277 |
if (pattern.length() > excludedBy.length()) {
|
|
278 |
excludedBy = pattern;
|
|
279 |
}
|
|
280 |
}
|
|
281 |
}
|
|
282 |
}
|
|
283 |
return excludedBy;
|
|
284 |
}
|
|
285 |
|
|
286 |
boolean isExcluded(String name) {
|
|
287 |
String allowedBy = allowedBy(name);
|
|
288 |
String excludedBy = excludedBy(name);
|
|
289 |
|
|
290 |
if (excludedBy == null) {
|
|
291 |
return false;
|
|
292 |
}
|
|
293 |
// not matched by allowed rule or exact match
|
|
294 |
if (allowedBy == null || name.equals(excludedBy)) {
|
|
295 |
return true;
|
|
296 |
}
|
|
297 |
|
|
298 |
if (allowedBy == null) {
|
|
299 |
return true;
|
|
300 |
}
|
|
301 |
if (allowedBy != null &&
|
|
302 |
excludedBy.length() > allowedBy.length()) {
|
|
303 |
return true;
|
|
304 |
}
|
|
305 |
return false;
|
|
306 |
}
|
|
307 |
}
|
|
308 |
|
|
309 |
private static String trimComment(String line) {
|
|
310 |
StringBuilder sb = new StringBuilder();
|
|
311 |
|
|
312 |
int pos = 0;
|
|
313 |
while (pos >= 0 && pos < line.length()) {
|
|
314 |
int c1 = line.indexOf("//", pos);
|
|
315 |
if (c1 > 0 && !Character.isWhitespace(line.charAt(c1-1))) {
|
|
316 |
// not a comment
|
|
317 |
c1 = -1;
|
|
318 |
}
|
|
319 |
|
|
320 |
int c2 = line.indexOf("/*", pos);
|
|
321 |
if (c2 > 0 && !Character.isWhitespace(line.charAt(c2-1))) {
|
|
322 |
// not a comment
|
|
323 |
c2 = -1;
|
|
324 |
}
|
|
325 |
|
|
326 |
int c = line.length();
|
|
327 |
int n = line.length();
|
|
328 |
if (c1 >= 0 || c2 >= 0) {
|
|
329 |
if (c1 >= 0) {
|
|
330 |
c = c1;
|
|
331 |
}
|
|
332 |
if (c2 >= 0 && c2 < c) {
|
|
333 |
c = c2;
|
|
334 |
}
|
|
335 |
int c3 = line.indexOf("*/", c2 + 2);
|
|
336 |
if (c == c2 && c3 > c2) {
|
|
337 |
n = c3 + 2;
|
|
338 |
}
|
|
339 |
}
|
|
340 |
if (c > 0) {
|
|
341 |
if (sb.length() > 0) {
|
|
342 |
// add a whitespace if multiple comments on one line
|
|
343 |
sb.append(" ");
|
|
344 |
}
|
|
345 |
sb.append(line.substring(pos, c));
|
|
346 |
}
|
|
347 |
pos = n;
|
|
348 |
}
|
|
349 |
return sb.toString();
|
|
350 |
}
|
|
351 |
|
|
352 |
private static boolean beginBlockComment(String line) {
|
|
353 |
int pos = 0;
|
|
354 |
while (pos >= 0 && pos < line.length()) {
|
|
355 |
int c = line.indexOf("/*", pos);
|
|
356 |
if (c < 0) {
|
|
357 |
return false;
|
|
358 |
}
|
|
359 |
|
|
360 |
if (c > 0 && !Character.isWhitespace(line.charAt(c-1))) {
|
|
361 |
return false;
|
|
362 |
}
|
|
363 |
|
|
364 |
int c1 = line.indexOf("//", pos);
|
|
365 |
if (c1 >= 0 && c1 < c) {
|
|
366 |
return false;
|
|
367 |
}
|
|
368 |
|
|
369 |
int c2 = line.indexOf("*/", c + 2);
|
|
370 |
if (c2 < 0) {
|
|
371 |
return true;
|
|
372 |
}
|
|
373 |
pos = c + 2;
|
|
374 |
}
|
|
375 |
return false;
|
|
376 |
}
|
|
377 |
|
|
378 |
static void setBaseModule(String name) {
|
|
379 |
baseModuleName = name;
|
|
380 |
}
|
|
381 |
// TODO: we shall remove "-" from the regex once we define
|
|
382 |
// the naming convention for the module names without dashes
|
|
383 |
static final Pattern classNamePattern = Pattern.compile("[\\w\\.\\*_$-/]+");
|
|
384 |
|
|
385 |
static List<ModuleConfig> readConfigurationFile(String file) throws IOException {
|
|
386 |
List<ModuleConfig> result = new ArrayList<ModuleConfig>();
|
|
387 |
// parse configuration file
|
|
388 |
FileInputStream in = new FileInputStream(file);
|
|
389 |
try {
|
|
390 |
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
|
|
391 |
String line;
|
|
392 |
|
|
393 |
int lineNumber = 0;
|
|
394 |
boolean inRoots = false;
|
|
395 |
boolean inIncludes = false;
|
|
396 |
boolean inAllows = false;
|
|
397 |
boolean inExcludes = false;
|
|
398 |
boolean inBlockComment = false;
|
|
399 |
ModuleConfig config = null;
|
|
400 |
|
|
401 |
while ((line = reader.readLine()) != null) {
|
|
402 |
lineNumber++;
|
|
403 |
|
|
404 |
if (inBlockComment) {
|
|
405 |
int c = line.indexOf("*/");
|
|
406 |
if (c >= 0) {
|
|
407 |
line = line.substring(c + 2, line.length());
|
|
408 |
inBlockComment = false;
|
|
409 |
} else {
|
|
410 |
// skip lines until end of comment block
|
|
411 |
continue;
|
|
412 |
}
|
|
413 |
}
|
|
414 |
|
|
415 |
inBlockComment = beginBlockComment(line);
|
|
416 |
|
|
417 |
line = trimComment(line).trim();
|
|
418 |
// ignore empty lines
|
|
419 |
if (line.length() == 0) {
|
|
420 |
continue;
|
|
421 |
}
|
|
422 |
|
|
423 |
String values;
|
|
424 |
if (inRoots || inIncludes || inExcludes || inAllows) {
|
|
425 |
values = line;
|
|
426 |
} else {
|
|
427 |
String[] s = line.split("\\s+");
|
|
428 |
String keyword = s[0].trim();
|
|
429 |
if (keyword.equals("module")) {
|
|
430 |
if (s.length != 3 || !s[2].trim().equals("{")) {
|
|
431 |
throw new RuntimeException(file + ", line " +
|
|
432 |
lineNumber + ", is malformed");
|
|
433 |
}
|
|
434 |
config = new ModuleConfig(s[1].trim());
|
|
435 |
result.add(config);
|
|
436 |
// switch to a new module; so reset the flags
|
|
437 |
inRoots = false;
|
|
438 |
inIncludes = false;
|
|
439 |
inExcludes = false;
|
|
440 |
inAllows = false;
|
|
441 |
continue;
|
|
442 |
} else if (keyword.equals("roots")) {
|
|
443 |
inRoots = true;
|
|
444 |
} else if (keyword.equals("include")) {
|
|
445 |
inIncludes = true;
|
|
446 |
} else if (keyword.equals("exclude")) {
|
|
447 |
inExcludes = true;
|
|
448 |
} else if (keyword.equals("allow")) {
|
|
449 |
inAllows = true;
|
|
450 |
} else if (keyword.equals("}")) {
|
|
451 |
if (config == null || s.length != 1) {
|
|
452 |
throw new RuntimeException(file + ", line " +
|
|
453 |
lineNumber + ", is malformed");
|
|
454 |
} else {
|
|
455 |
// end of a module
|
|
456 |
config = null;
|
|
457 |
continue;
|
|
458 |
}
|
|
459 |
} else {
|
|
460 |
throw new RuntimeException(file + ", \"" + keyword + "\" on line " +
|
|
461 |
lineNumber + ", is not recognized");
|
|
462 |
}
|
|
463 |
values = line.substring(keyword.length(), line.length()).trim();
|
|
464 |
}
|
|
465 |
|
|
466 |
if (config == null) {
|
|
467 |
throw new RuntimeException(file + ", module not specified");
|
|
468 |
}
|
|
469 |
|
|
470 |
int len = values.length();
|
|
471 |
if (len == 0) {
|
|
472 |
continue;
|
|
473 |
}
|
|
474 |
char lastchar = values.charAt(len - 1);
|
|
475 |
if (lastchar != ',' && lastchar != ';') {
|
|
476 |
throw new RuntimeException(file + ", line " +
|
|
477 |
lineNumber + ", is malformed:" +
|
|
478 |
" ',' or ';' is missing.");
|
|
479 |
}
|
|
480 |
|
|
481 |
values = values.substring(0, len - 1);
|
|
482 |
// parse the values specified for a keyword specified
|
|
483 |
for (String s : values.split(",")) {
|
|
484 |
s = s.trim();
|
|
485 |
if (s.length() > 0) {
|
|
486 |
if (!classNamePattern.matcher(s).matches()) {
|
|
487 |
throw new RuntimeException(file + ", line " +
|
|
488 |
lineNumber + ", is malformed: \"" + s + "\"");
|
|
489 |
}
|
|
490 |
if (inRoots) {
|
|
491 |
config.roots.add(s);
|
|
492 |
} else if (inIncludes) {
|
|
493 |
config.includes.add(s);
|
|
494 |
} else if (inExcludes) {
|
|
495 |
config.filter.exclude(s);
|
|
496 |
} else if (inAllows) {
|
|
497 |
config.filter.allow(s);
|
|
498 |
}
|
|
499 |
|
|
500 |
}
|
|
501 |
}
|
|
502 |
if (lastchar == ';') {
|
|
503 |
inRoots = false;
|
|
504 |
inIncludes = false;
|
|
505 |
inExcludes = false;
|
|
506 |
inAllows = false;
|
|
507 |
}
|
|
508 |
}
|
|
509 |
|
|
510 |
if (inBlockComment) {
|
|
511 |
throw new RuntimeException(file + ", line " +
|
|
512 |
lineNumber + ", missing \"*/\" to end a block comment");
|
|
513 |
}
|
|
514 |
if (config != null) {
|
|
515 |
throw new RuntimeException(file + ", line " +
|
|
516 |
lineNumber + ", missing \"}\" to end module definition" +
|
|
517 |
" for \"" + config.module + "\"");
|
|
518 |
}
|
|
519 |
|
|
520 |
} finally {
|
|
521 |
in.close();
|
|
522 |
}
|
|
523 |
|
|
524 |
return result;
|
|
525 |
}
|
|
526 |
|
|
527 |
private String format(String keyword, Collection<String> values) {
|
|
528 |
if (values.size() == 0) {
|
|
529 |
return "";
|
|
530 |
}
|
|
531 |
|
|
532 |
StringBuilder sb = new StringBuilder();
|
|
533 |
String format = "%4s%-9s";
|
|
534 |
String spaces = String.format(format, "", "");
|
|
535 |
sb.append(String.format(format, "", keyword));
|
|
536 |
int count = 0;
|
|
537 |
for (String s : values) {
|
|
538 |
if (count > 0) {
|
|
539 |
sb.append(",\n").append(spaces);
|
|
540 |
} else if (count++ > 0) {
|
|
541 |
sb.append(", ");
|
|
542 |
}
|
|
543 |
sb.append(s);
|
|
544 |
}
|
|
545 |
if (count > 0) {
|
|
546 |
sb.append(";\n");
|
|
547 |
}
|
|
548 |
return sb.toString();
|
|
549 |
}
|
|
550 |
|
|
551 |
@Override
|
|
552 |
public String toString() {
|
|
553 |
StringBuilder sb = new StringBuilder();
|
|
554 |
sb.append("module " + module).append(" {\n");
|
|
555 |
sb.append(format("include", includes));
|
|
556 |
sb.append(format("root", roots));
|
|
557 |
sb.append(format("allow", filter.allow));
|
|
558 |
sb.append(format("exclude", filter.exclude));
|
|
559 |
sb.append("}\n");
|
|
560 |
return sb.toString();
|
|
561 |
}
|
|
562 |
}
|