java asm如何收集异常

[复制链接]
查看11 | 回复1 | 2021-1-27 06:45:38 | 显示全部楼层 |阅读模式
java原始代码:
publicvoidthrowableCaught(Stringinput)throwsSQLException,TimeoutException{
if(input.equals("1"))
thrownewSQLException();
elseif(input.equals("2"))
thrownewTimeoutException();
elseif(input.equals("3"))
{
try
{
inti=1;
intj=0;
System.out.println(j);
j=i/j;
}
catch(Exceptione)
{
e.printStackTrace();
}
}
}
希望通过asm修改字节码将原始代码变成:
publicvoidthrowableCaught(Stringinput)throwsSQLException,TimeoutException
{
if(input.equals("1")){
SQLExceptionlocalSQLException=newSQLException();
ThrowablelocalThrowable1=(Throwable)localSQLException;
System.out.println(localThrowable1);
throwlocalSQLException;
}if(input.equals("2")){
TimeoutExceptionlocalTimeoutException=newTimeoutException();
ThrowablelocalThrowable2=(Throwable)localTimeoutException;
System.out.println(localThrowable2);
throwlocalTimeoutException;
}
}
如果将原始代码中的input.equals("3")那块屏蔽,asm修改后可以得到上面的代码;
如果不屏蔽input.equals("3")那块,结果是:
publicvoidthrowableCaught(Stringinput)throwsSQLException,TimeoutException
{
if(input.equals("1")){
SQLExceptionlocalSQLException=newSQLException();
ThrowablelocalThrowable1=(Throwable)localSQLException;
System.out.println(localThrowable1);
throwlocalSQLException;
}if(input.equals("2"))
{
TimeoutExceptionlocalTimeoutException;//----------这里没有实例化了--------------
ThrowablelocalThrowable2=(Throwable)localTimeoutException;
System.out.println(localThrowable2);
throwlocalTimeoutException;
}
inti=1;
intj=0;
System.out.println(j);
j=i/j;
}

asm代码:
publicclassDBMethodAdaptorextendsAdviceAdapter{
......
@Override
publicvoidvisitInsn(intopcode){
if(opcode==Opcodes.ATHROW){
//gettheThrowableobjectoffthestack
mv.visitInsn(Opcodes.DUP);
intexceptionVar=newLocal(Type.getType(Throwable.class));
mv.visitVarInsn(Opcodes.ASTORE,exceptionVar);
mv.visitVarInsn(Opcodes.ALOAD,exceptionVar);
mv.visitTypeInsn(Opcodes.CHECKCAST,"java/lang/Throwable");
intexceptionVar2=newLocal(Type.getType(Throwable.class));
mv.visitVarInsn(Opcodes.ASTORE,exceptionVar2);
mv.visitFieldInsn(GETSTATIC,"java/lang/System","out","Ljava/io/PrintStream;");
mv.visitVarInsn(ALOAD,exceptionVar2);
mv.visitMethodInsn(INVOKEVIRTUAL,"java/io/PrintStream","println","(Ljava/lang/Object;)V",false);
mv.visitVarInsn(ALOAD,exceptionVar);
}
mv.visitInsn(opcode);
}
@Override
publicvoidvisitMaxs(intmaxStack,intmaxLocals){
mv.visitMaxs(0,0);//COMPUTE_MAXS自动计算
}
}
分 -->
回复

使用道具 举报

千问 | 2021-1-27 06:45:38 | 显示全部楼层
mv.visitInsn(Opcodes.DUP);->exception|exception
int exceptionVar = newLocal(Type.getType(Throwable.class));->exceptionVar|exception|exception
mv.visitVarInsn(Opcodes.ASTORE, exceptionVar);->exception
mv.visitVarInsn(Opcodes.ALOAD, exceptionVar);->exceptionVar|exception
mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Throwable");->exception
int exceptionVar2 = newLocal(Type.getType(Throwable.class));->exceptionVar2|exception
mv.visitVarInsn(Opcodes.ASTORE, exceptionVar2);->没有了
mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");->out
mv.visitVarInsn(ALOAD, exceptionVar2);->out|exceptionVar2
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/Object;)V",false);没有了
mv.visitVarInsn(ALOAD, exceptionVar);这里又补回来了、
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行