public class Annotations extends Object
注解工具类,提供从AnnotatedElement及其层级结构中获取注解或合成注解的方法
工具类支持按get语义或find语义对AnnotatedElement上的元素进行查找:
get:指仅从AnnotatedElement.getDeclaredAnnotations()的范围中进行查找。
find:指从AnnotatedElement及其层级结构中进行查找,
当查找的AnnotatedElement不同时,查找对应的层级结构的定义也将有所不同:
Class、且Class.isAnnotation()返回false时,
此处层级结构即指类本身与其父类、父接口共同构成的层级结构,
find将查找层级结构中类、接口声明的注解;
Class、且Class.isAnnotation()返回true时,
此处层级结构指注解类及其元注解构成的层级结构,
find将查找该注解层级结构中的元注解,并对其进行处理;
Method时,此处层级结构指声明该方法的类的层级结构,
find将从层级结构中寻找与该方法签名相同的非桥接方法,并对其进行扫描;
Field时,此处层级结构指声明该属性的类的层级结构,
find将从层级结构中寻找该属性,并对其注解进行扫描;
AnnotationSearchMode或AnnotationScanner相关方法。
AnnotatedElement中规定的并不相符,
因此AnnotatedElement中对注解作用域的定义在当前工具类中无效。AnnotationSearchMode或AnnotationScanner相关方法。
Repeatable或RepeatableBy实现的可重复注解。AnnotationAggregator相关方法。
工具类支持将一批具有相关的注解对象“合并”为一个具有特殊属性的合成注解,
此类方法包括synthesize以及所有方法名以SynthesizedAnnotation结尾的方法。
合成注解一般用于合成一个指定的注解对象以及其元注解,
合成注解的属性将根据属性上的Link及其扩展注解而于源注解的属性有所不同,
根据Link.type()指定的RelationType,将会实现诸如属性互为镜像、属性互为别名或者属性互相覆盖的情况。
若有更细粒度的方法需求,可以参考AnnotationSynthesizer相关方法。
AnnotationSearchMode,
AnnotationSynthesizer,
SyntheticAnnotationResolver,
AnnotationAggregator,
RepeatableMappingRegistry| 限定符和类型 | 方法和说明 |
|---|---|
static <T extends Annotation> |
emptyAnnotations()
获取一个空注解数组
|
static <T extends Annotation> |
findAllDirectAnnotations(AnnotatedElement element,
Class<T> annotationType)
从元素的层级结构中获取指定注解
|
static <T extends Annotation> |
findAllDirectRepeatableAnnotations(AnnotatedElement element,
Class<T> annotationType)
从元素的层级结构中获取指定可重复注解
|
static <T extends Annotation> |
findAllIndirectAnnotations(AnnotatedElement element,
Class<T> annotationType)
从元素的层级结构及其元注解中获取指定注解
|
static <T extends Annotation> |
findAllIndirectRepeatableAnnotations(AnnotatedElement element,
Class<T> annotationType)
从元素的层级结构中获取指定可重复注解
|
static <T extends Annotation> |
findAllSynthesizedAnnotations(AnnotatedElement element,
Class<T> annotationType)
遍历元素的层级结构,并将扫描到的注解与其元注解“合并”,然后返回所有指定类型的合成注解
|
static <T extends Annotation> |
findDirectAnnotation(AnnotatedElement element,
Class<T> annotationType)
从元素的层级结构中获取指定注解
|
static <T extends Annotation> |
findIndirectAnnotation(AnnotatedElement element,
Class<T> annotationType)
从元素的层级结构中及其元注解中获取指定注解
|
static <T extends Annotation> |
findSynthesizedAnnotation(AnnotatedElement element,
Class<T> annotationType)
遍历元素的层级结构,并将扫描到的注解与其元注解“合并”,
若“合并”后的注解支持合成指定类型的合成注解,则返回该合成注解
|
static <T extends Annotation> |
getAllDirectAnnotations(AnnotatedElement element,
Class<T> annotationType)
从元素直接声明的注解中获取指定注解
|
static <T extends Annotation> |
getAllDirectRepeatableAnnotations(AnnotatedElement element,
Class<T> annotationType)
从元素直接声明的注解中获取指定可重复注解
|
static <T extends Annotation> |
getAllIndirectAnnotations(AnnotatedElement element,
Class<T> annotationType)
从元素直接声明的注解及元注解中获取指定注解
|
static <T extends Annotation> |
getAllIndirectRepeatableAnnotations(AnnotatedElement element,
Class<T> annotationType)
从元素直接声明的注解及元注解中获取指定可重复注解
|
static <T extends Annotation> |
getAllSynthesizedAnnotations(AnnotatedElement element,
Class<T> annotationType)
遍历元素的直接声明的注解,并将扫描到的注解与其元注解“合并”,然后返回所有指定类型的合成注解
|
static <T extends Annotation> |
getDeclaredAnnotation(AnnotatedElement element,
Class<T> annotationType)
获取直接声明的注解
|
static Annotation[] |
getDeclaredAnnotations(AnnotatedElement element)
获取直接声明的注解
|
static <T extends Annotation> |
getDirectAnnotation(AnnotatedElement element,
Class<T> annotationType)
从元素直接声明的注解中获取指定注解
|
static <T extends Annotation> |
getIndirectAnnotation(AnnotatedElement element,
Class<T> annotationType)
从元素直接声明的注解及元注解中获取指定注解
|
static <T extends Annotation> |
getNotSynthesizedAnnotation(T annotation)
若注解为一个合成注解,则返回该注解未被合成前的原始注解
|
static <T extends Annotation> |
getRepeatableFrom(Class<T> repeatableAnnotationType,
Annotation... annotations)
从指定的容器注解中获取可重复注解
|
static <T extends Annotation> |
getSynthesizedAnnotation(AnnotatedElement element,
Class<T> annotationType)
遍历元素的直接声明的注解,并将扫描到的注解与其元注解“合并”,
若“合并”后的注解支持合成指定类型的合成注解,则返回该合成注解
|
static boolean |
isAttributeMethod(Method method)
方法是否为注解属性方法。
|
static boolean |
isDirectAnnotationFound(AnnotatedElement element,
Class<? extends Annotation> annotationType)
判断注解是否在元素的层级结构中存在
|
static boolean |
isDirectAnnotationPresent(AnnotatedElement element,
Class<? extends Annotation> annotationType)
判断注解是否在元素直接声明的注解中存在
|
static boolean |
isIndirectAnnotationFound(AnnotatedElement element,
Class<? extends Annotation> annotationType)
判断注解是否在元素的层级结构及其元注解中存在
|
static boolean |
isIndirectAnnotationPresent(AnnotatedElement element,
Class<? extends Annotation> annotationType)
判断注解是否在元素直接声明的注解及元注解中存在
|
static boolean |
isSynthesizedAnnotation(Annotation annotation)
该注解是否为一个合成注解
|
static <T extends Annotation> |
synthesize(Annotation annotation,
Class<T> annotationType,
boolean includeMetaAnnotations)
将一个注解及其元注解合成为指定类型的合成注解
|
static <T extends Annotation> |
synthesize(Class<T> annotationType,
Annotation... annotations)
将一批注解按入参顺序合成为一个指定类型的注解,顺序越靠前的注解在合成中的优先级越高
|
public static Annotation[] getDeclaredAnnotations(AnnotatedElement element)
element - 注解元素public static <T extends Annotation> T getDeclaredAnnotation(AnnotatedElement element, Class<T> annotationType)
T - 注解类型element - 注解元素annotationType - 注解类型public static <T extends Annotation> T[] emptyAnnotations()
T - 注解类型public static boolean isAttributeMethod(Method method)
toString, hashCode与annotationTypemethod - 方法public static <T extends Annotation> T getDirectAnnotation(AnnotatedElement element, Class<T> annotationType)
T - 注解类型element - 查找的元素annotationType - 注解类型AnnotationSearchMode.SELF_AND_DIRECTpublic static <T extends Annotation> List<T> getAllDirectAnnotations(AnnotatedElement element, Class<T> annotationType)
T - 注解类型element - 查找的元素annotationType - 注解类型AnnotationSearchMode.SELF_AND_DIRECTpublic static <T extends Annotation> List<T> getAllDirectRepeatableAnnotations(AnnotatedElement element, Class<T> annotationType)
T - 注解类型element - 查找的元素annotationType - 可重复注解类型AnnotationSearchMode.SELF_AND_DIRECTpublic static boolean isDirectAnnotationPresent(AnnotatedElement element, Class<? extends Annotation> annotationType)
element - 查找的元素annotationType - 注解类型AnnotationSearchMode.SELF_AND_DIRECTpublic static <T extends Annotation> T getIndirectAnnotation(AnnotatedElement element, Class<T> annotationType)
T - 注解类型element - 查找的元素annotationType - 注解类型AnnotationSearchMode.SELF_AND_INDIRECTpublic static <T extends Annotation> List<T> getAllIndirectAnnotations(AnnotatedElement element, Class<T> annotationType)
T - 注解类型element - 查找的元素annotationType - 注解类型AnnotationSearchMode.SELF_AND_INDIRECTpublic static <T extends Annotation> List<T> getAllIndirectRepeatableAnnotations(AnnotatedElement element, Class<T> annotationType)
T - 注解类型element - 查找的元素annotationType - 可重复注解类型AnnotationSearchMode.SELF_AND_INDIRECTpublic static boolean isIndirectAnnotationPresent(AnnotatedElement element, Class<? extends Annotation> annotationType)
element - 查找的元素annotationType - 注解类型AnnotationSearchMode.SELF_AND_INDIRECTpublic static <T extends Annotation> T findDirectAnnotation(AnnotatedElement element, Class<T> annotationType)
T - 注解类型element - 查找的元素annotationType - 注解类型AnnotationSearchMode.TYPE_HIERARCHY_AND_DIRECTpublic static <T extends Annotation> List<T> findAllDirectAnnotations(AnnotatedElement element, Class<T> annotationType)
T - 注解类型element - 查找的元素annotationType - 注解类型AnnotationSearchMode.TYPE_HIERARCHY_AND_DIRECTpublic static <T extends Annotation> List<T> findAllDirectRepeatableAnnotations(AnnotatedElement element, Class<T> annotationType)
T - 注解类型element - 查找的元素annotationType - 可重复注解类型AnnotationSearchMode.TYPE_HIERARCHY_AND_DIRECTpublic static boolean isDirectAnnotationFound(AnnotatedElement element, Class<? extends Annotation> annotationType)
element - 查找的元素annotationType - 注解类型AnnotationSearchMode.TYPE_HIERARCHY_AND_DIRECTpublic static <T extends Annotation> T findIndirectAnnotation(AnnotatedElement element, Class<T> annotationType)
T - 注解类型element - 查找的元素annotationType - 注解类型AnnotationSearchMode.TYPE_HIERARCHY_AND_INDIRECTpublic static <T extends Annotation> List<T> findAllIndirectRepeatableAnnotations(AnnotatedElement element, Class<T> annotationType)
T - 注解类型element - 查找的元素annotationType - 可重复注解类型AnnotationSearchMode.TYPE_HIERARCHY_AND_DIRECTpublic static <T extends Annotation> List<T> findAllIndirectAnnotations(AnnotatedElement element, Class<T> annotationType)
T - 注解类型element - 查找的元素annotationType - 注解类型AnnotationSearchMode.TYPE_HIERARCHY_AND_INDIRECTpublic static boolean isIndirectAnnotationFound(AnnotatedElement element, Class<? extends Annotation> annotationType)
element - 查找的元素annotationType - 注解类型AnnotationSearchMode.TYPE_HIERARCHY_AND_INDIRECTpublic static <T extends Annotation> List<T> getRepeatableFrom(Class<T> repeatableAnnotationType, Annotation... annotations)
repeatableAnnotationType - 可重复注解类型annotations - 注解对象public static boolean isSynthesizedAnnotation(Annotation annotation)
annotation - 注解对象public static <T extends Annotation> T getNotSynthesizedAnnotation(T annotation)
T - 注解类型annotation - 注解对象public static <T extends Annotation> T synthesize(Annotation annotation, Class<T> annotationType, boolean includeMetaAnnotations)
T - 合成注解类型annotation - 待合成的注解annotationType - 注解类型includeMetaAnnotations - 是否扫描该注解的元注解public static <T extends Annotation> T synthesize(Class<T> annotationType, Annotation... annotations)
annotationType - 注解类型annotations - 待合成的注解public static <T extends Annotation> T getSynthesizedAnnotation(AnnotatedElement element, Class<T> annotationType)
element - 查找的元素annotationType - 注解类型AnnotationSearchMode.SELF_AND_DIRECTpublic static <T extends Annotation> List<T> getAllSynthesizedAnnotations(AnnotatedElement element, Class<T> annotationType)
element - 查找的元素annotationType - 注解类型AnnotationSearchMode.SELF_AND_DIRECTpublic static <T extends Annotation> T findSynthesizedAnnotation(AnnotatedElement element, Class<T> annotationType)
element - 查找的元素annotationType - 注解类型AnnotationSearchMode.TYPE_HIERARCHY_AND_DIRECTpublic static <T extends Annotation> List<T> findAllSynthesizedAnnotations(AnnotatedElement element, Class<T> annotationType)
element - 查找的元素annotationType - 注解类型AnnotationSearchMode.TYPE_HIERARCHY_AND_DIRECTCopyright © 2022. All rights reserved.