JavaAPI之注释类型Target

随时随地技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)

位于java.lang.annotation包下

声明:

@Documented
@Retention(value=RUNTIME)
@Target(value=ANNOTATION_TYPE)
public @interface Target

描述:

指示注释类型所适用的程序元素的种类。如果注释类型声明中不存在 Target 元注释,则声明的类型可以用在任一程序元素上。如果存在这样的元注释,则编译器强制实施指定的使用限制。 例如,此元注释指示该声明类型是其自身,即元注释类型。它只能用在注释类型声明上:

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

此元注释指示该声明类型只可作为复杂注释类型声明中的成员类型使用。它不能直接用于注释:

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

这是一个编译时错误,它表明一个 ElementType 常量在 Target 注释中出现了不只一次。例如,以下元注释是非法的:

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

元素信息

public abstract ElementType[] value

英文原描述:

Indicates the kinds of program element to which an annotation type is applicable. If a Target meta-annotation is not present on an annotation type declaration, the declared type may be used on any program element. If such a meta-annotation is present, the compiler will enforce the specified usage restriction. For example, this meta-annotation indicates that the declared type is itself a meta-annotation type. It can only be used on annotation type declarations:

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

This 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:

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

It is a compile-time error for a single ElementType constant to appear more than once in a Target annotation. For example, the following meta-annotation is illegal:

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