import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.commons.AdviceAdapter;
/**
* 对继承自AppCompatActivity的Activity进行插桩
*/
public class TraceVisitor extends ClassVisitor {
/**
* 类名
*/
private String className;
/**
* 父类名
*/
private String superName;
/**
* 该类实现的接口
*/
private String[] interfaces;
public TraceVisitor(String className, ClassVisitor classVisitor) {
super(Opcodes.ASM5, classVisitor);
}
/**
* ASM进入到类的方法时进行回调
*
* @param access
* @param name 方法名
* @param desc
* @param signature
* @param exceptions
* @return
*/
@Override
public MethodVisitor visitMethod(final int access, final String name, final String desc, final String signature,
String[] exceptions) {
MethodVisitor methodVisitor = cv.visitMethod(access, name, desc, signature, exceptions);
/**
* AdviceAdapter 可用于在一个方法的开头以及任意RETURN或ATHROW指令前插入代码
*/
methodVisitor = new AdviceAdapter(Opcodes.ASM5, methodVisitor, access, name, desc) {
private boolean isInject() {
//如果父类名是AppCompatActivity则拦截这个方法,实际应用中可以换成自己的父类例如BaseActivity
if (superName.contains("AppCompatActivity")) {
return true;
}
return false;
}
@Override
public void visitCode() {
super.visitCode();
}
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
return super.visitAnnotation(desc, visible);
}
@Override
public void visitFieldInsn(int opcode, String owner, String name, String desc) {
super.visitFieldInsn(opcode, owner, name, desc);
}
/**
* 方法开始之前回调
*/
@Override
protected void onMethodEnter() {
if (isInject()) {
if ("onCreate".equals(name)) {
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESTATIC,
"com/meishe/chajianandams/traceutils/TraceUtil",
"onActivityCreate", "(Landroid/app/Activity;)V",
false);
} else if ("onDestroy".equals(name)) {
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESTATIC, "com/meishe/chajianandams/traceutils/TraceUtil"
, "onActivityDestroy", "(Landroid/app/Activity;)V", false);
}
}
}
/**
* 方法结束时回调
* @param i
*/
@Override
protected void onMethodExit(int i) {
super.onMethodExit(i);
}
};
return methodVisitor;
}
/**
* 当ASM进入类时回调
*
* @param version
* @param access
* @param name 类名
* @param signature
* @param superName 父类名
* @param interfaces 实现的接口名
*/
@Override
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
super.visit(version, access, name, signature, superName, interfaces);
this.className = name;
this.superName = superName;
this.interfaces = interfaces;
}
}
AOP+ASM+插件化总结--实现基于注解的埋点和统计-- 代码篇之:AMS
最新推荐文章于 2024-06-13 19:11:09 发布