Class ResolvedAnnotationMapping

java.lang.Object
org.miaixz.bus.core.annotation.resolve.ResolvedAnnotationMapping
All Implemented Interfaces:
Annotation, AnnotationMapping<Annotation>

public class ResolvedAnnotationMapping extends Object implements AnnotationMapping<Annotation>
注解映射,用于包装并增强一个普通注解对象, 包装后的可以通过getResolvedXXX获得注解对象或属性值, 可以支持属性别名与属性覆写的属性解析机制

父子注解

当实例创建时,可通过source指定当前注解的子注解,多个实例通过该引用, 可以构成一条表示父子/元注解关系的单向链表。 当sourcenull时,认为当前注解即为根注解。

属性别名

注解内的属性可以通过Alias互相关联,当解析时, 对绑定中的任意一个属性的赋值,会被同步给其他直接或者间接关联的属性。 eg: 若注解存在a <=> b <=> c的属性别名关系,则对a赋值,此时bc也会被一并赋值。

属性覆写

当实例中source不为null,即当前注解存在至少一个或者多个子注解时, 若在子注解中的同名、同类型的属性,则获取值时将优先获取子注解的值,若该属性存在别名,则别名属性也如此。 属性覆写遵循如下机制:

  • 当覆写的属性存在别名属性时,别名属性也会一并被覆写; eg: 若注解存在a <=> b <=> c的属性别名关系,则覆写a,,属性bc也会被覆写;
  • 当属性可被多个子注解覆写时,总是优先选择离根注解最近的子注解覆写该属性; eg:若从根注解a到元注解b有依赖关系a => b => c, 此时若c中存在属性可同时被ab覆写,则优先选择a
  • 当覆写属性的子注解属性也被其子注解覆写时,等同于该子注解的子注解直接覆写的当前注解的属性; eg:若从根注解a到元注解b有依赖关系a => b => c, 此时若b中存在属性被a覆写,而b中被a覆写的属性又覆写c中属性, 则等同于c中被覆写的属性直接被a覆写。
Since:
Java 17+
Author:
Kimi Liu
See Also:
  • Field Details

    • NOT_FOUND_INDEX

      protected static final int NOT_FOUND_INDEX
      不存在的属性对应的默认下标
      See Also:
  • Method Details

    • create

      public static ResolvedAnnotationMapping create(Annotation annotation, boolean resolveAnnotationAttribute)
      构建一个注解映射对象
      Parameters:
      annotation - 注解对象
      resolveAnnotationAttribute - 是否解析注解属性,为true时获得的注解皆支持属性覆盖与属性别名机制
      Returns:
      注解映射对象
    • create

      public static ResolvedAnnotationMapping create(ResolvedAnnotationMapping source, Annotation annotation, boolean resolveAnnotationAttribute)
      构建一个注解映射对象,子注解及子注解的子注解们的属性会覆写注解对象的中的同名同名同类型属性, 当一个属性被多个子注解覆写时,优先选择离根注解最接近的注解中的属性用于覆写,
      Parameters:
      source - 子注解
      annotation - 注解对象
      resolveAnnotationAttribute - 是否解析注解属性,为true时获得的注解皆支持属性覆盖与属性别名机制
      Returns:
      注解映射对象
    • isRoot

      public boolean isRoot()
      当前注解是否为根注解
      Specified by:
      isRoot in interface AnnotationMapping<Annotation>
      Returns:
      是否
    • getRoot

      public ResolvedAnnotationMapping getRoot()
      获取根注解
      Returns:
      根注解的映射对象
    • getAttributes

      public Method[] getAttributes()
      获取注解属性
      Specified by:
      getAttributes in interface AnnotationMapping<Annotation>
      Returns:
      注解属性
    • getAnnotation

      public Annotation getAnnotation()
      获取注解对象
      Specified by:
      getAnnotation in interface AnnotationMapping<Annotation>
      Returns:
      注解对象
    • isResolved

      public boolean isResolved()
      当前注解是否存在被解析的属性,当该值为false时, 通过getResolvedAttributeValue获得的值皆为注解的原始属性值, 通过getResolvedAnnotation()获得注解对象为原始的注解对象。
      Specified by:
      isResolved in interface AnnotationMapping<Annotation>
      Returns:
      是否
    • getResolvedAnnotation

      public Annotation getResolvedAnnotation()
      根据当前映射对象,通过动态代理生成一个类型与被包装注解对象一致的合成注解,该注解相对原生注解:
      • 支持同注解内通过Alias构建的别名机制;
      • 支持子注解对元注解的同名同类型属性覆盖机制;
      isResolved()false时,则该方法返回被包装的原始注解对象。
      Specified by:
      getResolvedAnnotation in interface AnnotationMapping<Annotation>
      Returns:
      所需的注解,若isResolved()false则返回的是原始的注解对象
    • hasAttribute

      public boolean hasAttribute(String attributeName, Class<?> attributeType)
      注解是否存在指定属性
      Parameters:
      attributeName - 属性名称
      attributeType - 属性类型
      Returns:
      是否
    • hasAttribute

      public boolean hasAttribute(int index)
      该属性下标是否在注解中存在对应属性
      Parameters:
      index - 属性下标
      Returns:
      是否
    • getAttributeIndex

      public int getAttributeIndex(String attributeName, Class<?> attributeType)
      获取注解属性的下标
      Parameters:
      attributeName - 属性名称
      attributeType - 属性类型
      Returns:
      属性下标
    • getAttribute

      public Method getAttribute(int index)
      根据下标获取注解属性
      Parameters:
      index - 属性下标
      Returns:
      属性对象
    • getAttributeValue

      public <R> R getAttributeValue(String attributeName, Class<R> attributeType)
      获取属性值
      Specified by:
      getAttributeValue in interface AnnotationMapping<Annotation>
      Type Parameters:
      R - 返回值类型
      Parameters:
      attributeName - 属性名称
      attributeType - 属性类型
      Returns:
      属性值
    • getAttributeValue

      public <R> R getAttributeValue(int index)
      获取属性值
      Type Parameters:
      R - 返回值类型
      Parameters:
      index - 属性下标
      Returns:
      属性值
    • getResolvedAttributeValue

      public <R> R getResolvedAttributeValue(String attributeName, Class<R> attributeType)
      获取解析后的属性值
      Specified by:
      getResolvedAttributeValue in interface AnnotationMapping<Annotation>
      Type Parameters:
      R - 返回值类型
      Parameters:
      attributeName - 属性名称
      attributeType - 属性类型
      Returns:
      属性值
    • getResolvedAttributeValue

      public <R> R getResolvedAttributeValue(int index)
      获取解析后的属性值
      Type Parameters:
      R - 返回值类型
      Parameters:
      index - 属性下标
      Returns:
      属性值
    • equals

      public boolean equals(Object o)
      比较两个实例是否相等
      Specified by:
      equals in interface Annotation
      Overrides:
      equals in class Object
      Parameters:
      o - 对象
      Returns:
      是否
    • hashCode

      public int hashCode()
      获取实例哈希值
      Specified by:
      hashCode in interface Annotation
      Overrides:
      hashCode in class Object
      Returns:
      哈希值