42
42
import java .io .ObjectInputStream ;
43
43
import java .io .ObjectOutputStream ;
44
44
import java .io .ObjectStreamClass ;
45
+ import java .lang .foreign .Arena ;
46
+ import java .lang .foreign .FunctionDescriptor ;
47
+ import java .lang .foreign .Linker ;
48
+ import java .lang .foreign .MemorySegment ;
45
49
import java .lang .invoke .ConstantBootstraps ;
46
50
import java .lang .invoke .MethodHandle ;
47
51
import java .lang .invoke .MethodHandleProxies ;
75
79
public class DynamicAccessDetectionPhase extends BasePhase <CoreProviders > {
76
80
public enum DynamicAccessKind {
77
81
Reflection ("reflection-calls.json" ),
78
- Resource ("resource-calls.json" );
82
+ Resource ("resource-calls.json" ),
83
+ Foreign ("foreign-calls.json" );
79
84
80
85
public final String fileName ;
81
86
@@ -89,6 +94,7 @@ public record MethodInfo(DynamicAccessKind accessKind, String signature) {
89
94
90
95
private static final EconomicMap <Class <?>, Set <MethodSignature >> reflectionMethodSignatures = EconomicMap .create ();
91
96
private static final EconomicMap <Class <?>, Set <MethodSignature >> resourceMethodSignatures = EconomicMap .create ();
97
+ private static final EconomicMap <Class <?>, Set <MethodSignature >> foreignMethodSignatures = EconomicMap .create ();
92
98
93
99
private final DynamicAccessDetectionFeature dynamicAccessDetectionFeature ;
94
100
@@ -184,6 +190,11 @@ public record MethodInfo(DynamicAccessKind accessKind, String signature) {
184
190
resourceMethodSignatures .put (Class .class , Set .of (
185
191
new MethodSignature ("getResource" , String .class ),
186
192
new MethodSignature ("getResourceAsStream" , String .class )));
193
+
194
+ foreignMethodSignatures .put (Linker .class , Set .of (
195
+ new MethodSignature ("downcallHandle" , MemorySegment .class , FunctionDescriptor .class , Linker .Option [].class ),
196
+ new MethodSignature ("downcallHandle" , FunctionDescriptor .class , Linker .Option [].class ),
197
+ new MethodSignature ("upcallStub" , MethodHandle .class , FunctionDescriptor .class , Arena .class , Linker .Option [].class )));
187
198
}
188
199
189
200
public DynamicAccessDetectionPhase () {
@@ -216,7 +227,8 @@ protected void run(StructuredGraph graph, CoreProviders context) {
216
227
private static MethodInfo getMethodInfo (ResolvedJavaMethod method ) {
217
228
Class <?> declaringClass = OriginalClassProvider .getJavaClass (method .getDeclaringClass ());
218
229
if (!reflectionMethodSignatures .containsKey (declaringClass ) &&
219
- !resourceMethodSignatures .containsKey (declaringClass )) {
230
+ !resourceMethodSignatures .containsKey (declaringClass ) &&
231
+ !foreignMethodSignatures .containsKey (declaringClass )) {
220
232
return null ;
221
233
}
222
234
@@ -236,6 +248,9 @@ private static MethodInfo getMethodInfo(ResolvedJavaMethod method) {
236
248
} else if (resourceMethodSignatures .containsKey (declaringClass ) &&
237
249
resourceMethodSignatures .get (declaringClass ).contains (methodSignature )) {
238
250
return new MethodInfo (DynamicAccessKind .Resource , declaringClass .getName () + "#" + methodSignature );
251
+ } else if (foreignMethodSignatures .containsKey (declaringClass ) &&
252
+ foreignMethodSignatures .get (declaringClass ).contains (methodSignature )) {
253
+ return new MethodInfo (DynamicAccessKind .Foreign , declaringClass .getName () + "#" + methodSignature );
239
254
}
240
255
241
256
return null ;
@@ -288,6 +303,7 @@ private static NodeSourcePosition getRootSourcePosition(NodeSourcePosition nodeS
288
303
public static void clearMethodSignatures () {
289
304
reflectionMethodSignatures .clear ();
290
305
resourceMethodSignatures .clear ();
306
+ foreignMethodSignatures .clear ();
291
307
}
292
308
293
309
private static class MethodSignature {
0 commit comments