Java Reflect - 利用反射获取类上的注解

文章目录

        1. AnnotatedElement接口
        1. Class 类实现了AnnotatedElement接口
        1. 获取类上的注解

1. AnnotatedElement接口

AnnotatedElement接口表示目前正在此 JVM 中运行的程序的一个已注释元素,该接口允许反射性地读取注释。

该接口主要有如下几个实现类:

  • Class:类定义
  • Constructor:构造器定义
  • Field:类的成员变量定义
  • Method:类的方法定义
  • Package:类的包定义

调用AnnotatedElement对象的如下方法可以访问Annotation信息:

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

2. Class 类实现了AnnotatedElement接口

Class类实现了AnnotatedElement接口,因此可以通过反射获取加载该类上的注解。
在这里插入图片描述

  1. public final class Class<T> implements java.io.Serializable,
  2. GenericDeclaration,
  3. Type,
  4. AnnotatedElement {
  5. }

1. getAnnotation(Class annotationClass)

如果存在该元素的指定类型的注释,则返回这些注释,否则返回 null。

  1. public <A extends Annotation> A getAnnotation(Class<A> annotationClass)

2. isAnnotationPresent(Class<? extends Annotation> annotationClass)

如果指定类型的注释存在于此元素上,则返回 true,否则返回 false。

  1. public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass)

3. getAnnotations()

返回此元素上存在的所有注释。(如果此元素没有注释,则返回长度为零的数组。)

  1. public Annotation[] getAnnotations()

4. getDeclaredAnnotations()

返回直接存在于此元素上的所有注释。与此接口中的其他方法不同,该方法将忽略继承的注释。

  1. public Annotation[] getDeclaredAnnotations()

3. 获取类上的注解

① 自定义注解

  1. @Retention(value = RetentionPolicy.RUNTIME)
  2. @Target(value = {
  3. ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD,ElementType.TYPE})
  4. public @interface MyAnnotation {
  5. public int age();
  6. // 为name变量指定初始值
  7. public String name() default "张三";
  8. }

② 给Test类上加3个注解:@ControllerAdvice、@Controller、@MyAnnotation(name = “李四”,age=12)

  1. @ControllerAdvice
  2. @Controller
  3. @MyAnnotation(name = "李四",age=12)
  4. public class Test {
  5. public void show(){
  6. System.out.println("111");
  7. }
  8. }

③ 获取Test类上的指定类型的注解

  1. public class Main {
  2. public static void main(String[] args) throws NoSuchMethodException, ClassNotFoundException {
  3. // 得到Class类对象
  4. Class<?> clazz = Class.forName("com.example.redislock.annotation.Test");
  5. // 获取Test类上指定类型的注解
  6. MyAnnotation annotation = clazz.getAnnotation(MyAnnotation.class);
  7. int age = annotation.age();
  8. String name = annotation.name();
  9. System.out.println("name="+name+" age="+age);
  10. }
  11. }

在这里插入图片描述
④ 获取Test类上的所有注解

  1. public class Main {
  2. public static void main(String[] args) throws NoSuchMethodException, ClassNotFoundException {
  3. // 得到Class类对象
  4. Class<?> clazz = Class.forName("com.example.redislock.annotation.Test");
  5. // 获取Test类上所有的注解
  6. Annotation[] annotations = clazz.getAnnotations();
  7. for (Annotation annotation : annotations) {
  8. if(annotation instanceof MyAnnotation){
  9. MyAnnotation myAnnotation =(MyAnnotation) annotation;
  10. int age = myAnnotation.age();
  11. String name = myAnnotation.name();
  12. System.out.println("name="+ name +" age="+age);
  13. }
  14. }
  15. }
  16. }

在这里插入图片描述

  1. public class Main {
  2. public static void main(String[] args) throws NoSuchMethodException, ClassNotFoundException {
  3. // 得到Class类对象
  4. Class<?> clazz = Class.forName("com.example.redislock.annotation.Test");
  5. // 获取Test类上所有的注解
  6. Annotation[] annotations = clazz.getAnnotations();
  7. for (Annotation annotation : annotations) {
  8. // Test类上的所有注解
  9. Class<? extends Annotation> annotationType = annotation.annotationType();
  10. System.out.println(annotationType);
  11. }
  12. }
  13. }

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,96人围观)

还没有评论,来说两句吧...

相关阅读