31注解

注解就是给计算机看的注释

Invalid type Object for the annotation attribute First.object; only primitive type, String, Class, annotation, enum are permitted or 1-dimensional arrays thereof 说明支持的属性范围是 基本数据类型,String,Class,注解,枚举,以及对应形式的一位数组表现形式。

注解可以加载类上,方法上,方法参数上,行为太多了。很容易乱,所以要用元注解来限制注解的行为

元注解就是给注解的注解

1.@Target ----控制注解可以在什么位置使用

 

public enum ElementType {
    /** 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,

    /**
     * Use of a type
     *
     * @since 1.8
     */
    TYPE_USE
}

2.@Retention 控制注解的级别,什么时候生效

public enum RetentionPolicy {
    /**
     * 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 //运行时期
}

3.@Documented---可以让注解随着类一起问单独给出去

4.@Inherited ---可以让子类依然具有注解

posted @ 2019-07-29 16:22  三十六烦恼风x  阅读(185)  评论(0)    收藏  举报