Skip to content

Commit 148e12e

Browse files
author
Mihailo Markovic
committed
DynamicAccess divided into two different API's, for resource registration and reflection registration. alwaysTrue now alwaysInclude.
1 parent 53372f7 commit 148e12e

36 files changed

+466
-342
lines changed

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

-127
This file was deleted.

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,9 @@ interface AfterRegistrationAccess extends FeatureAccess {
176176
* {@link Feature#afterRegistration}
177177
*
178178
*/
179-
DynamicAccess createDynamicAccess();
179+
ReflectionDynamicAccess createReflectionDynamicAccess();
180+
181+
ResourceDynamicAccess createResourceDynamicAccess();
180182
}
181183

182184
/**

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

+6-7
Original file line numberDiff line numberDiff line change
@@ -44,29 +44,28 @@
4444

4545
/**
4646
* Condition that needs to be satisfied for inclusion of elements for runtime access.
47-
* {@link InclusionCondition} is used with {@link DynamicAccess} for programmatic registration of
48-
* metadata.
47+
* {@link InclusionCondition} is used with {@link ReflectionDynamicAccess} and
48+
* {@link ResourceDynamicAccess} for programmatic registration of metadata.
4949
* <p>
50-
* Currently, there is only two types of condition:
50+
* Currently, there is only two types of condition: typeReached and typeReachable(legacy)
5151
* <li><code>typeReached</code> (the default) that signifies that the type must be both reachable by
5252
* static analysis at build time, and reached at run time. A type is reached at run time, right
5353
* before the class-initialization routine starts for that type, or any of the type's subtypes are
5454
* reached.</li>
5555
* <p>
56-
*
5756
* User can only create <code>typeReached</code> conditions, since <code>typeReachable</code> are
58-
* deprecated. Conditions are created via {@link InclusionCondition#alwaysTrue} and
57+
* deprecated. Conditions are created via {@link InclusionCondition#alwaysInclude} and
5958
* {@link InclusionCondition#typeReached} factory methods.
6059
*/
6160
public interface InclusionCondition {
6261

6362
/**
6463
* Creates the type-reached condition that is always satisfied. Any element that is predicated
6564
* with this condition will always be included.
66-
*
65+
*
6766
* @return instance of the condition
6867
*/
69-
static InclusionCondition alwaysTrue() {
68+
static InclusionCondition alwaysInclude() {
7069
return TypeCondition.JAVA_LANG_OBJECT_REACHED;
7170
}
7271

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package org.graalvm.nativeimage.hosted;
2+
3+
import java.lang.reflect.Executable;
4+
import java.lang.reflect.Field;
5+
6+
/**
7+
* This class is used to register classes, methods, fields for reflection, serialization and JNI
8+
* access at runtime.
9+
*
10+
* It should only be used at {@link Feature#afterRegistration}.
11+
*/
12+
public interface ReflectionDynamicAccess {
13+
14+
/**
15+
* Makes the provided classes available for reflection at runtime and all of their accessible
16+
* members available for reflection queries at run time. A call to {@link Class#forName} for the
17+
* names of the classes will return the classes at run time.
18+
*
19+
* @param condition needs to be satisfied for inclusion of types for reflection at runtime
20+
*/
21+
void register(InclusionCondition condition, Class<?>... classes);
22+
23+
/**
24+
* Makes the provided class available for reflection at runtime if {@code condition} is
25+
* satisfied. A call to {@link Class#forName} for the name of the class will return the class
26+
* (if it exists) or a {@link ClassNotFoundException} at run time.
27+
*/
28+
void registerClassLookup(InclusionCondition condition, String className);
29+
30+
/**
31+
* Makes the provided methods available for reflection at runtime if {@code condition} is
32+
* satisfied. The methods will be returned by {@link Class#getMethod},
33+
* {@link Class#getDeclaredMethod(String, Class[])}, and all the other methods on {@link Class}
34+
* that return a single method.
35+
*/
36+
void register(InclusionCondition condition, Executable... methods);
37+
38+
void register(InclusionCondition condition, Field... fields);
39+
40+
/**
41+
* Makes the provided classes available for serialization and reflection at runtime if
42+
* {@code condition} is satisfied.
43+
*/
44+
void registerForSerialization(InclusionCondition condition, Class<?>... classes);
45+
46+
/**
47+
* Makes the provided classes available for JNI access at runtime if {@code condition} is
48+
* satisfied. Needed when native code looks up Java classes via <a href=
49+
* "https://docs.oracle.com/en/java/javase/17/docs/specs/jni/functions.html#findclass">FindClass</a>.
50+
*/
51+
void registerForJNIAccess(InclusionCondition condition, Class<?>... classes);
52+
53+
/**
54+
* Makes the provided methods available for JNI access at runtime if {@code condition} is
55+
* satisfied. Needed when native code looks up Java methods via <a href=
56+
* "https://docs.oracle.com/en/java/javase/17/docs/specs/jni/functions.html#getmethodid">GetMethodID</a>
57+
* or <a href=
58+
* "https://docs.oracle.com/en/java/javase/17/docs/specs/jni/functions.html#getstaticmethodid">GetStaticMethodID</a>.
59+
*/
60+
void registerForJNIAccess(InclusionCondition condition, Executable... methods);
61+
62+
/**
63+
* Makes the provided methods available for JNI access at runtime if {@code condition} is
64+
* satisfied. Needed when native code looks up Java methods via <a href=
65+
* "https://docs.oracle.com/en/java/javase/17/docs/specs/jni/functions.html#getmethodid">GetMethodID</a>
66+
* or <a href=
67+
* "https://docs.oracle.com/en/java/javase/17/docs/specs/jni/functions.html#getstaticmethodid">GetStaticMethodID</a>.
68+
*/
69+
void registerForJNIAccess(InclusionCondition condition, Field... fields);
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.graalvm.nativeimage.hosted;
2+
3+
/**
4+
* This class is used to register Java resources and ResourceBundles that should be accessible at
5+
* runtime.
6+
*/
7+
public interface ResourceDynamicAccess {
8+
9+
/**
10+
* If {@code pattern} contains any wildcard patterns, such as star(*) or globstar(**), pattern
11+
* is treated as glob pattern from {@code module} and will be registered if {@code condition} is
12+
* satisfied. All resources that match the glob are available at runtime. Otherwise pattern
13+
* represents Java resource from {@code module} that will be available at run time if
14+
* {@code condition} is satisfied.
15+
*/
16+
void register(InclusionCondition condition, Module module, String pattern);
17+
18+
default void register(InclusionCondition condition, String pattern) {
19+
register(condition, null, pattern);
20+
}
21+
22+
/**
23+
* Make Java ResourceBundle that is specified by a {@code bundleName} from module {@code module}
24+
* available at run time if {@code condition} is satisfied. If the given {@code module} is
25+
* unnamed, the ResourceBundle is looked up on the classpath instead.
26+
*/
27+
void registerResourceBundle(InclusionCondition condition, Module module, String bundleName);
28+
29+
default void registerResourceBundle(InclusionCondition condition, String bundleName) {
30+
registerResourceBundle(condition, null, bundleName);
31+
}
32+
33+
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public final class RuntimeForeignAccess {
6767
* @since 23.1
6868
*/
6969
public static void registerForDowncall(Object desc, Object... options) {
70-
ImageSingletons.lookup(RuntimeForeignAccessSupport.class).registerForDowncall(InclusionCondition.alwaysTrue(), desc, options);
70+
ImageSingletons.lookup(RuntimeForeignAccessSupport.class).registerForDowncall(InclusionCondition.alwaysInclude(), desc, options);
7171
}
7272

7373
/**
@@ -85,7 +85,7 @@ public static void registerForDowncall(Object desc, Object... options) {
8585
* @since 24.1
8686
*/
8787
public static void registerForUpcall(Object desc, Object... options) {
88-
ImageSingletons.lookup(RuntimeForeignAccessSupport.class).registerForUpcall(InclusionCondition.alwaysTrue(), desc, options);
88+
ImageSingletons.lookup(RuntimeForeignAccessSupport.class).registerForUpcall(InclusionCondition.alwaysInclude(), desc, options);
8989
}
9090

9191
/**
@@ -113,7 +113,7 @@ public static void registerForUpcall(Object desc, Object... options) {
113113
* @since 24.2
114114
*/
115115
public static void registerForDirectUpcall(MethodHandle target, Object desc, Object... options) {
116-
ImageSingletons.lookup(RuntimeForeignAccessSupport.class).registerForDirectUpcall(InclusionCondition.alwaysTrue(), target, desc, options);
116+
ImageSingletons.lookup(RuntimeForeignAccessSupport.class).registerForDirectUpcall(InclusionCondition.alwaysInclude(), target, desc, options);
117117
}
118118

119119
private RuntimeForeignAccess() {

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public final class RuntimeJNIAccess {
6565
* @since 22.3
6666
*/
6767
public static void register(Class<?>... classes) {
68-
ImageSingletons.lookup(RuntimeJNIAccessSupport.class).register(InclusionCondition.alwaysTrue(), classes);
68+
ImageSingletons.lookup(RuntimeJNIAccessSupport.class).register(InclusionCondition.alwaysInclude(), classes);
6969
}
7070

7171
/**
@@ -78,7 +78,7 @@ public static void register(Class<?>... classes) {
7878
* @since 22.3
7979
*/
8080
public static void register(Executable... methods) {
81-
ImageSingletons.lookup(RuntimeJNIAccessSupport.class).register(InclusionCondition.alwaysTrue(), false, methods);
81+
ImageSingletons.lookup(RuntimeJNIAccessSupport.class).register(InclusionCondition.alwaysInclude(), false, methods);
8282
}
8383

8484
/**
@@ -91,7 +91,7 @@ public static void register(Executable... methods) {
9191
* @since 22.3
9292
*/
9393
public static void register(Field... fields) {
94-
ImageSingletons.lookup(RuntimeJNIAccessSupport.class).register(InclusionCondition.alwaysTrue(), false, fields);
94+
ImageSingletons.lookup(RuntimeJNIAccessSupport.class).register(InclusionCondition.alwaysInclude(), false, fields);
9595
}
9696

9797
private RuntimeJNIAccess() {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public final class RuntimeProxyCreation {
6161
* @since 22.3
6262
*/
6363
public static void register(Class<?>... interfaces) {
64-
ImageSingletons.lookup(RuntimeProxyCreationSupport.class).addProxyClass(InclusionCondition.alwaysTrue(), interfaces);
64+
ImageSingletons.lookup(RuntimeProxyCreationSupport.class).addProxyClass(InclusionCondition.alwaysInclude(), interfaces);
6565
}
6666

6767
private RuntimeProxyCreation() {

0 commit comments

Comments
 (0)