0
点赞
收藏
分享

微信扫一扫

Java Reflect - 反射读取元素上的注解

菜菜捞捞 2022-04-29 阅读 56
javajava-ee

当开发者使用Annotation修饰了类、方法、Field等成员之后,这些Annotation不会自己生效,必须由开发者提供相应的工具来提取并处理Annotation信息。

Java 5在java.lang.reflect包下新增了AnnotatedElement接口,该接口代表程序中可以接受注释的程序元素。
在这里插入图片描述
该接口主要有如下几个实现类:

  • Class:类定义。

  • Constructor:构造器定义。

  • Field:类的成员变量定义。

  • Method:类的方法定义。

  • Package:类的包定义。

java.lang.reflect包下主要包含一些实现反射功能的工具类,从Java 5开始,java.lang.reflect包所提供的反射API扩充了读取运行时Annotation的能力。当一个Annotation类型被定义为运行时Annotation后,该Annotation才会在运行时可见,JVM才会在装载*.class文件时读取保存在class文件中的Annotation。

AnnotatedElement接口表示目前正在此 JVM 中运行的程序的一个已注释元素,该接口允许反射性地读取注释。它是所有元素(如Class、Method、Constructor等)的父接口,所以程序通过反射获取了某个类的AnnotatedElement对象(如Class、Method、Constructor等)之后,程序就可以调用该对象的如下方法来访问Annotation信息:

  • getAnnotation(Class<T>annotationClass):返回该程序元素上存在的指定类型的注释,如果该类型的注释不存在,则返回null。
  • Annotation[] getAnnotations():返回此元素上存在的所有注释。(如果此元素没有注释,则返回长度为零的数组。)
  • boolean isAnnotationPresent(Class<?extendsAnnotation>annotationClass):判断该程序元素上是否存在指定类型的注释,如果存在则返回true,否则返回false。
  • Annotation[] getDeclaredAnnotations():返回直接存在于此元素上的所有注释。与此接口中的其他方法不同,该方法将忽略继承的注释。

为了获得程序中的程序元素(如Class、Method等),必须使用反射知识。

// 必须是RetentionPolicy.RUNTIME,才可以通过反射读取元素上的注解
@Retention(value = RetentionPolicy.RUNTIME)
// 注解可以加载元素Method上
@Target(value = {ElementType.METHOD})
public @interface MyAnnotation {
    public int age();
    // 为name变量指定初始值
    public String name() default "张三";
}
public class Main {
    public static void main(String[] args) throws NoSuchMethodException, ClassNotFoundException {
        // 得到Class类对象
        Class<?> clazz = Class.forName("com.example.redislock.annotation.Test");

        // 获取当前类的show方法,得到Method对象,该元素实现了`AnnotatedElement`接口
        Method method = clazz.getMethod("show");

        // 获取该方法上加的所有注解
        Annotation[] annotations = method.getAnnotations();
        for (Annotation annotation : annotations) {
            // 如果注解类型为MyAnnotation
            if(annotation instanceof MyAnnotation){
                // 强制装换为MyAnnotation
                MyAnnotation myAnnotation = (MyAnnotation) annotation;
                int age = myAnnotation.age();
                String name = myAnnotation.name();
            }
        }
    }
}
举报

相关推荐

0 条评论