2 回答

TA貢獻(xiàn)1886條經(jīng)驗 獲得超2個贊
從構(gòu)造函數(shù)MethodNode(int access, String name, String descriptor, String signature, String[] exceptions)的文檔中:
...子類不得使用此構(gòu)造函數(shù)。相反,他們必須使用MethodNode(int, int, String, String, String, String[])版本。
由于您正在創(chuàng)建一個子類,因此您必須更改調(diào)用
new MethodNode(access, name, desc, signature, exceptions) {
…
}
到
new MethodNode(ASM7, access, name, desc, signature, exceptions) {
…
}

TA貢獻(xiàn)1844條經(jīng)驗 獲得超8個贊
只是為了提供一個完整的運行樣本來檢測main()給定類的函數(shù)
public class Instrumentation {
public byte[] instrument(String className) {
byte[] modifiedClass = null;
try {
ClassReader classReader = new ClassReader(className);
ClassWriter classWriter = new ClassWriter(classReader, 4);
ClassVisitor classVisitor = new ClassVisitor(ASM7) {
public MethodVisitor visitMethod(
int access,
String name,
String desc,
String signature,
String[] exceptions) {
if (name.equals("main")) {
MethodNode methodNode = new MethodNode(ASM7,access, name, desc, signature, exceptions) {
public void visitEnd() {
// do some stuff here; remove exceptions, insnnode etc. -- smaple iterates through instructions
for (int i = 0; i < this.instructions.size();i++) {
AbstractInsnNode node = this.instructions.get(i);
}
}
};
return methodNode;
} else {
return super.visitMethod(access, name, desc, signature, exceptions);
}
}
};
classReader.accept(classVisitor,0);
classReader.accept(classWriter, 0);
modifiedClass = classWriter.toByteArray();
} catch (IOException ex) {
// handle IOException here
}
return modifiedClass;
}
}
添加回答
舉報