public class AnnotationUtil extends Object
Constructor and Description |
---|
AnnotationUtil() |
Modifier and Type | Method and Description |
---|---|
static void |
clearCaches()
清空相关缓存
|
static <A extends Annotation> |
getAnnotation(AnnotatedElement annotationEle,
Class<A> annotationType)
获取指定注解
|
static <T extends Annotation> |
getAnnotationAlias(AnnotatedElement annotationEle,
Class<T> annotationType)
获取别名支持后的注解
|
static Method[] |
getAnnotationAttributes(Class<? extends Annotation> annotationType)
获取注解属性,若已有缓存则从缓存中获取
|
static Annotation[] |
getAnnotations(AnnotatedElement annotationEle,
boolean isToCombination)
获取指定注解
|
static <T> T[] |
getAnnotations(AnnotatedElement annotationEle,
boolean isToCombination,
Class<T> annotationType)
获取指定注解
|
static Annotation[] |
getAnnotations(AnnotatedElement annotationEle,
boolean isToCombination,
Predicate<Annotation> predicate)
获取指定注解
|
static <T> T |
getAnnotationValue(AnnotatedElement annotationEle,
Class<? extends Annotation> annotationType)
获取指定注解默认值
如果无指定的属性方法返回null |
static <T> T |
getAnnotationValue(AnnotatedElement annotationEle,
Class<? extends Annotation> annotationType,
String propertyName)
获取指定注解属性的值
如果无指定的属性方法返回null |
static <A extends Annotation,R> |
getAnnotationValue(AnnotatedElement annotationEle,
SerFunction<A,R> propertyName)
获取指定注解属性的值
如果无指定的属性方法返回null |
static Map<String,Object> |
getAnnotationValueMap(AnnotatedElement annotationEle,
Class<? extends Annotation> annotationType)
获取指定注解中所有属性值
如果无指定的属性方法返回null |
static <T> T[] |
getCombinationAnnotations(AnnotatedElement annotationEle,
Class<T> annotationType)
获取组合注解
|
static Annotation[] |
getDeclaredAnnotations(AnnotatedElement element)
获取直接声明的注解,若已有缓存则从缓存中获取
|
static RetentionPolicy |
getRetentionPolicy(Class<? extends Annotation> annotationType)
获取注解类的保留时间,可选值 SOURCE(源码时),CLASS(编译时),RUNTIME(运行时),默认为 CLASS
|
static ElementType[] |
getTargetType(Class<? extends Annotation> annotationType)
获取注解类可以用来修饰哪些程序元素,如 TYPE, METHOD, CONSTRUCTOR, FIELD, PARAMETER 等
|
static boolean |
hasAnnotation(AnnotatedElement annotationEle,
Class<? extends Annotation> annotationType)
检查是否包含指定注解
|
static boolean |
isAnnotationAttribute(Method attribute)
该方法是否是注解属性,需要满足下述条件:
不是
Object.equals(Object) ;
不是Object.hashCode() ;
不是Object.toString() ;
不是桥接方法;
不是合成方法;
不是静态方法;
是公共方法;
方法必须没有参数;
方法必须有返回值(返回值类型不为Void );
|
static boolean |
isDocumented(Class<? extends Annotation> annotationType)
是否会保存到 Javadoc 文档中
|
static boolean |
isInherited(Class<? extends Annotation> annotationType)
是否可以被继承,默认为 false
|
static void |
setValue(Annotation annotation,
String annotationField,
Object value)
设置新的注解的属性(字段)值
注意此方法在jdk9+中抛出异常,须添加`--add-opens=java.base/java.lang=ALL-UNNAMED`启动参数 |
static CombinationAnnotatedElement |
toCombination(AnnotatedElement annotationEle)
将指定的被注解的元素转换为组合注解元素
|
public static Annotation[] getDeclaredAnnotations(AnnotatedElement element)
element
- AnnotatedElement
public static CombinationAnnotatedElement toCombination(AnnotatedElement annotationEle)
annotationEle
- 注解元素public static Annotation[] getAnnotations(AnnotatedElement annotationEle, boolean isToCombination)
annotationEle
- AnnotatedElement
,可以是Class、Method、Field、Constructor、ReflectPermissionisToCombination
- 是否为转换为组合注解,组合注解可以递归获取注解的注解public static <T> T[] getCombinationAnnotations(AnnotatedElement annotationEle, Class<T> annotationType)
T
- 注解类型annotationEle
- AnnotatedElement
,可以是Class、Method、Field、Constructor、ReflectPermissionannotationType
- 限定的public static <T> T[] getAnnotations(AnnotatedElement annotationEle, boolean isToCombination, Class<T> annotationType)
T
- 注解类型annotationEle
- AnnotatedElement
,可以是Class、Method、Field、Constructor、ReflectPermissionisToCombination
- 是否为转换为组合注解,组合注解可以递归获取注解的注解annotationType
- 限定的public static Annotation[] getAnnotations(AnnotatedElement annotationEle, boolean isToCombination, Predicate<Annotation> predicate)
annotationEle
- AnnotatedElement
,可以是Class、Method、Field、Constructor、ReflectPermissionisToCombination
- 是否为转换为组合注解,组合注解可以递归获取注解的注解predicate
- 过滤器,Predicate.test(Object)
返回true
保留,否则不保留public static <A extends Annotation> A getAnnotation(AnnotatedElement annotationEle, Class<A> annotationType)
A
- 注解类型annotationEle
- AnnotatedElement
,可以是Class、Method、Field、Constructor、ReflectPermissionannotationType
- 注解类型public static boolean hasAnnotation(AnnotatedElement annotationEle, Class<? extends Annotation> annotationType)
annotationEle
- AnnotatedElement
,可以是Class、Method、Field、Constructor、ReflectPermissionannotationType
- 注解类型public static <T> T getAnnotationValue(AnnotatedElement annotationEle, Class<? extends Annotation> annotationType) throws HutoolException
T
- 注解值类型annotationEle
- AnnotatedElement
,可以是Class、Method、Field、Constructor、ReflectPermissionannotationType
- 注解类型HutoolException
- 调用注解中的方法时执行异常public static <A extends Annotation,R> R getAnnotationValue(AnnotatedElement annotationEle, SerFunction<A,R> propertyName)
A
- 注解类型R
- 注解类型值annotationEle
- AnnotatedElement
,可以是Class、Method、Field、Constructor、ReflectPermissionpropertyName
- 属性名,例如注解中定义了name()方法,则 此处传入nameHutoolException
- 调用注解中的方法时执行异常public static <T> T getAnnotationValue(AnnotatedElement annotationEle, Class<? extends Annotation> annotationType, String propertyName) throws HutoolException
T
- 注解值类型annotationEle
- AnnotatedElement
,可以是Class、Method、Field、Constructor、ReflectPermissionannotationType
- 注解类型propertyName
- 属性名,例如注解中定义了name()方法,则 此处传入nameHutoolException
- 调用注解中的方法时执行异常public static Map<String,Object> getAnnotationValueMap(AnnotatedElement annotationEle, Class<? extends Annotation> annotationType) throws HutoolException
annotationEle
- AnnotatedElement
,可以是Class、Method、Field、Constructor、ReflectPermissionannotationType
- 注解类型HutoolException
- 调用注解中的方法时执行异常public static RetentionPolicy getRetentionPolicy(Class<? extends Annotation> annotationType)
annotationType
- 注解类public static ElementType[] getTargetType(Class<? extends Annotation> annotationType)
annotationType
- 注解类public static boolean isDocumented(Class<? extends Annotation> annotationType)
annotationType
- 注解类public static boolean isInherited(Class<? extends Annotation> annotationType)
annotationType
- 注解类public static void setValue(Annotation annotation, String annotationField, Object value)
annotation
- 注解对象annotationField
- 注解属性(字段)名称value
- 要更新的属性值public static <T extends Annotation> T getAnnotationAlias(AnnotatedElement annotationEle, Class<T> annotationType)
T
- 注解类型annotationEle
- 被注解的类annotationType
- 注解类型Classpublic static Method[] getAnnotationAttributes(Class<? extends Annotation> annotationType)
annotationType
- 注解类型public static boolean isAnnotationAttribute(Method attribute)
Object.equals(Object)
;Object.hashCode()
;Object.toString()
;Void
);attribute
- 方法对象public static void clearCaches()
Copyright © 2025. All rights reserved.