java.lang.annotation.Retention可以在您定义Annotation型态时,指示编译器如何对待您的自定义 Annotation,预设上编译器会将Annotation资讯留在class档案中,但不被虚拟机器读取,而仅用于编译器或工具程式运行时提供资讯。
在使用Retention型态时,需要提供java.lang.annotation.RetentionPolicy的列举型态:
package java.lang.annotation;
public enum RetentionPolicy {
SOURCE, //编译器处理完Annotation资讯后就没事了
CLASS, //编译器将Annotation储存于class档中,预设
RUNTIME //编译器将Annotation储存于class档中,可由VM读入
}
RetentionPolicy为SOURCE的例子是SuppressWarnings,这个资讯的作用仅在告知编译器抑制警讯,所以不必将这个资讯储存于class档案。
RetentionPolicy为RUNTIME的时机,可像是您使用Java设计一个程式码分析工具,您要VM读出Annotation资讯,以在分析程式中使用,搭配Reflection机制,就可以达到这个目的。
在J2SE 5.0中新增了java.lang.reflect.AnnotatedElement这个介面,当中定义有四个方法:
public Annotation getAnnotation(Class annotationType);
public Annotation[] getAnnotations();
public Annotation[] getDeclaredAnnotations();
public boolean isAnnotationPresent(Class annotationType);
Class、Constructor、Field、Method、Package等类别,都实作了AnnotatedElement这个介面,所以您可以从这些类别的实例上,分别取得标示于其上的Annotation与其资讯,如果RetentionPolicy为RUNTIME的话。
举个例子来说,假设您设计了以下的Debug Annotation:
* Debug.java
package onlyfun.caterpillar;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface Debug {
String value();
String name();
}
由于RetentionPolicy为RUNTIME,编译器在处理Debug Annotation时,会将之编译至class档中,并可以VM读出Annotation资讯,接着我们将Debug用于程式中:
* SomeObject.java
package onlyfun.caterpillar;
public class SomeObject {
@Debug(
value = "unit",
name = "debug1"
)
public void doSomething() {
// ....
}
}
可以设计一个工具程式来读取Annotation资讯:
* DebugTool.java
package onlyfun.caterpillar;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
public class DebugTool {
public static void main(String[] args)
throws NoSuchMethodException {
Class<SomeObject> c = SomeObject.class;
Method method = c.getMethod("doSomething");
if(method.isAnnotationPresent(Debug.class)) {
System.out.println("@Debug is found.");
Debug debug = method.getAnnotation(Debug.class);
System.out.println("\tvalue = " + debug.value());
System.out.println("\tname = " + ());
}
else {
System.out.println("@Debug is not found.");
}
Annotation[] annotations = method.getAnnotations();
for(Annotation annotation : annotations) {
System.out.println(
annotation.annotationType().getName());
}
}
}
程式的执行结果如下:
@Debug is found.
value = unit
name = debug1
onlyfun.caterpillar.Debug
分享到:
相关推荐
@Retention(RetentionPolicy.RUNTIME ) SOURCE 给编译器看的# 源码存在,字节码不存在 CLASS 给虚拟机的类加载器看的,#源码,.class存在, RUNTIME 用于反射 #源码,.class 字节码 存在 @Documented 这个注解可以...
主要介绍了Java注解之Retention、Documented、Inherited注解介绍,本文内容和相关文章是系列文章,需要的朋友可以参考下
帮助大家复习java基础知识其中有 hashCode 2 toString 2 finalize 2 用已学知识做出简单的...@Retention 注解 19 @Target 注解 19 @Documented注解 20 @Inherited 注解 20 异常-Exception 21 编译异常 22 异常处理 2
2.@Retention, 3.@Documented, 4.@Inherited 这些类型和它们所⽀持的类在java.lang.annotation包中可以找到。下⾯我们看⼀下每个元注解的作⽤和相应分参数的使⽤说明。 @Target: @Target说明了...
@Retention:这个元注解用于指定被注解的注解的保留策略。它有一个RetentionPolicy枚举类型的属性value,可以取以下三个值: RetentionPolicy.SOURCE:注解仅保留在源代码中,编译后会被丢弃。 RetentionPolicy....
前言 Spring 框架学习,完全弃用 XML 配置。 ch1 ch1-1 使用 Spring 注解声明实体类,相关注解为 @controller 、@servies、@respository、@...@Retention(RetentionPolicy.CLASS) 默认的保留策略,注解在 class 字节
@Retention(RetentionPolicy.RUNTIME) public @interface MyAnnocation{ String value() default “默认值”; String [] name(); } 引用: @MyAnnocation(value="",name={"李白","杜甫"}) 系统注释: 过时修饰: ...
推荐新手java工程师+SpringBoot框架开发中常用注解,SpringBoot入门级必读 @EnableScheduling @EnableTransactionManagement @Configuration ... @Retention @Target @interface @componen @Resource
注解可以包含与其绑定的元注解,元注解为注解提供信息,有四种元注解类型:包括@Retention @Target @Document @Inherited2、@t
2.2RetentionPolicy类RetentionPolicy枚举类型中的常量与@Retention注解搭配使用,用于指定其他注解的保留时间,即保留策略:
@Retention(RetentionPolicy.RUNTIME) @Documented public @interface InProgress { String author(); //定义属性 String limited(); } 解析注释:利用反射 1、Class.forName()...
注意到了附加在 SuppressWarnings 批注后面的陌生的批注 @Target 和 @Retention 了吗?这些称为元数据批注,它们描述了该批注在哪里适用。我将在本系列的第二篇文章中介绍它们,以及介绍如何将元数据批注应用到您...
@Retention @Documented @Inherited @Target 用于描述注解的使用范围,可能的ElementType参数如下: CONSTRUCTOR:用于描述构造器 FIELD:用于描述域 LOCAL_VARIABLE:用于描述局部变量 METHOD:用于描述方法 PACKAGE:...
java7 源码 写在前面: 越来越多的Android框架都使用了注解来实现,如有名ButterKnife、Dagger2都是用编译时注解来...该注解用于编译时使用,生命周期由@Retention指定,@Taget表示该注解的使用范围,这里用于注解类、
SRAM retention testing
@Retention:描述注解的生命周期,传入value参数指定 (runtime>class>sources) @Documented:是否生成注解在Javadoc种 @Inherited:子类可以继承父类的注解 自定义注解 @interface 注解名{} 属性为注解的参数:...
利用spring如何实现接口限流 ...@Retention(RetentionPolicy.RUNTIME) public @interface AccessLimit { //限流唯一标识 String key() default ""; //限流时间 int time(); //限流次数 int count(); }
元注解:修饰注解的注解,@Target:注解用在哪种java元素上, @Retention:注解的生命周期,@Documented),再用自定义方法参数解析器HandlerMethodArgumentResolver取出request header中的user,调用service层方法...
元注解(JDK的元Annotation)4.1.@Target4.2.@Retention4.3.@Documented4.4.@Inherited4.5.@Result四、在程序中使用(解析)注解五、案例——简单的测试框架 一、注解是什么? 从JDK5开始,Java增加对元数据的支持...
达梦修改UNDO_RETENTION.zip