Skip to content

Commit 8549ba0

Browse files
loicottetzapster
authored andcommitted
[GR-71092] Introduce reflective access API using JVMCI types
PullRequest: graal/22507
2 parents fa1dfa2 + da1bddd commit 8549ba0

File tree

7 files changed

+648
-0
lines changed

7 files changed

+648
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (c) 2025, 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.svm.hosted.dynamicaccess;
26+
27+
import org.graalvm.nativeimage.dynamicaccess.AccessCondition;
28+
29+
import com.oracle.svm.util.OriginalClassProvider;
30+
31+
import jdk.vm.ci.meta.ResolvedJavaType;
32+
33+
/**
34+
* Mirror of {@link org.graalvm.nativeimage.dynamicaccess.AccessCondition} using JVMCI types.
35+
*/
36+
public class JVMCIAccessCondition {
37+
38+
/**
39+
* @see AccessCondition#typeReached(Class)
40+
*/
41+
public static AccessCondition typeReached(ResolvedJavaType type) {
42+
return AccessCondition.typeReached(OriginalClassProvider.getJavaClass(type));
43+
}
44+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright (c) 2025, 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.svm.hosted.dynamicaccess;
26+
27+
import java.lang.reflect.Executable;
28+
import java.lang.reflect.Field;
29+
30+
import org.graalvm.nativeimage.dynamicaccess.AccessCondition;
31+
32+
import com.oracle.svm.hosted.JNIAccessImpl;
33+
import com.oracle.svm.util.OriginalClassProvider;
34+
import com.oracle.svm.util.OriginalFieldProvider;
35+
import com.oracle.svm.util.OriginalMethodProvider;
36+
37+
import jdk.vm.ci.meta.ResolvedJavaField;
38+
import jdk.vm.ci.meta.ResolvedJavaMethod;
39+
import jdk.vm.ci.meta.ResolvedJavaType;
40+
41+
/**
42+
* Mirror of {@link org.graalvm.nativeimage.dynamicaccess.JNIAccess} using JVMCI types.
43+
*/
44+
public final class JVMCIJNIAccess {
45+
46+
private final JNIAccessImpl jniInstance;
47+
private static JVMCIJNIAccess instance;
48+
49+
private JVMCIJNIAccess() {
50+
jniInstance = JNIAccessImpl.singleton();
51+
}
52+
53+
public static JVMCIJNIAccess singleton() {
54+
if (instance == null) {
55+
instance = new JVMCIJNIAccess();
56+
}
57+
return instance;
58+
}
59+
60+
/**
61+
* @see org.graalvm.nativeimage.dynamicaccess.JNIAccess#register(AccessCondition, Class...)
62+
*/
63+
public void register(AccessCondition condition, ResolvedJavaType... types) {
64+
for (ResolvedJavaType type : types) {
65+
jniInstance.register(condition, OriginalClassProvider.getJavaClass(type));
66+
}
67+
}
68+
69+
/**
70+
* @see org.graalvm.nativeimage.dynamicaccess.JNIAccess#register(AccessCondition, Executable...)
71+
*/
72+
public void register(AccessCondition condition, ResolvedJavaMethod... methods) {
73+
for (ResolvedJavaMethod method : methods) {
74+
jniInstance.register(condition, OriginalMethodProvider.getJavaMethod(method));
75+
}
76+
}
77+
78+
/**
79+
* @see org.graalvm.nativeimage.dynamicaccess.JNIAccess#register(AccessCondition, Field...)
80+
*/
81+
public void register(AccessCondition condition, ResolvedJavaField... fields) {
82+
for (ResolvedJavaField field : fields) {
83+
jniInstance.register(condition, OriginalFieldProvider.getJavaField(field));
84+
}
85+
}
86+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* Copyright (c) 2025, 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.svm.hosted.dynamicaccess;
26+
27+
import java.lang.reflect.Executable;
28+
import java.lang.reflect.Field;
29+
import java.util.ArrayList;
30+
import java.util.List;
31+
32+
import org.graalvm.nativeimage.dynamicaccess.AccessCondition;
33+
import org.graalvm.nativeimage.dynamicaccess.ReflectiveAccess;
34+
35+
import com.oracle.svm.hosted.ReflectiveAccessImpl;
36+
import com.oracle.svm.util.OriginalClassProvider;
37+
import com.oracle.svm.util.OriginalFieldProvider;
38+
import com.oracle.svm.util.OriginalMethodProvider;
39+
40+
import jdk.vm.ci.meta.ResolvedJavaField;
41+
import jdk.vm.ci.meta.ResolvedJavaMethod;
42+
import jdk.vm.ci.meta.ResolvedJavaType;
43+
44+
/**
45+
* Mirror of {@link ReflectiveAccess} using JVMCI types.
46+
*/
47+
public final class JVMCIReflectiveAccess {
48+
private final ReflectiveAccessImpl rdaInstance;
49+
private static JVMCIReflectiveAccess instance;
50+
51+
private JVMCIReflectiveAccess() {
52+
rdaInstance = ReflectiveAccessImpl.singleton();
53+
}
54+
55+
public static JVMCIReflectiveAccess singleton() {
56+
if (instance == null) {
57+
instance = new JVMCIReflectiveAccess();
58+
}
59+
return instance;
60+
}
61+
62+
/**
63+
* @see ReflectiveAccess#register(AccessCondition, Class...)
64+
*/
65+
public void register(AccessCondition condition, ResolvedJavaType... types) {
66+
for (ResolvedJavaType type : types) {
67+
rdaInstance.register(condition, OriginalClassProvider.getJavaClass(type));
68+
}
69+
}
70+
71+
/**
72+
* @see ReflectiveAccess#register(AccessCondition, Executable...)
73+
*/
74+
public void register(AccessCondition condition, ResolvedJavaMethod... methods) {
75+
for (ResolvedJavaMethod method : methods) {
76+
rdaInstance.register(condition, OriginalMethodProvider.getJavaMethod(method));
77+
}
78+
}
79+
80+
/**
81+
* @see ReflectiveAccess#register(AccessCondition, Field...)
82+
*/
83+
public void register(AccessCondition condition, ResolvedJavaField... fields) {
84+
for (ResolvedJavaField field : fields) {
85+
rdaInstance.register(condition, OriginalFieldProvider.getJavaField(field));
86+
}
87+
}
88+
89+
/**
90+
* @see ReflectiveAccess#registerForSerialization(AccessCondition, Class...)
91+
*/
92+
public void registerForSerialization(AccessCondition condition, ResolvedJavaType... types) {
93+
for (ResolvedJavaType type : types) {
94+
rdaInstance.registerForSerialization(condition, OriginalClassProvider.getJavaClass(type));
95+
}
96+
}
97+
98+
/**
99+
* @see ReflectiveAccess#registerProxy(AccessCondition, Class...)
100+
*/
101+
public Class<?> registerProxy(AccessCondition condition, ResolvedJavaType... interfaces) {
102+
List<Class<?>> reflectionInterfaces = new ArrayList<>();
103+
for (ResolvedJavaType intf : interfaces) {
104+
reflectionInterfaces.add(OriginalClassProvider.getJavaClass(intf));
105+
}
106+
return rdaInstance.registerProxy(condition, reflectionInterfaces.toArray(Class[]::new));
107+
}
108+
109+
/**
110+
* @see ReflectiveAccess#registerForUnsafeAllocation(AccessCondition, Class...)
111+
*/
112+
public void registerForUnsafeAllocation(AccessCondition condition, ResolvedJavaType... types) {
113+
for (ResolvedJavaType type : types) {
114+
rdaInstance.registerForUnsafeAllocation(condition, OriginalClassProvider.getJavaClass(type));
115+
}
116+
}
117+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (c) 2025, 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.svm.hosted.dynamicaccess;
26+
27+
import java.lang.reflect.Executable;
28+
import java.lang.reflect.Field;
29+
30+
import org.graalvm.nativeimage.hosted.RuntimeJNIAccess;
31+
32+
import com.oracle.svm.util.OriginalClassProvider;
33+
import com.oracle.svm.util.OriginalFieldProvider;
34+
import com.oracle.svm.util.OriginalMethodProvider;
35+
36+
import jdk.vm.ci.meta.ResolvedJavaField;
37+
import jdk.vm.ci.meta.ResolvedJavaMethod;
38+
import jdk.vm.ci.meta.ResolvedJavaType;
39+
40+
/**
41+
* Mirror of {@link RuntimeJNIAccess} using JVMCI types.
42+
* <p>
43+
* This API is deprecated; use {@link JVMCIJNIAccess} instead.
44+
*/
45+
public final class JVMCIRuntimeJNIAccess {
46+
/**
47+
* @see RuntimeJNIAccess#register(Class...)
48+
*/
49+
public static void register(ResolvedJavaType... types) {
50+
for (ResolvedJavaType type : types) {
51+
RuntimeJNIAccess.register(OriginalClassProvider.getJavaClass(type));
52+
}
53+
}
54+
55+
/**
56+
* @see RuntimeJNIAccess#register(Executable...)
57+
*/
58+
public static void register(ResolvedJavaMethod... methods) {
59+
for (ResolvedJavaMethod method : methods) {
60+
RuntimeJNIAccess.register(OriginalMethodProvider.getJavaMethod(method));
61+
}
62+
}
63+
64+
/**
65+
* @see RuntimeJNIAccess#register(Field...)
66+
*/
67+
public static void register(ResolvedJavaField... fields) {
68+
for (ResolvedJavaField field : fields) {
69+
RuntimeJNIAccess.register(OriginalFieldProvider.getJavaField(field));
70+
}
71+
}
72+
73+
private JVMCIRuntimeJNIAccess() {
74+
}
75+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (c) 2025, 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.svm.hosted.dynamicaccess;
26+
27+
import java.util.Arrays;
28+
29+
import org.graalvm.nativeimage.hosted.RuntimeProxyCreation;
30+
31+
import com.oracle.svm.util.OriginalClassProvider;
32+
33+
import jdk.vm.ci.meta.ResolvedJavaType;
34+
35+
/**
36+
* Mirror of {@link RuntimeProxyCreation} using JVMCI types.
37+
* <p>
38+
* This API is deprecated; use {@link JVMCIReflectiveAccess} instead.
39+
*/
40+
public final class JVMCIRuntimeProxyCreation {
41+
/**
42+
* @see RuntimeProxyCreation#register(Class...)
43+
*/
44+
public static void register(ResolvedJavaType... interfaces) {
45+
Class<?>[] reflectionInterfaces = Arrays.stream(interfaces).map(OriginalClassProvider::getJavaClass).toArray(Class[]::new);
46+
RuntimeProxyCreation.register(reflectionInterfaces);
47+
}
48+
49+
private JVMCIRuntimeProxyCreation() {
50+
}
51+
}

0 commit comments

Comments
 (0)