注解@Retention的作用
2018年01月25日 20:48:38 二十六画生的博客 阅读数:4011
注解@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
英文好的可以阅读以下源码:
package java.lang.annotation;
/**
-
Annotation retention policy. The constants of this enumerated type
-
describe the various policies for retaining annotations. They are used
-
in conjunction with the {@link Retention} meta-annotation type to specify
-
how long annotations are to be retained.
-
@author Joshua Bloch
-
@since 1.5
/
public enum RetentionPolicy {
/*- Annotations are to be discarded by the compiler.
*/
SOURCE,
/**
- Annotations are to be recorded in the class file by the compiler
- but need not be retained by the VM at run time. This is the default
- behavior.
*/
CLASS,
/**
- Annotations are to be recorded in the class file by the compiler and
- retained by the VM at run time, so they may be read reflectively.
- @see java.lang.reflect.AnnotatedElement
/
RUNTIME
}
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.SOURCE)
public @interface SuppressWarnings {
/* - The set of warnings that are to be suppressed by the compiler in the
- annotated element. Duplicate names are permitted. The second and
- successive occurrences of a name are ignored. The presence of
- unrecognized warning names is not an error: Compilers must
- ignore any warning names they do not recognize. They are, however,
- free to emit a warning if an annotation contains an unrecognized
- warning name.
-
The string {@code "unchecked"} is used to suppress
- unchecked warnings. Compiler vendors should document the
- additional warning names they support in conjunction with this
- annotation type. They are encouraged to cooperate to ensure
- that the same names work across multiple compilers.
- @return the set of warnings to be suppressed
/
String[] value();
}
/
- Annotations are to be discarded by the compiler.
-
Copyright © 2003, 2013, Oracle and/or its affiliates. All rights reserved.
-
ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package java.lang;
import java.lang.annotation.;
import static java.lang.annotation.ElementType.;
/**
- A program element annotated @Deprecated is one that programmers
- are discouraged from using, typically because it is dangerous,
- or because a better alternative exists. Compilers warn when a
- deprecated program element is used or overridden in non-deprecated code.
- @author Neal Gafter
- @since 1.5
- @jls 9.6.3.6 @Deprecated
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE})
public @interface Deprecated {
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {