forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassFinderByteCodeScanner.java
More file actions
146 lines (129 loc) · 5.74 KB
/
ClassFinderByteCodeScanner.java
File metadata and controls
146 lines (129 loc) · 5.74 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
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.app.AppByteCodeScannerBase;
import act.asm.AnnotationVisitor;
import act.asm.MethodVisitor;
import act.asm.Type;
import static act.util.ClassFinderData.By.ANNOTATION;
import static act.util.ClassFinderData.By.SUPER_TYPE;
/**
* Scans all public non-abstract methods for {@link SubClassFinder}
* annotations. If found then it will create a {@link ClassFinderData}
* and schedule it to run finding process
*/
public class ClassFinderByteCodeScanner extends AppByteCodeScannerBase {
@Override
protected boolean shouldScan(String className) {
return true;
}
@Override
public ByteCodeVisitor byteCodeVisitor() {
return new _ByteCodeVisitor();
}
@Override
public void scanFinished(String className) {
}
private class _ByteCodeVisitor extends ByteCodeVisitor {
private String className;
@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 (!AsmTypes.isPublic(access)) {
return;
}
className = Type.getObjectType(name).getClassName();
}
@Override
public MethodVisitor visitMethod(int access, String name, String desc, final String signature, String[] exceptions) {
MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
if (null == className || !AsmTypes.isPublicNotAbstract(access)) {
return mv;
}
final String methodName = name;
final boolean isStatic = AsmTypes.isStatic(access);
return new MethodVisitor(ASM5, mv) {
@Override
public AnnotationVisitor visitAnnotation(final String desc, boolean visible) {
Type annoType = Type.getType(desc);
ClassFinderData.By by = null;
if (AsmTypes.SUB_CLASS_FINDER.asmType().equals(annoType)) {
by = SUPER_TYPE;
} else if (AsmTypes.ANN_CLASS_FINDER.asmType().equals(annoType)) {
by = ANNOTATION;
}
AnnotationVisitor av = super.visitAnnotation(desc, visible);
if (null == by) {
return av;
}
final ClassFinderData.By how = by;
return new AnnotationVisitor(ASM5, av) {
ClassFinderData finder = new ClassFinderData();
@Override
public void visit(String name, Object value) {
if ("value".equals(name)) {
Type type = (Type) value;
String className = type.getClassName();
finder.what(className);
} else if ("publicOnly".equals(name)) {
finder.publicOnly((Boolean) value);
} else if ("noAbstract".equals(name)) {
finder.noAbstract((Boolean) value);
}
super.visit(name, value);
}
@Override
public void visitEnum(String name, String desc, String value) {
finder.when(value);
super.visitEnum(name, desc, value);
}
@Override
public void visitEnd() {
if (SUPER_TYPE == how && !finder.whatSpecified()) {
finder.what(classNameFromMethodSignature());
}
finder.how(how);
finder.callback(className, methodName, isStatic);
if (finder.isValid()) {
finder.scheduleFind();
}
super.visitEnd();
}
/*
* Valid method signature should be something like
* (Ljava/lang/Class<Lorg/osgl/aaa/AAAPersistentService;>;)V
* And we need to get the type descriptor "Lorg/osgl/aaa/AAAPersistentService;"
* from inside
*/
private String classNameFromMethodSignature() {
String descriptor = signature.substring(18);
descriptor = descriptor.substring(0, descriptor.length() - 4);
if (descriptor.startsWith("+")) {
// this is an interface?
descriptor = descriptor.substring(1);
}
return Type.getType(descriptor).getClassName();
}
};
}
};
}
}
}