forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassDetector.java
More file actions
241 lines (210 loc) · 8.14 KB
/
ClassDetector.java
File metadata and controls
241 lines (210 loc) · 8.14 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package act.util;
/*-
* #%L
* ACT Framework
* %%
* Copyright (C) 2014 - 2017 ActFramework
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
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;
import java.util.ArrayList;
import java.util.List;
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) {
found = found || "value".equals(name) && (value instanceof Type) && ((Type) value).getClassName().equals(expected);
super.visit(name, value);
}
}
}
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 List<ClassDetector> matches = new ArrayList<>();
@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) {
for (ClassDetector detector : unmatched) {
AnnotationVisitor av0 = detector.visitAnnotation(desc, visible);
av0.visit(name, value);
if (detector.found()) {
matches.add(detector);
}
}
super.visit(name, value);
}
};
}
@Override
public boolean found() {
return !matches.isEmpty();
}
};
}
}