注解之Target

4 篇文章 0 订阅

Target源码

/**
 * 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}.
 * 
 * 指示注释类型适用的上下文。注释类型可适用的声明上下文和类型上下文在JLS 9.6.4.1中指定,
 * 并在源代码中由枚举常量{@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>如果{@code @Target}元注释没有出现在注释类型{@code T}上,
 * 那么{@code T}类型的注释 可以作为除类型参数声明之外的任何声明的修饰符编写 。
 * 
 * <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();
}

解析

  • @Documented - 在生成javadoc的时候就会把注解给显示出来。
  • @Retention(RetentionPolicy.RUNTIME) - VM在运行时也不丢弃该注解。
  • @Target(ElementType.ANNOTATION_TYPE) - Target作用范围为注解

ElementType源码

/**
 * The constants of this enumerated type provide a simple classification of the
 * syntactic locations where annotations may appear in a Java program. These
 * constants are used in {@link Target java.lang.annotation.Target}
 * meta-annotations to specify where it is legal to write annotations of a
 * given type.
 * 
 * 这种枚举类型的常量提供了Java程序中注释可能出现的语法位置的简单分类。
 * 这些常量在{@link Target java.lang.annotation中使用。
 * 元注释来指定在什么地方写给定类型的注释是合法的。
 * 
 * <p>The syntactic locations where annotations may appear are split into
 * <em>declaration contexts</em> , where annotations apply to declarations, and
 * <em>type contexts</em> , where annotations apply to types used in
 * declarations and expressions.
 *
 * <p>The constants {@link #ANNOTATION_TYPE} , {@link #CONSTRUCTOR} , {@link
 * #FIELD} , {@link #LOCAL_VARIABLE} , {@link #METHOD} , {@link #PACKAGE} ,
 * {@link #PARAMETER} , {@link #TYPE} , and {@link #TYPE_PARAMETER} correspond
 * to the declaration contexts in JLS 9.6.4.1.
 * 
 * 注释可能出现的句法位置被分为
 * <em>声明上下文</em>,注释适用于声明;
 * <em>类型上下文</em>,注释适用于声明和表达式中使用的类型。
 * 
 * <p>For example, an annotation whose type is meta-annotated with
 * {@code @Target(ElementType.FIELD)} may only be written as a modifier for a
 * field declaration.
 * 
 * <p>例如,类型被{@code @Target(ElementType.FIELD)}元注释的注释只能作为字段声明的修饰符编写。
 * 
 * <p>The constant {@link #TYPE_USE} corresponds to the 15 type contexts in JLS
 * 4.11, as well as to two declaration contexts: type declarations (including
 * annotation type declarations) and type parameter declarations.
 * 
 * <p>常量{@link #TYPE_USE}对应于JLS 4.11中的15个类型上下文,
 * 以及两个声明上下文:类型声明(包括注释类型声明)和类型参数声明。
 * 
 * <p>For example, an annotation whose type is meta-annotated with
 * {@code @Target(ElementType.TYPE_USE)} may be written on the type of a field
 * (or within the type of the field, if it is a nested, parameterized, or array
 * type), and may also appear as a modifier for, say, a class declaration.
 * 
 * 例如,< p >注释的类型是meta-annotated与{@code @Target (ElementType.TYPE_USE)}
 * 可能是写在一个字段的类型(或在字段的类型,如果是一个嵌套,参数化,或数组类型),
 * 也可以表现为一个修饰词,说,一个类声明。
 * 
 * <p>The {@code TYPE_USE} constant includes type declarations and type
 * parameter declarations as a convenience for designers of type checkers which
 * give semantics to annotation types. For example, if the annotation type
 * {@code NonNull} is meta-annotated with
 * {@code @Target(ElementType.TYPE_USE)}, then {@code @NonNull}
 * {@code class C {...}} could be treated by a type checker as indicating that
 * all variables of class {@code C} are non-null, while still allowing
 * variables of other classes to be non-null or not non-null based on whether
 * {@code @NonNull} appears at the variable's declaration.
 *
 * @author  Joshua Bloch
 * @since 1.5
 * @jls 9.6.4.1 @Target
 * @jls 4.1 The Kinds of Types and Values
 */
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
}

解析

  • @Target(ElementType.TYPE)——接口、类、枚举、注解
  • @Target(ElementType.FIELD)——字段、枚举的常量
  • @Target(ElementType.METHOD)——方法
  • @Target(ElementType.PARAMETER)——方法参数
  • @Target(ElementType.CONSTRUCTOR) ——构造函数
  • @Target(ElementType.LOCAL_VARIABLE)——局部变量
  • @Target(ElementType.ANNOTATION_TYPE)——注解
  • @Target(ElementType.PACKAGE)——包
  • @Target(ElementType.TYPE_PARAMETER)——类型参数
  • @Target(ElementType.TYPE_USE)——类型的使用

总结

  • 该注解在javadoc中显示出来
  • 该注解一直存在(生命周期)
  • 该注解作用在注解上
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值