public interface AnnotatedElement
getAnnotationsByType(Class)和getDeclaredAnnotationsByType(Class)方法支持元素上相同类型的多个注释。 如果任一方法的参数是可重复的注释类型(JLS 9.6),则该方法将“查看”容器注释(JLS 9.7)(如果存在),并返回容器内的任何注释。 容器注释可以在编译时生成以包裹参数类型的多个注释。
直接呈现 , 间接存在 , 呈现和关联的术语在整个界面中被使用以精确描述通过方法返回哪些注释:
RuntimeVisibleAnnotations或RuntimeVisibleParameterAnnotations或RuntimeVisibleTypeAnnotations属性,则注释A 直接出现在元素E上,该属性包含A。 RuntimeVisibleAnnotations或RuntimeVisibleParameterAnnotations或RuntimeVisibleTypeAnnotations属性,并且A的类型是可重复的,则该注释A 间接存在于元素E上,并且该属性只包含一个注释,其值元素包含A ,其类型是包含注释类型A的类型。 下表总结了此界面中哪种注释存在不同的方法。
Overview of kind of presence detected by different AnnotatedElement methods Kind of Presence Method Directly Present Indirectly Present Present AssociatedT getAnnotation(Class<T>) X Annotation[] getAnnotations() X T[] getAnnotationsByType(Class<T>) X T getDeclaredAnnotation(Class<T>) X Annotation[] getDeclaredAnnotations() X T[] getDeclaredAnnotationsByType(Class<T>) X X
对于get[Declared]AnnotationsByType( Class < T >)的get[Declared]AnnotationsByType( Class < T >) ,直接或间接地出现在元素E上的get[Declared]AnnotationsByType( Class < T >)的顺序被计算,就好像E上的间接存在的注释直接出现在E上 ,而不是它们的容器注释,按照它们出现在容器注释的value元素。
如果注释类型T最初不可重复,并且稍后被修改为可重复,则需要记住几个兼容性问题。 T的包含注释类型是TC 。
get[Declared]Annotation(Class<T>) (称为T或TC的参数)和get[Declared]Annotations()方法的get[Declared]Annotations() ,因为由于TC成为T的包含注释类型,方法的结果将不会改变。 get[Declared]AnnotationsByType(Class<T>)方法调用T的说法,因为这些方法现在认识类型TC的注释为容器标注为T和将“透视”,它揭露类型T的注解。 get[Declared]Annotation(Class<T>)方法和get[Declared]Annotations()方法的结果,因为这些方法现在只能在元素上看到容器注释,并且不会看到类型T的注释。 get[Declared]AnnotationsByType(Class<T>)方法的结果,因为它们的结果将会暴露类型T的附加注释,而之前它们仅暴露了单个T型注释。 如果此接口中的方法返回的注释包含(直接或间接)引用此虚拟机中无法访问的类的类值的成员,则尝试通过在返回的注释上调用相关的类返回方法来读取类将导致TypeNotPresentException 。
类似地,尝试读取枚举值成员将导致EnumConstantNotPresentException如果注释中的枚举常量不再存在于枚举类型中。
如果一个注释类型T ((meta-))用一个@Repeatable注释(其值元素指示类型TC )进行注释,但是TC没有声明具有返回类型为T []的value()方法,则抛出类型为AnnotationFormatError的异常。
最后,尝试读取其定义不兼容的成员将导致AnnotationTypeMismatchException或IncompleteAnnotationException 。
| Modifier and Type | Method and Description |
|---|---|
<T extends Annotation> |
getAnnotation(类<T> annotationClass)
返回该元素的,如果这样的注释
,否则返回null指定类型的注释。
|
Annotation[] |
getAnnotations()
返回此元素上
存在的注释。
|
default <T extends Annotation> |
getAnnotationsByType(类<T> annotationClass)
返回与此元素相关
联的注释 。
|
default <T extends Annotation> |
getDeclaredAnnotation(类<T> annotationClass)
如果这样的注释
直接存在 ,则返回指定类型的元素注释,否则返回null。
|
Annotation[] |
getDeclaredAnnotations()
返回
直接存在于此元素上的注释。
|
default <T extends Annotation> |
getDeclaredAnnotationsByType(类<T> annotationClass)
如果此类注释
直接存在或
间接存在,则返回该元素的注释(指定类型)。
|
default boolean |
isAnnotationPresent(类<? extends Annotation> annotationClass)
如果此元素上
存在指定类型的注释,则返回true,否则返回false。
|
default boolean isAnnotationPresent(类<? extends Annotation> annotationClass)
此方法返回的真值等效于: getAnnotation(annotationClass) != null
默认方法的主体被指定为上述代码。
annotationClass - 对应于注释类型的Class对象
NullPointerException - 如果给定的注释类为空
<T extends Annotation> T getAnnotation(类<T> annotationClass)
T - 要查询的注释的类型,如果存在则返回
annotationClass - 对应于注释类型的Class对象
NullPointerException - 如果给定的注释类为空
Annotation[] getAnnotations()
default <T extends Annotation> T[] getAnnotationsByType(类<T> annotationClass)
getAnnotation(Class)之间的区别在于此方法检测其参数是否是可重复注释类型 (JLS 9.6),如果是,则尝试通过“查看”容器注释来查找该类型的一个或多个注释。
该方法的调用者可以自由修改返回的数组;
它将对返回给其他调用者的数组没有影响。
getDeclaredAnnotationsByType(Class)传球annotationClass作为参数。
如果返回的数组的长度大于零,则返回数组。
如果返回的数组是零长度,而这个AnnotatedElement是一个类,并且参数类型是可继承的注释类型,并且该AnnotatedElement的AnnotatedElement是非空的,则返回的结果是在超类上调用getAnnotationsByType(Class)的结果,具有annotationClass作为论证。
否则返回零长度的数组。
T - 要查询的注释的类型,如果存在则返回
annotationClass - 对应于注释类型的Class对象
NullPointerException - 如果给定的注释类为空
default <T extends Annotation> T getDeclaredAnnotation(类<T> annotationClass)
getDeclaredAnnotations()返回第一个注释,其注释类型的参数类型相匹配。
T - 查询的注释类型,如果直接出现则返回
annotationClass - 对应于注释类型的Class对象
NullPointerException - 如果给定的注释类为空
default <T extends Annotation> T[] getDeclaredAnnotationsByType(类<T> annotationClass)
getDeclaredAnnotation(Class)之间的区别在于该方法检测其参数是否是可重复注释类型 (JLS 9.6),以及如果是,尝试通过“查看”容器注释(如果存在)来查找该类型的一个或多个注释。
该方法的调用者可以自由修改返回的数组;
它将对返回给其他调用者的数组没有影响。
getDeclaredAnnotation(Class)一次或多次以找到直接呈现的注释,如果注释类型是可重复的,则可以找到容器注释。
如果注释类型的注释annotationClass被发现直接和间接本既,然后getDeclaredAnnotations()将被调用,以确定所返回的数组中的元素的顺序。
或者,默认实现可以单次调用getDeclaredAnnotations() ,并且检查返回的数组直接和间接地呈现注释。 假设调用getDeclaredAnnotations()的结果与调用getDeclaredAnnotation(Class)的结果一致 。
T - 直接或间接出现的查询和返回的注释类型
annotationClass - 对应于注释类型的Class对象
NullPointerException - 如果给定的注释类为null
Annotation[] getDeclaredAnnotations()
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.