AnnotatedElement

1、元素与注解的四种关系

  • 直接存在(directly present)

直接存在或者直接引用。当一个注解直接在元素上引用时,则为直接引用,比如下面代码,@Configuration就是直接引用在类FilterConfig

image

  • 间接存在(indirectly present)

间接存在或间接引用。当一个注解A并没有注解引用在一个类E上,但是包含这个注解A的注解B被E直接引用了,这属于E对A的间接引用。@Component就是间接引用在类FilterConfig

image

  • 存在(present)

存在或引用。有直接存在间接存在着两种情况的都属于存在类型,
1、注解A直接存在或者间接存在元素E上,则A存在元素E上
2、注解A(用@Inherited标识注解)直接引用于E类的超类F,那么E与A的关系则为引用关系,@Configuration存在类XssFilterConfig

image

  • 关联(associated)

1、直接引用关系
2、间接引用关系
3、引用关系
4、一个注解A(用@Inherited标识注解)与E类的超类F相关联,那么E与A的关系也为关联关系

2、AnnotatedElement方法跟关联关系对应表

方法名称 直接存在 间接存在 存在 关联
T getAnnotation(Class<T>)
Annotation[] getAnnotations()
T[] getAnnotationsByType(Class<T>)
T getDeclaredAnnotation(Class<T>)
Annotation[] getDeclaredAnnotations()
T[] getDeclaredAnnotationsByType(Class<T>)

3、AnnotatedElement(接口)

3.1 AnnotatedElement类图

直接一级子接口 AnnotatedType、GenericDeclaration、TypeVariable
直接实现一级实现子类 Parameter、AccessibleObject

image

  • 方法
    // 判断元素上是否存在某类型注解(存在)
    default boolean isAnnotationPresent(Class<? extends Annotation> annotationClass)
	
    // 获取元素上某类型的注解(存在)
    <T extends Annotation> T getAnnotation(Class<T> annotationClass);
    
    // 获取元素上的所有的注解,返回数组(存在)
    Annotation[] getAnnotations();
    
    // 获取元素上指定类型的注解(关联)
    default <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass)
    
    // 获取元素上指定类型的注解(直接存在)
    default <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass)
    
    // 获取元素上直接存在的注解(直接存在)
    Annotation[] getDeclaredAnnotations();

    // 获取元素指定类型的注解(直接存在、间接存在)
    default <T extends Annotation> T[] getDeclaredAnnotationsByType(Class<T> annotationClass)

Demo

import java.lang.annotation.*;

@Inherited
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@SuppressWarnings("all")
public @interface Action {
    String value();
}

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@SuppressWarnings("all")
public @interface Gender {
    String type();
}

import java.lang.annotation.*;

@Repeatable(value = Hobbys.class)
@SuppressWarnings("all")
public @interface Hobby {
    String value();
}

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@SuppressWarnings("all")
public @interface Hobbys {
    Hobby[] value();
}

@Identity(value = "造物者")
@SuppressWarnings("all")
public class Nuwa {
}

@Action(value = "行走")
@SuppressWarnings("all")
public class Human extends Nuwa{
}

@Hobby(value = "篮球")
@Hobby(value = "台球")
@Gender(type = "女")
public class People extends Human {

}

 public static void main(String[] args) {
        
        // 获取注解数组(只获取直接引用到类上的注解),
        getDeclaredAnnotations(People.class);
        
        // 获取引用关系的注解数组,@Action、@Identity注解与People为引用关系
        getAnnotations(People.class);
        
        // 通过类型获取注解,Hobbys注解与People为间接引用关系
        getAnnotationByType(People.class, Hobbys.class);
        
        // 通过类型获取注解,@Identity与People为关联关系
        getAnnotationByType(People.class, Identity.class);
        
    }
    
    /**
     * 获取callObj元素上<em>directly present</em>关系注解的数组
     *
     * @param callObj 被测试的元素
     */
    private static void getDeclaredAnnotations(Class<?> callObj) {
        System.out.println("---------------getDeclaredAnnotations----------------");
        Annotation[] annotations = callObj.getDeclaredAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println(annotation.annotationType().getName());
        }
        System.out.println();
    }
    
    /**t</em>关系注
     * 获取callObj元素上<em>presen解的数组
     *
     * @param callObj 被测试的元素
     */
    private static void getAnnotations(Class<?> callObj) {
        System.out.println("---------------getAnnotations----------------");
        Annotation[] annotations = callObj.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println(annotation.annotationType().getName());
        }
        System.out.println();
    }
    
    /**
     * 获取callObj元素上<em>associated</em>关系注解的数组
     *
     * @param callObj         被测试的元素
     * @param queryAnnotation 要被查询的注解
     */
    private static void getAnnotationByType(Class<?> callObj, Class<? extends Annotation> queryAnnotation) {
        System.out.println("---------------getAnnotationByType----------------");
        Annotation[] annotations = callObj.getAnnotationsByType(queryAnnotation);
        for (Annotation annotation : annotations) {
            System.out.println(annotation.annotationType().getName());
        }
        System.out.println();
    }

image

posted on 2023-12-29 18:21  ccblblog  阅读(152)  评论(0)    收藏  举报

导航