- All Known Subinterfaces:
-
ArrayType
,DeclaredType
,Element
,ErrorType
,ExecutableElement
,ExecutableType
,IntersectionType
,ModuleElement
,NoType
,NullType
,PackageElement
,Parameterizable
,PrimitiveType
,QualifiedNameable
,RecordComponentElement
,ReferenceType
,TypeElement
,TypeMirror
,TypeParameterElement
,TypeVariable
,UnionType
,VariableElement
,WildcardType
In the definitions below, an annotation A has an annotation interface AI. If AI is a repeatable annotation interface, the type of the containing annotation is AIC.
Annotation A is directly present on a construct C if either:
- A is explicitly or implicitly declared as applying to the source code representation of C.
Typically, if exactly one annotation of type AI appears in the source code of representation of C, then A is explicitly declared as applying to C. An annotation of type AI on a record component can be implicitly propagated down to affiliated mandated members. Type annotations modifying the type of a record component can be also propagated to mandated members. Propagation of the annotations to mandated members is governed by rules given in the The Java Language Specification (JLS 8.10.1). If there are multiple annotations of type AI present on C, then if AI is repeatable annotation interface, an annotation of type AIC is implicitly declared on C.
- A representation of A appears in the executable output for C, such as the
RuntimeVisibleAnnotations
(JVMS 4.7.16) orRuntimeVisibleParameterAnnotations
(JVMS 4.7.17) attributes of a class file.
An annotation A is present on a construct C if either:
- A is directly present on C.
- No annotation of type AI is directly present on C, and C is a class and AI is inheritable and A is present on the superclass of C.
- AI is a repeatable annotation interface with a containing annotation interface AIC.
- An annotation of type AIC is directly present on C and A is an annotation included in the result of calling the
value
method of the directly present annotation of type AIC.
- A is directly or indirectly present on C.
- No annotation of type AI is directly or indirectly present on C, and C is a class, and AI is inheritable, and A is associated with the superclass of C.
- See Java Language Specification:
-
9.6 Annotation Interfaces
9.6.4.3@Inherited
9.7.4 Where Annotations May Appear
9.7.5 Multiple Annotations of the Same Interface
- Since:
- 1.8
-
Method Summary
Modifier and TypeMethodDescription<A extends Annotation>
AgetAnnotation
(Class<A> annotationType) Returns this construct's annotation of the specified type if such an annotation is present, elsenull
.List
<? extends AnnotationMirror> Returns the annotations that are directly present on this construct.<A extends Annotation>
A[]getAnnotationsByType
(Class<A> annotationType) Returns annotations that are associated with this construct.
-
Method Details
-
getAnnotationMirrors
List<? extends AnnotationMirror> getAnnotationMirrors()Returns the annotations that are directly present on this construct.- Returns:
- the annotations directly present on this construct; an empty list if there are none
-
getAnnotation
Returns this construct's annotation of the specified type if such an annotation is present, elsenull
.The annotation returned by this method could contain an element whose value is of type
Class
. This value cannot be returned directly: information necessary to locate and load a class (such as the class loader to use) is not available, and the class might not be loadable at all. Attempting to read aClass
object by invoking the relevant method on the returned annotation will result in aMirroredTypeException
, from which the correspondingTypeMirror
may be extracted. Similarly, attempting to read aClass[]
-valued element will result in aMirroredTypesException
.Note: This method is unlike others in this and related interfaces. It operates on runtime reflective information — representations of annotation interfaces currently loaded into the VM — rather than on the representations defined by and used throughout these interfaces. Consequently, calling methods on the returned annotation object can throw many of the exceptions that can be thrown when calling methods on an annotation object returned by core reflection. This method is intended for callers that are written to operate on a known, fixed set of annotation interfaces.
- Type Parameters:
-
A
- the annotation interface - Parameters:
-
annotationType
- theClass
object corresponding to the annotation interface - Returns:
-
this construct's annotation of the specified type if such an annotation is present, else
null
- See Java Language Specification:
-
9.6.1 Annotation Interface Elements
- See Also:
-
getAnnotationsByType
返回与此构造关联的注释。如果没有与此构造关联的注释,则返回值是长度为0的数组。直接或间接存在于构造体 C 上的注释的顺序被计算为如果间接存在于构造体 C 上的注释直接存在于构造体 C 上,取代它们的容器注释,在容器注释的值元素中出现的顺序。此方法与getAnnotation(Class)
的区别在于,此方法检测其参数是否为一个可重复注释接口,如果是,则尝试通过“查找”容器注释来找到一个或多个该类型的注释。此方法返回的注释可能包含一个值为
Class
类型的元素。这个值无法直接返回:用于定位和加载类的信息(例如要使用的类加载器)不可用,而且类可能根本无法加载。尝试通过调用返回的注释上的相关方法来读取Class
对象将导致一个MirroredTypeException
,从中可以提取相应的TypeMirror
。类似地,尝试读取一个Class[]
值的元素将导致一个MirroredTypesException
。注意: 此方法与此接口及相关接口中的其他方法不同。它操作运行时反射信息 —— 当前加载到VM中的注释接口的表示 —— 而不是这些接口中定义并在整个接口中使用的表示。因此,调用返回的注释对象上的方法可能会抛出许多在调用核心反射返回的注释对象上的方法时可能抛出的异常。此方法适用于编写为操作已知、固定的一组注释接口的调用者。
- 类型参数:
-
A
- 注释接口 - 参数:
-
annotationType
- 对应于注释接口的Class
对象 - 返回:
- 如果此构造上存在指定注释类型的注释,则返回此构造的注释,否则返回一个空数组
- 参见 Java 语言规范:
-
9.6 注释接口
9.6.1 注释接口元素
- 参见:
-