-
包括
Target
Retention
Documented 为像Javadoc这样的归档工具提供了一些提示
Inherited 只能应用于对类的注解。指明当这个注解应用于一个类的时候,能够自动被它的子类继承 -
介绍
@Retention 用于声明该注解生效的生命周期,有三个枚举值可以选择
-
RetentionPolicy.SOURCE 注释只保留在源码上面,编译成 class 的时候自动被编译器抹除
Annotations are to be discarded by the compiler. -
RetentionPolicy.CLASS 注释只保留到字节码上面,VM 加载字节码时自动抹除
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. -
RetentionPolicy.RUNTIME 注释永久保留,可以被 VM 加载时加载到内存中
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.
@Target 用于指定该注解可以声明在哪些成员上面
常见的值有
/**Class, interface (including annotation type), or enum declaration */
TYPE,
/** Field declaration (includes enum constants) */
FIELD,
/** Method declaration */
METHOD,
/** Formal parameter declaration */
PARAMETER,
/** Constructor declaration */
CONSTRUCTOR,
/** Local variable declaration */
LOCAL_VARIABLE,
/** Annotation type declaration */
ANNOTATION_TYPE,
/** Package declaration */
PACKAGE,
/**
* Type parameter declaration
*
* @since 1.8
*/
TYPE_PARAMETER
网友评论