AnnotatedElement接口

directly present的定义:直接标注在类上的注解
indirectly present的定义:如果一个类E上有一个注解容器,而该注解容器中包含有A类型的注解,那么A注解就是indirectly出现在E上
present的定义:如果注解A直接出现在类E上,或者类E上没有直接标注A,但是E的父类上标注有A,并且注解A是可继承的,那么A就present在E上
associated的定义:A直接或间接出现在E上,以及E的父类与A关联(associated),并且A是可继承的,那么A便与E关联(associated)

Overview of kind of presence detected by different AnnotatedElement methods
MethodKind of Presence
Return TypeSignatureDirectly PresentIndirectly PresentPresentAssociated
{@code T}{@link #getAnnotation(Class) getAnnotation(Class<T>)}
{@code Annotation[]}{@link #getAnnotations getAnnotations()}
{@code T[]}{@link #getAnnotationsByType(Class) getAnnotationsByType(Class<T>)}
{@code T}{@link #getDeclaredAnnotation(Class) getDeclaredAnnotation(Class<T>)}
{@code Annotation[]}{@link #getDeclaredAnnotations getDeclaredAnnotations()}
{@code T[]}{@link #getDeclaredAnnotationsByType(Class) getDeclaredAnnotationsByType(Class<T>)}
public interface AnnotatedElement {
    /**
     * 判断该类上是present指定的注解,即该类或其父类上是否直接出现指定的父类
     */
    default boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
        return getAnnotation(annotationClass) != null;
    }

   /**
     *获取present在该类上的具有指定类型的注解对象,不存在则返回null
     */
    <T extends Annotation> T getAnnotation(Class<T> annotationClass);

    /**
     * 获取present在该类上所有的注解对象,没有则返回长度为0的数组
     */
    Annotation[] getAnnotations();

    /**
     *获取所有相关的(associated)注解元素
     */
    default <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) {
    	//
         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;
     }

    /**
     * 获取directly出现在在该类上的注解
     */
    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;
     }

    /**
     *获取directly或indirectly出现在该类上的注解
     */
    default <T extends Annotation> T[] getDeclaredAnnotationsByType(Class<T> annotationClass) {
        Objects.requireNonNull(annotationClass);
        return AnnotationSupport.
            getDirectlyAndIndirectlyPresent(Arrays.stream(getDeclaredAnnotations()).                               	   collect(Collectors.toMap(Annotation::annotationType, unction.identity(),
((first,second) -> first),LinkedHashMap::new)),annotationClass);
}

    /**
     * 获取所有直接标注在当前类上的注解(directly present)
     */
    Annotation[] getDeclaredAnnotations();
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值