Annotation统计某些方法被调用次数

1.所需Jar
cglib.jar 动态代理
asm.jar cglib依赖于asm
2.看代码
2.1 注解定义
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface UseCaseAnnotation {
    int id() default 0;
}
2.2 要统计的方法
public class AnnotationTestTarget {

    @UseCaseAnnotation(id = 1)
    public void test1() {
    }

    @UseCaseAnnotation(id = 2)
    public void test2() {
    }
}
2.3 代理类(代理AnnotationTestTarget)
public class AnnotationProxy<T> implements MethodInterceptor {

    private T target;

    public T proxy(T target) {
        this.target = target;
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(target.getClass());
        enhancer.setCallback(this);
        return (T) enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        Object result = methodProxy.invoke(target, objects);
        UseCaseAnnotationHandler.countMethodInvokeNumber(method);
        return result;
    }
}
2.4 注解处理类 (根据方法id 统计次数)
public class UseCaseAnnotationHandler {

    private static Map<Integer, Integer> methodInvokeCount = new HashMap<>();

    public static void countMethodInvokeNumber(Method method) {
        UseCaseAnnotation annotation = method.getAnnotation(UseCaseAnnotation.class);
        if (Objects.nonNull(annotation)) {
            int id = annotation.id();
            Integer orDefault = methodInvokeCount.getOrDefault(id, 0);
            methodInvokeCount.put(id, orDefault + 1);
        }
    }

    public static Map<Integer, Integer> getMethodInvokeCount() {
        return methodInvokeCount;
    }
}
2.5 工厂类生成代理对象
public class BeanFactory {

    public static <T> T getBeanByClass(Class<T> clz) {
        try {
            T instance = clz.newInstance();
            return new AnnotationProxy<T>().proxy(instance);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
3.测试类
@Test
public void testAnnotation() throws Exception {
    AnnotationTestTarget target = BeanFactory.getBeanByClass(AnnotationTestTarget.class);
    if (Objects.isNull(target)) {
        throw new Exception("target为空");
    }
    for (int i = 0; i < 6; i ++) {
        target.test1();
    }
    for (int i = 0; i < 2; i ++) {
        target.test2();
    }
    Map<Integer, Integer> methodInvokeCount = UseCaseAnnotationHandler.getMethodInvokeCount();
    System.out.println(methodInvokeCount);
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容