有时我在摆弄ASM框架。我只想捕捉异常。我可以在ASM中基于方法签名抛出异常吗?
到目前为止,我能够在字节码中插入try-catch
块并捕获异常。
这就是我现在要做的。
public void visitMaxs(int maxStack, int maxLocals)
{
// visit try block end label
this.visitLabel(lblTryBlockEnd);
// visit normal execution exit block
//this.visitJumpInsn(Opcodes.GOTO, exitBlock);
// visit catch exception block
this.visitLabel(lblCatchExceptionBlockStart);
// store the exception
this.visitVarInsn(Opcodes.ASTORE, 1);
super.visitTypeInsn(Opcodes.NEW, "java/lang/Exception");
super.visitInsn(Opcodes.DUP);
// load the exception
this.visitVarInsn(Opcodes.ALOAD, 1);
// Initializing the exception object with the throwable cause
super.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Exception", "<init>", "(Ljava/lang/Throwable;)V");
// calling jensor method to write
super.visitMethodInsn(Opcodes.INVOKESTATIC,
"test/ExceptionHandleTest",
"exceptionHandler",
"(Ljava/lang/Exception;)V");
// call printStackTrace()
this.visitInsn(Opcodes.ATHROW);
// exit from this dynamic block
this.visitLabel(exitBlock);
super.visitMaxs(maxStack+2, maxLocals);
}
`
现在,我不想扔掉每捕获的异常(如我在做每一次athrow
现在),而不是我想只有当它与MethodVisitor
方法签名的exception
参数匹配扔。
我试图这么做,但得到了Falling off the end of the code
类验证错误。
可以使用ASM吗?
在此先感谢。
是的,ASMifier是伟大的 – omainegra