Class AnnotationAccess

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

public final class AnnotationAccess extends Object
This class provides same-VM annotation access for AnnotatedElements owned by the current context while trying to prevent, at image build time, side-effecting changes that impact the analysis results. Native Image builder code querying guest-owned JVMCI elements must use its builder-to-guest annotation facade instead. 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(AnnotatedElement, Class) only initializes the queried annotation class (when the annotation is actually present and therefore instantiated); isAnnotationPresent(AnnotatedElement, Class) and getAnnotationTypes(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. In a fully isolated image build, builder-owned elements are queried in the builder VM using core reflection. For those elements, these methods follow the JDK class initialization behavior; in particular, getAnnotationTypes(AnnotatedElement) can initialize annotation interfaces. Guest and application elements retain the reduced class initialization behavior described above. 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