Skip to content

Commit 14cb22d

Browse files
committed
avoid searching for inherited annotations unnecessarily
1 parent 8fc1208 commit 14cb22d

File tree

6 files changed

+19
-8
lines changed

6 files changed

+19
-8
lines changed

sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/AnnotationAccess.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -93,7 +93,7 @@ public static boolean isAnnotationPresent(AnnotatedElement element, Class<? exte
9393
@SuppressWarnings("unchecked")
9494
public static <T extends Annotation> T getAnnotation(AnnotatedElement element, Class<T> annotationType) {
9595
if (ImageInfo.inImageBuildtimeCode()) {
96-
return ImageSingletons.lookup(AnnotationExtractor.class).extractAnnotation(element, annotationType, false);
96+
return ImageSingletons.lookup(AnnotationExtractor.class).extractAnnotation(element, annotationType);
9797
} else {
9898
return element.getAnnotation(annotationType);
9999
}

sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/impl/AnnotationExtractor.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
package org.graalvm.nativeimage.impl;
4242

4343
import java.lang.annotation.Annotation;
44+
import java.lang.annotation.Inherited;
4445
import java.lang.reflect.AnnotatedElement;
4546

4647
import org.graalvm.nativeimage.Platform;
@@ -67,6 +68,16 @@ public interface AnnotationExtractor {
6768
*/
6869
<T extends Annotation> T extractAnnotation(AnnotatedElement element, Class<T> annotationType, boolean declaredOnly);
6970

71+
/**
72+
* Gets {@code element}'s annotation of type {@code annotationType} if such an annotation is
73+
* present, else null. This method will also search {@code element}'s superclasses if
74+
* {@code annotationType} is {@linkplain Inherited inherited}.
75+
*/
76+
default <T extends Annotation> T extractAnnotation(AnnotatedElement element, Class<T> annotationType) {
77+
Inherited inherited = annotationType.getAnnotation(Inherited.class);
78+
return extractAnnotation(element, annotationType, inherited == null);
79+
}
80+
7081
/**
7182
* Gets the {@link Annotation#annotationType()}s for all annotations on {@code element}. This
7283
* method includes inherited annotations.

substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/meta/AnalysisMethod.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ protected AnalysisMethod(AnalysisUniverse universe, ResolvedJavaMethod wrapped,
287287
if (universe.hostVM.buildingImageLayer()) {
288288
var annotationExtractor = universe.getAnnotationExtractor();
289289
if (annotationExtractor.hasAnnotation(wrapped, LayeredCompilationBehavior.class)) {
290-
LayeredCompilationBehavior behavior = annotationExtractor.extractAnnotation(wrapped, LayeredCompilationBehavior.class, true);
290+
LayeredCompilationBehavior behavior = annotationExtractor.extractAnnotation(wrapped, LayeredCompilationBehavior.class);
291291
compilationBehavior = behavior.value();
292292
if (compilationBehavior == LayeredCompilationBehavior.Behavior.PINNED_TO_INITIAL_LAYER && universe.hostVM.buildingExtensionLayer() && !isInBaseLayer) {
293293
var errorMessage = String.format("User methods with layered compilation behavior %s must be registered via %s in the initial layer",

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/ImageClassLoader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ private void findSystemElements(Class<?> systemClass) {
134134

135135
private boolean isInPlatform(AnnotatedElement element) {
136136
try {
137-
Platforms platformAnnotation = classLoaderSupport.annotationExtractor.extractAnnotation(element, Platforms.class, false);
137+
Platforms platformAnnotation = classLoaderSupport.annotationExtractor.extractAnnotation(element, Platforms.class);
138138
return NativeImageGenerator.includedIn(platform, platformAnnotation);
139139
} catch (Throwable t) {
140140
handleClassLoadingError(t);
@@ -167,7 +167,7 @@ void handleClass(Class<?> clazz) {
167167
do {
168168
Platforms platformsAnnotation;
169169
try {
170-
platformsAnnotation = classLoaderSupport.annotationExtractor.extractAnnotation(cur, Platforms.class, false);
170+
platformsAnnotation = classLoaderSupport.annotationExtractor.extractAnnotation(cur, Platforms.class);
171171
} catch (Throwable t) {
172172
handleClassLoadingError(t);
173173
return;

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/ImageSingletonsSupportImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ void seal() {
207207
static SingletonTraitMap getAnnotatedTraits(Class<?> singletonClass, AnnotationExtractor extractor, boolean layeredBuild) {
208208
SingletonTraitMap traitMap = SingletonTraitMap.create();
209209
if (extractor != null) {
210-
SingletonTraits annotation = extractor.extractAnnotation(singletonClass, SingletonTraits.class, false);
210+
SingletonTraits annotation = extractor.extractAnnotation(singletonClass, SingletonTraits.class);
211211

212212
if (annotation != null) {
213213
if (annotation.access() != null) {
@@ -630,7 +630,7 @@ public SingletonTraitMap getUninstalledSingletonTraitMap(Class<?> key) {
630630

631631
SingletonInfo info = configObjects.computeIfAbsent(key, k -> {
632632

633-
SingletonTraits annotation = extractor.extractAnnotation(k, SingletonTraits.class, false);
633+
SingletonTraits annotation = extractor.extractAnnotation(k, SingletonTraits.class);
634634
if (annotation != null) {
635635
if (annotation.layeredInstallationKind() != null) {
636636
var installationKindSupplierClass = annotation.layeredInstallationKind();

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/annotation/AnnotationWrapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ default boolean isAnnotationPresent(Class<? extends Annotation> annotationClass)
4545

4646
@Override
4747
default <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
48-
return ImageSingletons.lookup(AnnotationExtractor.class).extractAnnotation(this, annotationClass, false);
48+
return ImageSingletons.lookup(AnnotationExtractor.class).extractAnnotation(this, annotationClass);
4949
}
5050

5151
@Override

0 commit comments

Comments
 (0)