APIdia Launch Partner
Top Description Methods
javax.lang.model

public Interface AnnotatedConstruct

Known Direct Subinterfaces
javax.lang.model.element.Element, javax.lang.model.type.TypeMirror
Imports
java.lang.annotation.*, java.util.List, javax.lang.model.element.*, javax.lang.model.type.*

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. As defined by The Java Language Specification section 9.7.4, an annotation on an element is a declaration annotation and an annotation on a type is a type annotation. The terms directly present, present, indirectly present, and associated are used throughout this interface to describe precisely which annotations, either declaration annotations or type annotations, are returned by the methods in this interface.

In the definitions below, an annotation A has an annotation interface AI. If AI is a repeatable annotation interface, the type of the container annotation is AIC.

Annotation A is directly present on a construct C if either:

An annotation A is present on a construct C if either:

An annotation A is indirectly present on a construct C if both: An annotation A is associated with a construct C if either:
Since
1.8
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

Method Summary

Modifier and TypeMethod and Description
public <
the annotation interface
A extends Annotation
>
A

Returns:

this construct's annotation of the specified type if such an annotation is present, else null
getAnnotation
(Class<A>
the Class object corresponding to the annotation interface
annotationType
)

Returns this construct's annotation of the specified type if such an annotation is present, else null.

public List<? extends AnnotationMirror>

Returns:

the annotations directly present on this construct; an empty list if there are none
getAnnotationMirrors
()

Returns the annotations that are directly present on this construct.

public <
the annotation interface
A extends Annotation
>
A[]

Returns:

this construct's annotations for the specified annotation type if present on this construct, else an empty array
getAnnotationsByType
(Class<A>
the Class object corresponding to the annotation interface
annotationType
)

Returns annotations of the specified type that are associated with this construct.

Method Detail

getAnnotationback to summary
public <A extends Annotation> A getAnnotation(Class<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, from which the corresponding TypeMirror may be extracted. Similarly, attempting to read a Class[]-valued element will result in a MirroredTypesException.

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.

Parameters
<A>
the annotation interface
annotationType:Class<A>

the Class object corresponding to the annotation interface

Returns:A

this construct's annotation of the specified type if such an annotation is present, else null

Java Language Specification
9.6.1 Annotation Interface Elements
See Also
getAnnotationMirrors(), java.lang.reflect.AnnotatedElement#getAnnotation, EnumConstantNotPresentException, AnnotationTypeMismatchException, IncompleteAnnotationException, MirroredTypeException, MirroredTypesException
getAnnotationMirrorsback to summary
public List<? extends AnnotationMirror> getAnnotationMirrors()

Returns the annotations that are directly present on this construct.

Returns:List<? extends AnnotationMirror>

the annotations directly present on this construct; an empty list if there are none

getAnnotationsByTypeback to summary
public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationType)

Returns annotations of the specified type that are associated with this construct. If there are no annotations of the specified type 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) is that this method detects if its argument is a repeatable annotation interface, 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, from which the corresponding TypeMirror may be extracted. Similarly, attempting to read a Class[]-valued element will result in a MirroredTypesException.

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.

Parameters
<A>
the annotation interface
annotationType:Class<A>

the Class object corresponding to the annotation interface

Returns:A[]

this construct's annotations for the specified annotation type if present on this construct, else an empty array

Java Language Specification
9.6 Annotation Interfaces, 9.6.1 Annotation Interface Elements
See Also
getAnnotationMirrors(), getAnnotation(Class), java.lang.reflect.AnnotatedElement#getAnnotationsByType(Class), EnumConstantNotPresentException, AnnotationTypeMismatchException, IncompleteAnnotationException, MirroredTypeException, MirroredTypesException