随笔 - 177,  文章 - 0,  评论 - 12,  阅读 - 18万

AOP方式的无埋点 一般分几种:

  • Java 中的动态代理,运行时动态创建 Proxy 类实例
  • APT,注解处理器,编译时生成 .java 代码
  • Javassist for Android:一个移植到Android平台的非常知名的操纵字节码的java库,对 class 字节码进行修改
  • AspectJ:和Java语言无缝衔接的面向切面的编程的扩展工具(可用于Android)

AspectJ简单来说就是在代码的编译时期向项目代码植入我们的埋点代码

 

复制代码
package com.example.mytestaop

import android.app.Activity
import android.util.Log
import android.widget.ImageView
import android.widget.TextView
import org.aspectj.lang.ProceedingJoinPoint
import org.aspectj.lang.annotation.Around
import org.aspectj.lang.annotation.Aspect
import org.aspectj.lang.annotation.Before
import org.aspectj.lang.annotation.Pointcut
import org.aspectj.lang.reflect.MethodSignature
import java.util.*


@Aspect
class MethodAspect {
    // 此处指定一个切点,后面括号中的是切点表达式(个人理解:其表达的就是一个join point),详细见Aspect基本语法
    @Pointcut("execution(* android.view.View.OnClickListener+.onClick(..))")
//    @Pointcut("execution(* onResume()) && within(com.example.mytestaop.BaseActivity+)")
    fun clickMethod() {
    }

    //表示一个通知,类型为Before并指定切点为上面callMethod方法所表示的那个切点
    @Around("clickMethod()")
//    @Around("execution(* onResume())")
    @Throws(Throwable::class)
    fun clickAdvice(joinPoint: ProceedingJoinPoint): Any? {

//        Log.e("haha", "activityName->;;;;;;" + joinPoint.target.javaClass.name) //织入的代码
//        Log.e("haha", "activityName->;;;;;;" + joinPoint.target.javaClass.simpleName) //织入的代码


        Log.e("haha", "before->;;;;;;") //织入的代码

        Log.e("haha", "name1->" + joinPoint.signature.name) //方法名
        Log.e("haha", "name1->" + joinPoint.signature.declaringType.simpleName) //织入的代码
        Log.e("haha", "name1->" + joinPoint.signature.declaringTypeName) //织入的代码
        Log.e("haha", "name1->" + joinPoint.signature.modifiers) //织入的代码

        val args = joinPoint.args
        if (args != null) {
            for (i in args.indices) {
                Log.e("haha", "args->$i::" + args[i]) //织入的代码
                Log.e("haha", "argsNext->$i::" + args[i]) //织入的代码

                when {
                    args[i] is TextView -> {
                        val textView = args[i] as? TextView
                        Log.e("haha", "textViewId::->" + textView?.resources?.getResourceEntryName(textView.id)) //织入的代码
                        Log.e("haha", "context->;;;;;;" + (args[i] as TextView?)?.context) //织入的代码

                        val contextString = textView?.context.toString()
                        val pageName = contextString.substring(contextString.lastIndexOf(".") + 1, contextString.indexOf("@"))
                        Log.e("haha", "pageName->;;;;;;" + pageName) //织入的代码
                    }

                    args[i] is ImageView -> {
                        Log.e("haha", "img->" + (args[i] as ImageView?)?.id) //织入的代码
                    }
                }
            }
        }

        Log.e("haha", "after->;;;;;;") //织入的代码

        return joinPoint.proceed()
    }


}
复制代码

 

posted on   毕哥  阅读(51)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示