AnnotatedElement (Java Platform SE 8 ) (original) (raw)

Represents an annotated element of the program currently running in this VM. This interface allows annotations to be read reflectively. All annotations returned by methods in this interface are immutable and serializable. The arrays returned by methods of this interface may be modified by callers without affecting the arrays returned to other callers.

The getAnnotationsByType(Class) and getDeclaredAnnotationsByType(Class) methods support multiple annotations of the same type on an element. If the argument to either method is a repeatable annotation type (JLS 9.6), then the method will "look through" a container annotation (JLS 9.7), if present, and return any annotations inside the container. Container annotations may be generated at compile-time to wrap multiple annotations of the argument type.

The terms directly present, indirectly present,present, and associated are used throughout this interface to describe precisely which annotations are returned by methods:

The table below summarizes which kind of annotation presence different methods in this interface examine.

Overview of kind of presence detected by different AnnotatedElement methods

Kind of Presence
Method Directly Present Indirectly Present Present Associated
T getAnnotation(Class) X
Annotation[] getAnnotations() X
T[] getAnnotationsByType(Class) X
T getDeclaredAnnotation(Class) X
Annotation[] getDeclaredAnnotations() X
T[] getDeclaredAnnotationsByType(Class) X X

For an invocation of get[Declared]AnnotationsByType( Class < T >), the order of annotations which are directly or indirectly present on an element E is computed as if indirectly present annotations on E are directly present on E in place of their container annotation, in the order in which they appear in the value element of the container annotation.

There are several compatibility concerns to keep in mind if an annotation type T is originally not repeatable and later modified to be repeatable. The containing annotation type for T is TC.

If an annotation returned by a method in this interface contains (directly or indirectly) a Class-valued member referring to a class that is not accessible in this VM, attempting to read the class by calling the relevant Class-returning method on the returned annotation will result in a TypeNotPresentException.

Similarly, attempting to read an enum-valued member will result in a EnumConstantNotPresentException if the enum constant in the annotation is no longer present in the enum type.

If an annotation type T is (meta-)annotated with an@Repeatable annotation whose value element indicates a type_TC_, but TC does not declare a value() method with a return type of T[], then an exception of typeAnnotationFormatError is thrown.

Finally, attempting to read a member whose definition has evolved incompatibly will result in a AnnotationTypeMismatchException or anIncompleteAnnotationException.