Java中AnnotatedElement

  1. AnnotatedArrayType (被注解的数组类型)
  2. AnnotatedParameterizedType (被注解的参数化类型)
  3. AnnotatedType (被注解的类型)
  4. AnnotatedTypeVariable (被注解的类型变量)
  5. AnnotatedWildcardType (被注解的通配类型)
  6. GenericDeclaration (通用声明,用于表示声明型元素,如:类、方法、构造器等)
  7. TypeVariable<D> (类型变量)

实现类

  1. AccessibleObject(可访问对象,如:方法、构造器、属性等)
  2. Class(类,就是你用Java语言编程时每天都要写的那个东西)
  3. Constructor(构造器,类的构造方法的类型)
  4. Executable(可执行的,如构造器和方法)
  5. Field(属性,类中属性的类型)
  6. Method(方法,类中方法的类型)
  7. Package(包,你每天都在声明的包的类型)
  8. Parameter(参数,主要指方法或函数的参数,其实是这些参数的类型)

getAnnotationsByType(Class)和getDeclaredAnnotationsByType(Class)方法在一个元素上支持多个相同类型的注释。

default boolean isAnnotationPresent(Class<? extends Annotation> annotationClass)

如果指定类型的注解出现在当前元素上,则返回true,否则将返回false。这种方法主要是为了方便地访问一些已知的注解。

其默认实现如下:

default boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
    return getAnnotation(annotationClass) != null;
}

<T extends Annotation> T getAnnotation(Class<T> annotationClass)

如果在当前元素上存在参数所指定类型(annotationClass)的注解,则返回对应的注解,否则将返回null。

Annotation[] getAnnotations()

返回在这个元素上的所有注解。如果该元素没有注释,则返回值是长度为0的数组。该方法的调用者可以自由地修改返回的数组;它不会对返回给其他调用者的数组产生影响。

default <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass)

返回与该元素相关联的注解。如果没有与此元素相关联的注解,则返回值是长度为0的数组。这个方法与getAnnotation(Class)的区别在于,该方法检测其参数是否为可重复的注解类型(JLS 9.6),如果是,则尝试通过“looking through”容器注解来查找该类型的一个或多个注解。该方法的调用者可以自由地修改返回的数组;它不会对返回给其他调用者的数组产生影响。参考@Repeatable。

其默认实现如下:

default <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) {
     /*
      * Definition of associated: directly or indirectly present OR
      * neither directly nor indirectly present AND the element is
      * a Class, the annotation type is inheritable, and the
      * annotation type is associated with the superclass of the
      * element.
      */
     T[] result = getDeclaredAnnotationsByType(annotationClass);

     if (result.length == 0 && // Neither directly nor indirectly present
         this instanceof Class && // the element is a class
         AnnotationType.getInstance(annotationClass).isInherited()) { // Inheritable
         Class<?> superClass = ((Class<?>) this).getSuperclass();
         if (superClass != null) {
             // Determine if the annotation is associated with the
             // superclass
             result = superClass.getAnnotationsByType(annotationClass);
         }
     }

     return result;
 }

default <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass)

如果参数中所指定类型的注解是直接存在于当前元素上的,则返回对应的注解,否则将返回null。这个方法忽略了继承的注解。(如果没有直接在此元素上显示注释,则返回null。)

其默认实现如下:

default <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass) {
     Objects.requireNonNull(annotationClass);
     // Loop over all directly-present annotations looking for a matching one
     for (Annotation annotation : getDeclaredAnnotations()) {
         if (annotationClass.equals(annotation.annotationType())) {
             // More robust to do a dynamic cast at runtime instead
             // of compile-time only.
             return annotationClass.cast(annotation);
         }
     }
     return null;
 }

default <T extends Annotation> T[] getDeclaredAnnotationsByType(Class<T> annotationClass)

如果参数中所指定类型的注解是直接存在或间接存在于当前元素上的,则返回对应的注解。这种方法忽略了继承的注释。如果没有直接或间接地存在于此元素上的指定注解,则返回值是长度为0的数组。这个方法和getDeclaredAnnotation(Class)的区别在于,这个方法检测它的参数是否为可重复的注释类型(JLS 9.6),如果是,则尝试通过“looking through”容器注解来查找该类型的一个或多个注解。该方法的调用者可以自由地修改返回的数组;它不会对返回给其他调用者的数组产生影响。参考@Repeatable。

默认实现如下:

default <T extends Annotation> T[] getDeclaredAnnotationsByType(Class<T> annotationClass) {
    Objects.requireNonNull(annotationClass);
    return AnnotationSupport.
        getDirectlyAndIndirectlyPresent(Arrays.stream(getDeclaredAnnotations()).
                                        collect(Collectors.toMap(Annotation::annotationType,
                                                                 Function.identity(),
                                                                 ((first,second) -> first),
                                                                 LinkedHashMap::new)),
                                        annotationClass);
}

Annotation[] getDeclaredAnnotations()

返回直接出现在这个元素上的注解。这种方法忽略了继承的注解。如果在此元素上没有直接存在的注解,则返回值是长度为0的数组。该方法的调用者可以自由地修改返回的数组;它不会对返回给其他调用者的数组产生影响。



作者:JSON_NULL
链接:https://www.jianshu.com/p/953e26463fbc
来源:简书

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值