Class AnnoKit

java.lang.Object
org.aoju.bus.core.toolkit.AnnoKit

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

    • AnnoKit

      public AnnoKit()
  • Method Details

    • isJdkMetaAnnotation

      public static boolean isJdkMetaAnnotation(Class<? extends Annotation> annotationType)
      是否为Jdk自带的元注解 包括:
      Parameters:
      annotationType - 注解类型
      Returns:
      是否为Jdk自带的元注解
    • isNotJdkMateAnnotation

      public static boolean isNotJdkMateAnnotation(Class<? extends Annotation> annotationType)
      是否不为Jdk自带的元注解 包括:
      Parameters:
      annotationType - 注解类型
      Returns:
      是否为Jdk自带的元注解
    • toCombination

      public static Annotated 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)
      获取指定注解默认值 如果无指定的属性方法返回null
      Type Parameters:
      T - 注解值类型
      Parameters:
      annotationEle - AccessibleObject,可以是Class、Method、Field、Constructor、ReflectPermission
      annotationType - 注解类型
      Returns:
      注解对象
    • getAnnotationValue

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

      public static Map<String,Object> getAnnotationValueMap(AnnotatedElement annotationEle, Class<? extends Annotation> annotationType)
      获取指定注解中所有属性值 如果无指定的属性方法返回null
      Parameters:
      annotationEle - AnnotatedElement,可以是Class、Method、Field、Constructor、ReflectPermission
      annotationType - 注解类型
      Returns:
      注解对象
    • 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)
      设置新的注解的属性(字段)值
      Parameters:
      annotation - 注解对象
      annotationField - 注解属性(字段)名称
      value - 要更新的属性值
    • getAlias

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

      public static <T extends Annotation> T getSynthesis(Annotation annotation, Class<T> annotationType)
      将指定注解实例与其元注解转为合成注解
      Type Parameters:
      T - 注解类型
      Parameters:
      annotation - 注解
      annotationType - 注解类型
      Returns:
      合成注解
    • getAllSynthesis

      public static <T extends Annotation> List<T> getAllSynthesis(AnnotatedElement annotatedEle, Class<T> annotationType)
      获取元素上所有指定注解
      • 若元素是类,则递归解析全部父类和全部父接口上的注解
      • 若元素是方法、属性或注解,则只解析其直接声明的注解
      Type Parameters:
      T - 注解类型
      Parameters:
      annotatedEle - AnnotatedElement,可以是Class、Method、Field、Constructor、ReflectPermission
      annotationType - 注解类
      Returns:
      合成注解
    • scanMetaAnnotation

      public static List<Annotation> scanMetaAnnotation(Class<? extends Annotation> annotationType)
      扫描注解类,以及注解类的Class层级结构中的注解,将返回除了META_ANNOTATIONS中指定的JDK默认注解外, 按元注解对象与annotationType的距离和Class.getAnnotations()顺序排序的注解对象集合
      Parameters:
      annotationType - 注解类
      Returns:
      注解对象集合
    • scanClass

      public static List<Annotation> scanClass(Class<?> targetClass)

      扫描类以及类的Class层级结构中的注解,将返回除了META_ANNOTATIONS中指定的JDK默认元注解外, 全部类/接口的Class.getAnnotations()方法返回的注解对象 层级结构将按广度优先递归,遵循规则如下:

      • 同一层级中,优先处理父类,然后再处理父接口;
      • 同一个接口在不同层级出现,优先选择层级距离targetClass更近的接口;
      • 同一个接口在相同层级出现,优先选择其子类/子接口被先解析的那个;
      注解根据其声明类/接口被扫描的顺序排序,若注解都在同一个Class中被声明,则还会遵循Class.getAnnotations()的顺序
      Parameters:
      targetClass - 类
      Returns:
      注解对象集合
    • scanMethod

      public static List<Annotation> scanMethod(Method method)

      扫描方法,以及该方法所在类的Class层级结构中的具有相同方法签名的方法, 将返回除了META_ANNOTATIONS中指定的JDK默认元注解外, 全部匹配方法上AccessibleObject.getAnnotations()方法返回的注解对象 方法所在类的层级结构将按广度优先递归,遵循规则如下:

      • 同一层级中,优先处理父类,然后再处理父接口;
      • 同一个接口在不同层级出现,优先选择层级距离targetClass更近的接口;
      • 同一个接口在相同层级出现,优先选择其子类/子接口被先解析的那个;
      方法上的注解根据方法的声明类/接口被扫描的顺序排序,若注解都在同一个类的同一个方法中被声明,则还会遵循AccessibleObject.getAnnotations()的顺序
      Parameters:
      method - 方法
      Returns:
      注解对象集合
    • isAttributeMethod

      public static boolean isAttributeMethod(Method method)
      方法是否为注解属性方法 方法无参数,且有返回值的方法认为是注解属性的方法
      Parameters:
      method - 方法
      Returns:
      the boolean
    • getAttributeMethods

      public static Map<String,Method> getAttributeMethods(Class<? extends Annotation> annotationType)
      获取注解的全部属性值获取方法
      Parameters:
      annotationType - 注解
      Returns:
      注解的全部属性值
      Throws:
      IllegalArgumentException - 当别名属性在注解中不存在,或别名属性的值与原属性的值类型不一致时抛出