Class SyntheticMeta
- All Implemented Interfaces:
Annotation,AnnotatedElement,Synthetic
假设现有注解A,A上存在元注解B,B上存在元注解C,则对A解析得到的合成注解X,则CBA都是X的元注解,X为根注解
通过isAnnotationPresent(Class)可确定指定类型是注解是否是该合成注解的元注解,即是否为当前实例的“父类”
若指定注解是当前实例的元注解,则通过getAnnotation(Class)可获得动态代理生成的对应的注解实例
需要注意的是,由于认为合并注解X以最初的根注解A作为元注解,因此getAnnotations()或getDeclaredAnnotations() 都将只能获得A
若认为该合成注解X在第0层,则根注解A在第1层,B在第2层......以此类推, 则相同或不同的层级中可能会出现类型相同的注解对象,此时将通过SynthesizedSelector选择出最合适的注解对象,
该注解对象将在合成注解中作为唯一有效的元注解用于进行相关操作 默认情况下,将选择SynthesizedSelector.NEAREST_AND_OLDEST_PRIORITY选择器实例,
即层级越低的注解离根注解距离近,则该注解优先级越高,即遵循“就近原则”
合成注解中获取到的注解中可能会具有一些同名且同类型的属性, 此时将根据SynthesizedProcessor决定如何从这些注解的相同属性中获取属性值
默认情况下,将选择CacheableProcessor用于获取属性,
该处理器将选择距离根注解最近的注解中的属性用于获取属性值,getAnnotation(Class)获得的代理类实例的属性值遵循该规则
举个例子:若CBA同时存在属性y,则将X视为C,B或者A时,获得的y属性的值都与最底层元注解A的值保持一致
若两相同注解处于同一层级,则按照从其上一级“子注解”的AnnotatedElement.getAnnotations()的调用顺序排序
别名在合成注解中仍然有效,若注解X中任意属性上存在Alias注解,则Alias.value()指定的属性值将会覆盖注解属性的本身的值
Alias注解仅能指定注解X中存在的属性作为别名,不允许指定元注解或子类注解的属性
- Since:
- Java 17+
- Author:
- Kimi Liu
-
Nested Class Summary
Nested Classes -
Constructor Summary
ConstructorsConstructorDescriptionSyntheticMeta(Annotation source) 基于指定根注解,为其层级结构中的全部注解构造一个合成注解 当层级结构中出现了相同的注解对象时,将优先选择以距离根注解最近,且优先被扫描的注解对象, 当获取值时,同样遵循该规则SyntheticMeta(Annotation annotation, SynthesizedSelector annotationSelector, SynthesizedProcessor attributeProcessor) 基于指定根注解,为其层级结构中的全部注解构造一个合成注解 -
Method Summary
Modifier and TypeMethodDescriptionClass<? extends Annotation> 获取根注解类型<T extends Annotation>
TgetAnnotation(Class<T> annotationType) 获取被合成的注解获取全部注解获取合成注解选择器getAttribute(String attributeName, Class<?> attributeType) 根据指定的属性名与属性类型获取对应的属性值,若存在Alias则获取Alias.value()指定的别名属性的值获取合成注解属性处理器获取根注解直接声明的注解,该方法正常情况下当只返回原注解获取根注解getSynthesizedAnnotation(Class<?> annotationType) 获取已合成的注解booleanisAnnotationPresent(Class<? extends Annotation> annotationType) 当前合成注解中是否存在指定元注解<T extends Annotation>
TsyntheticAnnotation(Class<T> annotationType) 若合成注解在存在指定元注解,则使用动态代理生成一个对应的注解实例Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.lang.reflect.AnnotatedElement
getAnnotationsByType, getDeclaredAnnotation, getDeclaredAnnotationsByTypeMethods inherited from interface java.lang.annotation.Annotation
equals, hashCode, toString
-
Constructor Details
-
SyntheticMeta
基于指定根注解,为其层级结构中的全部注解构造一个合成注解 当层级结构中出现了相同的注解对象时,将优先选择以距离根注解最近,且优先被扫描的注解对象, 当获取值时,同样遵循该规则- Parameters:
source- 源注解
-
SyntheticMeta
public SyntheticMeta(Annotation annotation, SynthesizedSelector annotationSelector, SynthesizedProcessor attributeProcessor) 基于指定根注解,为其层级结构中的全部注解构造一个合成注解- Parameters:
annotation- 当前查找的注解对象annotationSelector- 合成注解选择器attributeProcessor- 注解属性处理器
-
-
Method Details
-
getSource
-
getAnnotationSelector
获取合成注解选择器- Specified by:
getAnnotationSelectorin interfaceSynthetic- Returns:
- 合成注解选择器
-
getAttributeProcessor
获取合成注解属性处理器- Specified by:
getAttributeProcessorin interfaceSynthetic- Returns:
- 合成注解属性处理器
-
getSynthesizedAnnotation
获取已合成的注解- Specified by:
getSynthesizedAnnotationin interfaceSynthetic- Parameters:
annotationType- 注解类型- Returns:
- 已合成的注解
-
annotationType
获取根注解类型- Specified by:
annotationTypein interfaceAnnotation- Specified by:
annotationTypein interfaceSynthetic- Returns:
- 注解类型
-
getAttribute
- Specified by:
getAttributein interfaceSynthetic- Parameters:
attributeName- 属性名attributeType- 属性类型- Returns:
- 属性
-
getAnnotation
获取被合成的注解- Specified by:
getAnnotationin interfaceAnnotatedElement- Specified by:
getAnnotationin interfaceSynthetic- Type Parameters:
T- 注解类型- Parameters:
annotationType- 注解类型- Returns:
- 注解对象
-
isAnnotationPresent
当前合成注解中是否存在指定元注解- Specified by:
isAnnotationPresentin interfaceAnnotatedElement- Specified by:
isAnnotationPresentin interfaceSynthetic- Parameters:
annotationType- 注解类型- Returns:
- 是否
-
getAnnotations
获取全部注解- Specified by:
getAnnotationsin interfaceAnnotatedElement- Specified by:
getAnnotationsin interfaceSynthetic- Returns:
- 注解对象
-
syntheticAnnotation
若合成注解在存在指定元注解,则使用动态代理生成一个对应的注解实例- Specified by:
syntheticAnnotationin interfaceSynthetic- Type Parameters:
T- 注解类型- Parameters:
annotationType- 注解类型- Returns:
- 合成注解对象
- See Also:
-
getDeclaredAnnotations
获取根注解直接声明的注解,该方法正常情况下当只返回原注解- Specified by:
getDeclaredAnnotationsin interfaceAnnotatedElement- Returns:
- 直接声明注解
-