元注解

2018-04-27  本文已影响4人  qpan
  1. RetentionPolicy.SOURCE 注释只保留在源码上面,编译成 class 的时候自动被编译器抹除
    Annotations are to be discarded by the compiler.

  2. 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.

  3. 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

上一篇 下一篇

猜你喜欢

热点阅读