我正在编写一个应该附加的Java代理 到正在运行的JVM。在运行JVM上,已经加载了一个类,并且代理想要检测该类的方法。
代理使用ASM框架在要检测的方法内容中添加try
/ catch
块。
以下是我为添加try
/ catch
块而编写的代码:
public void visitCode() {
if (mMethodName.equals("test")) {
mv.visitLabel(startFinally);
super.visitCode();
}
}
public void visitMaxs(int maxStack, int maxLocals) {
Label endFinally = new Label();
mv.visitTryCatchBlock(startFinally,endFinally,endFinally,
"java/lang/Exception");
mv.visitLabel(endFinally);
mv.visitVarInsn(ASTORE, 1);
mv.visitVarInsn(ALOAD, 1);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Exception",
"printStackTrace", "()V"););
}
我在ClassWriter.COMPUTE_FRAMES
中使用ClassWriter
,但我收到了错误消息:
java.lang.RuntimeException: JSR/RET are not supported with computeFrames option
at com.bea.objectweb.asm.Frame.a(Unknown Source)
at com.bea.objectweb.asm.MethodWriter.visitJumpInsn(Unknown Source)
at com.bea.objectweb.asm.ClassReader.a(Unknown Source)