Package org.graalvm.nativeimage
Class AnnotationAccess
java.lang.Object
org.graalvm.nativeimage.AnnotationAccess
This class provides methods to query annotation information on
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: getAnnotation(java.lang.reflect.AnnotatedElement, java.lang.Class<T>)
only initializes the
queried annotation class (when the annotation is actually present and therefore instantiated);
isAnnotationPresent(java.lang.reflect.AnnotatedElement, java.lang.Class<? extends java.lang.annotation.Annotation>)
and 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`.- Since:
- 22.3
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends Annotation>
TgetAnnotation
(AnnotatedElement element, Class<T> annotationType) Implementation ofAnnotatedElement.getAnnotation(Class)
.static Class<? extends Annotation>[]
getAnnotationTypes
(AnnotatedElement element) Implementation for retrieving allAnnotation.annotationType()
s for aelement
.static boolean
isAnnotationPresent
(AnnotatedElement element, Class<? extends Annotation> annotationClass) Implementation ofAnnotatedElement.isAnnotationPresent(Class)
.
-
Method Details
-
isAnnotationPresent
public static boolean isAnnotationPresent(AnnotatedElement element, Class<? extends Annotation> annotationClass) Implementation ofAnnotatedElement.isAnnotationPresent(Class)
.- Since:
- 22.3
-
getAnnotation
public static <T extends Annotation> T getAnnotation(AnnotatedElement element, Class<T> annotationType) Implementation ofAnnotatedElement.getAnnotation(Class)
.- Since:
- 22.3
-
getAnnotationTypes
Implementation for retrieving allAnnotation.annotationType()
s for aelement
.- Since:
- 22.3
-