Repository navigation
Expand file tree
/
Copy pathClassDetector.java
More file actions
219 lines (189 loc) · 7.45 KB
/
Copy pathClassDetector.java
File metadata and controls
219 lines (189 loc) · 7.45 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package act.util;
import act.asm.AnnotationVisitor;
import act.asm.ClassWriter;
import act.asm.Type;
import act.plugin.Extends;
import org.osgl.$;
import org.osgl.exception.NotAppliedException;
import org.osgl.logging.L;
import org.osgl.logging.Logger;
import org.osgl.util.C;
import org.osgl.util.E;
import org.osgl.util.S;
import java.lang.annotation.Annotation;
public abstract class ClassDetector extends ByteCodeVisitor {
protected static final Logger logger = L.get(ClassDetector.class);
private String className;
public abstract boolean found();
public String className() {
return className;
}
@Override
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
className = Type.getObjectType(name).getClassName();
super.visit(version, access, name, signature, superName, interfaces);
}
private static class FilteredClassDetector extends ClassDetector {
private final ClassFilter filter;
private boolean found = false;
private boolean skip = false;
FilteredClassDetector(ClassFilter filter) {
E.NPE(filter);
this.filter = filter;
}
@Override
public int hashCode() {
return $.hc(filter, FilteredClassDetector.class);
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof FilteredClassDetector) {
FilteredClassDetector that = (FilteredClassDetector)obj;
return $.eq(that.filter, this.filter);
}
return false;
}
@Override
public boolean found() {
return found;
}
@Override
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
super.visit(version, access, name, signature, superName, interfaces);
if (filter.noAbstract() && ((access & ACC_ABSTRACT) != 0 || (access & ACC_INTERFACE) != 0)) {
skip = true;
return;
}
if (filter.publicOnly() && (access & ACC_PUBLIC) != 1) {
skip = true;
return;
}
Class<?> superType = filter.superType();
if (null == superType) {
return; // we will check annotation type anyway
}
final String expected = superType.getName();
if (checkName(expected, superName)) {
found = true;
return;
}
int len = interfaces.length;
for (int i = 0; i < len; ++i) {
String s = interfaces[i];
if (checkName(expected, s)) {
found = true;
return;
}
}
}
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
AnnotationVisitor av = super.visitAnnotation(desc, visible);
if (found || skip) {
return av;
}
if (isExtendsAnnotation(desc)) {
Class<?> superType = filter.superType();
if (null != superType) {
return new ExtendedAnnotationVisitor(av, superType.getName());
} else {
return av;
}
}
Class<? extends Annotation> annoType = filter.annotationType();
if (null == annoType) {
return av;
}
if (isAnnotation(annoType, desc)) {
found = true;
}
return av;
}
private boolean checkName(String expected, String found) {
Type type = Type.getObjectType(found);
return type.getClassName().equals(expected);
}
private final class ExtendedAnnotationVisitor extends AnnotationVisitor {
private String expected;
public ExtendedAnnotationVisitor(AnnotationVisitor av, String expected) {
super(ASM5, av);
this.expected = expected;
}
@Override
public void visit(String name, Object value) {
super.visit(name, value);
found = found || "value".equals(name) && (value instanceof Type) && ((Type) value).getClassName().equals(expected);
}
}
}
private static boolean isExtendsAnnotation(String desc) {
return isAnnotation(Extends.class, desc);
}
private static boolean isAnnotation(Class<? extends Annotation> annoType, String desc) {
return S.eq(annoType.getName(), Type.getType(desc).getClassName());
}
public static ClassDetector chain(ClassWriter cw, ClassFilter... filters) {
return (ClassDetector) chain(cw, of(filters));
}
public static ClassDetector of(final ClassFilter... filters) {
E.illegalArgumentIf(filters.length == 0);
if (filters.length == 1) {
return new FilteredClassDetector(filters[0]);
}
return new ClassDetector() {
C.List<ClassDetector> detectors = C.listOf(filters).map(new $.F1<ClassFilter, ClassDetector>() {
@Override
public ClassDetector apply(ClassFilter classFilter) throws NotAppliedException, $.Break {
return new FilteredClassDetector(classFilter);
}
});
private C.List<ClassDetector> matches = C.newList();
@Override
public int hashCode() {
return $.hc(detectors);
}
@Override
public boolean equals(Object obj) {
return obj == this;
}
@Override
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
super.visit(version, access, name, signature, superName, interfaces);
for (ClassDetector detector : detectors) {
detector.visit(version, access, name, signature, superName, interfaces);
if (detector.found()) {
matches.add(detector);
}
}
}
@Override
public AnnotationVisitor visitAnnotation(final String desc, final boolean visible) {
AnnotationVisitor av = super.visitAnnotation(desc, visible);
if (matches.size() == detectors.size() || !isExtendsAnnotation(desc)) {
return av;
}
final C.List<ClassDetector> unmatched = detectors.without(matches);
return new AnnotationVisitor(ASM5, av) {
@Override
public void visit(String name, Object value) {
super.visit(name, value);
for (ClassDetector detector : unmatched) {
AnnotationVisitor av0 = detector.visitAnnotation(desc, visible);
av0.visit(name, value);
if (detector.found()) {
matches.add(detector);
}
}
}
};
}
@Override
public boolean found() {
return !matches.isEmpty();
}
};
}
}