期望通过注解的形式,给方法套上try catch。老样子,先看一下try catch的字节码。
CatchUtil
public class CatchUtil {
private void handle() {
try {
Date date = new Date();
} catch (Exception e) {
e.printStackTrace();
}
}
}
找到编译后的class文件,使用插件ASM Bytecode Viewer
private handle()V
TRYCATCHBLOCK L0 L1 L2 java/lang/Exception
L0
LINENUMBER 8 L0
NEW java/util/Date
DUP
INVOKESPECIAL java/util/Date.<init> ()V
ASTORE 1
L1
LINENUMBER 12 L1
GOTO L3
L2
LINENUMBER 9 L2
FRAME SAME1 java/lang/Exception
ASTORE 1
L4
LINENUMBER 10 L4
ALOAD 1
INVOKEVIRTUAL java/lang/Exception.printStackTrace ()V
L3
LINENUMBER 13 L3
FRAME SAME
RETURN
重点就是TRYCATCHBLOCK L0 L1 L2 java/lang/Exception
。label L0
是try块的起始点,label L1
是终点。方法正常调用不出异常的情况下try块走完GOTO
到L3,return结束方法;发生异常到异常处理处L2
,将异常加入局部变量表ASTORE
,然后ALOAD
异常,调用catch块中printStackTrace()打印堆栈。
所以我们需要在方法开始时插入TRYCATCHBLOCK
,异常处理标签L2后插入printStackTrace()
。
先弄个注解Catch
,只插入注解方法。
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface Catch {
}
CatchMethodVisitor
在方法前插入TRYCATCHBLOCK。
object CatchMethodVisitor {
operator fun invoke(
mv: MethodVisitor,
access: Int,
name: String?,
descriptor: String?,
): MethodVisitor {
return object : AdviceAdapter(Opcodes.ASM9, mv, access, name, descriptor) {
private var needTrack = false
private val start = Label()
private val end = Label()
private val catch = Label()
override fun visitAnnotation(descriptor: String?, visible: Boolean): AnnotationVisitor {
if (Type.getDescriptor(Catch::class.java) == descriptor) needTrack = true
return super.visitAnnotation(descriptor, visible)
}
override fun onMethodEnter() {
if (needTrack) {
visitLabel(start)
visitTryCatchBlock(start, end, catch, "java/lang/Exception")
}
super.onMethodEnter()
}
}
}
}
重写visitMaxs()
方法,在此之前插入catch块。
override fun visitMaxs(maxStack: Int, maxLocals: Int) {
if (needTrack) {
mv.visitLabel(end)
mv.visitLabel(catch)
mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, arrayOf<Any>("java/lang/Exception"))
val local = newLocal(Type.LONG_TYPE);
mv.visitVarInsn(ASTORE, local)
mv.visitVarInsn(ALOAD, local)
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Exception", "printStackTrace", "()V", false)
}
super.visitMaxs(maxStack, maxLocals)
}
try块结束标签end
,异常处理标签catch
,然后打印堆栈。
看起来很美好,跑个测试用例。
@Catch
private fun catchVoid() {
val date = Date()
}
@Catch
private fun catchObject() = Date()
找到transform中处理过的class文件,反编译成Java。
@Catch
private final void catchVoid() {
try {
new Date();
} catch (Exception var3) {
var3.printStackTrace();
}
}
@Catch
private final Date catchObject() {
try {
return new Date();
} catch (Exception var2) {
var2.printStackTrace();
}
}
嗯哼~原方法中有返回值就有问题了,插入try catch后方法结尾没有返回值报错。这里为了方便在catch块中将异常throw出去,当然也可以给方法返回一个默认值。
override fun visitMaxs(maxStack: Int, maxLocals: Int) {
if (needTrack) {
mv.visitLabel(end)
mv.visitLabel(catch)
mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, arrayOf<Any>("java/lang/Exception"))
val local = newLocal(Type.LONG_TYPE);
mv.visitVarInsn(ASTORE, local)
mv.visitVarInsn(ALOAD, local)
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Exception", "printStackTrace", "()V", false)
mv.visitVarInsn(ALOAD, local)
mv.visitInsn(ATHROW)
}
super.visitMaxs(maxStack, maxLocals)
}
简单的加个throw抛出异常,再跑一下测试用例。
@Catch
private final void catchVoid() {
try {
new Date();
} catch (Exception var3) {
var3.printStackTrace();
throw var3;
}
}
@Catch
private final Date catchObject() {
try {
return new Date();
} catch (Exception var2) {
var2.printStackTrace();
throw var2;
}
}
好起来了,个人感觉在catch块中处理异常后继续throw出去比较好,毕竟崩溃后定位问题也比较容易。返回默认值虽然能保证不崩溃,但业务逻辑可能产生其它奇奇怪怪的问题。
网友评论