jdk 1.8
- 注解API
package java.lang.annotation;
/**
* Indicates how long annotations with the annotated type are to
* be retained. If no Retention annotation is present on
* an annotation type declaration, the retention policy defaults to
* {@code RetentionPolicy.CLASS}.
* <trans>指示被@Retention注释的注解将被保留多长时间。如果注释类型上
* 没有@Retention注解,则保留策略默认为 RetentionPolicy.CLASS
*
* <p>A Retention meta-annotation has effect only if the
* meta-annotated type is used directly for annotation. It has no
* effect if the meta-annotated type is used as a member type in
* another annotation type.
* <trans>仅当@Retention元注解直接作用于注释类型时才有效,如果将其作
* 用于其他注释类型的成员类型则无效(只用于修饰注解的元注解)
*
* @author Joshua Bloch
* @since 1.5
* @jls 9.6.3.2 @Retention
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
//参数列表
/**
* Returns the retention policy.
* @return the retention policy
* <保留策略>
*/
RetentionPolicy value();
}
- 枚举保留策略
RUNTIME > CLASS > SOURCE
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.
* <trans>注释保留策略;此枚举类型的常量描述了用于保留注释的各种策略;它们与
* Retention元注释类型一起使用,以指定将保留注释多长时间。
*
* @author Joshua Bloch
* @since 1.5
*/
public enum RetentionPolicy {
/**
* Annotations are to be discarded by the compiler.
* <trans>注释将被编译器丢弃
*/
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.
* <trans>注释将被编译器记录在class文件中,但会在jvm加载class文件时丢弃,这是默认
* 行为
*/
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.
* <trans>注释将被编译器记录在class文件中,jvm加载class文件时也会保留,
* 因此可以通过反射方式读取它们
*
* @see java.lang.reflect.AnnotatedElement
*/
RUNTIME
}