1 |
| |
2 |
| |
3 |
| |
4 |
| package net.sourceforge.pmd.typeresolution.visitors; |
5 |
| |
6 |
| import org.objectweb.asm.AnnotationVisitor; |
7 |
| import org.objectweb.asm.Attribute; |
8 |
| import org.objectweb.asm.ClassVisitor; |
9 |
| import org.objectweb.asm.FieldVisitor; |
10 |
| import org.objectweb.asm.Label; |
11 |
| import org.objectweb.asm.MethodVisitor; |
12 |
| import org.objectweb.asm.Type; |
13 |
| import org.objectweb.asm.signature.SignatureReader; |
14 |
| import org.objectweb.asm.signature.SignatureVisitor; |
15 |
| |
16 |
| import java.util.ArrayList; |
17 |
| import java.util.HashMap; |
18 |
| import java.util.List; |
19 |
| import java.util.Map; |
20 |
| |
21 |
| public class PMDASMVisitor implements ClassVisitor { |
22 |
| |
23 |
| private Map packages = new HashMap(); |
24 |
| |
25 |
| private AnnotationVisitor annotationVisitor = new PMDAnnotationVisitor(this); |
26 |
| |
27 |
| private FieldVisitor fieldVisitor = new PMDFieldVisitor(this); |
28 |
| |
29 |
| private SignatureVisitor sigVisitor = new PMDSignatureVisitor(this); |
30 |
| |
31 |
| private MethodVisitor methodVisitor = new PMDMethodVisitor(this); |
32 |
| |
33 |
| public List innerClasses; |
34 |
| |
35 |
3
| public Map getPackages() {
|
36 |
3
| return packages;
|
37 |
| } |
38 |
| |
39 |
3
| public List getInnerClasses() {
|
40 |
3
| return innerClasses;
|
41 |
| } |
42 |
| |
43 |
23
| private String parseClassName(String name) {
|
44 |
23
| if (name == null) {
|
45 |
0
| return null;
|
46 |
| } |
47 |
| |
48 |
23
| String className = name;
|
49 |
23
| int n = name.lastIndexOf('/');
|
50 |
23
| if (n > -1) {
|
51 |
23
| className = name.substring(n + 1);
|
52 |
| } |
53 |
23
| name = name.replace('/', '.');
|
54 |
23
| packages.put(className, name);
|
55 |
23
| n = className.indexOf('$');
|
56 |
23
| if (n > -1) {
|
57 |
| |
58 |
2
| packages.put(className.substring(n + 1), name);
|
59 |
2
| packages.put(className.replace('$', '.'), name);
|
60 |
| } |
61 |
| |
62 |
23
| return name;
|
63 |
| } |
64 |
| |
65 |
13
| private void parseClassName(String[] names) {
|
66 |
13
| if (names != null) {
|
67 |
4
| for (int i = 0; i < names.length; i++) {
|
68 |
1
| parseClassName(names[i]);
|
69 |
| } |
70 |
| } |
71 |
| } |
72 |
| |
73 |
6
| private void extractSignature(String sig) {
|
74 |
6
| if (sig != null) {
|
75 |
0
| new SignatureReader(sig).accept(sigVisitor);
|
76 |
| } |
77 |
| } |
78 |
| |
79 |
| |
80 |
| |
81 |
4
| public void visit(int version, int access, String name, String sig, String superName, String[] interfaces) {
|
82 |
4
| parseClassName(name);
|
83 |
4
| parseClassName(interfaces);
|
84 |
4
| if (sig != null) {
|
85 |
0
| extractSignature(sig);
|
86 |
| } |
87 |
| } |
88 |
| |
89 |
0
| public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
|
90 |
0
| addType(Type.getType(desc));
|
91 |
0
| return annotationVisitor;
|
92 |
| } |
93 |
| |
94 |
0
| public FieldVisitor visitField(int access, String name, String desc, String sig, Object value) {
|
95 |
0
| if (sig != null) {
|
96 |
0
| extractSignature(sig);
|
97 |
| } |
98 |
| |
99 |
0
| addType(Type.getType(desc));
|
100 |
0
| if (value instanceof Type) {
|
101 |
0
| addType((Type) value);
|
102 |
| } |
103 |
0
| return fieldVisitor;
|
104 |
| } |
105 |
| |
106 |
9
| public MethodVisitor visitMethod(int access, String name, String desc, String sig, String[] exceptions) {
|
107 |
9
| if (sig != null) {
|
108 |
0
| extractSignature(sig);
|
109 |
| } |
110 |
9
| addMethodDesc(desc);
|
111 |
9
| parseClassName(exceptions);
|
112 |
9
| return methodVisitor;
|
113 |
| } |
114 |
| |
115 |
4
| public void visitSource(String source, String debug) {
|
116 |
| } |
117 |
| |
118 |
2
| public void visitInnerClass(String name, String outerName, String innerName, int access) {
|
119 |
2
| if (innerClasses == null) {
|
120 |
1
| innerClasses = new ArrayList();
|
121 |
| } |
122 |
2
| if (!innerClasses.contains(name.replace('/', '.'))) {
|
123 |
1
| innerClasses.add(name.replace('/', '.'));
|
124 |
| } |
125 |
2
| packages.put(innerName, name.replace('/', '.'));
|
126 |
| } |
127 |
| |
128 |
0
| public void visitOuterClass(String owner, String name, String desc) {
|
129 |
| } |
130 |
| |
131 |
4
| public void visitEnd() {
|
132 |
| } |
133 |
| |
134 |
15
| private void addMethodDesc(String desc) {
|
135 |
15
| addTypes(desc);
|
136 |
15
| addType(Type.getReturnType(desc));
|
137 |
| } |
138 |
| |
139 |
15
| private void addTypes(String desc) {
|
140 |
15
| Type[] types = Type.getArgumentTypes(desc);
|
141 |
15
| for (int i = 0; i < types.length; i++) {
|
142 |
3
| addType(types[i]);
|
143 |
| } |
144 |
| } |
145 |
| |
146 |
18
| private void addType(Type t) {
|
147 |
18
| switch (t.getSort()) {
|
148 |
0
| case Type.ARRAY:
|
149 |
0
| addType(t.getElementType());
|
150 |
0
| break;
|
151 |
10
| case Type.OBJECT:
|
152 |
10
| parseClassName(t.getClassName().replace('.', '/'));
|
153 |
10
| break;
|
154 |
| } |
155 |
| } |
156 |
| |
157 |
0
| public void visitAttribute(Attribute attr) {
|
158 |
| } |
159 |
| |
160 |
| |
161 |
| |
162 |
| |
163 |
| |
164 |
| private static class PMDFieldVisitor implements FieldVisitor { |
165 |
| |
166 |
| private PMDASMVisitor parent; |
167 |
| |
168 |
3
| public PMDFieldVisitor(PMDASMVisitor visitor) {
|
169 |
3
| parent = visitor;
|
170 |
| } |
171 |
| |
172 |
0
| public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
|
173 |
0
| parent.addType(Type.getType(desc));
|
174 |
0
| return parent.annotationVisitor;
|
175 |
| } |
176 |
| |
177 |
0
| public void visitAttribute(Attribute attr) {
|
178 |
| } |
179 |
| |
180 |
0
| public void visitEnd() {
|
181 |
| } |
182 |
| } |
183 |
| |
184 |
| private static class PMDAnnotationVisitor implements AnnotationVisitor { |
185 |
| private PMDASMVisitor parent; |
186 |
| |
187 |
3
| public PMDAnnotationVisitor(PMDASMVisitor visitor) {
|
188 |
3
| parent = visitor;
|
189 |
| } |
190 |
| |
191 |
0
| public AnnotationVisitor visitAnnotation(String name, String desc) {
|
192 |
0
| parent.addType(Type.getType(desc));
|
193 |
0
| return this;
|
194 |
| } |
195 |
| |
196 |
0
| public void visitEnum(String name, String desc, String value) {
|
197 |
0
| parent.addType(Type.getType(desc));
|
198 |
| } |
199 |
| |
200 |
0
| public AnnotationVisitor visitArray(String name) {
|
201 |
0
| return this;
|
202 |
| } |
203 |
| |
204 |
0
| public void visitEnd() {
|
205 |
| } |
206 |
| |
207 |
0
| public void visit(String name, Object value) {
|
208 |
0
| if (value instanceof Type) {
|
209 |
0
| parent.addType((Type) value);
|
210 |
| } |
211 |
| } |
212 |
| } |
213 |
| |
214 |
| private static class PMDSignatureVisitor implements SignatureVisitor { |
215 |
| private PMDASMVisitor parent; |
216 |
| |
217 |
3
| public PMDSignatureVisitor(PMDASMVisitor visitor) {
|
218 |
3
| this.parent = visitor;
|
219 |
| } |
220 |
| |
221 |
0
| public void visitFormalTypeParameter(String name) {
|
222 |
| } |
223 |
| |
224 |
0
| public SignatureVisitor visitClassBound() {
|
225 |
0
| return this;
|
226 |
| } |
227 |
| |
228 |
0
| public SignatureVisitor visitInterfaceBound() {
|
229 |
0
| return this;
|
230 |
| } |
231 |
| |
232 |
0
| public SignatureVisitor visitSuperclass() {
|
233 |
0
| return this;
|
234 |
| } |
235 |
| |
236 |
0
| public SignatureVisitor visitInterface() {
|
237 |
0
| return this;
|
238 |
| } |
239 |
| |
240 |
0
| public SignatureVisitor visitParameterType() {
|
241 |
0
| return this;
|
242 |
| } |
243 |
| |
244 |
0
| public SignatureVisitor visitReturnType() {
|
245 |
0
| return this;
|
246 |
| } |
247 |
| |
248 |
0
| public SignatureVisitor visitExceptionType() {
|
249 |
0
| return this;
|
250 |
| } |
251 |
| |
252 |
0
| public void visitBaseType(char descriptor) {
|
253 |
| } |
254 |
| |
255 |
0
| public void visitTypeVariable(String name) {
|
256 |
| } |
257 |
| |
258 |
0
| public SignatureVisitor visitArrayType() {
|
259 |
0
| return this;
|
260 |
| } |
261 |
| |
262 |
0
| public void visitClassType(String name) {
|
263 |
0
| parent.parseClassName(name);
|
264 |
| } |
265 |
| |
266 |
0
| public void visitInnerClassType(String name) {
|
267 |
0
| parent.parseClassName(name);
|
268 |
| } |
269 |
| |
270 |
0
| public void visitTypeArgument() {
|
271 |
| } |
272 |
| |
273 |
0
| public SignatureVisitor visitTypeArgument(char wildcard) {
|
274 |
0
| return this;
|
275 |
| } |
276 |
| |
277 |
0
| public void visitEnd() {
|
278 |
| } |
279 |
| } |
280 |
| |
281 |
| private static class PMDMethodVisitor implements MethodVisitor { |
282 |
| private PMDASMVisitor parent; |
283 |
| |
284 |
3
| public PMDMethodVisitor(PMDASMVisitor visitor) {
|
285 |
3
| parent = visitor;
|
286 |
| } |
287 |
| |
288 |
0
| public AnnotationVisitor visitParameterAnnotation(int parameter, String desc, boolean visible) {
|
289 |
0
| parent.addType(Type.getType(desc));
|
290 |
0
| return parent.annotationVisitor;
|
291 |
| } |
292 |
| |
293 |
0
| public AnnotationVisitor visitAnnotation(String name, String desc) {
|
294 |
0
| parent.addType(Type.getType(desc));
|
295 |
0
| return parent.annotationVisitor;
|
296 |
| } |
297 |
| |
298 |
2
| public void visitTypeInsn(int opcode, String desc) {
|
299 |
2
| if (desc.charAt(0) == '[') {
|
300 |
0
| parent.addType(Type.getType(desc));
|
301 |
| } else { |
302 |
2
| parent.parseClassName(desc);
|
303 |
| } |
304 |
| } |
305 |
| |
306 |
0
| public void visitFieldInsn(int opcode, String owner, String name, String desc) {
|
307 |
0
| parent.parseClassName(owner);
|
308 |
0
| parent.addType(Type.getType(desc));
|
309 |
| } |
310 |
| |
311 |
6
| public void visitMethodInsn(int opcode, String owner, String name, String desc) {
|
312 |
6
| parent.parseClassName(owner);
|
313 |
6
| parent.addMethodDesc(desc);
|
314 |
| } |
315 |
| |
316 |
| |
317 |
| |
318 |
| |
319 |
| |
320 |
| |
321 |
| |
322 |
| |
323 |
0
| public void visitLdcInsn(Object cst) {
|
324 |
0
| if (cst instanceof Type) {
|
325 |
0
| parent.addType((Type) cst);
|
326 |
0
| } else if (cst instanceof String) {
|
327 |
0
| parent.parseClassName((String) cst);
|
328 |
| } |
329 |
| } |
330 |
0
| public void visitMultiANewArrayInsn(String desc, int dims) {
|
331 |
0
| parent.addType(Type.getType(desc));
|
332 |
| } |
333 |
| |
334 |
6
| public void visitLocalVariable(String name, String desc, String sig, Label start, Label end, int index) {
|
335 |
6
| parent.extractSignature(sig);
|
336 |
| } |
337 |
| |
338 |
4
| public void visitCode() {
|
339 |
| } |
340 |
| |
341 |
0
| public void visitFrame(int type, int nLocal, Object[] local, int nStack, Object[] stack) {
|
342 |
| } |
343 |
| |
344 |
5
| public void visitInsn(int opcode) {
|
345 |
| } |
346 |
| |
347 |
0
| public void visitIntInsn(int opcode, int operand) {
|
348 |
| } |
349 |
| |
350 |
4
| public void visitVarInsn(int opcode, int var) {
|
351 |
| } |
352 |
| |
353 |
0
| public void visitJumpInsn(int opcode, Label label) {
|
354 |
| } |
355 |
| |
356 |
9
| public void visitLabel(Label label) {
|
357 |
| } |
358 |
| |
359 |
0
| public void visitIincInsn(int var, int increment) {
|
360 |
| } |
361 |
| |
362 |
0
| public void visitTableSwitchInsn(int min, int max, Label dflt, Label[] labels) {
|
363 |
| } |
364 |
| |
365 |
0
| public void visitLookupSwitchInsn(Label dflt, int[] keys, Label[] labels) {
|
366 |
| } |
367 |
| |
368 |
0
| public void visitTryCatchBlock(Label start, Label end, Label handler, String type) {
|
369 |
0
| parent.parseClassName(type);
|
370 |
| } |
371 |
| |
372 |
5
| public void visitLineNumber(int line, Label start) {
|
373 |
| } |
374 |
| |
375 |
4
| public void visitMaxs(int maxStack, int maxLocals) {
|
376 |
| } |
377 |
| |
378 |
0
| public AnnotationVisitor visitAnnotationDefault() {
|
379 |
0
| return parent.annotationVisitor;
|
380 |
| } |
381 |
| |
382 |
0
| public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
|
383 |
0
| parent.addType(Type.getType(desc));
|
384 |
0
| return parent.annotationVisitor;
|
385 |
| } |
386 |
| |
387 |
9
| public void visitEnd() {
|
388 |
| } |
389 |
| |
390 |
0
| public void visitAttribute(Attribute attr) {
|
391 |
| } |
392 |
| |
393 |
| } |
394 |
| } |