Class OpenAnnotation
java.lang.Object
cloud.opencode.base.reflect.OpenAnnotation
Annotation Facade Entry Class
注解门面入口类
Provides common annotation operations API.
提供常用注解操作API。
Features | 主要功能:
- Annotation retrieval - 注解获取
- Annotation presence checking - 注解存在检查
- Annotation attribute access - 注解属性访问
- Meta-annotation support - 元注解支持
Usage Examples | 使用示例:
// Check if annotation is present
boolean present = OpenAnnotation.isAnnotationPresent(method, Deprecated.class);
// Get annotation attribute value
Object value = OpenAnnotation.getAttributeValue(annotation, "value");
// Find inherited annotation
Optional<MyAnnotation> ann = OpenAnnotation.findAnnotationInherited(MyClass.class, MyAnnotation.class);
Security | 安全性:
- Thread-safe: Yes (stateless utility class) - 线程安全: 是(无状态工具类)
- Null-safe: No (caller must ensure non-null arguments) - 空值安全: 否(调用方须确保非空参数)
- Since:
- JDK 25, opencode-base-reflect V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic List<Annotation> filterAnnotations(AnnotatedElement element, Predicate<Annotation> predicate) Filters annotations by predicate 按谓词过滤注解static <A extends Annotation>
Optional<A> findAnnotation(AnnotatedElement element, Class<A> annotationClass) Gets an annotation from an element (Optional) 从元素获取注解(Optional)static <A extends Annotation>
Optional<A> findAnnotationInherited(Class<?> clazz, Class<A> annotationClass) Finds annotation including inherited (for classes) 查找注解(包含继承,用于类)static <A extends Annotation>
Optional<A> findAnnotationOnMethod(Method method, Class<A> annotationClass) Finds annotation on method including overridden methods 查找方法注解(包含被重写的方法)static <A extends Annotation>
Optional<A> findMetaAnnotation(Class<? extends Annotation> annotation, Class<A> metaAnnotation) Finds meta-annotation on an annotation 在注解上查找元注解static List<Constructor<?>> getAnnotatedConstructors(Class<?> clazz, Class<? extends Annotation> annotationClass) Gets all annotated constructors in a class 获取类中所有被注解的构造器getAnnotatedFields(Class<?> clazz, Class<? extends Annotation> annotationClass) Gets all annotated fields in a class 获取类中所有被注解的字段getAnnotatedMethods(Class<?> clazz, Class<? extends Annotation> annotationClass) Gets all annotated methods in a class 获取类中所有被注解的方法getAnnotatedParameters(Method method, Class<? extends Annotation> annotationClass) Gets all annotated parameters in a method 获取方法中所有被注解的参数static <A extends Annotation>
AgetAnnotation(AnnotatedElement element, Class<A> annotationClass) Gets an annotation from an element 从元素获取注解static Annotation[]getAnnotations(AnnotatedElement element) Gets all annotations from an element 获取元素的所有注解static <A extends Annotation>
A[]getAnnotationsByType(AnnotatedElement element, Class<A> annotationClass) Gets annotations by type (including repeatable) 按类型获取注解(包含可重复注解)static List<Annotation> getAnnotationsWithMeta(AnnotatedElement element, Class<? extends Annotation> metaAnnotation) Gets annotations annotated with a specific meta-annotation 获取被特定元注解标注的注解getAttributes(Class<? extends Annotation> annotationClass) Gets all annotation attributes (methods) 获取所有注解属性(方法)static ObjectgetAttributeValue(Annotation annotation, String attributeName) Gets annotation attribute value 获取注解属性值static <T> TgetAttributeValue(Annotation annotation, String attributeName, Class<T> type) Gets annotation attribute value with type 获取注解属性值(带类型)getAttributeValues(Annotation annotation) Gets all attribute values as a map 获取所有属性值(Map形式)static Annotation[]getDeclaredAnnotations(AnnotatedElement element) Gets declared annotations from an element 获取元素声明的注解(不含继承)static ObjectgetDefaultValue(Class<? extends Annotation> annotationClass, String attributeName) Gets the default value of an annotation attribute 获取注解属性的默认值static List<Annotation> getMetaAnnotations(Class<? extends Annotation> annotation) Gets all meta-annotations on an annotation 获取注解上的所有元注解static Optional<Class<? extends Annotation>> getRepeatableContainer(Class<? extends Annotation> annotationClass) Gets the container annotation for a repeatable annotation 获取可重复注解的容器注解static booleanisAllAnnotationsPresent(AnnotatedElement element, Class<? extends Annotation>... annotationClasses) Checks if all annotations are present 检查是否所有注解都存在static booleanisAnnotationPresent(AnnotatedElement element, Class<? extends Annotation> annotationClass) Checks if annotation is present 检查注解是否存在static booleanisAnyAnnotationPresent(AnnotatedElement element, Class<? extends Annotation>... annotationClasses) Checks if any of the annotations are present 检查是否存在任一注解static booleanisMetaAnnotationPresent(Class<? extends Annotation> annotation, Class<? extends Annotation> metaAnnotation) Checks if annotation is a meta-annotation of another 检查是否为元注解static booleanisRepeatable(Class<? extends Annotation> annotationClass) Checks if annotation is repeatable 检查注解是否可重复static booleanisRuntimeRetained(Class<? extends Annotation> annotationClass) Checks if annotation is runtime-retained 检查注解是否运行时保留
-
Method Details
-
getAnnotation
public static <A extends Annotation> A getAnnotation(AnnotatedElement element, Class<A> annotationClass) Gets an annotation from an element 从元素获取注解- Type Parameters:
A- the annotation type | 注解类型- Parameters:
element- the annotated element | 被注解元素annotationClass- the annotation class | 注解类- Returns:
- the annotation or null | 注解或null
-
findAnnotation
public static <A extends Annotation> Optional<A> findAnnotation(AnnotatedElement element, Class<A> annotationClass) Gets an annotation from an element (Optional) 从元素获取注解(Optional)- Type Parameters:
A- the annotation type | 注解类型- Parameters:
element- the annotated element | 被注解元素annotationClass- the annotation class | 注解类- Returns:
- Optional of annotation | 注解的Optional
-
getAnnotations
Gets all annotations from an element 获取元素的所有注解- Parameters:
element- the annotated element | 被注解元素- Returns:
- array of annotations | 注解数组
-
getDeclaredAnnotations
Gets declared annotations from an element 获取元素声明的注解(不含继承)- Parameters:
element- the annotated element | 被注解元素- Returns:
- array of annotations | 注解数组
-
getAnnotationsByType
public static <A extends Annotation> A[] getAnnotationsByType(AnnotatedElement element, Class<A> annotationClass) Gets annotations by type (including repeatable) 按类型获取注解(包含可重复注解)- Type Parameters:
A- the annotation type | 注解类型- Parameters:
element- the annotated element | 被注解元素annotationClass- the annotation class | 注解类- Returns:
- array of annotations | 注解数组
-
findAnnotationInherited
public static <A extends Annotation> Optional<A> findAnnotationInherited(Class<?> clazz, Class<A> annotationClass) Finds annotation including inherited (for classes) 查找注解(包含继承,用于类)- Type Parameters:
A- the annotation type | 注解类型- Parameters:
clazz- the class | 类annotationClass- the annotation class | 注解类- Returns:
- Optional of annotation | 注解的Optional
-
findAnnotationOnMethod
public static <A extends Annotation> Optional<A> findAnnotationOnMethod(Method method, Class<A> annotationClass) Finds annotation on method including overridden methods 查找方法注解(包含被重写的方法)- Type Parameters:
A- the annotation type | 注解类型- Parameters:
method- the method | 方法annotationClass- the annotation class | 注解类- Returns:
- Optional of annotation | 注解的Optional
-
isAnnotationPresent
public static boolean isAnnotationPresent(AnnotatedElement element, Class<? extends Annotation> annotationClass) Checks if annotation is present 检查注解是否存在- Parameters:
element- the annotated element | 被注解元素annotationClass- the annotation class | 注解类- Returns:
- true if present | 如果存在返回true
-
isAnyAnnotationPresent
@SafeVarargs public static boolean isAnyAnnotationPresent(AnnotatedElement element, Class<? extends Annotation>... annotationClasses) Checks if any of the annotations are present 检查是否存在任一注解- Parameters:
element- the annotated element | 被注解元素annotationClasses- the annotation classes | 注解类- Returns:
- true if any present | 如果任一存在返回true
-
isAllAnnotationsPresent
@SafeVarargs public static boolean isAllAnnotationsPresent(AnnotatedElement element, Class<? extends Annotation>... annotationClasses) Checks if all annotations are present 检查是否所有注解都存在- Parameters:
element- the annotated element | 被注解元素annotationClasses- the annotation classes | 注解类- Returns:
- true if all present | 如果全部存在返回true
-
getAttributeValue
Gets annotation attribute value 获取注解属性值- Parameters:
annotation- the annotation | 注解attributeName- the attribute name | 属性名- Returns:
- the value or null | 值或null
-
getAttributeValue
Gets annotation attribute value with type 获取注解属性值(带类型)- Type Parameters:
T- the value type | 值类型- Parameters:
annotation- the annotation | 注解attributeName- the attribute name | 属性名type- the expected type | 期望类型- Returns:
- the value or null | 值或null
-
getAttributeValues
Gets all attribute values as a map 获取所有属性值(Map形式)- Parameters:
annotation- the annotation | 注解- Returns:
- map of attribute name to value | 属性名到值的映射
-
getDefaultValue
public static Object getDefaultValue(Class<? extends Annotation> annotationClass, String attributeName) Gets the default value of an annotation attribute 获取注解属性的默认值- Parameters:
annotationClass- the annotation class | 注解类attributeName- the attribute name | 属性名- Returns:
- the default value or null | 默认值或null
-
isMetaAnnotationPresent
public static boolean isMetaAnnotationPresent(Class<? extends Annotation> annotation, Class<? extends Annotation> metaAnnotation) Checks if annotation is a meta-annotation of another 检查是否为元注解- Parameters:
annotation- the annotation | 注解metaAnnotation- the meta-annotation class | 元注解类- Returns:
- true if is meta-annotated | 如果被元注解标注返回true
-
findMetaAnnotation
public static <A extends Annotation> Optional<A> findMetaAnnotation(Class<? extends Annotation> annotation, Class<A> metaAnnotation) Finds meta-annotation on an annotation 在注解上查找元注解- Type Parameters:
A- the meta-annotation type | 元注解类型- Parameters:
annotation- the annotation | 注解metaAnnotation- the meta-annotation class | 元注解类- Returns:
- Optional of meta-annotation | 元注解的Optional
-
getMetaAnnotations
Gets all meta-annotations on an annotation 获取注解上的所有元注解- Parameters:
annotation- the annotation | 注解- Returns:
- list of meta-annotations | 元注解列表
-
isRuntimeRetained
Checks if annotation is runtime-retained 检查注解是否运行时保留- Parameters:
annotationClass- the annotation class | 注解类- Returns:
- true if runtime retained | 如果运行时保留返回true
-
isRepeatable
Checks if annotation is repeatable 检查注解是否可重复- Parameters:
annotationClass- the annotation class | 注解类- Returns:
- true if repeatable | 如果可重复返回true
-
getRepeatableContainer
public static Optional<Class<? extends Annotation>> getRepeatableContainer(Class<? extends Annotation> annotationClass) Gets the container annotation for a repeatable annotation 获取可重复注解的容器注解- Parameters:
annotationClass- the repeatable annotation class | 可重复注解类- Returns:
- Optional of container class | 容器类的Optional
-
getAttributes
Gets all annotation attributes (methods) 获取所有注解属性(方法)- Parameters:
annotationClass- the annotation class | 注解类- Returns:
- list of attribute methods | 属性方法列表
-
getAnnotatedFields
public static List<Field> getAnnotatedFields(Class<?> clazz, Class<? extends Annotation> annotationClass) Gets all annotated fields in a class 获取类中所有被注解的字段- Parameters:
clazz- the class | 类annotationClass- the annotation class | 注解类- Returns:
- list of fields | 字段列表
-
getAnnotatedMethods
public static List<Method> getAnnotatedMethods(Class<?> clazz, Class<? extends Annotation> annotationClass) Gets all annotated methods in a class 获取类中所有被注解的方法- Parameters:
clazz- the class | 类annotationClass- the annotation class | 注解类- Returns:
- list of methods | 方法列表
-
getAnnotatedConstructors
public static List<Constructor<?>> getAnnotatedConstructors(Class<?> clazz, Class<? extends Annotation> annotationClass) Gets all annotated constructors in a class 获取类中所有被注解的构造器- Parameters:
clazz- the class | 类annotationClass- the annotation class | 注解类- Returns:
- list of constructors | 构造器列表
-
getAnnotatedParameters
public static List<Parameter> getAnnotatedParameters(Method method, Class<? extends Annotation> annotationClass) Gets all annotated parameters in a method 获取方法中所有被注解的参数- Parameters:
method- the method | 方法annotationClass- the annotation class | 注解类- Returns:
- list of parameters | 参数列表
-
filterAnnotations
public static List<Annotation> filterAnnotations(AnnotatedElement element, Predicate<Annotation> predicate) Filters annotations by predicate 按谓词过滤注解- Parameters:
element- the annotated element | 被注解元素predicate- the predicate | 谓词- Returns:
- list of matching annotations | 匹配的注解列表
-
getAnnotationsWithMeta
public static List<Annotation> getAnnotationsWithMeta(AnnotatedElement element, Class<? extends Annotation> metaAnnotation) Gets annotations annotated with a specific meta-annotation 获取被特定元注解标注的注解- Parameters:
element- the annotated element | 被注解元素metaAnnotation- the meta-annotation class | 元注解类- Returns:
- list of annotations | 注解列表
-