diff --git a/sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/impl/AnnotationExtractor.java b/sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/impl/AnnotationExtractor.java index a5dcd1e53f36..79f5292165d5 100644 --- a/sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/impl/AnnotationExtractor.java +++ b/sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/impl/AnnotationExtractor.java @@ -52,7 +52,5 @@ public interface AnnotationExtractor { T extractAnnotation(AnnotatedElement element, Class annotationType, boolean declaredOnly); - Annotation[] extractAnnotations(AnnotatedElement element, boolean declaredOnly); - Class[] getAnnotationTypes(AnnotatedElement element); } diff --git a/substratevm/src/com.oracle.graal.pointsto.standalone/src/com/oracle/graal/pointsto/standalone/StandaloneAnnotationExtractor.java b/substratevm/src/com.oracle.graal.pointsto.standalone/src/com/oracle/graal/pointsto/standalone/StandaloneAnnotationExtractor.java index 73aac0eea268..affbd6a3d23b 100644 --- a/substratevm/src/com.oracle.graal.pointsto.standalone/src/com/oracle/graal/pointsto/standalone/StandaloneAnnotationExtractor.java +++ b/substratevm/src/com.oracle.graal.pointsto.standalone/src/com/oracle/graal/pointsto/standalone/StandaloneAnnotationExtractor.java @@ -48,15 +48,6 @@ public T extractAnnotation(AnnotatedElement element, Clas } } - @Override - public Annotation[] extractAnnotations(AnnotatedElement element, boolean declaredOnly) { - if (declaredOnly) { - return element.getDeclaredAnnotations(); - } else { - return element.getAnnotations(); - } - } - @SuppressWarnings("unchecked") @Override public Class[] getAnnotationTypes(AnnotatedElement element) { diff --git a/substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/meta/AnalysisElement.java b/substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/meta/AnalysisElement.java index 3c3968bf129a..44605d57d5d0 100644 --- a/substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/meta/AnalysisElement.java +++ b/substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/meta/AnalysisElement.java @@ -36,6 +36,7 @@ import java.util.function.BiConsumer; import java.util.function.Consumer; +import org.graalvm.compiler.debug.GraalError; import org.graalvm.nativeimage.hosted.Feature.DuringAnalysisAccess; import com.oracle.graal.pointsto.ObjectScanner; @@ -68,12 +69,12 @@ public final T getDeclaredAnnotation(Class annotationC @Override public final Annotation[] getAnnotations() { - return getUniverse().getAnnotationExtractor().extractAnnotations(getWrapped(), false); + throw GraalError.shouldNotReachHere("Getting all annotations is not supported because it initializes all annotation classes and their dependencies"); } @Override public final Annotation[] getDeclaredAnnotations() { - return getUniverse().getAnnotationExtractor().extractAnnotations(getWrapped(), true); + throw GraalError.shouldNotReachHere("Getting all annotations is not supported because it initializes all annotation classes and their dependencies"); } /** diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/annotation/AnnotationWrapper.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/annotation/AnnotationWrapper.java index b232a317e244..92166a681462 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/annotation/AnnotationWrapper.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/annotation/AnnotationWrapper.java @@ -30,6 +30,8 @@ import org.graalvm.nativeimage.ImageSingletons; import org.graalvm.nativeimage.impl.AnnotationExtractor; +import com.oracle.svm.core.util.VMError; + public interface AnnotationWrapper extends AnnotatedElement { AnnotatedElement getAnnotationRoot(); @@ -54,11 +56,11 @@ default T getDeclaredAnnotation(Class annotationClass) @Override default Annotation[] getAnnotations() { - return ImageSingletons.lookup(AnnotationExtractor.class).extractAnnotations(this, false); + throw VMError.shouldNotReachHere("Getting all annotations is not supported because it initializes all annotation classes and their dependencies"); } @Override default Annotation[] getDeclaredAnnotations() { - return ImageSingletons.lookup(AnnotationExtractor.class).extractAnnotations(this, true); + throw VMError.shouldNotReachHere("Getting all annotations is not supported because it initializes all annotation classes and their dependencies"); } } diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/annotation/SubstrateAnnotationExtractor.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/annotation/SubstrateAnnotationExtractor.java index 127bf4b18940..4e6b8b967da8 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/annotation/SubstrateAnnotationExtractor.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/annotation/SubstrateAnnotationExtractor.java @@ -75,8 +75,9 @@ * * The {@link SubstrateAnnotationExtractor} is tightly coupled with {@link AnnotationAccess}, which * provides implementations of {@link AnnotatedElement#isAnnotationPresent(Class)} and - * {@link AnnotatedElement#getAnnotation(Class)}. {@link AnnotatedElement#getAnnotations()} should - * in principle not be used during Native Image generation. + * {@link AnnotatedElement#getAnnotation(Class)}. {@link AnnotatedElement#getAnnotations()} must + * never be used during Native Image generation because it initializes all annotation classes and + * their dependencies. */ public class SubstrateAnnotationExtractor implements AnnotationExtractor { private final Map, AnnotationValue[]> annotationCache = new ConcurrentHashMap<>(); @@ -166,11 +167,6 @@ public Class[] getAnnotationTypes(AnnotatedElement element return Arrays.stream(getAnnotationData(element, false)).map(AnnotationValue::getType).filter(t -> t != null).toArray(Class[]::new); } - @Override - public Annotation[] extractAnnotations(AnnotatedElement element, boolean declaredOnly) { - return Arrays.stream(getAnnotationData(element, declaredOnly)).map(v -> v.get(v.type)).toArray(Annotation[]::new); - } - public AnnotationValue[] getDeclaredAnnotationData(AnnotatedElement element) { return getAnnotationData(element, true); } diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/meta/HostedElement.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/meta/HostedElement.java index 1c3522747fec..054340bb93b6 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/meta/HostedElement.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/meta/HostedElement.java @@ -27,6 +27,8 @@ import java.lang.annotation.Annotation; import java.lang.reflect.AnnotatedElement; +import com.oracle.svm.core.util.VMError; + public abstract class HostedElement implements AnnotatedElement { protected abstract AnnotatedElement getWrapped(); @@ -48,11 +50,11 @@ public final T getDeclaredAnnotation(Class annotationC @Override public final Annotation[] getAnnotations() { - return getWrapped().getAnnotations(); + throw VMError.shouldNotReachHere("Getting all annotations is not supported because it initializes all annotation classes and their dependencies"); } @Override public final Annotation[] getDeclaredAnnotations() { - return getWrapped().getDeclaredAnnotations(); + throw VMError.shouldNotReachHere("Getting all annotations is not supported because it initializes all annotation classes and their dependencies"); } } diff --git a/substratevm/src/com.oracle.svm.truffle/src/com/oracle/svm/truffle/TruffleBaseFeature.java b/substratevm/src/com.oracle.svm.truffle/src/com/oracle/svm/truffle/TruffleBaseFeature.java index 9e842b4060e6..aefe0fc025eb 100644 --- a/substratevm/src/com.oracle.svm.truffle/src/com/oracle/svm/truffle/TruffleBaseFeature.java +++ b/substratevm/src/com.oracle.svm.truffle/src/com/oracle/svm/truffle/TruffleBaseFeature.java @@ -678,7 +678,7 @@ private static void initializeTruffleLibrariesAtBuildTime(AnalysisType type) { /* Trigger computation of uncachedDispatch. */ factory.getUncached(); } - if (type.getDeclaredAnnotationsByType(ExportLibrary.class).length != 0) { + if (type.isAnnotationPresent(ExportLibrary.class) || type.isAnnotationPresent(ExportLibrary.Repeat.class)) { /* Eagerly resolve receiver type. */ invokeStaticMethod("com.oracle.truffle.api.library.LibraryFactory$ResolvedDispatch", "lookup", Collections.singleton(Class.class), type.getJavaClass());