注解@Retention的作用

33 篇文章 1 订阅

注解@Retention可以用来修饰注解,是注解的注解,称为元注解。
Retention注解有一个属性value,是RetentionPolicy类型的,Enum RetentionPolicy是一个枚举类型,
这个枚举决定了Retention注解应该如何去保持,也可理解为Rentention 搭配 RententionPolicy使用。RetentionPolicy有3个值:CLASS  RUNTIME   SOURCE
按生命周期来划分可分为3类:
1、RetentionPolicy.SOURCE:注解只保留在源文件,当Java文件编译成class文件的时候,注解被遗弃;
2、RetentionPolicy.CLASS:注解被保留到class文件,但jvm加载class文件时候被遗弃,这是默认的生命周期;
3、RetentionPolicy.RUNTIME:注解不仅被保存到class文件中,jvm加载class文件之后,仍然存在;
这3个生命周期分别对应于:Java源文件(.java文件) ---> .class文件 ---> 内存中的字节码。
那怎么来选择合适的注解生命周期呢?
首先要明确生命周期长度 SOURCE < CLASS < RUNTIME ,所以前者能作用的地方后者一定也能作用。
一般如果需要在运行时去动态获取注解信息,那只能用 RUNTIME 注解,比如@Deprecated使用RUNTIME注解
如果要在编译时进行一些预处理操作,比如生成一些辅助代码(如 ButterKnife),就用 CLASS注解;
如果只是做一些检查性的操作,比如 @Override 和 @SuppressWarnings,使用SOURCE 注解。

注解@Override用在方法上,当我们想重写一个方法时,在方法上加@Override,当我们方法的名字出错时,编译器就会报错
注解@Deprecated,用来表示某个类或属性或方法已经过时,不想别人再用时,在属性和方法上用@Deprecated修饰
注解@SuppressWarnings用来压制程序中出来的警告,比如在没有用泛型或是方法已经过时的时候

转自:
http://blog.csdn.net/liuwenbo0920/article/details/7290586
http://blog.csdn.net/github_35180164/article/details/52118286
英文好的可以阅读以下源码:

 

 
  1. package java.lang.annotation;

  2.  
  3. /**

  4. * Annotation retention policy. The constants of this enumerated type

  5. * describe the various policies for retaining annotations. They are used

  6. * in conjunction with the {@link Retention} meta-annotation type to specify

  7. * how long annotations are to be retained.

  8. *

  9. * @author Joshua Bloch

  10. * @since 1.5

  11. */

  12. public enum RetentionPolicy {

  13. /**

  14. * Annotations are to be discarded by the compiler.

  15. */

  16. SOURCE,

  17.  
  18. /**

  19. * Annotations are to be recorded in the class file by the compiler

  20. * but need not be retained by the VM at run time. This is the default

  21. * behavior.

  22. */

  23. CLASS,

  24.  
  25. /**

  26. * Annotations are to be recorded in the class file by the compiler and

  27. * retained by the VM at run time, so they may be read reflectively.

  28. *

  29. * @see java.lang.reflect.AnnotatedElement

  30. */

  31. RUNTIME

  32. }

 

 
  1. @Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})

  2. @Retention(RetentionPolicy.SOURCE)

  3. public @interface SuppressWarnings {

  4. /**

  5. * The set of warnings that are to be suppressed by the compiler in the

  6. * annotated element. Duplicate names are permitted. The second and

  7. * successive occurrences of a name are ignored. The presence of

  8. * unrecognized warning names is <i>not</i> an error: Compilers must

  9. * ignore any warning names they do not recognize. They are, however,

  10. * free to emit a warning if an annotation contains an unrecognized

  11. * warning name.

  12. *

  13. * <p> The string {@code "unchecked"} is used to suppress

  14. * unchecked warnings. Compiler vendors should document the

  15. * additional warning names they support in conjunction with this

  16. * annotation type. They are encouraged to cooperate to ensure

  17. * that the same names work across multiple compilers.

  18. * @return the set of warnings to be suppressed

  19. */

  20. String[] value();

  21. }

 
  1. /*

  2. * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.

  3. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.

  4. *

  5. */

  6.  
  7. package java.lang;

  8.  
  9. import java.lang.annotation.*;

  10. import static java.lang.annotation.ElementType.*;

  11.  
  12. /**

  13. * A program element annotated @Deprecated is one that programmers

  14. * are discouraged from using, typically because it is dangerous,

  15. * or because a better alternative exists. Compilers warn when a

  16. * deprecated program element is used or overridden in non-deprecated code.

  17. *

  18. * @author Neal Gafter

  19. * @since 1.5

  20. * @jls 9.6.3.6 @Deprecated

  21. */

  22. @Documented

  23. @Retention(RetentionPolicy.RUNTIME)

  24. @Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE})

  25. public @interface Deprecated {

  26. }

 
  1. @Target(ElementType.METHOD)

  2. @Retention(RetentionPolicy.SOURCE)

  3. public @interface Override {

  4. }

转自:https://blog.csdn.net/u010002184/article/details/79166478

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值