【java】 @Retention注解源码

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
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mitays

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值