2021-06-23 ActivityTimeAspectJ打点计时记录

package com.xxx.base.aspectj;

import android.util.Log;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

import java.util.HashMap;
import java.util.Map;

/**
 * author: liweipeng
 * date: 2020-04-09
 */
@Aspect
public class ActivityTimeAspectJ {

    private Map<String, Long> timeMap = new HashMap<>();

    @Pointcut("execution(* android.app.Activity+.onCreate(..))")
    public void activityOnCreate() {

    }

    @Pointcut("execution(* android.app.Activity+.onResume(..))")
    public void activityOnResume() {

    }

    @Pointcut("execution(* android.support.v4.app.Fragment+.onCreate(..))")
    public void fragmentOnCreate() {

    }

    @Pointcut("execution(* android.support.v4.app.Fragment+.onResume(..))")
    public void fragmentOnResume() {

    }

    @Before("activityOnCreate()||fragmentOnCreate()")
    public void activityOnCreateBefore(JoinPoint joinPoint) {
        String name = joinPoint.getThis().getClass().getName();
        Log.e("aspectj", "aspectj---onCreate,name=" + name);
        long startTime = System.currentTimeMillis();
        timeMap.put(name, startTime);
    }

    @After("activityOnResume()||fragmentOnResume()")
    public void activityOnOnResumeAfter(JoinPoint joinPoint) {
        String name = joinPoint.getThis().getClass().getName();
        Log.e("aspectj", "aspectj---onResume,name=" + name);
        long endTime = System.currentTimeMillis();
        if (timeMap.containsKey(name)) {
            Long startTime = timeMap.remove(name);
            Log.e("aspectj", "aspectj---onResume---" + (endTime - startTime)+"ms--"+name);
        }


    }

//    @Around("onCreateOrOnResume()")
//    public Object onCreateOrOnResumeAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
//        Object result = null;
//        try {
//            AppCompatActivity activity = (AppCompatActivity) proceedingJoinPoint.getTarget();
//            //        Log.d("AJAOP", "Aop Info" + activity.getClass().getCanonicalName() +
//            //                "\r\nkind : " + thisJoinPoint.getKind() +
//            //                "\r\nargs : " + thisJoinPoint.getArgs() +
//            //                "\r\nClass : " + thisJoinPoint.getClass() +
//            //                "\r\nsign : " + thisJoinPoint.getSignature() +
//            //                "\r\nsource : " + thisJoinPoint.getSourceLocation() +
//            //                "\r\nthis : " + thisJoinPoint.getThis()
//            //        );
//            long startTime = System.currentTimeMillis();
//            result = proceedingJoinPoint.proceed();
//            String activityName = activity.getClass().getCanonicalName();
//
//            Signature signature = proceedingJoinPoint.getSignature();
//            String sign = "";
//            String methodName = "";
//            if (signature != null) {
//                sign = signature.toString();
//                methodName = signature.getName();
//            }
//            if (!TextUtils.isEmpty(activityName) && !TextUtils.isEmpty(sign) && sign.contains(activityName)) {
//                Log.d("", "aspectj---activityName=" + activityName + ",methodName=" + methodName + ",sign=" + sign);
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        } catch (Throwable throwable) {
//            throwable.printStackTrace();
//        }
//        return result;
//    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值