Java中的自定义注解

元注解


要声明一个注解, 我们需要元注解, 元注解是指注解的注解,包括@Retention, @Target, @Document, @Inherited.

@Retention

注解的保留位置(枚举RetentionPolicy),RetentionPolicy可选值:

  • SOURCE 注解仅存在于源码中,在class字节码文件中不包含, 如果只是做一些检查性的操作,比如 @Override 和 @SuppressWarnings,则可选用 SOURCE 注解。
  • CLASS 默认的保留策略,注解在class字节码文件中存在,但运行时无法获得, 如果要在编译时进行一些预处理操作,比如生成一些辅助代码(如 ButterKnife),就用 CLASS注解
  • RUNTIME 注解在class字节码文件中存在,在运行时可以通过反射获取到, 如果需要在运行时去动态获取注解信息,那只能用 RUNTIME 注解

@Inherited

说明子类可以继承父类中的该注解

@Documented

声明注解能够被javadoc等识别

@Target

用来声明注解范围(枚举ElementType),ElementType可选值:

  • TYPE 接口、类、枚举、注解
  • FIELD 字段、枚举的常量
  • METHOD 方法
  • PARAMETER 方法参数
  • CONSTRUCTOR 构造函数
  • LOCAL_VARIABLE 局部变量
  • ANNOTATION_TYPE 注解
  • PACKAGE

Demo


1.声明注解类型

@Target(value = ElementType.METHOD) //声明该注解的运行目标: 方法
@Retention(value = RetentionPolicy.RUNTIME) //该注解的生命周期: 运行时
public @interface CanRun { // 通过@interface表示注解类型
    String str() default "wow"; // 注解中携带的元数据
}

2.使用自定义注解

public class AnnotationRunner {

    public void method1() {
        System.out.println("method 1");
    }

    @CanRun(str = "foobar") // 方法2添加了自定义注解的标签同时传入str值
    public void method2() {
        System.out.println("method 2");
    }

    public void method3() {
        System.out.println("method 3");
    }

}

3.测试自定义注解

public class AnnotationTest {

    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
        AnnotationRunner runner = new AnnotationRunner();
        Method[] methods = runner.getClass().getMethods();


        for (Method method : methods){
            CanRun annos = method.getAnnotation(CanRun.class);
            //System.out.println("1");

            if (annos != null){
                method.invoke(runner);
                System.out.println(annos.str());
            }
        }
    }
}

运行结果:

method 2
foobar
posted @ 2019-05-27 19:33  Yusuzhan  阅读(1305)  评论(0编辑  收藏  举报
Fork me on GitHub