2
我正在使用Java Instrumentation和ASM ByteCode庫來開發Javaagent。 如何獲得由方法拋出的運行時異常?ASM ByteCode - 異常的堆棧跟蹤
附代碼。這裏得到方法是否正常終止或拋出 例外。但無法檢索異常。如何檢索異常?
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);
}
}
嗨,你的幫助..我已附上代碼,在此代碼,其中插入你所指定的行感謝... – 2013-03-06 05:03:41
因此,可以說,我有啓動手柄/任何結束方法,我可以添加一些東西。但是我想打印由當前方法或方法調用的當前方法拋出的任何異常的堆棧跟蹤。我將如何獲得這個方法拋出的異常對象? – AKS 2016-02-02 12:52:04
在我的示例代碼中,使用'ASTORE,1'的行將異常存儲在局部變量1中。然後,您可以根據需要多次檢索該異常。在我的例子中,我加載了'ALOAD,1'並且在其上調用'printStackTrace'。 – 2016-02-02 16:14:03