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
730 lines (661 loc) · 29.7 KB
/
ControllerByteCodeScanner.java
File metadata and controls
730 lines (661 loc) · 29.7 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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
package act.controller.bytecode;
import act.app.App;
import act.app.AppByteCodeScannerBase;
import act.app.event.AppEventId;
import act.asm.AnnotationVisitor;
import act.asm.MethodVisitor;
import act.asm.Opcodes;
import act.asm.Type;
import act.asm.signature.SignatureReader;
import act.asm.signature.SignatureVisitor;
import act.conf.AppConfig;
import act.controller.Controller;
import act.controller.meta.*;
import act.route.RouteSource;
import act.route.Router;
import act.util.AsmTypes;
import act.util.ByteCodeVisitor;
import act.util.GeneralAnnoInfo;
import act.util.PropertySpec;
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 org.osgl.mvc.util.Binder;
import org.osgl.util.C;
import org.osgl.util.E;
import org.osgl.util.ListBuilder;
import org.osgl.util.S;
import java.lang.annotation.Annotation;
import java.util.BitSet;
import java.util.List;
import java.util.Map;
/**
* 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;
public ControllerByteCodeScanner() {
}
@Override
protected boolean shouldScan(String className) {
boolean possibleController = config().possibleControllerClass(className);
classInfo = new ControllerClassMetaInfo().possibleController(possibleController);
return possibleController;
}
@Override
protected void onAppSet() {
router = app().router();
}
@Override
public ByteCodeVisitor byteCodeVisitor() {
return new _ByteCodeVisitor();
}
@Override
public void scanFinished(String className) {
if (classInfo.isController()) {
classInfoBase().registerControllerMetaInfo(classInfo);
}
}
private ControllerClassMetaInfoManager classInfoBase() {
if (null == classInfoBase) {
synchronized (this) {
if (null == classInfoBase) {
classInfoBase = app().classLoader().controllerClassMetaInfoManager();
}
}
}
return classInfoBase;
}
private class _ByteCodeVisitor extends ByteCodeVisitor {
private String[] ports = {};
@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 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)) {
classInfo.isController(true);
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.possibleController() || !isEligibleMethod(access, name)) {
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) {
return !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());
}
}
@Override
public AnnotationVisitor visitArray(String name) {
AnnotationVisitor av = super.visitArray(name);
if ("port".equals(name)) {
return new StringArrayVisitor(av) {
@Override
public void visitEnd() {
ports = new String[strings.size()];
ports = strings.toArray(ports);
super.visitEnd();
}
};
}
return av;
}
}
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;
private PropertySpec.MetaInfo propSpec;
private Map<Integer, List<ParamAnnoInfoTrait>> paramAnnoInfoList = C.newMap();
private Map<Integer, List<GeneralAnnoInfo>> genericParamAnnoInfoList = C.newMap();
private BitSet contextInfo = new BitSet();
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);
if (null != propSpec) {
methodInfo.propertySpec(propSpec);
}
return new ActionAnnotationVisitor(av, AnnotationMethodLookup.get(c), ControllerClassMetaInfo.isActionUtilAnnotation(c));
} else if (ControllerClassMetaInfo.isInterceptorAnnotation(c)) {
markRequireScan();
InterceptorAnnotationVisitor visitor = new InterceptorAnnotationVisitor(av, c);
methodInfo = visitor.info;
if (null != propSpec) {
methodInfo.propertySpec(propSpec);
}
return visitor;
} else if ($.eq(AsmTypes.PROPERTY_SPEC.asmType(), type)) {
propSpec = new PropertySpec.MetaInfo();
if (null != methodInfo) {
methodInfo.propertySpec(propSpec);
}
return new AnnotationVisitor(ASM5, av) {
@Override
public AnnotationVisitor visitArray(String name) {
AnnotationVisitor av0 = super.visitArray(name);
if (S.eq("value", name)) {
return new AnnotationVisitor(ASM5, av0) {
@Override
public void visit(String name, Object value) {
super.visit(name, value);
propSpec.onValue(S.string(value));
}
};
} else if (S.eq("cli", name)) {
return new AnnotationVisitor(ASM5, av0) {
@Override
public void visit(String name, Object value) {
super.visit(name, value);
propSpec.onCli(S.string(value));
}
};
} else if (S.eq("http", name)) {
return new AnnotationVisitor(ASM5, av0) {
@Override
public void visit(String name, Object value) {
super.visit(name, value);
propSpec.onHttp(S.string(value));
}
};
} else {
return av0;
}
}
};
} else if ($.eq(AsmTypes.DISABLE_JSON_CIRCULAR_REF_DETECT.asmType(), type)) {
methodInfo.disableJsonCircularRefDetect(true);
}
//markNotTargetClass();
return av;
}
@Override
public AnnotationVisitor visitParameterAnnotation(int parameter, String desc, boolean visible) {
AnnotationVisitor av = super.visitParameterAnnotation(parameter, desc, visible);
Type type = Type.getType(desc);
if ($.eq(type, AsmTypes.PARAM.asmType())) {
return new ParamAnnotationVisitor(av, parameter);
} else if ($.eq(type, AsmTypes.BIND.asmType())) {
return new BindAnnotationVisitor(av, parameter);
} else if ($.eq(type, AsmTypes.CONTEXT.asmType())) {
contextInfo.set(parameter);
return av;
} else {
//return av;
GeneralAnnoInfo info = new GeneralAnnoInfo(type);
List<GeneralAnnoInfo> list = genericParamAnnoInfoList.get(parameter);
if (null == list) {
list = C.newList();
genericParamAnnoInfoList.put(parameter, list);
}
list.add(info);
return new GeneralAnnoInfo.Visitor(av, info);
}
}
@Override
public void visitEnd() {
if (!requireScan()) {
super.visitEnd();
return;
}
classInfo.isController(true);
if (null == methodInfo) {
ActionMethodMetaInfo action = new ActionMethodMetaInfo(classInfo);
methodInfo = action;
classInfo.addAction(action);
}
final 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.ACTION_CONTEXT.asmType().equals(type)) {
ctxByParam = true;
info.appContextViaParam(i);
}
HandlerParamMetaInfo param = new HandlerParamMetaInfo().type(type);
if (contextInfo.get(i)) {
param.setContext();
}
List<ParamAnnoInfoTrait> paraAnnoList = paramAnnoInfoList.get(i);
if (null != paraAnnoList) {
for (ParamAnnoInfoTrait trait : paraAnnoList) {
trait.attachTo(param);
}
}
List<GeneralAnnoInfo> list = genericParamAnnoInfoList.get(i);
if (null != list) {
param.addGeneralAnnotations(list);
}
info.addParam(param);
}
if (!ctxByParam) {
if (classInfo.hasCtxField() && !isStatic) {
info.appContextViaField(classInfo.ctxField());
} else {
info.appContextViaLocalStorage();
}
}
if (null != signature) {
SignatureReader sr = new SignatureReader(signature);
final $.Var<Integer> id = new $.Var<Integer>(-1);
sr.accept(new SignatureVisitor(ASM5) {
boolean startParsing;
@Override
public SignatureVisitor visitParameterType() {
id.set(id.get() + 1);
return this;
}
@Override
public SignatureVisitor visitTypeArgument(char wildcard) {
if (wildcard == '=') {
startParsing = true;
}
return this;
}
@Override
public void visitClassType(String name) {
if (startParsing) {
Type type = Type.getObjectType(name);
int n = id.get();
if (n < 0) {
info.returnComponentType(type);
} else {
info.param(n).componentType(type);
}
}
startParsing = false;
}
});
}
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();
}
}
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();
List<String> paths = C.newList();
boolean isUtil = false;
public ActionAnnotationVisitor(AnnotationVisitor av, H.Method method, boolean isUtil) {
super(ASM5, av);
if (null != method) {
httpMethods.add(method);
}
this.isUtil = isUtil;
}
@Override
public AnnotationVisitor visitArray(String name) {
AnnotationVisitor av = super.visitArray(name);
if ("value".equals(name)) {
return new AnnotationVisitor(ASM5, av) {
@Override
public void visit(String name, Object value) {
super.visit(name, value);
paths.add((String) value);
}
};
} else if ("methods".equals(name)) {
return new AnnotationVisitor(ASM5, av) {
@Override
public void visitEnum(String name, String desc, String value) {
super.visitEnum(name, desc, value);
String enumClass = Type.getType(desc).getClassName();
if (H.Method.class.getName().equals(enumClass)) {
H.Method method = H.Method.valueOf(value);
httpMethods.add(method);
}
}
};
} else {
return av;
}
}
@Override
public void visitEnd() {
super.visitEnd();
if (isUtil) {
return;
}
if (httpMethods.isEmpty()) {
// start(*) match
httpMethods.addAll(H.Method.actionMethods());
}
StringBuilder sb = S.builder(classInfo.className().replace('/', '.')).append(".").append(methodName);
final String action = sb.toString();
final List<Router> routers = C.newList();
if (null == ports || ports.length == 0) {
routers.add(app().router());
} else {
App app = app();
for (String portName : ports) {
Router r = app.router(portName);
if (null == r) {
if (S.eq(AppConfig.PORT_CLI_OVER_HTTP, portName)) {
// cli over http is disabled
return;
}
throw E.invalidConfiguration("Cannot find configuration for named port[%s]", portName);
}
routers.add(r);
}
}
if (paths.isEmpty()) {
paths.add("");
}
/*
* Note we need to schedule route registration after all app code scanned because we need the
* parent context information be set on class meta info, which is done after controller scanning
*/
app().jobManager().on(AppEventId.APP_CODE_SCANNED, new RouteRegister(httpMethods, paths, action, routers, classInfo));
}
}
private abstract class ParamAnnotationVisitorBase<T extends ParamAnnoInfoTrait>
extends AnnotationVisitor implements Opcodes {
protected int index;
protected T info;
public ParamAnnotationVisitorBase(AnnotationVisitor av, int index) {
super(ASM5, av);
this.index = index;
this.info = createAnnotationInfo(index);
}
@Override
public void visitEnd() {
List<ParamAnnoInfoTrait> traits = paramAnnoInfoList.get(index);
if (null == traits) {
traits = C.newList();
paramAnnoInfoList.put(index, traits);
} else {
for (ParamAnnoInfoTrait trait : traits) {
if (!info.compatibleWith(trait)) {
throw E.unexpected(info.compatibilityErrorMessage(trait));
}
}
}
traits.add(info);
super.visitEnd();
}
protected abstract T createAnnotationInfo(int index);
}
private class ParamAnnotationVisitor extends ParamAnnotationVisitorBase<ParamAnnoInfo> {
public ParamAnnotationVisitor(AnnotationVisitor av, int index) {
super(av, index);
}
@Override
protected ParamAnnoInfo createAnnotationInfo(int index) {
return new ParamAnnoInfo(index);
}
@Override
public void visit(String name, Object value) {
if (S.eq("value", name)) {
info.bindName((String) value);
} else if (S.eq("defVal", name)) {
info.defVal(String.class, value);
} else if (S.eq("defIntVal", name)) {
info.defVal(Integer.class, value);
} else if (S.eq("defBooleanVal", name)) {
info.defVal(Boolean.class, value);
} else if (S.eq("defLongVal", name)) {
info.defVal(Long.class, value);
} else if (S.eq("defDoubleVal", name)) {
info.defVal(Double.class, value);
} else if (S.eq("defFloatVal", name)) {
info.defVal(Float.class, value);
} else if (S.eq("defCharVal", name)) {
info.defVal(Character.class, value);
} else if (S.eq("defByteVal", name)) {
info.defVal(Byte.class, name);
}
super.visit(name, value);
}
private <T> T c(Object v) {
return $.cast(v);
}
}
private class BindAnnotationVisitor extends ParamAnnotationVisitorBase<BindAnnoInfo> {
public BindAnnotationVisitor(AnnotationVisitor av, int index) {
super(av, index);
}
@Override
protected BindAnnoInfo createAnnotationInfo(int index) {
return new BindAnnoInfo(index);
}
@Override
public void visit(String name, Object value) {
if ("model".endsWith(name)) {
info.model((String) value);
}
}
@Override
public AnnotationVisitor visitArray(String name) {
AnnotationVisitor av = super.visitArray(name);
if ("value".equals(name)) {
return new AnnotationVisitor(ASM5, av) {
@Override
public void visit(String name, Object value) {
Type type = (Type) value;
Class<? extends Binder> c = $.classForName(type.getClassName(), getClass().getClassLoader());
info.binder(c);
}
};
}
return av;
}
}
}
}
private static class RouteRegister implements Runnable {
List<Router> routers;
List<String> paths;
String action;
ControllerClassMetaInfo classInfo;
List<H.Method> httpMethods;
RouteRegister(List<H.Method> methods, List<String> paths, String action, List<Router> routers, ControllerClassMetaInfo classInfo) {
this.routers = routers;
this.paths = paths;
this.action = action;
this.classInfo = classInfo;
this.httpMethods = methods;
}
@Override
public void run() {
for (Router r: routers) {
for (String actionPath : paths) {
if (!actionPath.startsWith("/")) {
String ctxPath = classInfo.contextPath();
if (!(S.blank(ctxPath) || "/".equals(ctxPath))) {
if (ctxPath.endsWith("/")) {
ctxPath = ctxPath.substring(0, ctxPath.length() - 1);
}
StringBuilder sb = new StringBuilder(ctxPath);
if (!actionPath.startsWith("/")) {
sb.append("/");
}
sb.append(actionPath);
actionPath = sb.toString();
}
}
for (H.Method m : httpMethods) {
r.addMapping(m, actionPath, action, RouteSource.ACTION_ANNOTATION);
}
}
}
}
}
}