手写Spring源码05 - 元数据读取器 MetadataReader 设计与实现(四)

本篇记录合并注解的定义和实现,如下图圈出的部分:
在这里插入图片描述

合并注解定义和实现
合并注解定义 - MergedAnnotation

代码实现:

package cn.zhanghongyang.core.annotation;

import java.lang.annotation.Annotation;

/**
 * @Author: zhanghongyang
 * @Description: 合并的注解
 */
public interface MergedAnnotation<A extends Annotation> {

    /**
     * 获取注解类型
     * @return
     */
    Class<A> getType();

    /**
     * 获取距离
     * @return
     */
    int getDistance();

    /**
     * 注解来源
     * @return
     */
    Object source();


    /**
     * 创建具体实现类
     * @param source
     * @param annotationType
     * @return
     */
    static MergedAnnotation of(String source, Class<? extends Annotation> annotationType){
        return TypeMappedAnnotation.of(source, annotationType);
    }
}
合并注解集合定义 - MergedAnnotations

代码实现:

package cn.zhanghongyang.core.annotation;

import java.util.Collection;

/**
 * @Author: zhanghongyang
 * @Description:
 */
public interface MergedAnnotations {

    static MergedAnnotations of(Collection<MergedAnnotation> annotations){
        return MergedAnnotationsCollection.of(annotations);
    }

}

合并注解实现 - TypeMappedAnnotation

代码实现:

package cn.zhanghongyang.core.annotation;

import java.lang.annotation.Annotation;

/**
 * @Author: zhanghongyang
 * @Description:
 */
public class TypeMappedAnnotation implements MergedAnnotation{

    /** 注解类型 */
    private AnnotationTypeMapping mapping;

    /** 注解来源全称 */
    private String source;

    public TypeMappedAnnotation(AnnotationTypeMapping mapping, String source) {
        this.mapping = mapping;
        this.source = source;
    }

    public static MergedAnnotation of(String source, Class<? extends Annotation> annotationType){
        // 这个的作用相当于将annotationType注解封装成AnnotationTypeMapping,这里使用AnnotationTypeMappings获取了注解的元注解,但是只是取了注解本身(索引0的元素就是注解本身),
        // AnnotationTypeMappings获取annotationType的元注解以及元注解的元注解...,但是AnnotationTypeMappings使用了缓存,所有这样写并不影响速度,反而从缓存中取还少创建了对象
        AnnotationTypeMappings mappings = AnnotationTypeMappings.forAnnotationType(annotationType);

        return new TypeMappedAnnotation(mappings.get(0), source);
    }

    @Override
    public Class getType() {
        return this.mapping.getAnnotationType();
    }

    @Override
    public int getDistance() {
        return this.mapping.getDistance();
    }

    @Override
    public Object source() {
        return this.mapping.getSource();
    }
}
合并注解集合实现 - MergedAnnotationsCollection

代码实现:

package cn.zhanghongyang.core.annotation;

import java.util.Collection;

/**
 * @Author: zhanghongyang
 * @Description: MergedAnnotations 实现类,
 * 保存了所有注解,和注解过滤后的元注解
 */
public class MergedAnnotationsCollection implements MergedAnnotations{

    public static MergedAnnotations of(Collection<MergedAnnotation> annotations){
        return new MergedAnnotationsCollection(annotations);
    }


    /** 保存注解的数组 */
    private MergedAnnotation[] annotations;

    /**
     * 保存注解的元注解信息,
     * 每一个AnnotationTypeMappings,包含一个AnnotationTypeMapping数组,AnnotationTypeMapping保存了一个元注解信息,每一个AnnotationTypeMappings就是一个元注解集合
     * 所以和上面annotations一一对应,annotations[i]过滤后的所有元注解是AnnotationTypeMappings[i]
     */
    private AnnotationTypeMappings[] mappings;


    public MergedAnnotationsCollection(Collection<MergedAnnotation> annotations) {
        this.annotations = annotations.toArray(new MergedAnnotation[0]);
        this.mappings = new AnnotationTypeMappings[this.annotations.length];
        for (int i = 0; i < this.annotations.length; i++) {
            this.mappings[i] = AnnotationTypeMappings.forAnnotationType(this.annotations[i].getType());
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值