• 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
  • 相关阅读:
    C++指针与引用类型的区别
    Linux介绍和基本操作
    CMD 命令
    C语言关键字
    错误: 在类中找不到 main 方法, 请将 main 方法定义为:    public static void main(String[] args) 否则 JavaFX 应用程序类必须扩展javafx.application.Application 。
    根据补码定义,由真值求补码。
    域名直接访问项目|Tomcat http 自动跳转Https
    Python扩展库NumPy快速入门
    项目慢查询性能分析
    Springboot 配置 https
  • 原文地址:https://www.cnblogs.com/fortitude/p/10932623.html
Copyright © 2020-2023  润新知