Top Description Constructors Methods
org.graalvm.nativeimage

public final Class AnnotationAccess

extends Object
Class Inheritance
Imports
java.lang.annotation.Annotation, java.lang.reflect.AnnotatedElement, java.util.Arrays, org.graalvm.nativeimage.impl.AnnotationExtractor

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 only initializes the queried annotation class (when the annotation is actually present and therefore instantiated); isAnnotationPresent and getAnnotationTypes 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

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static <T extends Annotation> T
getAnnotation(AnnotatedElement element, Class<T> annotationType)

Implementation of AnnotatedElement#getAnnotation(Class) .

public static Class<? extends Annotation>[]
getAnnotationTypes(AnnotatedElement element)

Implementation for retrieving all Annotation#annotationType()s for a element.

public static boolean
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

AnnotationAccessback to summary
private AnnotationAccess()

Method Detail

getAnnotationback to summary
public static <T extends Annotation> T getAnnotation(AnnotatedElement element, Class<T> annotationType)

Implementation of AnnotatedElement#getAnnotation(Class) .

Annotations
@SuppressWarnings:unchecked
Since
22.3
getAnnotationTypesback to summary
public static Class<? extends Annotation>[] getAnnotationTypes(AnnotatedElement element)

Implementation for retrieving all Annotation#annotationType()s for a element.

Annotations
@SuppressWarnings:unchecked
Since
22.3
isAnnotationPresentback to summary
public static boolean isAnnotationPresent(AnnotatedElement element, Class<? extends Annotation> annotationClass)

Implementation of AnnotatedElement#isAnnotationPresent(Class).

Since
22.3