Skip to content

Commit 4115f50

Browse files
committed
[GR-40198] Provide public API for feature-based JNI / Resource / Proxy / Serialization registration.
PullRequest: graal/12395
2 parents 5869534 + 779ce73 commit 4115f50

File tree

33 files changed

+720
-251
lines changed

33 files changed

+720
-251
lines changed

sdk/src/org.graalvm.nativeimage/snapshot.sigtest

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,10 +997,28 @@ meth public !varargs static void registerAsQueried(java.lang.reflect.Executable[
997997
meth public !varargs static void registerForReflectiveInstantiation(java.lang.Class<?>[])
998998
supr java.lang.Object
999999

1000+
CLSS public final org.graalvm.nativeimage.hosted.RuntimeJNIAccess
1001+
meth public !varargs static void register(java.lang.Class<?>[])
1002+
meth public !varargs static void register(java.lang.reflect.Executable[])
1003+
meth public !varargs static void register(java.lang.reflect.Field[])
1004+
supr java.lang.Object
1005+
10001006
CLSS public final org.graalvm.nativeimage.hosted.RuntimeSerialization
10011007
meth public !varargs static void register(java.lang.Class<?>[])
10021008
meth public static void registerIncludingAssociatedClasses(java.lang.Class<?>)
10031009
meth public static void registerWithTargetConstructorClass(java.lang.Class<?>,java.lang.Class<?>)
1010+
meth public static void registerLambdaCapturingClass(java.lang.Class<?>)
1011+
meth public !varargs static void registerProxyClass(java.lang.Class<?>[])
1012+
supr java.lang.Object
1013+
1014+
CLSS public final org.graalvm.nativeimage.hosted.RuntimeProxyCreation
1015+
meth public !varargs static void register(java.lang.Class<?>[])
1016+
supr java.lang.Object
1017+
1018+
CLSS public final org.graalvm.nativeimage.hosted.RuntimeResourceAccess
1019+
meth public static void addResource(java.lang.Module,java.lang.String)
1020+
meth public static void addResourceBundle(java.lang.Module,java.lang.String,java.util.Locale[])
1021+
meth public static void addResourceBundle(java.lang.Module,java.lang.String)
10041022
supr java.lang.Object
10051023

10061024
CLSS public abstract interface org.graalvm.nativeimage.impl.InternalPlatform
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package org.graalvm.nativeimage.hosted;
42+
43+
import java.lang.reflect.Executable;
44+
import java.lang.reflect.Field;
45+
46+
import org.graalvm.nativeimage.ImageSingletons;
47+
import org.graalvm.nativeimage.Platform;
48+
import org.graalvm.nativeimage.Platforms;
49+
import org.graalvm.nativeimage.impl.ConfigurationCondition;
50+
import org.graalvm.nativeimage.impl.RuntimeJNIAccessSupport;
51+
52+
/**
53+
* This class provides methods that can be called during native image generation to register
54+
* classes, methods, and fields for JNI access at run time.
55+
*
56+
* @since 22.3
57+
*/
58+
@Platforms(Platform.HOSTED_ONLY.class)
59+
public final class RuntimeJNIAccess {
60+
61+
/**
62+
* Makes the provided classes available for JNI access at run time. Needed when native code
63+
* looks up Java classes via <a href=
64+
* "https://docs.oracle.com/en/java/javase/17/docs/specs/jni/functions.html#findclass">FindClass</a>.
65+
*
66+
* @since 22.3
67+
*/
68+
public static void register(Class<?>... classes) {
69+
ImageSingletons.lookup(RuntimeJNIAccessSupport.class).register(ConfigurationCondition.alwaysTrue(), classes);
70+
}
71+
72+
/**
73+
* Makes the provided methods available for JNI access at run time. Needed when native code
74+
* looks up Java methods via <a href=
75+
* "https://docs.oracle.com/en/java/javase/17/docs/specs/jni/functions.html#getmethodid">GetMethodID</a>
76+
* or <a href=
77+
* "https://docs.oracle.com/en/java/javase/17/docs/specs/jni/functions.html#getstaticmethodid">GetStaticMethodID</a>.
78+
*
79+
* @since 22.3
80+
*/
81+
public static void register(Executable... methods) {
82+
ImageSingletons.lookup(RuntimeJNIAccessSupport.class).register(ConfigurationCondition.alwaysTrue(), false, methods);
83+
}
84+
85+
/**
86+
* Makes the provided fields available for JNI access at run time. Needed when native code looks
87+
* up Java fields via <a href=
88+
* "https://docs.oracle.com/en/java/javase/17/docs/specs/jni/functions.html#getfieldid">GetFieldID</a>
89+
* or <a href=
90+
* "https://docs.oracle.com/en/java/javase/17/docs/specs/jni/functions.html#getstaticfieldid">GetStaticFieldID</a>.
91+
*
92+
* @since 22.3
93+
*/
94+
public static void register(Field... fields) {
95+
ImageSingletons.lookup(RuntimeJNIAccessSupport.class).register(ConfigurationCondition.alwaysTrue(), false, fields);
96+
}
97+
98+
private RuntimeJNIAccess() {
99+
}
100+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package org.graalvm.nativeimage.hosted;
42+
43+
import org.graalvm.nativeimage.ImageSingletons;
44+
import org.graalvm.nativeimage.Platform;
45+
import org.graalvm.nativeimage.Platforms;
46+
import org.graalvm.nativeimage.impl.RuntimeProxyCreationSupport;
47+
48+
/**
49+
* This class can be used to make creating dynamic proxy classes at run time valid.
50+
*
51+
* @since 22.3
52+
*/
53+
@Platforms(Platform.HOSTED_ONLY.class)
54+
public final class RuntimeProxyCreation {
55+
56+
/**
57+
* Enables registering specifications of {@link java.lang.reflect.Proxy} classes during the
58+
* image build so that matching proxy objects can be created at runtime. The proxy class is
59+
* fully specified by the interfaces it implements.
60+
*
61+
* @since 22.3
62+
*/
63+
public static void register(Class<?>... interfaces) {
64+
ImageSingletons.lookup(RuntimeProxyCreationSupport.class).addProxyClass(interfaces);
65+
}
66+
67+
private RuntimeProxyCreation() {
68+
}
69+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package org.graalvm.nativeimage.hosted;
42+
43+
import java.util.Arrays;
44+
import java.util.Locale;
45+
import java.util.Objects;
46+
import java.util.regex.Pattern;
47+
48+
import org.graalvm.nativeimage.ImageSingletons;
49+
import org.graalvm.nativeimage.Platform;
50+
import org.graalvm.nativeimage.Platforms;
51+
import org.graalvm.nativeimage.impl.ConfigurationCondition;
52+
import org.graalvm.nativeimage.impl.RuntimeResourceSupport;
53+
54+
/**
55+
* This class can be used to register Java resources and ResourceBundles that should be accessible
56+
* at run time.
57+
*
58+
* @since 22.3
59+
*/
60+
@Platforms(Platform.HOSTED_ONLY.class)
61+
public final class RuntimeResourceAccess {
62+
63+
/**
64+
* Make Java resource {@code resourcePath} from {@code module} available at run time. If the
65+
* given {@code module} is unnamed, the resource is looked up on the classpath instead.
66+
*
67+
* @since 22.3
68+
*/
69+
public static void addResource(Module module, String resourcePath) {
70+
ImageSingletons.lookup(RuntimeResourceSupport.class).addResources(ConfigurationCondition.alwaysTrue(),
71+
withModuleName(module, Pattern.quote(resourcePath)));
72+
}
73+
74+
/**
75+
* Make Java ResourceBundle that is specified by a {@code baseBundleName} and {@code locales}
76+
* from module {@code module} available at run time. If the given {@code module} is unnamed, the
77+
* ResourceBundle is looked up on the classpath instead.
78+
*
79+
* @since 22.3
80+
*/
81+
public static void addResourceBundle(Module module, String baseBundleName, Locale[] locales) {
82+
Objects.requireNonNull(locales);
83+
ImageSingletons.lookup(RuntimeResourceSupport.class).addResourceBundles(ConfigurationCondition.alwaysTrue(),
84+
withModuleName(module, baseBundleName), Arrays.asList(locales));
85+
}
86+
87+
/**
88+
* Make Java ResourceBundle that is specified by a {@code bundleName} from module {@code module}
89+
* available at run time. If the given {@code module} is unnamed, the ResourceBundle is looked
90+
* up on the classpath instead.
91+
*
92+
* @since 22.3
93+
*/
94+
public static void addResourceBundle(Module module, String bundleName) {
95+
ImageSingletons.lookup(RuntimeResourceSupport.class).addResourceBundles(ConfigurationCondition.alwaysTrue(),
96+
withModuleName(module, bundleName));
97+
}
98+
99+
private static String withModuleName(Module module, String str) {
100+
Objects.requireNonNull(module);
101+
Objects.requireNonNull(str);
102+
return (module.isNamed() ? module.getName() : "ALL-UNNAMED") + ":" + str;
103+
}
104+
105+
private RuntimeResourceAccess() {
106+
}
107+
}

sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/hosted/RuntimeSerialization.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,28 @@ public static void registerWithTargetConstructorClass(Class<?> clazz, Class<?> c
100100
ImageSingletons.lookup(RuntimeSerializationSupport.class).registerWithTargetConstructorClass(ConfigurationCondition.alwaysTrue(), clazz, customTargetConstructorClazz);
101101
}
102102

103+
/**
104+
* Makes a class available for serialization at runtime that is created for the lambda
105+
* expressions (a class that has a $deserializeLambda$ method) specified by the
106+
* lambdaCapturingClass.
107+
*
108+
* @since 22.3
109+
*/
110+
public static void registerLambdaCapturingClass(Class<?> lambdaCapturingClass) {
111+
ImageSingletons.lookup(RuntimeSerializationSupport.class).registerLambdaCapturingClass(ConfigurationCondition.alwaysTrue(), lambdaCapturingClass);
112+
}
113+
114+
/**
115+
* Makes a dynamic proxy class (class that extends {@link java.lang.reflect.Proxy}) available
116+
* for serialization at runtime that is specified by the given interfaces the proxy class
117+
* implements.
118+
*
119+
* @since 22.3
120+
*/
121+
public static void registerProxyClass(Class<?>... implementedInterfaces) {
122+
ImageSingletons.lookup(RuntimeSerializationSupport.class).registerProxyClass(ConfigurationCondition.alwaysTrue(), implementedInterfaces);
123+
}
124+
103125
private RuntimeSerialization() {
104126
}
105127
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package org.graalvm.nativeimage.impl;
42+
43+
public interface RuntimeJNIAccessSupport extends ReflectionRegistry {
44+
// needed as JNI-specific ImageSingletons key
45+
}

0 commit comments

Comments
 (0)