我想获得MethodNode
函数的main()
public class TestMethodNode {
public void main() {
}
}
所以我尝试了这个
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.tree.MethodNode;
import static org.objectweb.asm.Opcodes.ASM7;
import java.io.IOException;
public class Instrumentation {
public byte[] editFunction(String className) throws IOException {
byte[] modifiedClass = null;
try {
ClassReader classReader = new ClassReader(className);
ClassWriter classWriter = new ClassWriter(classReader, ClassWriter.COMPUTE_FRAMES);
ClassVisitor classAdapter = new ClassVisitor(ASM7, classWriter) {
public MethodVisitor visitMethod(
int access,
String name,
String desc,
String signature,
String[] exceptions) {
if (name.equals("main")) {
final MethodVisitor methodVisitor = super.visitMethod(access, name, desc, signature, exceptions);
MethodNode methodNode = new MethodNode(access, name, desc, signature, exceptions) {
public void visitEnd() {
// transform / analyze method here
accept(methodVisitor);
}
};
return methodNode;
} else {
return super.visitMethod(access, name, desc, signature, exceptions);
}
}
};
classReader.accept(classAdapter, 0);
modifiedClass = classWriter.toByteArray();
} catch (IOException ex) {
throw ex;
}
return modifiedClass;
}
}
并在创建IllegalStateException
时从... / asm / tree / MethodNode.java获得了MethodNode
public MethodNode(
final int access,
final String name,
final String descriptor,
final String signature,
final String[] exceptions) {
this(Opcodes.ASM7, access, name, descriptor, signature, exceptions);
if (getClass() != MethodNode.class) {
throw new IllegalStateException();
}
}
我做错了什么? (我不想入侵Minecraft,我正在研究并尝试操纵涉及lambda表达式和工作流引擎的嵌套/内部类的调用栈,该工作流引擎在运行时使用字节码操纵。)
答案 0 :(得分:0)
... 子类不得使用此构造函数。相反,他们必须使用
MethodNode(int, int, String, String, String, String[])
版本。
自创建子类以来,您必须更改调用
git push -u -f origin master
到
new MethodNode(access, name, desc, signature, exceptions) {
…
}
答案 1 :(得分:0)
只需为给定类的仪器功能main()
提供完整的示例
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;
}
}