AnnotatedConstruct (Java SE 11 & JDK 11 ) (original) (raw)
- All Known Subinterfaces:
[ArrayType](type/ArrayType.html "interface in javax.lang.model.type")
,[DeclaredType](type/DeclaredType.html "interface in javax.lang.model.type")
,[Element](element/Element.html "interface in javax.lang.model.element")
,[ErrorType](type/ErrorType.html "interface in javax.lang.model.type")
,[ExecutableElement](element/ExecutableElement.html "interface in javax.lang.model.element")
,[ExecutableType](type/ExecutableType.html "interface in javax.lang.model.type")
,[IntersectionType](type/IntersectionType.html "interface in javax.lang.model.type")
,[ModuleElement](element/ModuleElement.html "interface in javax.lang.model.element")
,[NoType](type/NoType.html "interface in javax.lang.model.type")
,[NullType](type/NullType.html "interface in javax.lang.model.type")
,[PackageElement](element/PackageElement.html "interface in javax.lang.model.element")
,[Parameterizable](element/Parameterizable.html "interface in javax.lang.model.element")
,[PrimitiveType](type/PrimitiveType.html "interface in javax.lang.model.type")
,[QualifiedNameable](element/QualifiedNameable.html "interface in javax.lang.model.element")
,[ReferenceType](type/ReferenceType.html "interface in javax.lang.model.type")
,[TypeElement](element/TypeElement.html "interface in javax.lang.model.element")
,[TypeMirror](type/TypeMirror.html "interface in javax.lang.model.type")
,[TypeParameterElement](element/TypeParameterElement.html "interface in javax.lang.model.element")
,[TypeVariable](type/TypeVariable.html "interface in javax.lang.model.type")
,[UnionType](type/UnionType.html "interface in javax.lang.model.type")
,[VariableElement](element/VariableElement.html "interface in javax.lang.model.element")
,[WildcardType](type/WildcardType.html "interface in javax.lang.model.type")
public interface AnnotatedConstruct
Represents a construct that can be annotated. A construct is either an element or a type. Annotations on an element are on a declaration, whereas annotations on a type are on a specific use of a type name. The terms directly present, present,indirectly present, and associated are used throughout this interface to describe precisely which annotations are returned by the methods defined herein.
In the definitions below, an annotation A has an annotation type AT. If AT is a repeatable annotation type, the type of the containing annotation is ATC.
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 AT appears in the source code of representation of C, then A is explicitly declared as applying to C. If there are multiple annotations of type AT present on_C_, then if AT is repeatable annotation type, an annotation of type ATC is implicitly declared on C.A representation of A appears in the executable output for C, such as the
RuntimeVisibleAnnotations
orRuntimeVisibleParameterAnnotations
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 AT is directly present on_C_, and C is a class and AT is inheritable and A is present on the superclass of C.
An annotation A is indirectly present on a construct_C_ if both:AT is a repeatable annotation type with a containing annotation type ATC.
An annotation of type ATC 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 ATC.
An annotation A is associated with a construct_C_ if either:A is directly or indirectly present on C.
No annotation of type AT is directly or indirectly present on C, and C is a class, and AT is inheritable, and A is associated with the superclass of_C_.
Since:
1.8
See The Java™ Language Specification:
9.6 Annotation Types, 9.6.3.3 @InheritedMethod Summary
All Methods Instance Methods Abstract Methods
Modifier and Type Method Description <A extends Annotation>A getAnnotation(Class annotationType) Returns this construct's annotation of the specified type if such an annotation is present, else null. List<? extends AnnotationMirror> getAnnotationMirrors() Returns the annotations that are directly present on this construct. <A extends Annotation>A[] getAnnotationsByType(Class annotationType) Returns annotations that are associated with this construct. Method Detail
* #### getAnnotationMirrors [List](../../../../java.base/java/util/List.html "interface in java.util")<? extends [AnnotationMirror](element/AnnotationMirror.html "interface in javax.lang.model.element")> 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 <A extends [Annotation](../../../../java.base/java/lang/annotation/Annotation.html "interface in java.lang.annotation")> A getAnnotation([Class](../../../../java.base/java/lang/Class.html "class in java.lang")<A> annotationType) Returns this construct's annotation of the specified type if such an annotation is _present_, else `null`. 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 a `Class` object by invoking the relevant method on the returned annotation will result in a [MirroredTypeException](type/MirroredTypeException.html "class in javax.lang.model.type"), from which the corresponding [TypeMirror](type/TypeMirror.html "interface in javax.lang.model.type") may be extracted. Similarly, attempting to read a `Class[]`\-valued element will result in a [MirroredTypesException](type/MirroredTypesException.html "class in javax.lang.model.type"). > _Note:_ This method is unlike others in this and related interfaces. It operates on runtime reflective information — representations of annotation types 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 types. Type Parameters: `A` \- the annotation type Parameters: `annotationType` \- the `Class` object corresponding to the annotation type Returns: this construct's annotation for the specified annotation type if present, else `null` See Also: [getAnnotationMirrors()](#getAnnotationMirrors%28%29), [AnnotatedElement.getAnnotation(java.lang.Class<T>)](../../../../java.base/java/lang/reflect/AnnotatedElement.html#getAnnotation%28java.lang.Class%29), [EnumConstantNotPresentException](../../../../java.base/java/lang/EnumConstantNotPresentException.html "class in java.lang"), [AnnotationTypeMismatchException](../../../../java.base/java/lang/annotation/AnnotationTypeMismatchException.html "class in java.lang.annotation"), [IncompleteAnnotationException](../../../../java.base/java/lang/annotation/IncompleteAnnotationException.html "class in java.lang.annotation"), [MirroredTypeException](type/MirroredTypeException.html "class in javax.lang.model.type"), [MirroredTypesException](type/MirroredTypesException.html "class in javax.lang.model.type") See The Java™ Language Specification: 9.6.1 Annotation Type Elements * #### getAnnotationsByType <A extends [Annotation](../../../../java.base/java/lang/annotation/Annotation.html "interface in java.lang.annotation")> A[] getAnnotationsByType([Class](../../../../java.base/java/lang/Class.html "class in java.lang")<A> annotationType) Returns annotations that are _associated_ with this construct. If there are no annotations associated with this construct, the return value is an array of length 0\. The order of annotations which are directly or indirectly present on a construct _C_ is computed as if indirectly present annotations on _C_ are directly present on _C_ in place of their container annotation, in the order in which they appear in the value element of the container annotation. The difference between this method and [getAnnotation(Class)](#getAnnotation%28java.lang.Class%29) is that this method detects if its argument is a _repeatable annotation type_, and if so, attempts to find one or more annotations of that type by "looking through" a container annotation. The annotations 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 a `Class` object by invoking the relevant method on the returned annotation will result in a [MirroredTypeException](type/MirroredTypeException.html "class in javax.lang.model.type"), from which the corresponding [TypeMirror](type/TypeMirror.html "interface in javax.lang.model.type") may be extracted. Similarly, attempting to read a `Class[]`\-valued element will result in a [MirroredTypesException](type/MirroredTypesException.html "class in javax.lang.model.type"). > _Note:_ This method is unlike others in this and related interfaces. It operates on runtime reflective information — representations of annotation types 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 types. Type Parameters: `A` \- the annotation type Parameters: `annotationType` \- the `Class` object corresponding to the annotation type Returns: this construct's annotations for the specified annotation type if present on this construct, else an empty array See Also: [getAnnotationMirrors()](#getAnnotationMirrors%28%29), [getAnnotation(Class)](#getAnnotation%28java.lang.Class%29), [AnnotatedElement.getAnnotationsByType(Class)](../../../../java.base/java/lang/reflect/AnnotatedElement.html#getAnnotationsByType%28java.lang.Class%29), [EnumConstantNotPresentException](../../../../java.base/java/lang/EnumConstantNotPresentException.html "class in java.lang"), [AnnotationTypeMismatchException](../../../../java.base/java/lang/annotation/AnnotationTypeMismatchException.html "class in java.lang.annotation"), [IncompleteAnnotationException](../../../../java.base/java/lang/annotation/IncompleteAnnotationException.html "class in java.lang.annotation"), [MirroredTypeException](type/MirroredTypeException.html "class in javax.lang.model.type"), [MirroredTypesException](type/MirroredTypesException.html "class in javax.lang.model.type") See The Java™ Language Specification: 9.6 Annotation Types, 9.6.1 Annotation Type Elements
Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2025, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.