zoukankan      html  css  js  c++  java
  • 元注解——java.lang.annotation.Target(1.8)

    参考资料:https://docs.oracle.com/javase/8/docs/api/java/lang/annotation/Target.html

    普通注解’只能用来注解’代码’,而’元注解’只能用来注解 ‘普通注解’。

    @Target:限制注解的元素种类。

    不加元注解@Target的情况下,注解可以修饰各种元素,比如可以修饰类,可以修饰变量,可以修饰方法等,但是如果要限制注解的元素种类,则需要加入@Target。

    源码:

    package java.lang.annotation;
    
    /**
     * Indicates the contexts in which an annotation type is applicable. The
     * declaration contexts and type contexts in which an annotation type may be
     * applicable are specified in JLS 9.6.4.1, and denoted in source code by enum
     * constants of {@link ElementType java.lang.annotation.ElementType}.
     *
     * <p>If an {@code @Target} meta-annotation is not present on an annotation type
     * {@code T} , then an annotation of type {@code T} may be written as a
     * modifier for any declaration except a type parameter declaration.
     *
     * <p>If an {@code @Target} meta-annotation is present, the compiler will enforce
     * the usage restrictions indicated by {@code ElementType}
     * enum constants, in line with JLS 9.7.4.
     *
     * <p>For example, this {@code @Target} meta-annotation indicates that the
     * declared type is itself a meta-annotation type.  It can only be used on
     * annotation type declarations:
     * <pre>
     *    &#064;Target(ElementType.ANNOTATION_TYPE)
     *    public &#064;interface MetaAnnotationType {
     *        ...
     *    }
     * </pre>
     *
     * <p>This {@code @Target} meta-annotation indicates that the declared type is
     * intended solely for use as a member type in complex annotation type
     * declarations.  It cannot be used to annotate anything directly:
     * <pre>
     *    &#064;Target({})
     *    public &#064;interface MemberType {
     *        ...
     *    }
     * </pre>
     *
     * <p>It is a compile-time error for a single {@code ElementType} constant to
     * appear more than once in an {@code @Target} annotation.  For example, the
     * following {@code @Target} meta-annotation is illegal:
     * <pre>
     *    &#064;Target({ElementType.FIELD, ElementType.METHOD, ElementType.FIELD})
     *    public &#064;interface Bogus {
     *        ...
     *    }
     * </pre>
     *
     * @since 1.5
     * @jls 9.6.4.1 @Target
     * @jls 9.7.4 Where Annotations May Appear
     */
    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.ANNOTATION_TYPE)
    public @interface Target {
        /**
         * Returns an array of the kinds of elements an annotation type
         * can be applied to.
         * @return an array of the kinds of elements an annotation type
         * can be applied to
         */
        ElementType[] value();
    }
    View Code

    指定注释类型适用的上下文。即:用于设定注解的使用范围(被描述的注解可以用在什么地方)。

    在JLS 9.6.4.1中规定了注解类型可能适用的声明上下文和类型上下文,并且在源码中用java.lang.annotation.ElementType的枚举常量表示。即:Target用ElementType来指定注解可使用范围的枚举集合。

    如果注解类型T中不存在@Target元注释,则声明的注解类型T可被用于除了类型参数声明之外的任何声明的修饰符。即,如果注释类型声明中不存在 Target 元注释,则声明的类型可以用在任一程序元素上 。

    如果存在@Target元注释,则编译器将强制执行由ElementType枚举常量指定的使用限制。

    例如:这个@Target元注释表明声明的类型本身就是一个元注释类型。它只能用于注解类型声明:

        @Target(ElementType.ANNOTATION_TYPE)
        public @interface MetaAnnotationType {
            ...
        }

    这个@Target元注释表明,声明的类型仅用作复杂注释类型声明中的成员类型。它不能用来直接注解任何东西。

        @Target({})
        public @interface MemberType {
            ...
        }

    单个ElementType常量在@Target注释中出现多次是编译时错误,例如,下面的元注释是非法的:

        @Target({ElementType.FIELD, ElementType.METHOD, ElementType.FIELD})
        public @interface Bogus {
            ...
        }

    ElementType[]:

      返回注释类型可用的各种元素数组。

       取值有:

        TYPE:类、接口(包括注解类型) 或enum声明。

        FIELD:字段声明(包括enum常量).

        METHOD:方法声明。

        PARAMTER:形式参数声明。

        CONSTRUCTOR:构造函数声明。

        LOCAL_VARIABLE:局部变量声明。

        ANNOTATION_TYPE:注解类型声明。

        PACKAGE:包声明

        TYPE_PARAMETER:参数类型声明

        TYPE_USE:使用一个类型,详见:java.lang.annotation.ElementType

  • 相关阅读:
    作业11图
    作业11
    作业10
    总结一
    物联网相关知识
    第十二次作业
    附加题4
    第十一次作业
    附加题3
    第十次作业
  • 原文地址:https://www.cnblogs.com/shea/p/7912486.html
Copyright © 2011-2022 走看看