forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControllerByteCodeScanner.java
More file actions
413 lines (363 loc) · 15.5 KB
/
ControllerByteCodeScanner.java
File metadata and controls
413 lines (363 loc) · 15.5 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package act.controller.bytecode;
import act.app.AppByteCodeScannerBase;
import act.asm.*;
import act.controller.Controller;
import act.controller.meta.*;
import act.util.AsmTypes;
import org.osgl._;
import org.osgl.http.H;
import org.osgl.logging.L;
import org.osgl.logging.Logger;
import org.osgl.mvc.annotation.With;
import act.route.Router;
import act.util.ByteCodeVisitor;
import org.osgl.util.C;
import org.osgl.util.ListBuilder;
import org.osgl.util.S;
import java.lang.annotation.Annotation;
import java.util.List;
/**
* New controller scanner implementation
*/
public class ControllerByteCodeScanner extends AppByteCodeScannerBase {
private final static Logger logger = L.get(ControllerByteCodeScanner.class);
private Router router;
private ControllerClassMetaInfo classInfo;
private volatile ControllerClassMetaInfoManager classInfoBase;
@Override
protected boolean shouldScan(String className) {
boolean isController = config().possibleControllerClass(className);
classInfo = new ControllerClassMetaInfo().isController(isController);
return isController;
}
@Override
protected void onAppSet() {
router = app().router();
}
@Override
public ByteCodeVisitor byteCodeVisitor() {
return new _ByteCodeVisitor();
}
@Override
public void scanFinished(String className) {
classInfoBase().registerControllerMetaInfo(classInfo);
}
@Override
public void allScanFinished() {
classInfoBase().mergeActionMetaInfo();
}
private ControllerClassMetaInfoManager classInfoBase() {
if (null == classInfoBase) {
synchronized (this) {
if (null == classInfoBase) {
classInfoBase = app().classLoader().controllerClassMetaInfoManager();
}
}
}
return classInfoBase;
}
private class _ByteCodeVisitor extends ByteCodeVisitor {
@Override
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
logger.trace("Scanning %s", name);
classInfo.className(name);
String className = name.replace('/', '.');
if (router.possibleController(className)) {
classInfo.isController(true);
}
Type superType = Type.getObjectType(superName);
classInfo.superType(superType);
if (isAbstract(access)) {
classInfo.setAbstract();
}
super.visit(version, access, name, signature, superName, interfaces);
}
@Override
public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
if (classInfo.isController() && AsmTypes.APP_CONTEXT_DESC.equals(desc)) {
classInfo.ctxField(name, isPrivate(access));
}
return super.visitField(access, name, desc, signature, value);
}
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
AnnotationVisitor av = super.visitAnnotation(desc, visible);
if (Type.getType(Controller.class).getDescriptor().equals(desc)) {
classInfo.isController(true);
return new ControllerAnnotationVisitor(av);
}
if (Type.getType(With.class).getDescriptor().equals(desc)) {
return new WithAnnotationVisitor(av);
}
return super.visitAnnotation(desc, visible);
}
@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
if (!classInfo.isController() || !isEligibleMethod(access, name, desc)) {
return mv;
}
String className = classInfo.className();
boolean isRoutedMethod = router.isActionMethod(className, name);
return new ActionMethodVisitor(isRoutedMethod, mv, access, name, desc, signature, exceptions);
}
private boolean isEligibleMethod(int access, String name, String desc) {
return isPublic(access) && !isAbstract(access) && !isConstructor(name);
}
private class StringArrayVisitor extends AnnotationVisitor {
protected ListBuilder<String> strings = ListBuilder.create();
public StringArrayVisitor(AnnotationVisitor av) {
super(ASM5, av);
}
@Override
public void visit(String name, Object value) {
strings.add(value.toString());
super.visit(name, value);
}
}
private class WithAnnotationVisitor extends AnnotationVisitor {
public WithAnnotationVisitor(AnnotationVisitor av) {
super(ASM5, av);
}
@Override
public AnnotationVisitor visitArray(String name) {
AnnotationVisitor av = super.visitArray(name);
if ("value".equals(name)) {
return new StringArrayVisitor(av) {
@Override
public void visitEnd() {
String[] sa = new String[strings.size()];
sa = strings.toArray(sa);
classInfo.addWith(sa);
super.visitEnd();
}
};
}
return av;
}
}
private class ControllerAnnotationVisitor extends AnnotationVisitor {
ControllerAnnotationVisitor(AnnotationVisitor av) {
super(ASM5, av);
}
@Override
public void visit(String name, Object value) {
if ("value".equals(name)) {
classInfo.contextPath(value.toString());
}
}
}
private class ActionMethodVisitor extends MethodVisitor implements Opcodes {
private String methodName;
private int access;
private String desc;
private String signature;
private String[] exceptions;
private boolean requireScan;
private boolean isRoutedMethod;
private HandlerMethodMetaInfo methodInfo;
ActionMethodVisitor(boolean isRoutedMethod, MethodVisitor mv, int access, String methodName, String desc, String signature, String[] exceptions) {
super(ASM5, mv);
this.isRoutedMethod = isRoutedMethod;
this.access = access;
this.methodName = methodName;
this.desc = desc;
this.signature = signature;
this.exceptions = exceptions;
if (isRoutedMethod) {
markRequireScan();
}
}
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
AnnotationVisitor av = super.visitAnnotation(desc, visible);
Type type = Type.getType(desc);
String className = type.getClassName();
Class<? extends Annotation> c = _.classForName(className);
if (ControllerClassMetaInfo.isActionAnnotation(c)) {
markRequireScan();
ActionMethodMetaInfo tmp = new ActionMethodMetaInfo(classInfo);
methodInfo = tmp;
classInfo.addAction(tmp);
return new ActionAnnotationVisitor(av, AnnotationMethodLookup.get(c));
} else if (ControllerClassMetaInfo.isInterceptorAnnotation(c)) {
markRequireScan();
InterceptorAnnotationVisitor visitor = new InterceptorAnnotationVisitor(av, c);
methodInfo = visitor.info;
return visitor;
}
//markNotTargetClass();
return av;
}
@Override
public void visitEnd() {
if (!requireScan()) {
super.visitEnd();
return;
}
if (null == methodInfo) {
ActionMethodMetaInfo action = new ActionMethodMetaInfo(classInfo);
methodInfo = action;
classInfo.addAction(action);
}
HandlerMethodMetaInfo info = methodInfo;
info.name(methodName);
boolean isStatic = AsmTypes.isStatic(access);
if (isStatic) {
info.invokeStaticMethod();
} else {
info.invokeInstanceMethod();
}
info.returnType(Type.getReturnType(desc));
Type[] argTypes = Type.getArgumentTypes(desc);
boolean ctxByParam = false;
for (int i = 0; i < argTypes.length; ++i) {
Type type = argTypes[i];
if (AsmTypes.APP_CONTEXT.asmType().equals(type)) {
ctxByParam = true;
info.appContextViaParam(i);
}
ParamMetaInfo param = new ParamMetaInfo().type(type);
info.addParam(param);
}
if (!ctxByParam) {
if (classInfo.hasCtxField() && !isStatic) {
info.appContextViaField(classInfo.ctxField());
} else {
info.appContextViaLocalStorage();
}
}
super.visitEnd();
}
private void markRequireScan() {
this.requireScan = true;
}
private boolean requireScan() {
return requireScan;
}
private class InterceptorAnnotationVisitor extends AnnotationVisitor implements Opcodes {
private InterceptorMethodMetaInfo info;
private InterceptorType interceptorType;
public InterceptorAnnotationVisitor(AnnotationVisitor av, Class<? extends Annotation> annoCls) {
super(ASM5, av);
interceptorType = InterceptorType.of(annoCls);
info = interceptorType.createMetaInfo(classInfo);
classInfo.addInterceptor(info, annoCls);
}
@Override
public void visit(String name, Object value) {
if ("priority".equals(name)) {
info.priority((Integer) value);
}
super.visit(name, value);
}
@Override
public AnnotationVisitor visitArray(String name) {
if ("only".equals(name)) {
return new OnlyValueVisitor(av);
} else if ("except".equals(name)) {
return new ExceptValueVisitor(av);
} else if ("value".equals(name)) {
if (info instanceof CatchMethodMetaInfo) {
return new CatchValueVisitor(av);
}
}
return super.visitArray(name);
}
private class OnlyValueVisitor extends StringArrayVisitor {
public OnlyValueVisitor(AnnotationVisitor av) {
super(av);
}
@Override
public void visitEnd() {
String[] sa = new String[strings.size()];
sa = strings.toArray(sa);
info.addOnly(sa);
super.visitEnd();
super.visitEnd();
}
}
private class ExceptValueVisitor extends StringArrayVisitor {
public ExceptValueVisitor(AnnotationVisitor av) {
super(av);
}
@Override
public void visitEnd() {
String[] sa = new String[strings.size()];
sa = strings.toArray(sa);
info.addExcept(sa);
super.visitEnd();
}
}
private class CatchValueVisitor extends AnnotationVisitor {
List<String> exceptions = C.newList();
public CatchValueVisitor(AnnotationVisitor av) {
super(ASM5, av);
}
@Override
public void visit(String name, Object value) {
exceptions.add(((Type) value).getClassName());
}
@Override
public void visitEnd() {
CatchMethodMetaInfo ci = (CatchMethodMetaInfo) info;
ci.exceptionClasses(exceptions);
super.visitEnd();
}
}
}
private class ActionAnnotationVisitor extends AnnotationVisitor implements Opcodes {
List<H.Method> httpMethods = C.newList();
String path;
public ActionAnnotationVisitor(AnnotationVisitor av, H.Method method) {
super(ASM5, av);
if (null != method) {
httpMethods.add(method);
}
}
@Override
public void visit(String name, Object value) {
if ("value".equals(name)) {
path = value.toString();
}
}
@Override
public void visitEnum(String name, String desc, String value) {
if (null == name) {
name = Type.getType(desc).getClassName();
}
if (H.Method.class.getName().equals(name)) {
H.Method method = H.Method.valueOf(value);
httpMethods.add(method);
}
super.visitEnum(name, desc, value);
}
@Override
public void visitEnd() {
if (httpMethods.isEmpty()) {
// start(*) match
httpMethods.addAll(H.Method.actionMethods());
}
StringBuilder sb = S.builder(classInfo.className().replace('/', '.')).append(".").append(methodName);
String action = sb.toString();
String actionPath = path;
String ctxPath = classInfo.contextPath();
if (!(S.blank(ctxPath) || "/".equals(ctxPath))) {
if (ctxPath.endsWith("/")) {
ctxPath = ctxPath.substring(0, ctxPath.length() - 1);
}
sb = new StringBuilder(ctxPath);
if (!actionPath.startsWith("/")) {
sb.append("/");
}
sb.append(actionPath);
actionPath = sb.toString();
}
for (H.Method m : httpMethods) {
router.addMappingIfNotMapped(m, actionPath, action);
}
}
}
}
}
}