Skip to content

Commit 9fa5f68

Browse files
committed
[GR-57649] Use replaceObjectWithConstant in reflection plugin.
PullRequest: graal/18737
2 parents 9460c4c + 55facce commit 9fa5f68

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,14 +622,18 @@ public Object replaceObject(Object source) {
622622
}
623623

624624
public JavaConstant replaceObjectWithConstant(Object source) {
625+
return replaceObjectWithConstant(source, getHostedValuesProvider()::forObject);
626+
}
627+
628+
public JavaConstant replaceObjectWithConstant(Object source, Function<Object, JavaConstant> converter) {
625629
assert !(source instanceof ImageHeapConstant) : source;
626630

627631
var replacedObject = replaceObject0(source, true);
628632
if (replacedObject instanceof ImageHeapConstant constant) {
629633
return constant;
630634
}
631635

632-
return getHostedValuesProvider().forObject(replacedObject);
636+
return converter.apply(replacedObject);
633637
}
634638

635639
/**

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/snippets/ReflectionPlugins.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,22 @@ private boolean processClassGetClassLoader(GraphBuilderContext b, ResolvedJavaMe
422422
return false;
423423
}
424424

425-
return pushConstant(b, targetMethod, clazz::getName, JavaKind.Object, clazz.getClassLoader(), true) != null;
425+
// GR-57649 generalize code if needed in more places
426+
ClassLoader loader = clazz.getClassLoader();
427+
JavaConstant result;
428+
if (loader == null) {
429+
result = JavaConstant.NULL_POINTER;
430+
} else {
431+
result = getIntrinsicConstant(b, loader);
432+
}
433+
434+
if (result != null) {
435+
b.addPush(JavaKind.Object, ConstantNode.forConstant(result, b.getMetaAccess()));
436+
traceConstant(b, targetMethod, clazz::getName, result);
437+
return true;
438+
}
439+
440+
return false;
426441
}
427442

428443
/**
@@ -669,6 +684,25 @@ private <T> T getIntrinsic(GraphBuilderContext context, T element) {
669684
return (T) aUniverse.replaceObject(element);
670685
}
671686

687+
/**
688+
* Same as {@link #getIntrinsic}, but returns a {@link JavaConstant}.
689+
*/
690+
private JavaConstant getIntrinsicConstant(GraphBuilderContext context, Object element) {
691+
if (reason == ParsingReason.AutomaticUnsafeTransformation || reason == ParsingReason.EarlyClassInitializerAnalysis) {
692+
/* We are analyzing the static initializers and should always intrinsify. */
693+
return context.getSnippetReflection().forObject(element);
694+
}
695+
if (isDeleted(element, context.getMetaAccess())) {
696+
/*
697+
* Should not intrinsify. Will fail during the reflective lookup at runtime. @Delete-ed
698+
* elements are ignored by the reflection plugins regardless of the value of
699+
* ReportUnsupportedElementsAtRuntime.
700+
*/
701+
return null;
702+
}
703+
return aUniverse.replaceObjectWithConstant(element, context.getSnippetReflection()::forObject);
704+
}
705+
672706
private static <T> boolean isDeleted(T element, MetaAccessProvider metaAccess) {
673707
AnnotatedElement annotated = null;
674708
try {

0 commit comments

Comments
 (0)