public enum ElementType extends Enum<ElementType>
java.lang.annotation.Target元注释中使用这些常量来指定写入给定类型的注释的合法位置。
注释可能出现的句法位置分为声明上下文 ,其中注释适用于声明,并键入上下文 ,其中注释适用于声明和表达式中使用的类型。
常量ANNOTATION_TYPE , CONSTRUCTOR , 字段 , LOCAL_VARIABLE , METHOD , PACKAGE , PARAMETER , TYPE和TYPE_PARAMETER对应于JLS 9.6.4.1声明上下文。
例如,其类型用@Target(ElementType.FIELD)进行元注释的@Target(ElementType.FIELD)只能作为字段声明的修饰符写入。
常数TYPE_USE对应于JLS 4.11中的15个类型上下文,以及两个声明上下文:类型声明(包括注释类型声明)和类型参数声明。
例如,其类型用@Target(ElementType.TYPE_USE)进行元注释的@Target(ElementType.TYPE_USE)可以写入字段的类型(或者在字段的类型中,如果它是嵌套的,参数化的或数组类型的),并且也可以显示为修饰语,说,一个类声明。
TYPE_USE常数包括类型声明和类型参数声明,以便为给注释类型赋予语义的类型检查器的设计者方便。 例如,如果在注释类型NonNull是间注解为@Target(ElementType.TYPE_USE) ,然后@NonNull class C {...}可以由类型检查器被视为指示类的所有变量C非空,同时仍然允许其他类的变量是非空或者不是基于@NonNull是否出现在变量的声明中是非空的。
| Enum Constant and Description |
|---|
ANNOTATION_TYPE
注解类型声明
|
CONSTRUCTOR
构造函数声明
|
字段
字段声明(包括枚举常数)
|
LOCAL_VARIABLE
局部变量声明
|
METHOD
方法声明
|
PACKAGE
包装声明
|
PARAMETER
正式参数声明
|
TYPE
类,接口(包括注释类型)或枚举声明
|
TYPE_PARAMETER
键入参数声明
|
TYPE_USE
使用类型
|
| Modifier and Type | Method and Description |
|---|---|
static ElementType |
valueOf(String name)
以指定的名称返回此类型的枚举常量。
|
static ElementType[] |
values()
按照它们声明的顺序返回一个包含此枚举类型常量的数组。
|
public static final ElementType TYPE
public static final ElementType FIELD
public static final ElementType METHOD
public static final ElementType PARAMETER
public static final ElementType CONSTRUCTOR
public static final ElementType LOCAL_VARIABLE
public static final ElementType ANNOTATION_TYPE
public static final ElementType PACKAGE
public static final ElementType TYPE_PARAMETER
public static final ElementType TYPE_USE
public static ElementType[] values()
for (ElementType c : ElementType.values())
System.out.println(c);
public static ElementType valueOf(String name)
name - 要返回的枚举常量的名称。
IllegalArgumentException - 如果此枚举类型没有指定名称的常量
NullPointerException - 如果参数为空
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.