public final class AnnotationAccess extends Object
AnnotatedElement
s while
trying to prevent, at image build time, side-effecting changes that impact the analysis results.
The getAnnotation implementation in the JDK for Class, Field, and Method initializes the classes
of all annotations present on that element, not just the class of the queried annotation. This
leads to problems when not all annotation classes are present on the classpath/modulepath:
querying an annotation whose class is present can fail with an exception because not all
annotation classes are present. When this class's methods are called at image build time, then
the minimal amount of class initialization is done: AnnotationAccess.getAnnotation(java.lang.reflect.AnnotatedElement, java.lang.Class<T>)
only initializes the
queried annotation class (when the annotation is actually present and therefore instantiated);
AnnotationAccess.isAnnotationPresent(java.lang.reflect.AnnotatedElement, java.lang.Class<? extends java.lang.annotation.Annotation>)
and AnnotationAccess.getAnnotationTypes(java.lang.reflect.AnnotatedElement)
do not initialize any classes.
When methods of this class are called not at image build time, i.e., at image run time or during
the execution of a Java application not involving native image, then the JDK method to query the
annotation is invoked. In these cases, there is no difference to the class initialization
behavior of the JDK.
Note that there is intentionally no `getAnnotations` method to query all annotations: all
annotation classes must be initialized anyways by this method, so the JDK method can be invoke
directly. In the image generator it should be generally avoided to use `getAnnotations`.Modifier and Type | Method and Description |
---|---|
static <T extends Annotation> |
getAnnotation(AnnotatedElement element,
Class<T> annotationType)
Implementation of
AnnotatedElement.getAnnotation(Class) . |
static Class<? extends Annotation>[] |
getAnnotationTypes(AnnotatedElement element)
Implementation for retrieving all
Annotation.annotationType() s for a element . |
static boolean |
isAnnotationPresent(AnnotatedElement element,
Class<? extends Annotation> annotationClass)
Implementation of
AnnotatedElement.isAnnotationPresent(Class) . |
public static boolean isAnnotationPresent(AnnotatedElement element, Class<? extends Annotation> annotationClass)
AnnotatedElement.isAnnotationPresent(Class)
.public static <T extends Annotation> T getAnnotation(AnnotatedElement element, Class<T> annotationType)
AnnotatedElement.getAnnotation(Class)
.public static Class<? extends Annotation>[] getAnnotationTypes(AnnotatedElement element)
Annotation.annotationType()
s for a element
.