私は時々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
と一致する場合にのみスローします。exception
MethodVisitor
そうしようとしましたが、Falling off the end of the code
クラス検証エラーが発生しました。
ASM を使用して行うことは可能ですか?
前もって感謝します。