4

我试图通过异常捕获运行时异常。我能够捕获常用的方法退出事件。但是,控制从来都达不到opcode==Opcodes.ATHROW

我认为我在调用事件时做错了。

这是我的示例代码:

 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");
                 }

在运行时异常时,现有的堆栈内容被清除,只剩下可抛出的对象。我可以通过 ASM 获得栈顶元素(即基本上是栈帧)吗?

提前致谢。

编辑 :

这是我添加 try - catch 块后修改的程序。

@Holger:感谢您的帮助。真的很感激。

所以,我试图在 ASM 的 MethodVisitor 中插入动态 try-catch 块。

这是我的代码:

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);

    }

我的意图是添加动态 try-catch 块,如果抛出任何异常,打印它。

4

1 回答 1

5
于 2013-09-20T14:29:26.380 回答