@@ -422,13 +422,44 @@ private static <S, T, U extends ResolvedJavaMethod> Map<S, U> createStubs(
422
422
return created ;
423
423
}
424
424
425
+ private static final String JLI_PREFIX = "java.lang.invoke." ;
426
+
427
+ /**
428
+ * List of (generated) classes that provide accessor methods for memory segments. Those methods
429
+ * are referenced with {@code java.lang.invoke.SegmentVarHandle}. Unfortunately, the classes
430
+ * containing the methods are not subclasses of {@link java.lang.invoke.VarHandle} and so the
431
+ * automatic registration for reflective access (see
432
+ * {@link com.oracle.svm.hosted.methodhandles.MethodHandleFeature#beforeAnalysis}) does not
433
+ * trigger.
434
+ */
435
+ @ BasedOnJDKFile ("https://github.com/openjdk/jdk/blob/jdk-25+13/src/java.base/share/classes/java/lang/invoke/VarHandles.java#L313-L344" ) //
436
+ private static final List <String > VAR_HANDLE_SEGMENT_ACCESSORS = List .of (
437
+ "VarHandleSegmentAsBooleans" ,
438
+ "VarHandleSegmentAsBytes" ,
439
+ "VarHandleSegmentAsShorts" ,
440
+ "VarHandleSegmentAsChars" ,
441
+ "VarHandleSegmentAsInts" ,
442
+ "VarHandleSegmentAsLongs" ,
443
+ "VarHandleSegmentAsFloats" ,
444
+ "VarHandleSegmentAsDoubles" );
445
+
446
+ private static void registerVarHandleMethodsForReflection (FeatureAccess access , Class <?> subtype ) {
447
+ assert subtype .getPackage ().getName ().equals (JLI_PREFIX .substring (0 , JLI_PREFIX .length () - 1 ));
448
+ RuntimeReflection .register (subtype .getDeclaredMethods ());
449
+ }
450
+
425
451
@ Override
426
452
public void beforeAnalysis (BeforeAnalysisAccess a ) {
427
453
var access = (FeatureImpl .BeforeAnalysisAccessImpl ) a ;
428
454
sealed = true ;
429
455
430
456
AbiUtils .singleton ().checkLibrarySupport ();
431
457
458
+ for (String simpleName : VAR_HANDLE_SEGMENT_ACCESSORS ) {
459
+ Class <?> varHandleSegmentAsXClass = ReflectionUtil .lookupClass (JLI_PREFIX + simpleName );
460
+ access .registerSubtypeReachabilityHandler (ForeignFunctionsFeature ::registerVarHandleMethodsForReflection , varHandleSegmentAsXClass );
461
+ }
462
+
432
463
/*
433
464
* Specializing an adapter would define a new class at runtime, which is not allowed in
434
465
* SubstrateVM
0 commit comments