public abstract class AbstractAnnotationScanner extends Object implements AnnotationScanner
注解扫描器抽象类,用于从指定的AnnotatedElement及其可能存在的层级结构中获取注解对象。
扫描器支持通过ScanOptions对一些参数进行配置,
并且默认在AnnotationSearchMode中提供了一些预设了参数的常用扫描器。
在AbstractAnnotationScanner以及AnnotationProcessor中提到的垂直索引与水平索引将遵循如下定义:
若以扫描的AnnotatedElement为原点建立坐标系,
以被扫描到的注解所在的类层级作为y轴,同一类层级中注解被扫描到的顺序作为x轴,则有:
VERTICAL_INDEX_START_POINT开始递增;
HORIZONTAL_INDEX_START_POINT开始递增。
AnnotatedElement之间的位置关系。
默认情况下,扫描器将按照广度优先遍历被扫描的AnnotatedElement的层级结构中的被注解元素。
同一层级中的注解,按照其所属AnnotatedElement的被扫描顺序(元素本身 -> 父类 -> 父接口)、
以及其在AnnotatedElement.getDeclaredAnnotations()返回的注解数组中的顺序决定。
若允许扫描元注解,则在获得注解对象时,将优先扫描该注解对象的层级结构,
然后再继续扫描该与该注解处于统一层级,但是顺序靠后的注解。
比如:现有类X,上有注解A,A又有元注解B; 类X存在父类Y,上有注解C,C又有元注解D, 现对X进行扫描,若不扫描元注解,则依次获得A,C; 若扫描元注解,则依次获得A,B,C,D。
AbstractAnnotationScanner定义了根据配置项从待扫描元素的层级结构中获取注解的基本逻辑,
调用者需要在实现类中实现下述方法,以补全注解扫描相关功能的逻辑:
类的层级结构递归相关:
collectAnnotationTypeIfNecessary(List, Class):递归扫描到的注解的元注解层级结构;collectSuperTypeIfNecessary(List, Class):递归注解接口类的层级结构;collectInterfaceTypeIfNecessary(List, Class):递归父类的层级结构;getAnnotationFromType(Class, Class):从类上获取注解;getAnnotationsFromTypeDeclaredMethod(Class, Method):从类声明的方法上获取注解;getAnnotationsFromTypeDeclaredField(Class, Field):从类声明的属性上获取注解;AnnotationFilter,
AnnotationProcessor,
AnnotationSearchMode| 限定符和类型 | 类和说明 |
|---|---|
protected static class |
AbstractAnnotationScanner.Context
扫描上下文,用于存储一次扫描动作中的一些共享信息
|
| 限定符和类型 | 字段和说明 |
|---|---|
static int |
HORIZONTAL_INDEX_START_POINT
垂直索引起始点
|
protected ScanOptions |
options
扫描配置
|
static int |
VERTICAL_INDEX_START_POINT
水平索引起始点
|
| 限定符 | 构造器和说明 |
|---|---|
protected |
AbstractAnnotationScanner(ScanOptions options)
构造一个通用注解扫描器
|
| 限定符和类型 | 方法和说明 |
|---|---|
protected abstract void |
collectAnnotationTypeIfNecessary(List<Class<?>> nextTypeHierarchies,
Class<?> type)
若
ScanOptions#isEnableScanMetaAnnotation为true,则将目标类元注解也添加到队列 |
protected abstract void |
collectInterfaceTypeIfNecessary(List<Class<?>> nextTypeHierarchies,
Class<?> type)
若
ScanOptions#isEnableScanInterface为true,则将目标类的父接口也添加到队列 |
protected abstract void |
collectSuperTypeIfNecessary(List<Class<?>> nextTypeHierarchies,
Class<?> type)
若
ScanOptions#isEnableScanSuperClass为true,则将目标类的父类也添加到队列 |
ScanOptions |
copyOptions()
获取与当前扫描器相同的配置类
|
protected abstract Annotation[] |
getAnnotationFromType(Class<?> type,
Class<?> element)
从类获取注解对象
|
protected abstract Annotation[] |
getAnnotationsFromTypeDeclaredField(Class<?> type,
Field element)
从类中的指定属性获取注解对象
|
protected abstract Annotation[] |
getAnnotationsFromTypeDeclaredMethod(Class<?> type,
Method element)
从类中的指定方法获取注解对象
|
void |
scan(AnnotatedElement element,
AnnotationProcessor processor,
AnnotationFilter filter)
扫描指定元素上的注解
|
public static final int VERTICAL_INDEX_START_POINT
public static final int HORIZONTAL_INDEX_START_POINT
protected final ScanOptions options
protected AbstractAnnotationScanner(ScanOptions options)
options - 扫描配置public ScanOptions copyOptions()
public void scan(AnnotatedElement element, AnnotationProcessor processor, AnnotationFilter filter)
scan 在接口中 AnnotationScannerelement - 待扫描的元素processor - 注解处理器filter - 过滤器,若为空则不过滤任何注解protected abstract void collectAnnotationTypeIfNecessary(List<Class<?>> nextTypeHierarchies, Class<?> type)
ScanOptions#isEnableScanMetaAnnotation为true,则将目标类元注解也添加到队列nextTypeHierarchies - 下一层级待处理的类队列type - 当前正在处理的类对象protected abstract void collectInterfaceTypeIfNecessary(List<Class<?>> nextTypeHierarchies, Class<?> type)
ScanOptions#isEnableScanInterface为true,则将目标类的父接口也添加到队列nextTypeHierarchies - 下一层级待处理的类队列type - 当前正在处理的类对象protected abstract void collectSuperTypeIfNecessary(List<Class<?>> nextTypeHierarchies, Class<?> type)
ScanOptions#isEnableScanSuperClass为true,则将目标类的父类也添加到队列nextTypeHierarchies - 下一层级待处理的类队列type - 当前正在处理的类对象protected abstract Annotation[] getAnnotationsFromTypeDeclaredField(Class<?> type, Field element)
type - 当前正在处理的类对象element - 最开始扫描的对象protected abstract Annotation[] getAnnotationsFromTypeDeclaredMethod(Class<?> type, Method element)
type - 当前正在处理的类对象element - 最开始扫描的对象protected abstract Annotation[] getAnnotationFromType(Class<?> type, Class<?> element)
type - 当前正在处理的类对象element - 最开始扫描的对象Copyright © 2022. All rights reserved.