天天看点

Java 注解、反射、自定义注解、使用反射获得自定义注解信息

1、注解

什么是注解

​ 注解是一种能被添加到java代码中的元数据,类、方法、变量、参数和包都可以用注解来修饰。注解对于它所修饰的代码并没有直接的影响。

通过官方描述得出以下结论:

  1. 注解是一种元数据形式。即注解是属于java的一种数据类型,和类、接口、数组、枚举类似。
  2. 注解用来修饰,类、方法、变量、参数、包。
  3. 注解不会对所修饰的代码产生直接的影响。
注解的分类:内置注解、元注解、自定义注解

内置注解:重写、废弃、镇压警告等。

元注解:元注解的作用就是负责解释其他的注解。

1、@target:用来描述当前注解的作用范围

2、@document :用来产生当前注解的文档信息

3、@inherited:表明子类可以继承父类的该注解

4、@retention:表明需要在什么级别保存该注解,用于描述注解的生命周期

如何使用注解

​ 可以在程序代码中的关键节点(类、方法、变量、参数、包)上打上这些标记,然后程序在编译时或运行时可以检测到这些标记从而执行一些特殊操作。因此可以得出自定义注解使用的基本流程:

第一步,定义注解——相当于定义标记;

第二步,配置注解——把标记打在需要用到的程序代码中;

第三步,解析注解——在编译期或运行时检测到标记,并进行特殊操作。

2、自定义注解

基本语法

1、使用关键字 @interface

2、格式:

public @interface{ }

3、可以带有参数, 只有一个参数的时候, 建议名为 : value

自定义注解
// 标记运行时候的注解
@Retention(value = RetentionPolicy.RUNTIME)
// 标记产生文档
@Documented
// 标记作用的范围
@Target({ElementType.METHOD, ElementType.TYPE, ElementType.FIELD})

@interface my1 {
    // 使用 value的时候, 注解的参数可以直接写
    String value() default "value";

    int age();

    // 可以设置默认值
    int id() default 1;

    // 数组类型
    String[] school() default {"洛阳师范学院", "北大"};
}

           

3、使用反射来获取注解的信息

测试类
@my1(age = 1, id = 15)
public class TestAnnotation {
    @my1(age = 2)
    public int age;

    @my1(age = 2, id = 8)
    public int id;

    @SuppressWarnings("all")
    @my1(value = "1", age = 2, id = 9)
    public void testmethod() {
        System.out.println(id + age);
    }
  
    public static void main(String[] args)  {
            
    }
}
           
测试获得字段上的注解信息
public static void main(String[] args)  {
        TestAnnotation test = new TestAnnotation();
        Class<? extends TestAnnotation> aClass = test.getClass();
        System.out.println("得到字段上的注解  =========================");
        //得到字段上的注解  =========================

        Field age = aClass.getField("id");
        System.out.println(age);

        my1 annotation2 = age.getAnnotation(my1.class);
        System.out.println(annotation2.id());
    }

           
Java 注解、反射、自定义注解、使用反射获得自定义注解信息
测试获得方法上的注解信息
public static void main(String[] args)  {
        TestAnnotation test = new TestAnnotation();
        Class<? extends TestAnnotation> aClass = test.getClass();
        System.out.println("得到 方法上的注解  =========================");
        // 得到 方法上的注解  =========================
        Method testmethod = aClass.getMethod("testmethod");
        System.out.println(testmethod);

        my1 annotation1 = testmethod.getAnnotation(my1.class);
        System.out.println(annotation1.school()[0]);

        System.out.println(annotation1.value());

        System.out.println(annotation1.id());
    }
           
Java 注解、反射、自定义注解、使用反射获得自定义注解信息
测试获得类上的注解信息
public static void main(String[] args)  {
        TestAnnotation test = new TestAnnotation();
        Class<? extends TestAnnotation> aClass = test.getClass();

        System.out.println("得到类上的注解  =========================");
        // 得到类上的注解  =========================

        my1 annotation = aClass.getAnnotation(my1.class);

        String value = annotation.value();
        String[] school = annotation.school();

        System.out.println(school[0]);
        System.out.println(annotation.value());
    }
           
Java 注解、反射、自定义注解、使用反射获得自定义注解信息

4、完整程序

@SuppressWarnings("all")
@my1(age = 1, id = 15)
public class TestAnnotation {
    @my1(age = 2)
    public int age;

    @my1(age = 2, id = 8)
    public int id;

    @SuppressWarnings("all")
    @my1(value = "1", age = 2, id = 9)
    public void testmethod() {
        System.out.println(id + age);
    }

    public static void main(String[] args) throws NoSuchMethodException, NoSuchFieldException {
        /*   new TestAnnotation().testmethod();*/
        TestAnnotation test = new TestAnnotation();
        Class<? extends TestAnnotation> aClass = test.getClass();
        System.out.println("得到字段上的注解  =========================");
        //得到字段上的注解  =========================

        Field age = aClass.getField("id");
        System.out.println(age);

        my1 annotation2 = age.getAnnotation(my1.class);
        System.out.println(annotation2.id());


        System.out.println("得到 方法上的注解  =========================");
        // 得到 方法上的注解  =========================
        Method testmethod = aClass.getMethod("testmethod");
        System.out.println(testmethod);

        my1 annotation1 = testmethod.getAnnotation(my1.class);
        System.out.println(annotation1.school()[0]);

        System.out.println(annotation1.value());

        System.out.println(annotation1.id());
        System.out.println("得到类上的注解  =========================");
        // 得到类上的注解  =========================

        my1 annotation = aClass.getAnnotation(my1.class);

        String value = annotation.value();
        String[] school = annotation.school();

        System.out.println(school[0]);
        System.out.println(annotation.value());
    }
}

/**
 * @author Administrator
 */

// 标记运行时候的注解
@Retention(value = RetentionPolicy.RUNTIME)
// 标记产生文档
@Documented
// 标记作用的范围
@Target({ElementType.METHOD, ElementType.TYPE, ElementType.FIELD})
@interface my1 {
    // 使用 value的时候, 注解的参数可以直接写
    String value() default "value";

    int age();

    // 可以设置默认值
    int id() default 1;

    // 数组类型
    String[] school() default {"洛阳师范学院", "北大"};
}

           

5、反射的更多叙述

反射详解