Skip to content

Commit 41634f3

Browse files
author
Christian Wimmer
committed
[GR-39848] Disallow getAnnotations for analysis and hosted elements.
PullRequest: graal/14307
2 parents 5b8cc51 + 9b5dcf8 commit 41634f3

File tree

7 files changed

+15
-25
lines changed

7 files changed

+15
-25
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,5 @@ public interface AnnotationExtractor {
5252

5353
<T extends Annotation> T extractAnnotation(AnnotatedElement element, Class<T> annotationType, boolean declaredOnly);
5454

55-
Annotation[] extractAnnotations(AnnotatedElement element, boolean declaredOnly);
56-
5755
Class<? extends Annotation>[] getAnnotationTypes(AnnotatedElement element);
5856
}

substratevm/src/com.oracle.graal.pointsto.standalone/src/com/oracle/graal/pointsto/standalone/StandaloneAnnotationExtractor.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,6 @@ public <T extends Annotation> T extractAnnotation(AnnotatedElement element, Clas
4848
}
4949
}
5050

51-
@Override
52-
public Annotation[] extractAnnotations(AnnotatedElement element, boolean declaredOnly) {
53-
if (declaredOnly) {
54-
return element.getDeclaredAnnotations();
55-
} else {
56-
return element.getAnnotations();
57-
}
58-
}
59-
6051
@SuppressWarnings("unchecked")
6152
@Override
6253
public Class<? extends Annotation>[] getAnnotationTypes(AnnotatedElement element) {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.util.function.BiConsumer;
3737
import java.util.function.Consumer;
3838

39+
import org.graalvm.compiler.debug.GraalError;
3940
import org.graalvm.nativeimage.hosted.Feature.DuringAnalysisAccess;
4041

4142
import com.oracle.graal.pointsto.ObjectScanner;
@@ -68,12 +69,12 @@ public final <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationC
6869

6970
@Override
7071
public final Annotation[] getAnnotations() {
71-
return getUniverse().getAnnotationExtractor().extractAnnotations(getWrapped(), false);
72+
throw GraalError.shouldNotReachHere("Getting all annotations is not supported because it initializes all annotation classes and their dependencies");
7273
}
7374

7475
@Override
7576
public final Annotation[] getDeclaredAnnotations() {
76-
return getUniverse().getAnnotationExtractor().extractAnnotations(getWrapped(), true);
77+
throw GraalError.shouldNotReachHere("Getting all annotations is not supported because it initializes all annotation classes and their dependencies");
7778
}
7879

7980
/**

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import org.graalvm.nativeimage.ImageSingletons;
3131
import org.graalvm.nativeimage.impl.AnnotationExtractor;
3232

33+
import com.oracle.svm.core.util.VMError;
34+
3335
public interface AnnotationWrapper extends AnnotatedElement {
3436
AnnotatedElement getAnnotationRoot();
3537

@@ -54,11 +56,11 @@ default <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass)
5456

5557
@Override
5658
default Annotation[] getAnnotations() {
57-
return ImageSingletons.lookup(AnnotationExtractor.class).extractAnnotations(this, false);
59+
throw VMError.shouldNotReachHere("Getting all annotations is not supported because it initializes all annotation classes and their dependencies");
5860
}
5961

6062
@Override
6163
default Annotation[] getDeclaredAnnotations() {
62-
return ImageSingletons.lookup(AnnotationExtractor.class).extractAnnotations(this, true);
64+
throw VMError.shouldNotReachHere("Getting all annotations is not supported because it initializes all annotation classes and their dependencies");
6365
}
6466
}

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@
7575
*
7676
* The {@link SubstrateAnnotationExtractor} is tightly coupled with {@link AnnotationAccess}, which
7777
* provides implementations of {@link AnnotatedElement#isAnnotationPresent(Class)} and
78-
* {@link AnnotatedElement#getAnnotation(Class)}. {@link AnnotatedElement#getAnnotations()} should
79-
* in principle not be used during Native Image generation.
78+
* {@link AnnotatedElement#getAnnotation(Class)}. {@link AnnotatedElement#getAnnotations()} must
79+
* never be used during Native Image generation because it initializes all annotation classes and
80+
* their dependencies.
8081
*/
8182
public class SubstrateAnnotationExtractor implements AnnotationExtractor {
8283
private final Map<Class<?>, AnnotationValue[]> annotationCache = new ConcurrentHashMap<>();
@@ -166,11 +167,6 @@ public Class<? extends Annotation>[] getAnnotationTypes(AnnotatedElement element
166167
return Arrays.stream(getAnnotationData(element, false)).map(AnnotationValue::getType).filter(t -> t != null).toArray(Class[]::new);
167168
}
168169

169-
@Override
170-
public Annotation[] extractAnnotations(AnnotatedElement element, boolean declaredOnly) {
171-
return Arrays.stream(getAnnotationData(element, declaredOnly)).map(v -> v.get(v.type)).toArray(Annotation[]::new);
172-
}
173-
174170
public AnnotationValue[] getDeclaredAnnotationData(AnnotatedElement element) {
175171
return getAnnotationData(element, true);
176172
}

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/meta/HostedElement.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import java.lang.annotation.Annotation;
2828
import java.lang.reflect.AnnotatedElement;
2929

30+
import com.oracle.svm.core.util.VMError;
31+
3032
public abstract class HostedElement implements AnnotatedElement {
3133

3234
protected abstract AnnotatedElement getWrapped();
@@ -48,11 +50,11 @@ public final <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationC
4850

4951
@Override
5052
public final Annotation[] getAnnotations() {
51-
return getWrapped().getAnnotations();
53+
throw VMError.shouldNotReachHere("Getting all annotations is not supported because it initializes all annotation classes and their dependencies");
5254
}
5355

5456
@Override
5557
public final Annotation[] getDeclaredAnnotations() {
56-
return getWrapped().getDeclaredAnnotations();
58+
throw VMError.shouldNotReachHere("Getting all annotations is not supported because it initializes all annotation classes and their dependencies");
5759
}
5860
}

substratevm/src/com.oracle.svm.truffle/src/com/oracle/svm/truffle/TruffleBaseFeature.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ private static void initializeTruffleLibrariesAtBuildTime(AnalysisType type) {
696696
/* Trigger computation of uncachedDispatch. */
697697
factory.getUncached();
698698
}
699-
if (type.getDeclaredAnnotationsByType(ExportLibrary.class).length != 0) {
699+
if (type.isAnnotationPresent(ExportLibrary.class) || type.isAnnotationPresent(ExportLibrary.Repeat.class)) {
700700
/* Eagerly resolve receiver type. */
701701
invokeStaticMethod("com.oracle.truffle.api.library.LibraryFactory$ResolvedDispatch", "lookup",
702702
Collections.singleton(Class.class), type.getJavaClass());

0 commit comments

Comments
 (0)