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
|
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.plugin.classanalysis;
import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.Handle;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
/**
* Picks up classes used in method bodies.
*
* @author Tony Vaagenes
* @author ollivir
*/
class AnalyzeMethodVisitor extends MethodVisitor implements ImportCollector {
private final Set<String> imports = new HashSet<>();
private final AnalyzeClassVisitor analyzeClassVisitor;
AnalyzeMethodVisitor(AnalyzeClassVisitor analyzeClassVisitor) {
super(Opcodes.ASM7);
this.analyzeClassVisitor = analyzeClassVisitor;
}
public Set<String> imports() {
return imports;
}
@Override
public AnnotationVisitor visitParameterAnnotation(int parameter, String desc, boolean visible) {
return visitAnnotation(desc, visible);
}
@Override
public AnnotationVisitor visitAnnotationDefault() {
return Analyze.visitAnnotationDefault(this);
}
@Override
public void visitAttribute(Attribute attribute) {
addImport(Type.getObjectType(attribute.type));
}
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
addImportWithTypeDesc(desc);
return Analyze.visitAnnotationDefault(this);
}
@Override
public void visitEnd() {
super.visitEnd();
analyzeClassVisitor.addImports(imports);
}
@Override
public void visitMultiANewArrayInsn(String desc, int dims) {
addImportWithTypeDesc(desc);
}
@Override
public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) {
addImportWithInternalName(owner);
Arrays.asList(Type.getArgumentTypes(desc)).forEach(this::addImport);
addImport(Type.getReturnType(desc));
}
@Override
public void visitFieldInsn(int opcode, String owner, String name, String desc) {
addImportWithInternalName(owner);
addImportWithTypeDesc(desc);
}
@Override
public void visitTypeInsn(int opcode, String type) {
addImportWithInternalName(type);
}
@Override
public void visitTryCatchBlock(Label start, Label end, Label handler, String type) {
if (type != null) { //null means finally block
addImportWithInternalName(type);
}
}
@Override
public void visitLocalVariable(String name, String desc, String signature, Label start, Label end, int index) {
addImportWithTypeDesc(desc);
}
@Override
public void visitLdcInsn(Object constant) {
if (constant instanceof Type) {
addImport((Type) constant);
}
}
@Override
public void visitInvokeDynamicInsn(String name, String desc, Handle bootstrapMethod, Object... bootstrapMethodArgs) {
for (Object arg : bootstrapMethodArgs) {
if (arg instanceof Type) {
addImport((Type) arg);
} else if (arg instanceof Handle) {
addImportWithInternalName(((Handle) arg).getOwner());
Arrays.asList(Type.getArgumentTypes(desc)).forEach(this::addImport);
} else if ((arg instanceof Number) == false && (arg instanceof String) == false) {
throw new AssertionError("Unexpected type " + arg.getClass() + " with value '" + arg + "'");
}
}
}
@Override
public void visitMaxs(int maxStack, int maxLocals) {
}
@Override
public void visitLineNumber(int line, Label start) {
}
//only for debugging
@Override
public void visitLookupSwitchInsn(Label dflt, int[] keys, Label[] labels) {
}
@Override
public void visitTableSwitchInsn(int min, int max, Label dflt, Label... labels) {
super.visitTableSwitchInsn(min, max, dflt, labels);
}
@Override
public void visitIincInsn(int variable, int increment) {
}
@Override
public void visitLabel(Label label) {
}
@Override
public void visitJumpInsn(int opcode, Label label) {
}
@Override
public void visitVarInsn(int opcode, int variable) {
}
@Override
public void visitIntInsn(int opcode, int operand) {
}
@Override
public void visitInsn(int opcode) {
}
@Override
public void visitFrame(int type, int nLocal, Object[] local, int nStack, Object[] stack) {
}
@Override
public void visitCode() {
}
}
|