public class AnnotatedElementUtil extends Object
AnnotatedElement
工具类,提供对层级结构中AnnotatedElement
上注解及元注解的访问支持,
并提供诸如基于Alias
的属性别名、基于父子注解间的属性值覆盖等特殊的属性映射机制支持。
搜索层级结构
参考 Spring 中AnnotatedElementUtils
,
工具类提供get以及find两种语义的搜索:
AnnotatedElement
本身;AnnotatedElement
本身外,
若AnnotatedElement
是类,则还会搜索其所有关联的父类和父接口;
若AnnotatedElement
是方法,则还会搜索其声明类关联的所有父类和父接口中,与该方法具有相同方法签名的方法对象;搜索元注解
工具类支持搜索注解的元注解。在所有格式为getXXX或findXXX的静态方法中,
若不带有directly关键字,则该方法支持搜索元注解,否则不支持搜索元注解。
eg:
若类A分别有父类和父接口B、C,上面分别有注解X与其元注解Y,
则此时基于A有:
Inherited
的效果,即通过directly方法将无法获得父类上带有Inherited
的注解。
注解属性映射
工具类支持注解对象属性上的一些属性映射机制,即当注解被扫描时, 将根据一些属性映射机制“解析”为其他类型的属性,这里支持的机制包括:
Alias
的属性别名:若注解属性通过Alias
互相关联,则对其中任意属性赋值,则等同于对所有关联属性赋值;
// set aliased attributes
@interface FooAnnotation {
@Alias("alias")
default String value() default "";
@Alias("value")
default String alias() default "";
}
@FooAnnotation("foo")
class Foo { }
// get resolved annotation
FooAnnotation annotation = getResolvedAnnotation(Foo.class, FooAnnotation.class);
annotation.value(); // = "foo"
annotation.alias(); // = "foo"
}
@interface Meta {
default String value() default "";
}
@Meta("meta")
@interface Root {
default String value() default ""; // overwrite for @Meta.value
}
@Root("foo")
class Foo { }
// get resolved annotation
Meta meta = getResolvedAnnotation(Foo.class, Meta.class);
meta.value(); // = "foo"
Root root = getResolvedAnnotation(Foo.class, Root.class);
root.value(); // = "foo"
可重复注解支持
工具类中格式为findAllXXX或getAllXXX格式的方法,
支持获得AnnotatedElement
上的可重复注解。
此处的可重复注解定义包括两方面:
AnnotatedElement
存在直接声明的注解,该注解有且仅有一个value属性,
该属性类型为注解数组,且数组中注解被Repeatable
注解,
则认为被包括的注解为可重复注解;AnnotatedElement
存在直接声明的注解,该注解与其他根注解皆有相同的元注解,
则获得元注解时,可以获得多个该相同的元注解。AnnotatedElement
可以获得两个Z
缓存
为了避免注解以及AnnotatedElement
层级结构解析过程中的大量反射调用,
工具类为AnnotatedElement
及其元注解信息进行了缓存。
缓存功能默认基于WeakConcurrentMap
实现,会在gc时自动回收部分缓存数据。
但是若有必要,也可以调用clearCaches()
方法主动清空缓存。
ResolvedAnnotationMapping
,
GenericAnnotationMapping
,
HierarchicalAnnotatedElements
,
RepeatableMetaAnnotatedElement
,
MetaAnnotatedElement
,
RepeatableAnnotationCollector
Constructor and Description |
---|
AnnotatedElementUtil() |
Modifier and Type | Method and Description |
---|---|
static AnnotatedElement |
asElement(Annotation... annotations)
将一组注解中的非
null 注解对象合并为一个AnnotatedElement |
static void |
clearCaches()
清空相关缓存,包括:
AnnotatedElementUtil 中的AnnotatedElement 及AnnotationMapping 缓存;
AnnotationUtil 中的AnnotatedElement 上直接声明的注解缓存;
RepeatableAnnotationCollector 中单例的注解属性缓存;
|
static AnnotatedElement |
emptyElement()
获取一个不包含任何注解的
AnnotatedElement |
static <T extends Annotation> |
findAllAnnotations(AnnotatedElement element,
Class<T> annotationType)
从
element 所处层级结构的所有AnnotatedElement 上直接声明的注解、
这些注解包含的可重复注解,以及上述所有注解的元注解中获取指定类型注解 |
static <T extends Annotation> |
findAllDirectlyAnnotations(AnnotatedElement element,
Class<T> annotationType)
从
element 上直接声明的注解、这些注解包含的可重复注解,
以及上述所有注解的元注解中获取指定类型注解。 |
static <T extends Annotation> |
findAllDirectlyResolvedAnnotations(AnnotatedElement element,
Class<T> annotationType)
从
element 所处层级结构的所有AnnotatedElement 上直接声明的注解、
这些注解包含的可重复注解,以及上述所有注解的元注解中获取指定类型注解。 |
static <T extends Annotation> |
findAllResolvedAnnotations(AnnotatedElement element,
Class<T> annotationType)
从
element 所处层级结构的所有AnnotatedElement 上直接声明的注解、
这些注解包含的可重复注解,以及上述所有注解的元注解中获取指定类型注解。 |
static <T extends Annotation> |
findAnnotation(AnnotatedElement element,
Class<T> annotationType)
从
element 所处层级结构的所有AnnotatedElement 上,获取该类型的注解或元注解 |
static Annotation[] |
findAnnotations(AnnotatedElement element)
从
element 所处层级结构的所有AnnotatedElement 上,获取所有的注解或元注解 |
static <T extends Annotation> |
findDirectlyAnnotation(AnnotatedElement element,
Class<T> annotationType)
从
element 所处层级结构的所有AnnotatedElement 上获取该类型的注解 |
static Annotation[] |
findDirectlyAnnotations(AnnotatedElement element)
从
element 所处层级结构的所有AnnotatedElement 上获取所有的注解 |
static <T extends Annotation> |
findDirectlyResolvedAnnotation(AnnotatedElement element,
Class<T> annotationType)
从
element 所处层级结构的所有AnnotatedElement 上,获取所有的注解。 |
static Annotation[] |
findDirectlyResolvedAnnotations(AnnotatedElement element)
从
element 所处层级结构的所有AnnotatedElement 上获取所有的注解。 |
static <T extends Annotation> |
findResolvedAnnotation(AnnotatedElement element,
Class<T> annotationType)
从
element 所处层级结构的所有AnnotatedElement 上,获取所有的注解或元注解。 |
static Annotation[] |
findResolvedAnnotations(AnnotatedElement element)
从
element 所处层级结构的所有AnnotatedElement 上,获取所有的注解或元注解。 |
static <T extends Annotation> |
getAllAnnotations(AnnotatedElement element,
Class<T> annotationType)
从
element 上直接声明的注解、这些注解包含的可重复注解,以及上述所有注解的元注解中获取指定类型注解 |
static <T extends Annotation> |
getAllDirectlyAnnotations(AnnotatedElement element,
Class<T> annotationType)
从
element 上直接声明的注解、这些注解包含的可重复注解中获取指定类型注解 |
static <T extends Annotation> |
getAllDirectlyResolvedAnnotations(AnnotatedElement element,
Class<T> annotationType)
从
element 上直接声明的注解、这些注解包含的可重复注解中获取指定类型注解 |
static <T extends Annotation> |
getAllResolvedAnnotations(AnnotatedElement element,
Class<T> annotationType)
|
static <T extends Annotation> |
getAnnotation(AnnotatedElement element,
Class<T> annotationType)
从
element 上,获取该类型的注解或元注解 |
static Annotation[] |
getAnnotations(AnnotatedElement element)
从
element 上,获取所有的注解或元注解 |
static <T extends Annotation> |
getDirectlyAnnotation(AnnotatedElement element,
Class<T> annotationType)
从
element 上获取该类型的注解 |
static Annotation[] |
getDirectlyAnnotations(AnnotatedElement element)
从
element 上获取所有的注解 |
static <T extends Annotation> |
getDirectlyResolvedAnnotation(AnnotatedElement element,
Class<T> annotationType)
从
element 上,获取所有的注解。 |
static Annotation[] |
getDirectlyResolvedAnnotations(AnnotatedElement element)
从
element 上,获取所有的注解。 |
static <T extends Annotation> |
getResolvedAnnotation(AnnotatedElement element,
Class<T> annotationType)
从
element 上,获取所有的注解或元注解。 |
static Annotation[] |
getResolvedAnnotations(AnnotatedElement element)
从
element 上,获取所有的注解或元注解。 |
static boolean |
isAnnotated(AnnotatedElement element,
Class<? extends Annotation> annotationType)
在
element 所处层级结构的所有AnnotatedElement 上,是否存在该类型的注解或元注解 |
static boolean |
isAnnotationPresent(AnnotatedElement element,
Class<? extends Annotation> annotationType)
在
element 上,是否存在该类型的注解或元注解 |
static AnnotatedElement |
toHierarchyElement(AnnotatedElement element)
扫描
element 所处层级结构中的AnnotatedElement ,
再把所有对象合并为HierarchicalAnnotatedElements
得到的对象可访问element 所处层级结构中所有AnnotatedElement 上的注解。 |
static AnnotatedElement |
toHierarchyMetaElement(AnnotatedElement element,
boolean resolved)
扫描
element 所处层级结构中的AnnotatedElement ,
并将其全部转为MetaAnnotatedElement 后,
再把所有对象合并为HierarchicalAnnotatedElements 。 |
static AnnotatedElement |
toHierarchyRepeatableMetaElement(AnnotatedElement element,
boolean resolved)
扫描
element 所处层级结构中的AnnotatedElement ,
并将其全部转为RepeatableMetaAnnotatedElement 后,
再把所有对象合并为HierarchicalAnnotatedElements 。 |
static AnnotatedElement |
toMetaElement(AnnotatedElement element,
boolean resolved)
|
static AnnotatedElement |
toRepeatableMetaElement(AnnotatedElement element,
boolean resolved)
将
AnnotatedElement 转为RepeatableMetaAnnotatedElement ,
得到的对象可访问AnnotatedElement 上的直接声明的注解,这些注解包含的可重复注解,以及上述注解的所有元注解。 |
static AnnotatedElement |
toRepeatableMetaElement(AnnotatedElement element,
RepeatableAnnotationCollector collector,
boolean resolved)
将
AnnotatedElement 转为RepeatableMetaAnnotatedElement ,
得到的对象可访问AnnotatedElement 上的直接声明的注解,
通过collector 从这些注解获得的可重复注解,以及上述注解的所有元注解。 |
public static boolean isAnnotated(AnnotatedElement element, Class<? extends Annotation> annotationType)
element
所处层级结构的所有AnnotatedElement
上,是否存在该类型的注解或元注解element
- AnnotatedElement
annotationType
- 注解类型public static <T extends Annotation> T findAnnotation(AnnotatedElement element, Class<T> annotationType)
element
所处层级结构的所有AnnotatedElement
上,获取该类型的注解或元注解T
- 注解类型element
- AnnotatedElement
annotationType
- 注解类型public static <T extends Annotation> T[] findAllAnnotations(AnnotatedElement element, Class<T> annotationType)
element
所处层级结构的所有AnnotatedElement
上直接声明的注解、
这些注解包含的可重复注解,以及上述所有注解的元注解中获取指定类型注解T
- 注解类型element
- AnnotatedElement
annotationType
- 注解类型public static Annotation[] findAnnotations(AnnotatedElement element)
element
所处层级结构的所有AnnotatedElement
上,获取所有的注解或元注解element
- AnnotatedElement
public static <T extends Annotation> T findResolvedAnnotation(AnnotatedElement element, Class<T> annotationType)
T
- 注解类型element
- AnnotatedElement
annotationType
- 注解类型public static Annotation[] findResolvedAnnotations(AnnotatedElement element)
element
- AnnotatedElement
public static <T extends Annotation> T[] findAllResolvedAnnotations(AnnotatedElement element, Class<T> annotationType)
element
所处层级结构的所有AnnotatedElement
上直接声明的注解、
这些注解包含的可重复注解,以及上述所有注解的元注解中获取指定类型注解。Alias
的别名、及子注解对元注解中同名同类型属性进行覆写的特殊机制。T
- 注解类型element
- AnnotatedElement
annotationType
- 注解类型public static <T extends Annotation> T findDirectlyAnnotation(AnnotatedElement element, Class<T> annotationType)
element
所处层级结构的所有AnnotatedElement
上获取该类型的注解T
- 注解类型element
- AnnotatedElement
annotationType
- 注解类型public static <T extends Annotation> T[] findAllDirectlyAnnotations(AnnotatedElement element, Class<T> annotationType)
element
上直接声明的注解、这些注解包含的可重复注解,
以及上述所有注解的元注解中获取指定类型注解。T
- 注解类型element
- AnnotatedElement
annotationType
- 注解类型public static Annotation[] findDirectlyAnnotations(AnnotatedElement element)
element
所处层级结构的所有AnnotatedElement
上获取所有的注解element
- AnnotatedElement
public static <T extends Annotation> T findDirectlyResolvedAnnotation(AnnotatedElement element, Class<T> annotationType)
T
- 注解类型element
- AnnotatedElement
annotationType
- 注解类型public static Annotation[] findDirectlyResolvedAnnotations(AnnotatedElement element)
element
- AnnotatedElement
public static <T extends Annotation> T[] findAllDirectlyResolvedAnnotations(AnnotatedElement element, Class<T> annotationType)
element
所处层级结构的所有AnnotatedElement
上直接声明的注解、
这些注解包含的可重复注解,以及上述所有注解的元注解中获取指定类型注解。Alias
的别名、及子注解对元注解中同名同类型属性进行覆写的特殊机制。T
- 注解类型element
- AnnotatedElement
annotationType
- 注解类型public static boolean isAnnotationPresent(AnnotatedElement element, Class<? extends Annotation> annotationType)
element
上,是否存在该类型的注解或元注解element
- AnnotatedElement
annotationType
- 注解类型public static <T extends Annotation> T getAnnotation(AnnotatedElement element, Class<T> annotationType)
element
上,获取该类型的注解或元注解T
- 注解类型element
- AnnotatedElement
annotationType
- 注解类型public static Annotation[] getAnnotations(AnnotatedElement element)
element
上,获取所有的注解或元注解element
- AnnotatedElement
public static <T extends Annotation> T[] getAllAnnotations(AnnotatedElement element, Class<T> annotationType)
element
上直接声明的注解、这些注解包含的可重复注解,以及上述所有注解的元注解中获取指定类型注解T
- 注解类型element
- AnnotatedElement
annotationType
- 注解类型public static <T extends Annotation> T getResolvedAnnotation(AnnotatedElement element, Class<T> annotationType)
T
- 注解类型element
- AnnotatedElement
annotationType
- 注解类型public static Annotation[] getResolvedAnnotations(AnnotatedElement element)
element
- AnnotatedElement
public static <T extends Annotation> T[] getAllResolvedAnnotations(AnnotatedElement element, Class<T> annotationType)
T
- 注解类型element
- AnnotatedElement
annotationType
- 注解类型public static <T extends Annotation> T getDirectlyAnnotation(AnnotatedElement element, Class<T> annotationType)
element
上获取该类型的注解T
- 注解类型element
- AnnotatedElement
annotationType
- 注解类型public static <T extends Annotation> T[] getAllDirectlyAnnotations(AnnotatedElement element, Class<T> annotationType)
element
上直接声明的注解、这些注解包含的可重复注解中获取指定类型注解T
- 注解类型element
- AnnotatedElement
annotationType
- 注解类型public static Annotation[] getDirectlyAnnotations(AnnotatedElement element)
element
上获取所有的注解element
- AnnotatedElement
public static <T extends Annotation> T getDirectlyResolvedAnnotation(AnnotatedElement element, Class<T> annotationType)
T
- 注解类型element
- AnnotatedElement
annotationType
- 注解类型public static Annotation[] getDirectlyResolvedAnnotations(AnnotatedElement element)
element
- AnnotatedElement
public static <T extends Annotation> T[] getAllDirectlyResolvedAnnotations(AnnotatedElement element, Class<T> annotationType)
element
上直接声明的注解、这些注解包含的可重复注解中获取指定类型注解T
- 注解类型element
- AnnotatedElement
annotationType
- 注解类型public static AnnotatedElement toHierarchyMetaElement(AnnotatedElement element, boolean resolved)
扫描element
所处层级结构中的AnnotatedElement
,
并将其全部转为MetaAnnotatedElement
后,
再把所有对象合并为HierarchicalAnnotatedElements
。
得到的对象可访问element
所处层级结构中所有AnnotatedElement
上的注解及元注解。
element
- 元素resolved
- 是否解析注解属性,若为true
则获得的注解将支持属性别名以及属性覆盖机制HierarchicalAnnotatedElements
实例getMetaElementCache(AnnotatedElement)
,
getResolvedMetaElementCache(AnnotatedElement)
public static AnnotatedElement toHierarchyRepeatableMetaElement(AnnotatedElement element, boolean resolved)
扫描element
所处层级结构中的AnnotatedElement
,
并将其全部转为RepeatableMetaAnnotatedElement
后,
再把所有对象合并为HierarchicalAnnotatedElements
。
得到的对象可访问element
所处层级结构中所有AnnotatedElement
上的直接声明的注解,
这些注解包含的可重复注解,以及上述注解的所有元注解。
element
- 元素resolved
- 是否解析注解属性,若为true
则获得的注解将支持属性别名以及属性覆盖机制HierarchicalAnnotatedElements
实例getRepeatableMetaElementCache(AnnotatedElement)
,
getResolvedRepeatableMetaElementCache(AnnotatedElement)
public static AnnotatedElement toHierarchyElement(AnnotatedElement element)
扫描element
所处层级结构中的AnnotatedElement
,
再把所有对象合并为HierarchicalAnnotatedElements
得到的对象可访问element
所处层级结构中所有AnnotatedElement
上的注解。
element
- 元素AnnotatedElement
实例public static AnnotatedElement toMetaElement(AnnotatedElement element, boolean resolved)
element
- 元素resolved
- 是否解析注解属性,若为true
则获得的注解将支持属性别名以及属性覆盖机制AnnotatedElement
实例getMetaElementCache(AnnotatedElement)
,
getResolvedMetaElementCache(AnnotatedElement)
public static AnnotatedElement toRepeatableMetaElement(AnnotatedElement element, boolean resolved)
AnnotatedElement
转为RepeatableMetaAnnotatedElement
,
得到的对象可访问AnnotatedElement
上的直接声明的注解,这些注解包含的可重复注解,以及上述注解的所有元注解。element
- 元素resolved
- 是否解析注解属性,若为true
则获得的注解将支持属性别名以及属性覆盖机制AnnotatedElement
实例getMetaElementCache(AnnotatedElement)
,
getResolvedMetaElementCache(AnnotatedElement)
public static AnnotatedElement toRepeatableMetaElement(AnnotatedElement element, RepeatableAnnotationCollector collector, boolean resolved)
将AnnotatedElement
转为RepeatableMetaAnnotatedElement
,
得到的对象可访问AnnotatedElement
上的直接声明的注解,
通过collector
从这些注解获得的可重复注解,以及上述注解的所有元注解。
注意:方法将不会通过缓存结果,因此每次调用都需要重新通过反射并获得相关注解。
collector
- 可重复注解收集器,为null
时等同于RepeatableAnnotationCollector.none()
element
- 元素resolved
- 是否解析注解属性,若为true
则获得的注解将支持属性别名以及属性覆盖机制AnnotatedElement
实例public static AnnotatedElement asElement(Annotation... annotations)
null
注解对象合并为一个AnnotatedElement
annotations
- 注解AnnotatedElement
实例ConstantElement
public static AnnotatedElement emptyElement()
AnnotatedElement
AnnotatedElement
实例EmptyElement
public static void clearCaches()
AnnotatedElementUtil
中的AnnotatedElement
及AnnotationMapping
缓存;AnnotationUtil
中的AnnotatedElement
上直接声明的注解缓存;RepeatableAnnotationCollector
中单例的注解属性缓存;Copyright © 2025. All rights reserved.