-
Notifications
You must be signed in to change notification settings - Fork 1
/
ReadForbidden.java
60 lines (58 loc) · 1.86 KB
/
ReadForbidden.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import tester.annotations.*;
public class ReadForbidden {
public static void main(String[] args) throws Exception {
if (args.length == 0) {
System.err.println("missing class argument");
System.exit(-1);
}
final StringBuilder grep = new StringBuilder("egrep '(java(\\.|/)lang(\\.|/)ClassLoader|java(\\.|/)lang(\\.|/)reflect|java(\\.|/)lang(\\.|/)System(\\.|/)exit|java(\\.|/)awt(\\.|/)|javax(\\.|/)swing(\\.|/)");
final StringBuilder grep2 = new StringBuilder("egrep -v '(");
String sep = "";
boolean hasNotForbidden = false;
ClassLoader cl = ClassLoader.getSystemClassLoader();
for (String testClassName : args) {
Class<?> newClass = cl.loadClass(testClassName);
Forbidden forbidden = newClass.getAnnotation(Forbidden.class);
if (forbidden == null) {
continue;
}
for (String s : forbidden.value()) {
grep.append('|').append(getRegex(s, forbidden.type()));
}
NotForbidden notforbidden = newClass.getAnnotation(NotForbidden.class);
if (notforbidden == null) {
continue;
}
for (String s : notforbidden.value()) {
hasNotForbidden = true;
grep2.append(sep).append("(\\W|:L|\\[L)").append(getRegex(s, notforbidden.type()));
sep = "|";
}
}
grep.append(")'");
grep2.append(")'");
String result = grep.toString();
if (hasNotForbidden) {
result = grep2 + " | " + result;
}
System.out.println(result);
}
private static String getRegex(final String classSpec, final Forbidden.Type type) {
switch (type) {
case PREFIX -> {
return classSpec.replaceAll("\\.", "(\\\\.|/)");
}
case FIXED -> {
return classSpec.replaceAll("\\.", "(\\\\.|/)") + "(\\W|$)";
}
case WILDCARD -> {
return classSpec.replaceAll("\\.", "(\\\\.|/)").replaceAll("\\*", "[^\\\\./\\\\s]*");
}
default -> {
System.err.println("unsupported type for @Forbidden");
System.exit(-2);
return null;
}
}
}
}