Java注解之Retention与RetentionPolicy

Retention注解与RetentionPolicy配合使用,用于标注一个注解在什么阶段有效。
如果一个注解没有使用@Retention则默认使用RetentionPolicy.CLASS策略


/**
 * 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
运行时有效,并且可以通过反射获取。
posted @ 2020-08-09 11:46  预言2018  阅读(624)  评论(0)    收藏  举报