Class AnnotationUtil

java.lang.Object
cloud.opencode.base.reflect.AnnotationUtil

public final class AnnotationUtil extends Object
Annotation Utility Class 注解工具类

Provides utilities for annotation discovery and processing.

提供注解发现和处理的工具。

Features | 主要功能:

  • Annotation discovery with caching - 带缓存的注解发现
  • Inherited annotation resolution - 继承注解解析
  • Repeatable annotation support - 可重复注解支持

Usage Examples | 使用示例:

Annotation ann = AnnotationUtil.findAnnotation(element, MyAnnotation.class);
List<Annotation> all = AnnotationUtil.findAnnotations(element);

Security | 安全性:

  • Thread-safe: Yes (uses ConcurrentHashMap for caching) - 线程安全: 是(使用ConcurrentHashMap缓存)
  • Null-safe: No (caller must ensure non-null arguments) - 空值安全: 否(调用方须确保非空参数)

Performance | 性能特性:

  • Time complexity: O(1) for cached lookups; O(h) for first access where h is the class hierarchy depth - 时间复杂度: 缓存命中时 O(1);首次访问为 O(h),h为类层次深度
  • Space complexity: O(a) for the annotation cache where a is the number of distinct annotations - 空间复杂度: O(a),a为不同注解的数量
Since:
JDK 25, opencode-base-reflect V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • getAnnotation

      public static <A extends Annotation> A getAnnotation(AnnotatedElement element, Class<A> annotationClass)
      Gets annotation (cached) 获取注解(缓存)
      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 annotation as Optional 获取注解为Optional
      Type Parameters:
      A - the annotation type | 注解类型
      Parameters:
      element - the annotated element | 被注解元素
      annotationClass - the annotation class | 注解类
      Returns:
      Optional of annotation | 注解的Optional
    • hasAnnotation

      public static boolean hasAnnotation(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
    • getAnnotations

      public static Annotation[] getAnnotations(AnnotatedElement element)
      Gets all annotations 获取所有注解
      Parameters:
      element - the annotated element | 被注解元素
      Returns:
      array of annotations | 注解数组
    • getDeclaredAnnotations

      public static Annotation[] getDeclaredAnnotations(AnnotatedElement element)
      Gets declared annotations 获取声明的注解
      Parameters:
      element - the annotated element | 被注解元素
      Returns:
      array of annotations | 注解数组
    • getRepeatableAnnotations

      public static <A extends Annotation> A[] getRepeatableAnnotations(AnnotatedElement element, Class<A> annotationClass)
      Gets repeatable annotations 获取可重复注解
      Type Parameters:
      A - the annotation type | 注解类型
      Parameters:
      element - the annotated element | 被注解元素
      annotationClass - the annotation class | 注解类
      Returns:
      array of annotations | 注解数组
    • findMetaAnnotation

      public static <A extends Annotation> A findMetaAnnotation(AnnotatedElement element, Class<A> annotationClass)
      Finds annotation including meta-annotations 查找注解包含元注解
      Type Parameters:
      A - the annotation type | 注解类型
      Parameters:
      element - the annotated element | 被注解元素
      annotationClass - the annotation class | 注解类
      Returns:
      the annotation or null | 注解或null
    • hasMetaAnnotation

      public static boolean hasMetaAnnotation(AnnotatedElement element, Class<? extends Annotation> annotationClass)
      Checks if has meta-annotation 检查是否有元注解
      Parameters:
      element - the annotated element | 被注解元素
      annotationClass - the annotation class | 注解类
      Returns:
      true if has | 如果有返回true
    • getAttributeValue

      public static Object getAttributeValue(Annotation annotation, String attributeName)
      Gets annotation attribute value 获取注解属性值
      Parameters:
      annotation - the annotation | 注解
      attributeName - the attribute name | 属性名
      Returns:
      the value | 值
    • getAttributeValue

      public static <T> T getAttributeValue(Annotation annotation, String attributeName, Class<T> type)
      Gets annotation attribute value with type 获取注解属性值(指定类型)
      Type Parameters:
      T - the type | 类型
      Parameters:
      annotation - the annotation | 注解
      attributeName - the attribute name | 属性名
      type - the expected type | 期望类型
      Returns:
      the value | 值
    • getAttributes

      public static Map<String,Object> getAttributes(Annotation annotation)
      Gets all annotation attributes as map 获取所有注解属性为Map
      Parameters:
      annotation - the annotation | 注解
      Returns:
      map of attributes | 属性映射
    • getValue

      public static Object getValue(Annotation annotation)
      Gets annotation "value" attribute 获取注解"value"属性
      Parameters:
      annotation - the annotation | 注解
      Returns:
      the value | 值
    • getValue

      public static <T> T getValue(Annotation annotation, Class<T> type)
      Gets annotation "value" attribute with type 获取注解"value"属性(指定类型)
      Type Parameters:
      T - the type | 类型
      Parameters:
      annotation - the annotation | 注解
      type - the expected type | 期望类型
      Returns:
      the value | 值
    • isRepeatable

      public static boolean isRepeatable(Class<? extends Annotation> annotationClass)
      Checks if annotation is repeatable 检查注解是否可重复
      Parameters:
      annotationClass - the annotation class | 注解类
      Returns:
      true if repeatable | 如果可重复返回true
    • getRepeatableContainer

      public static Class<? extends Annotation> getRepeatableContainer(Class<? extends Annotation> annotationClass)
      Gets repeatable container class 获取可重复注解容器类
      Parameters:
      annotationClass - the annotation class | 注解类
      Returns:
      the container class or null | 容器类或null
    • getAnnotationType

      public static Class<? extends Annotation> getAnnotationType(Annotation annotation)
      Gets annotation type 获取注解类型
      Parameters:
      annotation - the annotation | 注解
      Returns:
      the annotation type | 注解类型
    • getAttributeMethods

      public static List<Method> getAttributeMethods(Class<? extends Annotation> annotationClass)
      Gets annotation attribute methods 获取注解属性方法
      Parameters:
      annotationClass - the annotation class | 注解类
      Returns:
      list of methods | 方法列表
    • findAnnotationOnClass

      public static <A extends Annotation> A findAnnotationOnClass(Class<?> clazz, Class<A> annotationClass)
      Finds annotation on class hierarchy 在类层次结构中查找注解
      Type Parameters:
      A - the annotation type | 注解类型
      Parameters:
      clazz - the class | 类
      annotationClass - the annotation class | 注解类
      Returns:
      the annotation or null | 注解或null
    • findAllAnnotationsOnClass

      public static <A extends Annotation> List<A> findAllAnnotationsOnClass(Class<?> clazz, Class<A> annotationClass)
      Collects all annotations of type on class hierarchy 收集类层次结构中所有指定类型的注解
      Type Parameters:
      A - the annotation type | 注解类型
      Parameters:
      clazz - the class | 类
      annotationClass - the annotation class | 注解类
      Returns:
      list of annotations | 注解列表
    • clearCache

      public static void clearCache()
      Clears annotation cache 清除注解缓存