-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
ASMClassNodeAdapter.java
471 lines (393 loc) · 17.6 KB
/
ASMClassNodeAdapter.java
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
/*
* Copyright 2016 NAVER Corp.
*
* 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.
*/
package com.navercorp.pinpoint.profiler.instrument;
import com.navercorp.pinpoint.bootstrap.instrument.InstrumentContext;
import com.navercorp.pinpoint.profiler.util.JavaAssistUtils;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.AnnotationNode;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.FieldInsnNode;
import org.objectweb.asm.tree.FieldNode;
import org.objectweb.asm.tree.InnerClassNode;
import org.objectweb.asm.tree.InsnList;
import org.objectweb.asm.tree.InsnNode;
import org.objectweb.asm.tree.MethodNode;
import org.objectweb.asm.tree.VarInsnNode;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* @author jaehong.kim
*/
public class ASMClassNodeAdapter {
public static ASMClassNodeAdapter get(final InstrumentContext pluginContext, final ClassLoader classLoader, final String className) {
return get(pluginContext, classLoader, className, false);
}
public static ASMClassNodeAdapter get(final InstrumentContext pluginContext, final ClassLoader classLoader, final String className, final boolean skipCode) {
if (pluginContext == null || className == null) {
throw new IllegalArgumentException("plugin context or class name must not be null.");
}
InputStream in = null;
try {
in = pluginContext.getResourceAsStream(classLoader, className + ".class");
if (in != null) {
final ClassReader classReader = new ClassReader(in);
final ClassNode classNode = new ClassNode();
if (skipCode) {
classReader.accept(classNode, ClassReader.SKIP_CODE);
} else {
classReader.accept(classNode, 0);
}
return new ASMClassNodeAdapter(pluginContext, classLoader, classNode, skipCode);
}
} catch (IOException ignored) {
// not found class.
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ignored) {
}
}
}
return null;
}
private final InstrumentContext pluginContext;
private final ClassLoader classLoader;
private final ClassNode classNode;
private final boolean skipCode;
public ASMClassNodeAdapter(final InstrumentContext pluginContext, final ClassLoader classLoader, final ClassNode classNode) {
this(pluginContext, classLoader, classNode, false);
}
public ASMClassNodeAdapter(final InstrumentContext pluginContext, final ClassLoader classLoader, final ClassNode classNode, final boolean skipCode) {
this.pluginContext = pluginContext;
this.classLoader = classLoader;
this.classNode = classNode;
this.skipCode = skipCode;
}
public String getName() {
return this.classNode.name;
}
public String getInternalName() {
return this.classNode.name == null ? null : JavaAssistUtils.jvmNameToJavaName(this.classNode.name);
}
public String getSuperClassName() {
return this.classNode.superName;
}
public String getSuperClassInternalName() {
return this.classNode.superName == null ? null : JavaAssistUtils.jvmNameToJavaName(this.classNode.superName);
}
public boolean isInterface() {
return (classNode.access & Opcodes.ACC_INTERFACE) != 0;
}
public boolean isAnnotation() {
return (classNode.access & Opcodes.ACC_ANNOTATION) != 0;
}
public String[] getInterfaceInternalNames() {
final List<String> interfaces = this.classNode.interfaces;
if (interfaces == null || interfaces.size() == 0) {
return new String[0];
}
final List<String> list = new ArrayList<String>();
for (String name : interfaces) {
if (name != null) {
// to internal name.
list.add(JavaAssistUtils.jvmNameToJavaName(name));
}
}
return list.toArray(new String[list.size()]);
}
public ASMMethodNodeAdapter getDeclaredMethod(final String methodName, final String desc) {
if (this.skipCode) {
throw new IllegalStateException("not supported operation, skipCode option is true.");
}
return findDeclaredMethod(methodName, desc);
}
public boolean hasDeclaredMethod(final String methodName, final String desc) {
return findDeclaredMethod(methodName, desc) != null;
}
private ASMMethodNodeAdapter findDeclaredMethod(final String methodName, final String desc) {
if (methodName == null) {
return null;
}
final List<MethodNode> declaredMethods = classNode.methods;
if (declaredMethods == null) {
return null;
}
for (MethodNode methodNode : declaredMethods) {
if (methodNode.name == null || !methodNode.name.equals(methodName)) {
continue;
}
if (desc == null || (methodNode.desc != null && methodNode.desc.startsWith(desc))) {
return new ASMMethodNodeAdapter(getName(), methodNode);
}
}
return null;
}
public List<ASMMethodNodeAdapter> getDeclaredMethods() {
if (this.skipCode) {
throw new IllegalStateException("not supported operation, skipCode option is true.");
}
final List<ASMMethodNodeAdapter> methodNodes = new ArrayList<ASMMethodNodeAdapter>();
if (this.classNode.methods == null) {
return methodNodes;
}
for (MethodNode methodNode : this.classNode.methods) {
if (methodNode.name == null || methodNode.name.equals("<init>") || methodNode.name.equals("<clinit>")) {
// skip constructor(<init>) and static initializer block(<clinit>)
continue;
}
methodNodes.add(new ASMMethodNodeAdapter(getName(), methodNode));
}
return methodNodes;
}
public boolean hasOutClass(final String methodName, final String desc) {
if (methodName == null || this.classNode.outerClass == null || this.classNode.outerMethod == null || !this.classNode.outerMethod.equals(methodName)) {
return false;
}
if (desc == null || (this.classNode.outerMethodDesc != null && this.classNode.outerMethodDesc.startsWith(desc))) {
return true;
}
return false;
}
public boolean hasMethod(final String methodName, final String desc) {
if (hasDeclaredMethod(methodName, desc)) {
return true;
}
if (this.classNode.superName != null) {
// skip code.
final ASMClassNodeAdapter classNode = ASMClassNodeAdapter.get(this.pluginContext, this.classLoader, this.classNode.superName, true);
if (classNode != null) {
return classNode.hasMethod(methodName, desc);
}
}
return false;
}
public ASMFieldNodeAdapter getField(final String fieldName, final String fieldDesc) {
if (fieldName == null || this.classNode.fields == null) {
return null;
}
final List<FieldNode> fields = this.classNode.fields;
for (FieldNode fieldNode : fields) {
if ((fieldNode.name != null && fieldNode.name.equals(fieldName)) && (fieldDesc == null || (fieldNode.desc != null && fieldNode.desc.equals(fieldDesc)))) {
return new ASMFieldNodeAdapter(fieldNode);
}
}
// find interface.
final List<String> interfaces = this.classNode.interfaces;
if (interfaces != null && interfaces.size() > 0) {
for (String interfaceClassName : interfaces) {
if (interfaceClassName == null) {
continue;
}
final ASMClassNodeAdapter classNodeAdapter = ASMClassNodeAdapter.get(this.pluginContext, this.classLoader, interfaceClassName, true);
if (classNodeAdapter != null) {
final ASMFieldNodeAdapter fieldNode = classNodeAdapter.getField(fieldName, fieldDesc);
if (fieldNode != null) {
return fieldNode;
}
}
}
}
// find super class.
if (this.classNode.superName != null) {
final ASMClassNodeAdapter classNodeAdapter = ASMClassNodeAdapter.get(this.pluginContext, this.classLoader, this.classNode.superName, true);
if (classNodeAdapter != null) {
final ASMFieldNodeAdapter fieldNode = classNodeAdapter.getField(fieldName, fieldDesc);
if (fieldNode != null) {
return fieldNode;
}
}
}
return null;
}
public ASMFieldNodeAdapter addField(final String fieldName, final Class<?> fieldClass) {
if (fieldName == null || fieldClass == null) {
throw new IllegalArgumentException("fieldNode name or fieldNode class must not be null.");
}
final Type type = Type.getType(fieldClass);
final FieldNode fieldNode = new FieldNode(Opcodes.ACC_PRIVATE, fieldName, type.getDescriptor(), null, null);
if (this.classNode.fields == null) {
this.classNode.fields = new ArrayList<FieldNode>();
}
this.classNode.fields.add(fieldNode);
return new ASMFieldNodeAdapter(fieldNode);
}
public ASMMethodNodeAdapter addDelegatorMethod(final ASMMethodNodeAdapter superMethodNode) {
if (superMethodNode == null) {
throw new IllegalArgumentException("super method annotation must not be null.");
}
String[] exceptions = null;
if (superMethodNode.getExceptions() != null) {
exceptions = superMethodNode.getExceptions().toArray(new String[superMethodNode.getExceptions().size()]);
}
final ASMMethodNodeAdapter methodNode = new ASMMethodNodeAdapter(getName(), new MethodNode(superMethodNode.getAccess(), superMethodNode.getName(), superMethodNode.getDesc(), superMethodNode.getSignature(), exceptions));
methodNode.addDelegator(superMethodNode.getDeclaringClassName());
if (this.classNode.methods == null) {
this.classNode.methods = new ArrayList<MethodNode>();
}
this.classNode.methods.add(methodNode.getMethodNode());
return methodNode;
}
public void addGetterMethod(final String methodName, final ASMFieldNodeAdapter fieldNode) {
if (methodName == null || fieldNode == null) {
throw new IllegalArgumentException("method name or fieldNode annotation must not be null.");
}
// no argument is ().
final String desc = "()" + fieldNode.getDesc();
final MethodNode methodNode = new MethodNode(Opcodes.ACC_PUBLIC, methodName, desc, null, null);
if (methodNode.instructions == null) {
methodNode.instructions = new InsnList();
}
final InsnList instructions = methodNode.instructions;
// load this.
instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
// get fieldNode.
instructions.add(new FieldInsnNode(Opcodes.GETFIELD, classNode.name, fieldNode.getName(), fieldNode.getDesc()));
// return of type.
final Type type = Type.getType(fieldNode.getDesc());
instructions.add(new InsnNode(type.getOpcode(Opcodes.IRETURN)));
if (this.classNode.methods == null) {
this.classNode.methods = new ArrayList<MethodNode>();
}
this.classNode.methods.add(methodNode);
}
public void addSetterMethod(final String methodName, final ASMFieldNodeAdapter fieldNode) {
if (methodName == null || fieldNode == null) {
throw new IllegalArgumentException("method name or fieldNode annotation must not be null.");
}
// void is V.
final String desc = "(" + fieldNode.getDesc() + ")V";
final MethodNode methodNode = new MethodNode(Opcodes.ACC_PUBLIC, methodName, desc, null, null);
if (methodNode.instructions == null) {
methodNode.instructions = new InsnList();
}
final InsnList instructions = methodNode.instructions;
// load this.
instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
final Type type = Type.getType(fieldNode.getDesc());
// put field.
instructions.add(new VarInsnNode(type.getOpcode(Opcodes.ILOAD), 1));
instructions.add(new FieldInsnNode(Opcodes.PUTFIELD, classNode.name, fieldNode.getName(), fieldNode.getDesc()));
// return.
instructions.add(new InsnNode(Opcodes.RETURN));
if (this.classNode.methods == null) {
this.classNode.methods = new ArrayList<MethodNode>();
}
this.classNode.methods.add(methodNode);
}
public void addInterface(final String interfaceInternalName) {
if (interfaceInternalName == null) {
throw new IllegalArgumentException("interface internal name must not be null.");
}
if (this.classNode.interfaces == null) {
this.classNode.interfaces = new ArrayList<String>();
}
this.classNode.interfaces.add(JavaAssistUtils.javaNameToJvmName(interfaceInternalName));
}
public void copyMethod(final ASMMethodNodeAdapter methodNode) {
if (methodNode == null) {
throw new IllegalArgumentException("method annotation must not be null");
}
// change local call.
final ASMMethodInsnNodeRemapper remapper = new ASMMethodInsnNodeRemapper();
remapper.addFilter(methodNode.getDeclaringClassName(), null, null);
remapper.setOwner(this.classNode.name);
// remap method call.
methodNode.remapMethodInsnNode(remapper);
// remap desc of this.
methodNode.remapLocalVariables("this", Type.getObjectType(this.classNode.name).getDescriptor());
if (this.classNode.methods == null) {
this.classNode.methods = new ArrayList<MethodNode>();
}
this.classNode.methods.add(methodNode.getMethodNode());
}
public boolean hasAnnotation(final Class<?> annotationClass) {
if (annotationClass == null) {
return false;
}
final String desc = Type.getDescriptor(annotationClass);
return hasAnnotation(desc, this.classNode.invisibleAnnotations) || hasAnnotation(desc, this.classNode.visibleAnnotations);
}
private boolean hasAnnotation(final String annotationClassDesc, final List<AnnotationNode> annotationNodes) {
if (annotationClassDesc == null || annotationNodes == null) {
return false;
}
for (AnnotationNode annotation : annotationNodes) {
if (annotation.desc != null && annotation.desc.equals(annotationClassDesc)) {
return true;
}
}
return false;
}
public boolean subclassOf(final String superInternalName) {
if (superInternalName == null) {
return false;
}
if (superInternalName.equals("java.lang.Object")) {
// super is root.
return true;
}
ASMClassNodeAdapter classNode = this;
while (classNode != null) {
if (superInternalName.equals(classNode.getInternalName())) {
return true;
}
final String superClassName = classNode.getSuperClassName();
if (superClassName == null || superClassName.equals("java/lang/Object")) {
// find root annotation.
return false;
}
// skip code.
classNode = ASMClassNodeAdapter.get(this.pluginContext, this.classLoader, superClassName, true);
}
return false;
}
public List<ASMClassNodeAdapter> getInnerClasses() {
if (this.classNode.innerClasses == null) {
return Collections.EMPTY_LIST;
}
final List<ASMClassNodeAdapter> innerClasses = new ArrayList<ASMClassNodeAdapter>();
final List<InnerClassNode> innerClassNodes = this.classNode.innerClasses;
for (InnerClassNode node : innerClassNodes) {
if (node.name == null) {
continue;
}
// skip code.
ASMClassNodeAdapter adapter = get(this.pluginContext, this.classLoader, node.name, true);
if (adapter != null) {
innerClasses.add(adapter);
}
}
return innerClasses;
}
public byte[] toByteArray() {
final int majorVersion = this.classNode.version & 0xFFFF;
int flags = ClassWriter.COMPUTE_FRAMES;
if (majorVersion <= 49) {
// java 1.5 and less.
flags = ClassWriter.COMPUTE_MAXS;
}
final ClassWriter classWriter = new ASMClassWriter(this.pluginContext, this.classNode.name, this.classNode.superName, flags, this.classLoader);
this.classNode.accept(classWriter);
return classWriter.toByteArray();
}
}