2

Javaagentの開発にJavaInstrumentationとASMByteCodeLibraryを使用しています。メソッドによってスローされたランタイム例外を取得するにはどうすればよいですか?

コードを添付しました。ここでは、メソッドが正常に終了するか、例外をスローするかを取得しています。ただし、例外を取得できません。例外を取得する方法は?

 package com.abc.agent.servlet;

 import org.objectweb.asm.Label;
 import org.objectweb.asm.MethodVisitor;
 import org.objectweb.asm.Opcodes;
 import org.objectweb.asm.Type;

 import com.abc.agent.matcher.HttpServletMethodMatcher;

public class AbcServletMethodVisitorAdapter extends MethodVisitor {
    private String methodName;
    private String className;
    private String description;
    private boolean doMethodMatch;
    private int opcode = -1;

public AbcServletMethodVisitorAdapter(MethodVisitor mv , String  methodName , String description , String className) {
    super(Opcodes.ASM4, mv);
    this.methodName = methodName;
    this.className = className;
    this.description = description;
    this.doMethodMatch = false;
}

public void visitCode() {
    super.visitCode();
    if(methodName.equals("<clinit>") || methodName.equals("<init>"))
        return;
    HttpServletMethodMatcher httpServletMethodMatcher = new HttpServletMethodMatcher(className , methodName , description);
    this.doMethodMatch =  httpServletMethodMatcher.isHttpServletMatch();
    if(this.doMethodMatch){
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitLdcInsn(this.className);
        mv.visitLdcInsn(this.methodName);
        mv.visitLdcInsn(this.description);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "com/abc/agent/trace/RootTracer", "httpServletDoMethodBegin", "(Ljava/lang/Object;Ljavax/servlet/http/HttpServletRequest;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
        mv.visitCode();
    }
    else // Other Methods defined in the HttpServlet...
    {
        mv.visitLdcInsn(this.className);
        mv.visitLdcInsn(this.methodName);
        mv.visitLdcInsn(this.description);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "com/abc/agent/trace/RootTracer", "httpServletOtherMethodBegin", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
        mv.visitCode();
    }
}

public void visitMaxs(int maxStack, int maxLocals) {
    super.visitMaxs(maxStack + 4, maxLocals);
    }

public void visitInsn(int opcode) {
    if(methodName.equals("<clinit>") || methodName.equals("<init>")){
        // Do nothing....
    }
    else{
        if ((opcode >= Opcodes.IRETURN && opcode <= Opcodes.RETURN) || opcode == Opcodes.ATHROW) {
            this.opcode = opcode;
            mv.visitLdcInsn(this.className);
            mv.visitLdcInsn(this.methodName);
            mv.visitLdcInsn(this.description);
            mv.visitLdcInsn(this.opcode);
            if(this.doMethodMatch) {
                mv.visitMethodInsn(Opcodes.INVOKESTATIC, "com/abc/agent/trace/RootTracer", "httpServletDoMethodEnd", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V");
            }
            else{
                mv.visitMethodInsn(Opcodes.INVOKESTATIC, "com/abc/agent/trace/RootTracer", "httpServletOtherMethodEnd", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V");
            }
        }
    }
    mv.visitInsn(opcode);
}
}
4

1 に答える 1