Class AnnotationAccess

java.lang.Object
org.graalvm.nativeimage.AnnotationAccess

public final class AnnotationAccess extends Object
This class provides methods to query annotation information on AnnotatedElements 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