`

告知编译程序如何处理@Retention

阅读更多

告知编译程序如何处理@Retention:

java.lang.annotation.Retention型态可以在您定义Annotation型态时,指示编译程序该如何对待您的自定义Annotation型态。

预定义上编译程序会将Annotation信息留在.class文档中,但不被虚拟机读取,而仅用于编译程序或工具程序运行时提供信息。

java.lang.annotation.RetentionPolicy 有三个枚举类型:CLASS、RUNTIME、SOURCE

只有当Annotation被指示成RUNTIME时,在运行时通过反射机制才能被JVM读取,否则,JVM是读取不到这个Annotation的。

 

java 代码
  1. package com.test;   
  2.   
  3. import java.lang.annotation.Retention;   
  4. import java.lang.annotation.RetentionPolicy;   
  5.   
  6. @Retention(RetentionPolicy.RUNTIME)   
  7. public @interface RetentionTest {   
  8.     String hello() default "hello";   
  9.     String world();   
  10. }   

 

java 代码
  1. package com.test;   
  2.   
  3. public class MyTest {   
  4.     @RetentionTest(hello = "beijing", world = "shanghai")   
  5.     @Deprecated  
  6.     @SuppressWarnings("unchecked")   
  7.     public void output()   
  8.     {   
  9.         System.out.println("output");   
  10.     }   
  11. }   

 

java 代码
  1. package com.test;   
  2.   
  3. import java.lang.annotation.Annotation;   
  4. import java.lang.reflect.Method;   
  5.   
  6. public class ReflectRetentionTest {   
  7.   
  8.     public static void main(String[] args) throws Exception{   
  9.         MyTest mt = new MyTest();   
  10.         Class c = MyTest.class;   
  11.         Method method = c.getMethod("output",new Class[]{});   
  12.         if(method.isAnnotationPresent(RetentionTest.class))   
  13.         {   
  14.             method.invoke(mt, new Object[]{});//output   
  15.                
  16.             RetentionTest  retentionTest = method.getAnnotation(RetentionTest.class);   
  17.                
  18.             System.out.println(retentionTest.hello());//beijing   
  19.             System.out.println(retentionTest.world());//shanghai   
  20.         }   
  21.            
  22.         Annotation[] annotations = method.getAnnotations();   
  23.         for(Annotation annotation: annotations)   
  24.         {   
  25.             System.out.println(annotation.annotationType().getName());   
  26.         }   
  27.         //for循环里输出的结果是com.test.RetentionTest以及java.lang.Deprecated,而没有出来java.lang.SuppressWarnings   
  28.         //因为java.lang.SuppressWarnings的Retention是被设置成RetentionPolicy.SOURCE类型的,所以在运行时是不会被虚拟机读取的。   
  29.     }   
  30. }  
分享到:
评论

相关推荐

    注解的使用 注释文档的生成

    @Retention(RetentionPolicy.RUNTIME ) SOURCE 给编译器看的# 源码存在,字节码不存在 CLASS 给虚拟机的类加载器看的,#源码,.class存在, RUNTIME 用于反射 #源码,.class 字节码 存在 @Documented 这个注解可以...

    Java注解之Retention、Documented、Inherited介绍

    主要介绍了Java注解之Retention、Documented、Inherited注解介绍,本文内容和相关文章是系列文章,需要的朋友可以参考下

    SRAM retention test

    SRAM retention testing

    观看韩顺平学习整理java的笔记到异常

    帮助大家复习java基础知识其中有 hashCode 2 toString 2 finalize 2 用已学知识做出简单的房屋出租系统 3 类方法使用注意事项和细节讨论 4 main()方法 4 代码块 4 代码块使用注意事项和细节 5 ...异常处理 2

    springlearning:Spring学习

    前言 Spring 框架学习,完全弃用 XML 配置。 ch1 ch1-1 使用 Spring 注解声明实体类,相关注解为 @controller 、@servies、@respository、@...@Retention(RetentionPolicy.CLASS) 默认的保留策略,注解在 class 字节

    达梦修改UNDO_RETENTION.zip

    达梦修改UNDO_RETENTION.zip

    SpringBoot框架开发常用注解

    推荐新手java工程师+SpringBoot框架开发中常用注解,SpringBoot入门级必读 @EnableScheduling @EnableTransactionManagement @Configuration ... @Retention @Target @interface @componen @Resource

    31.1、自定义注解1

    注解可以包含与其绑定的元注解,元注解为注解提供信息,有四种元注解类型:包括@Retention @Target @Document @Inherited2、@t

    java7源码-AnnotationDemo:Android/Java编译时注解处理Demo。用于自动生成工厂代码

    java7 源码 写在前面: 越来越多的Android框架都使用了注解来实现,如有名ButterKnife、Dagger2都是用编译时注解来...该注解用于编译时使用,生命周期由@Retention指定,@Taget表示该注解的使用范围,这里用于注解类、

    Data Retention Time and Electrical Characteristics of Cell Transistor

    Data Retention Time and Electrical Characteristics of Cell Transistor According to STI Materials in 90 nm DRAM

    Java版水果管理系统源码-huihe_2020summer:2020假期spring学习,vue留给你们了,有兴趣自己可以看官方文档,中文很

    @Retention - 标识这个注解怎么保存,是只在代码中,还是编入class文件中,或者是在运行时可以通过反射访问。 @Documented - 标记这些注解是否包含在用户文档中。 @Target - 标记这个注解应该是哪种 Java 成员。 @...

    源码分析1

    2.2RetentionPolicy类RetentionPolicy枚举类型中的常量与@Retention注解搭配使用,用于指定其他注解的保留时间,即保留策略:

    java元注解.docx

    @Retention:这个元注解用于指定被注解的注解的保留策略。它有一个RetentionPolicy枚举类型的属性value,可以取以下三个值: RetentionPolicy.SOURCE:注解仅保留在源代码中,编译后会被丢弃。 RetentionPolicy....

    Java中三种标准注解和四种元注解.pdf

     2.@Retention,  3.@Documented,  4.@Inherited  这些类型和它们所⽀持的类在java.lang.annotation包中可以找到。下⾯我们看⼀下每个元注解的作⽤和相应分参数的使⽤说明。  @Target:  @Target说明了...

    A Decentralized Public Key Infrastructure with Identity Retention

    作者:Conner Fromknecht (conner@mit.edu), Dragos Velicanu (velicanu@mit.edu), Sophia Yakoubov (sonka89@mit.edu)

    JPA 和 注释文档

    @Retention(RetentionPolicy.RUNTIME) public @interface MyAnnocation{ String value() default “默认值”; String [] name(); } 引用: @MyAnnocation(value="",name={"李白","杜甫"}) 系统注释: 过时修饰: ...

    编译时注解 AbstractProcessor (Activity路由Demo)

    概述 前一篇文章已经整理过注解的一些概念,也是附上了运行时注解的Demo,如果对注解概念不是很熟的读者建议先看下前一篇文章:android 注解入门(Acitivity路由demo) 此篇文章主要讲一下编译时注解的...@Retention

    android 注解入门(Acitivity路由demo)

    @Retention @Documented @Inherited @Target 用于描述注解的使用范围,可能的ElementType参数如下: CONSTRUCTOR:用于描述构造器 FIELD:用于描述域 LOCAL_VARIABLE:用于描述局部变量 METHOD:用于描述方法 PACKAGE:...

    利用spring如何实现接口限流

    利用spring如何实现接口限流 ...@Retention(RetentionPolicy.RUNTIME) public @interface AccessLimit { //限流唯一标识 String key() default ""; //限流时间 int time(); //限流次数 int count(); }

    JavaSE-注解与反射(框架底层实现机制)

    @Retention:描述注解的生命周期,传入value参数指定 (runtime>class>sources) @Documented:是否生成注解在Javadoc种 @Inherited:子类可以继承父类的注解 自定义注解 @interface 注解名{} 属性为注解的参数:...

Global site tag (gtag.js) - Google Analytics