本篇记录合并注解的定义和实现,如下图圈出的部分:
合并注解定义和实现
合并注解定义 - 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());
}
}
}