打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
@Retention
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

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Java Annotation详解
java.lang和java.lang.annotation中实现Annotation的类小结
深入理解Java 注解原理
张龙 Annotation学习笔记
深入理解Java:注解(Annotation)自定义注解入门
JAVA annotation
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服