Class SyntheticMeta

java.lang.Object
org.aoju.bus.core.scanner.SyntheticMeta
All Implemented Interfaces:
Annotation, AnnotatedElement, Synthetic

public class SyntheticMeta extends Object implements Synthetic
表示一个根注解与根注解上的多层元注解合成的注解

假设现有注解A,A上存在元注解B,B上存在元注解C,则对A解析得到的合成注解X,则CBA都是X的元注解,X为根注解 通过isAnnotationPresent(Class)可确定指定类型是注解是否是该合成注解的元注解,即是否为当前实例的“父类” 若指定注解是当前实例的元注解,则通过getAnnotation(Class)可获得动态代理生成的对应的注解实例 需要注意的是,由于认为合并注解X以最初的根注解A作为元注解,因此getAnnotations()getDeclaredAnnotations() 都将只能获得A

若认为该合成注解X在第0层,则根注解A在第1层,B在第2层......以此类推, 则相同或不同的层级中可能会出现类型相同的注解对象,此时将通过SynthesizedSelector选择出最合适的注解对象, 该注解对象将在合成注解中作为唯一有效的元注解用于进行相关操作 默认情况下,将选择SynthesizedSelector.NEAREST_AND_OLDEST_PRIORITY选择器实例, 即层级越低的注解离根注解距离近,则该注解优先级越高,即遵循“就近原则”

合成注解中获取到的注解中可能会具有一些同名且同类型的属性, 此时将根据SynthesizedProcessor决定如何从这些注解的相同属性中获取属性值 默认情况下,将选择CacheableProcessor用于获取属性, 该处理器将选择距离根注解最近的注解中的属性用于获取属性值,getAnnotation(Class)获得的代理类实例的属性值遵循该规则 举个例子:若CBA同时存在属性y,则将X视为C,B或者A时,获得的y属性的值都与最底层元注解A的值保持一致 若两相同注解处于同一层级,则按照从其上一级“子注解”的AnnotatedElement.getAnnotations()的调用顺序排序 别名在合成注解中仍然有效,若注解X中任意属性上存在Alias注解,则Alias.value()指定的属性值将会覆盖注解属性的本身的值 Alias注解仅能指定注解X中存在的属性作为别名,不允许指定元注解或子类注解的属性

Since:
Java 17+
Author:
Kimi Liu
  • Constructor Details

    • SyntheticMeta

      public SyntheticMeta(Annotation source)
      基于指定根注解,为其层级结构中的全部注解构造一个合成注解 当层级结构中出现了相同的注解对象时,将优先选择以距离根注解最近,且优先被扫描的注解对象, 当获取值时,同样遵循该规则
      Parameters:
      source - 源注解
    • SyntheticMeta

      public SyntheticMeta(Annotation annotation, SynthesizedSelector annotationSelector, SynthesizedProcessor attributeProcessor)
      基于指定根注解,为其层级结构中的全部注解构造一个合成注解
      Parameters:
      annotation - 当前查找的注解对象
      annotationSelector - 合成注解选择器
      attributeProcessor - 注解属性处理器
  • Method Details

    • getSource

      public Annotation getSource()
      获取根注解
      Returns:
      根注解
    • getAnnotationSelector

      public SynthesizedSelector getAnnotationSelector()
      获取合成注解选择器
      Specified by:
      getAnnotationSelector in interface Synthetic
      Returns:
      合成注解选择器
    • getAttributeProcessor

      public SynthesizedProcessor getAttributeProcessor()
      获取合成注解属性处理器
      Specified by:
      getAttributeProcessor in interface Synthetic
      Returns:
      合成注解属性处理器
    • getSynthesizedAnnotation

      public Synthesized getSynthesizedAnnotation(Class<?> annotationType)
      获取已合成的注解
      Specified by:
      getSynthesizedAnnotation in interface Synthetic
      Parameters:
      annotationType - 注解类型
      Returns:
      已合成的注解
    • annotationType

      public Class<? extends Annotation> annotationType()
      获取根注解类型
      Specified by:
      annotationType in interface Annotation
      Specified by:
      annotationType in interface Synthetic
      Returns:
      注解类型
    • getAttribute

      public Object getAttribute(String attributeName, Class<?> attributeType)
      根据指定的属性名与属性类型获取对应的属性值,若存在Alias则获取Alias.value()指定的别名属性的值

      当不同层级的注解之间存在同名同类型属性时,将优先获取更接近根注解的属性

      Specified by:
      getAttribute in interface Synthetic
      Parameters:
      attributeName - 属性名
      attributeType - 属性类型
      Returns:
      属性
    • getAnnotation

      public <T extends Annotation> T getAnnotation(Class<T> annotationType)
      获取被合成的注解
      Specified by:
      getAnnotation in interface AnnotatedElement
      Specified by:
      getAnnotation in interface Synthetic
      Type Parameters:
      T - 注解类型
      Parameters:
      annotationType - 注解类型
      Returns:
      注解对象
    • isAnnotationPresent

      public boolean isAnnotationPresent(Class<? extends Annotation> annotationType)
      当前合成注解中是否存在指定元注解
      Specified by:
      isAnnotationPresent in interface AnnotatedElement
      Specified by:
      isAnnotationPresent in interface Synthetic
      Parameters:
      annotationType - 注解类型
      Returns:
      是否
    • getAnnotations

      public Annotation[] getAnnotations()
      获取全部注解
      Specified by:
      getAnnotations in interface AnnotatedElement
      Specified by:
      getAnnotations in interface Synthetic
      Returns:
      注解对象
    • syntheticAnnotation

      public <T extends Annotation> T syntheticAnnotation(Class<T> annotationType)
      若合成注解在存在指定元注解,则使用动态代理生成一个对应的注解实例
      Specified by:
      syntheticAnnotation in interface Synthetic
      Type Parameters:
      T - 注解类型
      Parameters:
      annotationType - 注解类型
      Returns:
      合成注解对象
      See Also:
      • SyntheticProxy.create(Class, Synthetic)
    • getDeclaredAnnotations

      public Annotation[] getDeclaredAnnotations()
      获取根注解直接声明的注解,该方法正常情况下当只返回原注解
      Specified by:
      getDeclaredAnnotations in interface AnnotatedElement
      Returns:
      直接声明注解