I am trying to catch runtime exception through exception. I am able to capture usual method exiting events.
But , the control never reaches in opcode==Opcodes.ATHROW
.
I think I am doing something wrong in calling the events.
Here is my sample code :
public void visitCode() {
// mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
// mv.visitLdcInsn("Entering method " + fQMethodName);
// mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream","println","(Ljava/lang/String;)V");
}
@Override
public void visitInsn(int opcode)
{
if (opcode == Opcodes.ATHROW)
{
mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "err", "Ljava/io/PrintStream;");
mv.visitLdcInsn("Exiting on exception " );
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println",
"(Ljava/lang/String;)V");
}
super.visitInsn(opcode);
}
public void visitMethodInsn(int opcode, String owner, String name,
String desc) {
super.visitMethodInsn(opcode, owner, name, desc);
//
if (opcode == Opcodes.ATHROW)
{
mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "err", "Ljava/io/PrintStream;");
mv.visitLdcInsn("Exiting on exception " + name);
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println",
"(Ljava/lang/String;)V");
}
else if (!name.equals("println")
&& !name.equals("<init>")
&& (opcode == Opcodes.INVOKEVIRTUAL || opcode == Opcodes.INVOKESPECIAL
|| opcode == Opcodes.INVOKESTATIC || opcode == Opcodes.INVOKEDYNAMIC)) {
this.currentMethod = name;
onFinally(opcode);
}
}
private void onFinally(int opcode) {
mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "err",
"Ljava/io/PrintStream;");
mv.visitLdcInsn("Returning to " + fQMethodName + " from " + currentMethod);
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println",
"(Ljava/lang/String;)V");
}
At the time of runtime exception , the existing stack content is cleared and only the throwable object remains. Can I get the stack top element ( i.e stack frame basically ) through ASM ?
Thanks in adavance.
EDIT :
Here is my modified program after adding try - catch block.
@Holger : Thanks for your help. Really appreciate it.
So , I am trying to insert dynamic try-catch block in MethodVisitor of ASM.
Here is my code :
public void visitCode()
{
super.visitCode();
this.visitTryCatchBlock(lblTryBlockStart, lblTryBlockEnd, lblCatchExceptionBlockStart, "java/lang/Exception");
this.visitLabel(lblTryBlockStart);
}
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);
// load the exception
this.visitVarInsn(Opcodes.ALOAD, 1);
// call printStackTrace()
//this.visitInsn(Opcodes.ATHROW);
this.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Exception", "printStackTrace", "()V");
// exit from this dynamic block
this.visitLabel(exitBlock);
super.visitMaxs(maxStack+2, maxLocals+2);
}
My intention is to add dynamic try - catch block , and if any exception is thrown , print it.