@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


转载注明出处 https://blog.csdn.net/zhawabcd/article/details/78439733

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值