Skip to content

Commit 9ed4eaf

Browse files
committed
svm: migrate ScalaFeature to JVMCI reflection
1 parent d2f2ae6 commit 9ed4eaf

File tree

1 file changed

+22
-19
lines changed
  • substratevm/src/com.oracle.svm.polyglot/src/com/oracle/svm/polyglot/scala

1 file changed

+22
-19
lines changed

substratevm/src/com.oracle.svm.polyglot/src/com/oracle/svm/polyglot/scala/ScalaFeature.java

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,25 @@
2424
*/
2525
package com.oracle.svm.polyglot.scala;
2626

27-
import java.lang.reflect.Field;
28-
import java.lang.reflect.Method;
2927
import java.util.Arrays;
3028
import java.util.function.BooleanSupplier;
3129

3230
import org.graalvm.nativeimage.ImageSingletons;
3331
import org.graalvm.nativeimage.hosted.RuntimeClassInitialization;
34-
import org.graalvm.nativeimage.hosted.RuntimeReflection;
3532

33+
import com.oracle.graal.pointsto.meta.AnalysisType;
3634
import com.oracle.svm.core.ParsingReason;
3735
import com.oracle.svm.core.feature.InternalFeature;
3836
import com.oracle.svm.core.util.UserError;
3937
import com.oracle.svm.hosted.FeatureImpl.BeforeAnalysisAccessImpl;
38+
import com.oracle.svm.hosted.dynamicaccess.JVMCIRuntimeReflection;
39+
import com.oracle.svm.util.JVMCIReflectionUtil;
4040
import com.oracle.svm.util.ModuleSupport;
4141

4242
import jdk.graal.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins;
4343
import jdk.graal.compiler.phases.util.Providers;
44+
import jdk.vm.ci.meta.ResolvedJavaField;
45+
import jdk.vm.ci.meta.ResolvedJavaMethod;
4446

4547
public class ScalaFeature implements InternalFeature {
4648

@@ -76,8 +78,8 @@ public void registerGraphBuilderPlugins(Providers providers, Plugins plugins, Pa
7678
plugins.appendNodePlugin(new ScalaAnalysisPlugin());
7779
}
7880

79-
private static boolean isValDef(Field[] fields, Method m) {
80-
return Arrays.stream(fields).anyMatch(fd -> fd.getName().equals(m.getName()) && fd.getType().equals(m.getReturnType()));
81+
private static boolean isValDef(ResolvedJavaField[] fields, ResolvedJavaMethod m) {
82+
return Arrays.stream(fields).anyMatch(fd -> fd.getName().equals(m.getName()) && fd.getType().equals(JVMCIReflectionUtil.getResolvedReturnType(m)));
8183
}
8284

8385
/**
@@ -87,27 +89,28 @@ private static boolean isValDef(Field[] fields, Method m) {
8789
private static void initializeScalaEnumerations(BeforeAnalysisAccess beforeAnalysisAccess) {
8890
BeforeAnalysisAccessImpl access = (BeforeAnalysisAccessImpl) beforeAnalysisAccess;
8991

90-
Class<?> scalaEnum = access.findClassByName("scala.Enumeration");
92+
AnalysisType scalaEnum = access.findTypeByName("scala.Enumeration");
9193
UserError.guarantee(scalaEnum != null, "%s", UNSUPPORTED_SCALA_VERSION);
92-
Class<?> scalaEnumVal = access.findClassByName("scala.Enumeration$Val");
94+
AnalysisType scalaEnumVal = access.findTypeByName("scala.Enumeration$Val");
9395
UserError.guarantee(scalaEnumVal != null, "%s", UNSUPPORTED_SCALA_VERSION);
94-
Class<?> valueClass = access.findClassByName("scala.Enumeration$Value");
96+
AnalysisType valueClass = access.findTypeByName("scala.Enumeration$Value");
9597
UserError.guarantee(valueClass != null, "%s", UNSUPPORTED_SCALA_VERSION);
9698

97-
access.findSubclasses(scalaEnum).forEach(enumClass -> {
99+
access.findSubtypes(scalaEnum).forEach(enumClass -> {
98100
/* this is based on implementation of scala.Enumeration.populateNamesMap */
99-
RuntimeReflection.registerAllDeclaredFields(enumClass);
101+
JVMCIRuntimeReflection.registerAllDeclaredFields(enumClass);
100102
// all method relevant for Enums
101-
Method[] relevantMethods = Arrays.stream(enumClass.getDeclaredMethods())
102-
.filter(m -> m.getParameterTypes().length == 0 &&
103-
m.getDeclaringClass() != scalaEnum &&
104-
valueClass.isAssignableFrom(m.getReturnType()) &&
105-
isValDef(enumClass.getDeclaredFields(), m))
106-
.toArray(Method[]::new);
107-
RuntimeReflection.register(relevantMethods);
103+
ResolvedJavaMethod[] relevantMethods = Arrays.stream(enumClass.getDeclaredMethods(false))
104+
.filter(m -> m.getParameters().length == 0 &&
105+
!scalaEnum.equals(m.getDeclaringClass()) &&
106+
valueClass.isAssignableFrom(JVMCIReflectionUtil.getResolvedReturnType(m)) &&
107+
(isValDef(enumClass.getInstanceFields(false), m) ||
108+
isValDef(enumClass.getStaticFields(), m)))
109+
.toArray(ResolvedJavaMethod[]::new);
110+
JVMCIRuntimeReflection.register(relevantMethods);
108111
try {
109-
RuntimeReflection.register(scalaEnumVal.getDeclaredMethod("id"));
110-
} catch (NoSuchMethodException e) {
112+
JVMCIRuntimeReflection.register(JVMCIReflectionUtil.getDeclaredMethod(access.getMetaAccess(), scalaEnumVal, "id"));
113+
} catch (NoSuchMethodError e) {
111114
throw UserError.abort("%s", UNSUPPORTED_SCALA_VERSION);
112115
}
113116
});

0 commit comments

Comments
 (0)