Class AnnoKit

java.lang.Object
org.miaixz.bus.core.xyz.AnnoKit

public class AnnoKit extends Object
注解工具类 快速获取注解对象、注解值等工具封装
Since:
Java 17+
Author:
Kimi Liu
  • Constructor Details

    • AnnoKit

      public AnnoKit()
  • Method Details

    • getDeclaredAnnotations

      public static Annotation[] getDeclaredAnnotations(AnnotatedElement element)
      获取直接声明的注解,若已有缓存则从缓存中获取
      Parameters:
      element - AnnotatedElement
      Returns:
      注解
    • toCombination

      public static CombinationAnnotatedElement toCombination(AnnotatedElement annotationEle)
      将指定的被注解的元素转换为组合注解元素
      Parameters:
      annotationEle - 注解元素
      Returns:
      组合注解元素
    • getAnnotations

      public static Annotation[] getAnnotations(AnnotatedElement annotationEle, boolean isToCombination)
      获取指定注解
      Parameters:
      annotationEle - AnnotatedElement,可以是Class、Method、Field、Constructor、ReflectPermission
      isToCombination - 是否为转换为组合注解,组合注解可以递归获取注解的注解
      Returns:
      注解对象
    • getCombinationAnnotations

      public static <T> T[] getCombinationAnnotations(AnnotatedElement annotationEle, Class<T> annotationType)
      获取组合注解
      Type Parameters:
      T - 注解类型
      Parameters:
      annotationEle - AnnotatedElement,可以是Class、Method、Field、Constructor、ReflectPermission
      annotationType - 限定的
      Returns:
      注解对象数组
    • getAnnotations

      public static <T> T[] getAnnotations(AnnotatedElement annotationEle, boolean isToCombination, Class<T> annotationType)
      获取指定注解
      Type Parameters:
      T - 注解类型
      Parameters:
      annotationEle - AnnotatedElement,可以是Class、Method、Field、Constructor、ReflectPermission
      isToCombination - 是否为转换为组合注解,组合注解可以递归获取注解的注解
      annotationType - 限定的
      Returns:
      注解对象数组
    • getAnnotations

      public static Annotation[] getAnnotations(AnnotatedElement annotationEle, boolean isToCombination, Predicate<Annotation> predicate)
      获取指定注解
      Parameters:
      annotationEle - AnnotatedElement,可以是Class、Method、Field、Constructor、ReflectPermission
      isToCombination - 是否为转换为组合注解,组合注解可以递归获取注解的注解
      predicate - 过滤器,Predicate.test(Object)返回true保留,否则不保留
      Returns:
      注解对象
    • getAnnotation

      public static <A extends Annotation> A getAnnotation(AnnotatedElement annotationEle, Class<A> annotationType)
      获取指定注解
      Type Parameters:
      A - 注解类型
      Parameters:
      annotationEle - AnnotatedElement,可以是Class、Method、Field、Constructor、ReflectPermission
      annotationType - 注解类型
      Returns:
      注解对象
    • hasAnnotation

      public static boolean hasAnnotation(AnnotatedElement annotationEle, Class<? extends Annotation> annotationType)
      检查是否包含指定注解
      Parameters:
      annotationEle - AnnotatedElement,可以是Class、Method、Field、Constructor、ReflectPermission
      annotationType - 注解类型
      Returns:
      是否包含指定注解
    • getAnnotationValue

      public static <T> T getAnnotationValue(AnnotatedElement annotationEle, Class<? extends Annotation> annotationType) throws InternalException
      获取指定注解默认值 如果无指定的属性方法返回null
      Type Parameters:
      T - 注解值类型
      Parameters:
      annotationEle - AnnotatedElement,可以是Class、Method、Field、Constructor、ReflectPermission
      annotationType - 注解类型
      Returns:
      注解对象
      Throws:
      InternalException - 调用注解中的方法时执行异常
    • getAnnotationValue

      public static <A extends Annotation, R> R getAnnotationValue(AnnotatedElement annotationEle, FunctionX<A,R> propertyName)
      获取指定注解属性的值 如果无指定的属性方法返回null
      Type Parameters:
      A - 注解类型
      R - 注解类型值
      Parameters:
      annotationEle - AnnotatedElement,可以是Class、Method、Field、Constructor、ReflectPermission
      propertyName - 属性名,例如注解中定义了name()方法,则 此处传入name
      Returns:
      注解对象
      Throws:
      InternalException - 调用注解中的方法时执行异常
    • getAnnotationValue

      public static <T> T getAnnotationValue(AnnotatedElement annotationEle, Class<? extends Annotation> annotationType, String propertyName) throws InternalException
      获取指定注解属性的值 如果无指定的属性方法返回null
      Type Parameters:
      T - 注解值类型
      Parameters:
      annotationEle - AnnotatedElement,可以是Class、Method、Field、Constructor、ReflectPermission
      annotationType - 注解类型
      propertyName - 属性名,例如注解中定义了name()方法,则 此处传入name
      Returns:
      注解对象
      Throws:
      InternalException - 调用注解中的方法时执行异常
    • getAnnotationValueMap

      public static Map<String,Object> getAnnotationValueMap(AnnotatedElement annotationEle, Class<? extends Annotation> annotationType) throws InternalException
      获取指定注解中所有属性值 如果无指定的属性方法返回null
      Parameters:
      annotationEle - AnnotatedElement,可以是Class、Method、Field、Constructor、ReflectPermission
      annotationType - 注解类型
      Returns:
      注解对象
      Throws:
      InternalException - 调用注解中的方法时执行异常
    • getRetentionPolicy

      public static RetentionPolicy getRetentionPolicy(Class<? extends Annotation> annotationType)
      获取注解类的保留时间,可选值 SOURCE(源码时),CLASS(编译时),RUNTIME(运行时),默认为 CLASS
      Parameters:
      annotationType - 注解类
      Returns:
      保留时间枚举
    • getTargetType

      public static ElementType[] getTargetType(Class<? extends Annotation> annotationType)
      获取注解类可以用来修饰哪些程序元素,如 TYPE, METHOD, CONSTRUCTOR, FIELD, PARAMETER 等
      Parameters:
      annotationType - 注解类
      Returns:
      注解修饰的程序元素数组
    • isDocumented

      public static boolean isDocumented(Class<? extends Annotation> annotationType)
      是否会保存到 Javadoc 文档中
      Parameters:
      annotationType - 注解类
      Returns:
      是否会保存到 Javadoc 文档中
    • isInherited

      public static boolean isInherited(Class<? extends Annotation> annotationType)
      是否可以被继承,默认为 false
      Parameters:
      annotationType - 注解类
      Returns:
      是否会保存到 Javadoc 文档中
    • setValue

      public static void setValue(Annotation annotation, String annotationField, Object value)
      设置新的注解的属性(字段)值 注意此方法在jdk9+中抛出异常,须添加`--add-opens=java.base/java.lang=ALL-UNNAMED`启动参数
      Parameters:
      annotation - 注解对象
      annotationField - 注解属性(字段)名称
      value - 要更新的属性值
    • getAnnotationAlias

      public static <T extends Annotation> T getAnnotationAlias(AnnotatedElement annotationEle, Class<T> annotationType)
      获取别名支持后的注解
      Type Parameters:
      T - 注解类型
      Parameters:
      annotationEle - 被注解的类
      annotationType - 注解类型Class
      Returns:
      别名支持后的注解
    • getAnnotationAttributes

      public static Method[] getAnnotationAttributes(Class<? extends Annotation> annotationType)
      获取注解属性,若已有缓存则从缓存中获取
      Parameters:
      annotationType - 注解类型
      Returns:
      注解属性
    • isAnnotationAttribute

      public static boolean isAnnotationAttribute(Method attribute)
      该方法是否是注解属性,需要满足下述条件:
      Parameters:
      attribute - 方法对象
      Returns:
      是否
    • clearCaches

      public static void clearCaches()
      清空相关缓存