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
1028 lines (934 loc) · 42.6 KB
/
ControllerByteCodeScanner.java
File metadata and controls
1028 lines (934 loc) · 42.6 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
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package act.controller.bytecode;
/*-
* #%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.Act;
import act.app.App;
import act.app.AppByteCodeScannerBase;
import act.app.AppClassLoader;
import act.app.event.SysEventId;
import act.asm.*;
import act.asm.signature.SignatureReader;
import act.asm.signature.SignatureVisitor;
import act.conf.AppConfig;
import act.controller.Controller;
import act.controller.annotation.Port;
import act.controller.annotation.TemplateContext;
import act.controller.meta.*;
import act.handler.builtin.controller.RequestHandlerProxy;
import act.route.DuplicateRouteMappingException;
import act.route.RouteSource;
import act.route.Router;
import act.sys.Env;
import act.sys.meta.EnvAnnotationVisitor;
import act.util.*;
import act.ws.WsEndpoint;
import org.osgl.$;
import org.osgl.http.H;
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.*;
/**
* New controller scanner implementation
*/
public class ControllerByteCodeScanner extends AppByteCodeScannerBase {
private Router router;
private ControllerClassMetaInfo classInfo;
private volatile ControllerClassMetaInfoManager classInfoBase;
private $.Var<Boolean> envMatches = $.var(true);
private EnvAnnotationVisitor eav;
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 = {};
private Set<String> methodNames = new HashSet<>();
private void checkMethodName(String methodName) {
if (methodNames.contains(methodName)) {
throw AsmException.of("Duplicate action/interceptor method name found: %s", methodName);
}
methodNames.add(methodName);
}
@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);
Class<? extends Annotation> c = AsmType.classForDesc(desc);
if (Controller.class == c) {
classInfo.isController(true);
return new ControllerAnnotationVisitor(av);
} else if (ControllerClassMetaInfo.isUrlContextAnnotation(c)) {
classInfo.isController(true);
return new ClassUrlContextAnnotationVisitor(av, ControllerClassMetaInfo.isUrlContextAnnotationSupportInheritance(c));
} else if (TemplateContext.class == c) {
classInfo.isController(true);
return new TemplateContextAnnotationVisitor(av);
} else if (Port.class == c) {
return new PortAnnotationVisitor(av);
} else if (With.class == c) {
classInfo.isController(true);
return new ClassWithAnnotationVisitor(av);
} else if (WsEndpoint.class == c) {
classInfo.isController(true);
return new WsEndpointAnnotationVisitor(av);
} else if (Env.isEnvAnnoDescriptor(desc)) {
eav = new EnvAnnotationVisitor(av, desc);
return eav;
}
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);
}
@Override
public void visitEnd() {
if (null != eav && !eav.matched()) {
envMatches.set(false);
}
}
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 WsEndpointAnnotationVisitor extends AnnotationVisitor {
WsEndpointAnnotationVisitor(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() {
List<Router> routers = routers();
if (strings.isEmpty()) {
strings.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(SysEventId.APP_CODE_SCANNED, new RouteRegister(envMatches, C.list(H.Method.GET), strings, WsEndpoint.PSEUDO_METHOD, routers, classInfo, false, $.var(false)));
super.visitEnd();
}
};
}
return super.visitArray(name);
}
}
private class ClassWithAnnotationVisitor extends AnnotationVisitor {
public ClassWithAnnotationVisitor(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.urlContext(value.toString());
}
super.visit(name, value);
}
@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 ClassUrlContextAnnotationVisitor extends AnnotationVisitor {
private final boolean supportInheritance;
ClassUrlContextAnnotationVisitor(AnnotationVisitor av, boolean supportInheritance) {
super(ASM5, av);
this.supportInheritance = supportInheritance;
}
@Override
public void visit(String name, Object value) {
if ("value".equals(name)) {
String pathComponent = value.toString();
if (!supportInheritance && !pathComponent.startsWith("/")) {
pathComponent = "/" + pathComponent;
}
classInfo.urlContext(pathComponent);
}
super.visit(name, value);
}
}
private class TemplateContextAnnotationVisitor extends AnnotationVisitor {
TemplateContextAnnotationVisitor(AnnotationVisitor av) {
super(ASM5, av);
}
@Override
public void visit(String name, Object value) {
if ("value".equals(name)) {
classInfo.templateContext(value.toString());
}
super.visit(name, value);
}
}
private class PortAnnotationVisitor extends AnnotationVisitor {
PortAnnotationVisitor(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() {
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 boolean isStatic;
private boolean requireScan;
private boolean disableJsonCircularRefDetect;
private HandlerMethodMetaInfo methodInfo;
private PropertySpec.MetaInfo propSpec;
List<String> paths = new ArrayList<>();
private Map<Integer, List<ParamAnnoInfoTrait>> paramAnnoInfoList = new HashMap<>();
private Map<Integer, List<GeneralAnnoInfo>> genericParamAnnoInfoList = new HashMap<>();
private BitSet contextInfo = new BitSet();
private $.Var<Boolean> isVirtual = $.var(false);
private HandlerWithAnnotationVisitor withAnnotationVisitor;
private $.Var<Boolean> isGlobal = $.var(false);
private List<InterceptorAnnotationVisitor> interceptorAnnotationVisitors = new ArrayList<>();
private EnvAnnotationVisitor eav;
private $.Var<Boolean> envMatched = $.var(true);
ActionMethodVisitor(boolean isRoutedMethod, MethodVisitor mv, int access, String methodName, String desc, String signature, String[] exceptions) {
super(ASM5, mv);
this.access = access;
this.methodName = methodName;
this.desc = desc;
this.signature = signature;
this.isStatic = isStatic(access);
if (classInfo.isAbstract()) {
this.isVirtual.set(true);
}
if (isRoutedMethod) {
markRequireScan();
}
}
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
AnnotationVisitor av = super.visitAnnotation(desc, visible);
Type type = Type.getType(desc);
Class<? extends Annotation> c = AsmType.classForType(type);
if (Virtual.class.getName().equals(c.getName())) {
isVirtual.set(true);
return av;
}
if (Global.class.getName().equals(c.getName())) {
isGlobal.set(true);
return av;
}
if (Type.getType(With.class).getDescriptor().equals(desc)) {
classInfo.isController(true);
withAnnotationVisitor = new HandlerWithAnnotationVisitor(av);
return withAnnotationVisitor;
}
if (Env.isEnvAnnoDescriptor(desc)) {
eav = new EnvAnnotationVisitor(av, desc);
return eav;
}
if (ControllerClassMetaInfo.isActionAnnotation(c)) {
checkMethodName(methodName);
markRequireScan();
methodInfo = new ActionMethodMetaInfo(classInfo);
classInfo.addAction((ActionMethodMetaInfo) methodInfo);
if (null != propSpec) {
methodInfo.propertySpec(propSpec);
}
return new ActionAnnotationVisitor(av, ControllerClassMetaInfo.lookupHttpMethod(c), ControllerClassMetaInfo.isActionUtilAnnotation(c), isStatic, ControllerClassMetaInfo.noDefPath(c));
} else if (ControllerClassMetaInfo.isUrlContextAnnotation(c)) {
return new MethodUrlContextAnnotationVisitor(av, ControllerClassMetaInfo.isUrlContextAnnotationSupportAbsolutePath(c));
} else if (ControllerClassMetaInfo.isInterceptorAnnotation(c)) {
checkMethodName(methodName);
markRequireScan();
InterceptorAnnotationVisitor visitor = new InterceptorAnnotationVisitor(av, c);
methodInfo = visitor.info;
if (null != propSpec) {
methodInfo.propertySpec(propSpec);
}
interceptorAnnotationVisitors.add(visitor);
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) {
propSpec.onValue(S.string(value));
super.visit(name, value);
}
};
} else if (S.eq("cli", name)) {
return new AnnotationVisitor(ASM5, av0) {
@Override
public void visit(String name, Object value) {
propSpec.onCli(S.string(value));
super.visit(name, value);
}
};
} else if (S.eq("http", name)) {
return new AnnotationVisitor(ASM5, av0) {
@Override
public void visit(String name, Object value) {
propSpec.onHttp(S.string(value));
super.visit(name, value);
}
};
} else {
return av0;
}
}
};
}
//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 = new ArrayList<>();
genericParamAnnoInfoList.put(parameter, list);
}
list.add(info);
return new GeneralAnnoInfo.Visitor(av, info);
}
}
@Override
public void visitEnd() {
if (!requireScan()) {
super.visitEnd();
return;
}
if (null != eav && !eav.matched()) {
envMatched.set(false);
}
if (isGlobal.get()) {
for (InterceptorAnnotationVisitor visitor : interceptorAnnotationVisitors) {
visitor.registerGlobalInterceptor();
}
}
classInfo.isController(true);
if (null == methodInfo) {
ActionMethodMetaInfo action = new ActionMethodMetaInfo(classInfo);
methodInfo = action;
classInfo.addAction(action);
}
if (null != withAnnotationVisitor) {
if (methodInfo instanceof ActionMethodMetaInfo) {
ActionMethodMetaInfo actionInfo = $.cast(methodInfo);
actionInfo.addWith(withAnnotationVisitor.withArray);
}
}
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 class HandlerWithAnnotationVisitor extends AnnotationVisitor {
private String[] withArray;
public HandlerWithAnnotationVisitor(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);
withArray = sa;
super.visitEnd();
}
};
}
return av;
}
}
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);
}
void registerGlobalInterceptor() {
RequestHandlerProxy.registerGlobalInterceptor(info, interceptorType);
}
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 = new ArrayList<>();
public CatchValueVisitor(AnnotationVisitor av) {
super(ASM5, av);
}
@Override
public void visit(String name, Object value) {
exceptions.add(((Type) value).getClassName());
super.visit(name, value);
}
@Override
public void visitEnd() {
CatchMethodMetaInfo ci = (CatchMethodMetaInfo) info;
ci.exceptionClasses(exceptions);
super.visitEnd();
}
}
}
private class MethodUrlContextAnnotationVisitor extends AnnotationVisitor {
private final boolean supportAbsolutePath;
MethodUrlContextAnnotationVisitor(AnnotationVisitor av, boolean supportAbsolutePath) {
super(ASM5, av);
this.supportAbsolutePath = supportAbsolutePath;
}
@Override
public void visit(String name, Object value) {
if ("value".equals(name)) {
String pathComponent = value.toString();
if (!supportAbsolutePath && pathComponent.startsWith("/")) {
pathComponent = pathComponent.substring(1);
}
paths.add(pathComponent);
}
super.visit(name, value);
}
}
private class ActionAnnotationVisitor extends AnnotationVisitor implements Opcodes {
List<H.Method> httpMethods = new ArrayList<>();
List<String> paths = new ArrayList<>();
boolean isUtil;
boolean isStatic;
boolean noDefPath;
public ActionAnnotationVisitor(AnnotationVisitor av, H.Method method, boolean isUtil, boolean staticMethod, boolean noDefPath) {
super(ASM5, av);
if (null != method) {
httpMethods.add(method);
}
this.isUtil = isUtil;
this.isStatic = staticMethod;
this.noDefPath = noDefPath;
}
@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) {
paths.add((String) value);
super.visit(name, value);
}
};
} else if ("methods".equals(name)) {
return new AnnotationVisitor(ASM5, av) {
@Override
public void visitEnum(String name, String desc, String value) {
String enumClass = Type.getType(desc).getClassName();
if (H.Method.class.getName().equals(enumClass)) {
H.Method method = H.Method.valueOf(value);
httpMethods.add(method);
}
super.visitEnum(name, desc, value);
}
};
} else {
return av;
}
}
@Override
public void visitEnd() {
super.visitEnd();
if (isUtil) {
return;
}
if (httpMethods.isEmpty()) {
// start(*) match
httpMethods.addAll(H.Method.actionMethods());
}
final List<Router> routers = routers();
if (!noDefPath && 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(SysEventId.APP_CODE_SCANNED, new RouteRegister(envMatched, httpMethods, paths, methodName, routers, classInfo, classInfo.isAbstract() && !isStatic, isVirtual));
}
}
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 = new ArrayList<>();
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);
}
super.visit(name, 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);
super.visit(name, value);
}
};
}
return av;
}
}
}
private List<Router> routers() {
final List<Router> routers = new ArrayList<>();
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 routers;
}
throw E.invalidConfiguration("Cannot find configuration for named port[%s]", portName);
}
routers.add(r);
}
}
return routers;
}
}
private static class RouteRegister implements Runnable {
List<Router> routers;
List<String> paths;
String methodName;
ControllerClassMetaInfo classInfo;
List<H.Method> httpMethods;
$.Var<Boolean> isVirtual;
boolean noRegister; // do not register virtual method of an abstract class
$.Var<Boolean> envMatched;
RouteRegister($.Var<Boolean> envMatched, List<H.Method> methods, List<String> paths, String methodName, List<Router> routers, ControllerClassMetaInfo classInfo, boolean noRegister, $.Var<Boolean> isVirtual) {
this.routers = routers;
this.paths = paths;
this.methodName = methodName;
this.classInfo = classInfo;
this.httpMethods = methods;
this.noRegister = noRegister;
this.isVirtual = isVirtual;
this.envMatched = envMatched;
}
@Override
public void run() {
if (!envMatched.get()) {
return;
}
final Set<String> contexts = new HashSet<>();
if (!noRegister) {
String contextPath = classInfo.urlContext();
String className = classInfo.className();
String action = WsEndpoint.PSEUDO_METHOD == methodName ? methodName : S.concat(className, ".", methodName);
registerOnContext(contextPath, action);
contexts.add(contextPath);
}
if (!isVirtual.get()) {
// not virtual handler method, so don't need to register sub class routes
return;
}
// now check on sub classes
App app = Act.app();
final AppClassLoader classLoader = app.classLoader();
ClassNode node = classLoader.classInfoRepository().node(classInfo.className());
node.visitSubTree(new $.Visitor<ClassNode>() {
@Override
public void visit(ClassNode classNode) throws $.Break {
String className = classNode.name();
ControllerClassMetaInfo subClassInfo = classLoader.controllerClassMetaInfo(className);
if (null != subClassInfo) {
String subClassContextPath = subClassInfo.urlContext();
if (null != subClassContextPath) {
if (!contexts.contains(subClassContextPath)) {
registerOnContext(subClassContextPath, S.builder(subClassInfo.className()).append(".").append(methodName).toString());
contexts.add(subClassContextPath);
} else {
throw E.invalidConfiguration("the context path of Sub controller %s has already been registered: %s", className, subClassContextPath);
}
}
}
}
}, true, true);
}
private void registerOnContext(String ctxPath, String action) {
RouteSource routeSource = action.startsWith("act.") ? RouteSource.BUILD_IN : RouteSource.ACTION_ANNOTATION;
S.Buffer sb = S.newBuffer();
if (paths.isEmpty()) {
paths.add("");
}
for (Router r : routers) {
for (String urlPath : paths) {