public interface AnnotatedElement
的getAnnotationsByType(Class)
和getDeclaredAnnotationsByType(Class)
方法支持元素的多个同类型的注释。如果参数或方法是一个可重复的注释类型(年9.6),然后将“看穿”容器标注(JLS 9.7),如果存在,并返回容器内的任何注释。可以在编译时生成容器注释,以包装参数类型的多个注释。
直接存在的,间接的,现在的,和相关联的术语在这个接口中使用,精确地描述注释是通过方法返回的:
RuntimeVisibleAnnotations
或RuntimeVisibleParameterAnnotations
或RuntimeVisibleTypeAnnotations
属性,和属性包含空一。RuntimeVisibleAnnotations
或RuntimeVisibleParameterAnnotations
或RuntimeVisibleTypeAnnotations
属性,和空一的类型是可重复的,和属性包含一个注释的价值元素包含空一和其类型包含空一类型的注释类型。下面的表格总结了在这个接口检查中的注释存在不同的方法。
Kind of Presence | |||||
---|---|---|---|---|---|
方法 | Directly Present | Indirectly Present | Present | Associated | |
T |
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 >)
调用,注解,都直接或间接地存在于元努尔计算如果间接提出注释努尔直接在容器标注的地方努尔命令,在其出现的价值容器标注元素的阶。
如果注释类型nullt本来是不可重复的,后来改为可重复的有几个兼容性问题要牢记。包含nullt注释类型为空总费用。
get[Declared]Annotation(Class<T>)
(所谓的nullt或空总费用参数)和get[Declared]Annotations()
方法由于方法的结果不会因空总费用成为含nullt注释类型的变化。get[Declared]AnnotationsByType(Class<T>)
方法称为nullt争论的结果,因为这些方法现在认识型空总费用注释为nullt容器标注并将“看”到揭露型nullt注释。get[Declared]Annotations()
get[Declared]Annotation(Class<T>)
方法的结果,因为这些方法现在只看到元素容器的注释和看不nullt注释类型。get[Declared]AnnotationsByType(Class<T>)
方法的结果,因为结果会暴露型nullt附加注释而以前他们暴露的只有一个单一的注释类型nullt。如果注释的方法返回包含此接口(直接或间接)一类
-valued成员指的是在这个虚拟机是无法访问的类,试图读取类通过调用相关的类返回的方法返回的注释会导致TypeNotPresentException
。
同样,试图读取一个枚举值的成员如果在注释常数枚举不再枚举类型结果在EnumConstantNotPresentException
。
如果注释类型nullt是(元)注释的一个@Repeatable
诠释其价值元素表示一个类型的空总费用,但空总费用不有nullt[]
返回类型声明一个value()
方法,然后AnnotationFormatError
抛出一个异常类型。
最后,试图读取一个成员的定义已经不会导致AnnotationTypeMismatchException
或IncompleteAnnotationException
。
Modifier and Type | Method and Description |
---|---|
<T extends Annotation> |
getAnnotation(类<T> annotationClass)
如果存在此注释,则返回此元素的指定类型的注释,否则为空。
|
Annotation[] |
getAnnotations()
返回此元素上的注释。
|
default <T extends Annotation> |
getAnnotationsByType(类<T> annotationClass)
返回与此元素关联的注释。
|
default <T extends Annotation> |
getDeclaredAnnotation(类<T> annotationClass)
返回此元素的指定类型的注释,如果这样的注释是直接存在的,否则为空。
|
Annotation[] |
getDeclaredAnnotations()
返回直接在该元素上的注释。
|
default <T extends Annotation> |
getDeclaredAnnotationsByType(类<T> annotationClass)
如果这样的注释直接存在或间接存在,则返回指定类型的该元素的注释(s)。
|
default boolean |
isAnnotationPresent(类<? extends Annotation> annotationClass)
如果在这个元素上存在指定类型的注释,则返回真,否则为假。
|
default boolean isAnnotationPresent(类<? extends Annotation> annotationClass)
此方法返回的真实价值相当于:getAnnotation(annotationClass) != null
默认方法的主体被指定为上面的代码。
annotationClass
-对应的注释类型的类对象
NullPointerException
如果注释类是无效的
<T extends Annotation> T getAnnotation(类<T> annotationClass)
T
-查询并返回如果目前的标注类型
annotationClass
-对应的注释类型的类对象
NullPointerException
如果注释类是无效的
Annotation[] getAnnotations()
default <T extends Annotation> T[] getAnnotationsByType(类<T> annotationClass)
getAnnotation(Class)
之间的区别是,该方法检测如果它的参数是一个可重复的注释类型(年9.6),如果是这样的话,试图找到一个或更多的注释,通过“浏览”式容器标注。此方法的调用方可以自由修改返回的数组;它将对返回给其他调用方的数组没有影响。
getDeclaredAnnotationsByType(Class)
annotationClass
作为参数传递。如果返回的数组长度大于零,则返回数组。如果返回的数组长度为零,这
AnnotatedElement
一类是参数类型是一个继承性的注释类型,而这
AnnotatedElement
父类是非空的,则返回的结果是打电话
getAnnotationsByType(Class)
对
annotationClass
父类作为参数的结果。否则,返回一零个长度的数组。
T
-查询并返回如果目前的标注类型
annotationClass
-对应的注释类型的类对象
NullPointerException
如果注释类是无效的
default <T extends Annotation> T getDeclaredAnnotation(类<T> annotationClass)
getDeclaredAnnotations()
返回的注释类型的参数类型匹配第一个注释。
T
-查询,如果直接提出返回的标注类型
annotationClass
-对应的注释类型的类对象
NullPointerException
如果注释类是无效的
default <T extends Annotation> T[] getDeclaredAnnotationsByType(类<T> annotationClass)
getDeclaredAnnotation(Class)
之间的区别是,该方法检测如果它的参数是一个可重复的注释类型(年9.6),如果是这样的话,试图找到一个或更多的注释,通过“浏览”式容器注释如果存在。此方法的调用方可以自由修改返回的数组;它将对返回给其他调用方的数组没有影响。
getDeclaredAnnotation(Class)
一次或多次找到直接提出注释,如果注释类型是可重复的,找一个容器标注。如果
annotationClass
注释注释类型有直接和间接的礼物,然后
getDeclaredAnnotations()
将被确定在返回的数组中元素的顺序。
另外,默认的实现可以称getDeclaredAnnotations()
单时间和返回的数组进行直接和间接地提出注释。打电话getDeclaredAnnotations()
结果被认为是与呼叫getDeclaredAnnotation(Class)
结果一致。
T
-注释类型来查询并返回如果直接或间接存在
annotationClass
-对应的注释类型的类对象
NullPointerException
如果注释类是无效的
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.