Skip to content

Commit 02f862f

Browse files
committed
Review comments
1 parent e8e79ed commit 02f862f

File tree

7 files changed

+102
-81
lines changed

7 files changed

+102
-81
lines changed

sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/dynamicaccess/AccessCondition.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444

4545
/**
4646
* A condition that must be satisfied to register elements for dynamic access (i.e., reflection,
47-
* serialization, JNI access, resource access, and foreign access at runtime).
47+
* serialization, JNI access, resource access, and foreign access at run time).
4848
* {@link AccessCondition} is used for programmatic metadata registration in conjunction with:
4949
* <ul>
5050
* <li>{@link ReflectiveAccess}</li>
@@ -63,7 +63,12 @@
6363
* </ul>
6464
* <p>
6565
* Conditions can only be created via the {@link #unconditional} and {@link #typeReached} factory
66-
* methods.
66+
* methods. These methods are best used with static import methods. For example:
67+
*
68+
* <pre>{@code
69+
* reflection.register(unconditional(), ReflectivelyAccessed.class);
70+
* reflection.register(typeReached(ConditionType.class), ConditionallyAccessed.class)
71+
* }</pre>
6772
*
6873
* @since 25.0
6974
*/
@@ -82,46 +87,47 @@ static AccessCondition unconditional() {
8287
}
8388

8489
/**
85-
* Creates the {@code typeReached} condition that is satisfied when the type is reached at
86-
* runtime. A type is reached at runtime, right before the class-initialization routine starts
87-
* for that type, or any of the type's subtypes are reached. Metadata predicated with this
88-
* condition is only included if the condition is satisfied.
90+
* Creates the {@code typeReached} condition that is satisfied when the type is reached at run
91+
* time. A type is reached at run time, right before the class-initialization routine starts for
92+
* that type, or any of the type's subtypes are reached. Elements predicated with this condition
93+
* will be included in the image only if the type is <em>reachable</em> at build time, but will
94+
* be accessible when the type is <em>reached</em> at run time.
8995
* <p>
9096
* <strong>Example:</strong>
9197
*
9298
* <pre>{@code
9399
* class SuperType {
94100
* static {
95-
* // ConditionType reached (subtype reached) => metadata is available
101+
* // ConditionType reached (subtype reached) => element access allowed
96102
* }
97103
* }
98104
*
99105
* class ConditionType extends SuperType {
100106
* static {
101-
* // ConditionType reached (before static initializer) => metadata is available
107+
* // ConditionType reached (before static initializer) => element access allowed
102108
* }
103109
*
104110
* static ConditionType singleton() {
105-
* // ConditionType reached (already initialized) => metadata is available
111+
* // ConditionType reached (already initialized) => element access allowed
106112
* }
107113
* }
108114
*
109115
* public class App {
110116
* public static void main(String[] args) {
111-
* // ConditionType not reached => metadata is not available
117+
* // ConditionType not reached => element access not allowed
112118
* Class<?> clazz = ConditionType.class;
113119
* // ConditionType not reached (ConditionType.class doesn't start class initialization)
114-
* // => metadata is not available
120+
* // => element access not allowed
115121
* ConditionType.singleton();
116-
* // ConditionType reached (already initialized) => metadata is available
122+
* // ConditionType reached (already initialized) => element access allowed
117123
* }
118124
* }
119125
* }</pre>
120126
* <p>
121127
* Type is also reached, if it is marked as {@code --initialize-at-build-time} or any of its
122128
* subtypes are marked as {@code --initialize-at-build-time} and they exist on the classpath.
123-
* Array types are never marked as reached and therefore cannot be used as the type in a
124-
* condition.
129+
* Array types (e.g., <code>int[]</code>) are never marked as reached and therefore cannot be
130+
* used as the <code>type</code> in a condition.
125131
*
126132
* @param type the type that has to be reached for this condition to be satisfied, must not be
127133
* {@code null}

sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/dynamicaccess/ForeignAccess.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
import java.lang.invoke.MethodType;
4747

4848
/**
49-
* This interface is used to register classes, methods, and fields for foreign access at runtime. An
49+
* This interface is used to register classes, methods, and fields for foreign access at run time. An
5050
* instance of this interface is acquired via
5151
* {@link Feature.AfterRegistrationAccess#getForeignAccess()}.
5252
* <p>
@@ -76,9 +76,9 @@ public interface ForeignAccess {
7676
* Registers the provided function descriptor and options pair at image build time for downcalls
7777
* into foreign code, if the {@code condition} is satisfied. Required to get a downcall method
7878
* handle using {@link java.lang.foreign.Linker#downcallHandle} for the same descriptor and
79-
* options at runtime.
79+
* options at run time.
8080
* <p>
81-
* Even though this method is weakly typed for compatibility reasons, runtime checks will be
81+
* Even though this method is weakly typed for compatibility reasons, run-time checks will be
8282
* performed to ensure that the arguments have the expected type. It will be deprecated in favor
8383
* of strongly typed variant as soon as possible.
8484
*
@@ -95,9 +95,9 @@ public interface ForeignAccess {
9595
* Registers the provided function descriptor and options pair at image build time for upcalls
9696
* from foreign code, if the {@code condition} is satisfied. Required to get an upcall stub
9797
* function pointer using {@link java.lang.foreign.Linker#upcallStub} for the same descriptor
98-
* and options at runtime.
98+
* and options at run time.
9999
* <p>
100-
* Even though this method is weakly typed for compatibility reasons, runtime checks will be
100+
* Even though this method is weakly typed for compatibility reasons, run-time checks will be
101101
* performed to ensure that the arguments have the expected type. It will be deprecated in favor
102102
* of strongly typed variant as soon as possible.
103103
*
@@ -124,7 +124,7 @@ public interface ForeignAccess {
124124
* method handle to denoted static method.
125125
* </p>
126126
* <p>
127-
* Even though this method is weakly typed for compatibility reasons, runtime checks will be
127+
* Even though this method is weakly typed for compatibility reasons, run-time checks will be
128128
* performed to ensure that the arguments have the expected type. It will be deprecated in favor
129129
* of strongly typed variant as soon as possible.
130130
* </p>

sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/dynamicaccess/JNIAccess.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,23 +78,23 @@
7878
*/
7979
public interface JNIAccess {
8080
/**
81-
* Registers the provided classes for JNI access at runtime, if the {@code condition} is
81+
* Registers the provided classes for JNI access at run time, if the {@code condition} is
8282
* satisfied.
8383
*
8484
* @since 25.0
8585
*/
8686
void register(AccessCondition condition, Class<?>... classes);
8787

8888
/**
89-
* Registers the provided methods for JNI access at runtime, if the {@code condition} is
89+
* Registers the provided methods for JNI access at run time, if the {@code condition} is
9090
* satisfied.
9191
*
9292
* @since 25.0
9393
*/
9494
void register(AccessCondition condition, Executable... methods);
9595

9696
/**
97-
* Registers the provided fields for JNI access at runtime, if the {@code condition} is
97+
* Registers the provided fields for JNI access at run time, if the {@code condition} is
9898
* satisfied.
9999
*
100100
* @since 25.0

sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/dynamicaccess/ReflectiveAccess.java

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -40,34 +40,38 @@
4040
*/
4141
package org.graalvm.nativeimage.dynamicaccess;
4242

43-
import org.graalvm.nativeimage.hosted.Feature;
44-
4543
import java.lang.reflect.Executable;
4644
import java.lang.reflect.Field;
4745

46+
import org.graalvm.nativeimage.hosted.Feature;
47+
4848
/**
4949
* This interface is used to register classes, methods, and fields for use with the
50-
* {@link java.lang.reflect} API at runtime, and for serialization at runtime. An instance of this
50+
* {@link java.lang.reflect} API at run time, and for serialization at run time. An instance of this
5151
* interface is acquired via {@link Feature.AfterRegistrationAccess#getReflectiveAccess()}.
5252
* <p>
5353
* All methods in {@link ReflectiveAccess} require a {@link AccessCondition} as their first
54-
* parameter. A class and its members will be registered for dynamic access only if the specified
55-
* condition is satisfied.
54+
* parameter. A class and its members will be accessible at run-time only if the specified condition
55+
* is satisfied.
5656
*
5757
* <h3>How to use</h3>
5858
*
59-
* {@link ReflectiveAccess} should only be used during {@link Feature#afterRegistration}. Any
60-
* attempt to register metadata in any other phase will result in an error.
59+
* {@link ReflectiveAccess} should only be used during {@link Feature#afterRegistration}. Attempts
60+
* to register metadata in any other phase of the {@link Feature} API will result in an error.
6161
* <p>
6262
* <strong>Example:</strong>
6363
*
6464
* <pre>{@code @Override
6565
* public void afterRegistration(AfterRegistrationAccess access) {
6666
* ReflectionAccess reflection = access.getReflectionAccess();
67+
*
6768
* AccessCondition condition = AccessCondition.typeReached(ConditionType.class);
6869
* reflection.register(condition, Foo.class, Bar.class);
70+
*
6971
* reflection.register(AccessCondition.unconditional(), Foo.class.getMethod("method"));
72+
*
7073
* reflection.registerUnsafeAllocation(condition, Foo.class);
74+
*
7175
* Class<?> proxyClass = reflection.registerProxy(condition, Interface1.class, Interface2.class);
7276
* reflection.registerForSerialization(AccessCondition.unconditional(), proxyClass);
7377
* }
@@ -78,19 +82,25 @@
7882
public interface ReflectiveAccess {
7983

8084
/**
81-
* Registers the provided classes for reflection at runtime, if the {@code condition} is
82-
* satisfied. This means all reflection methods defined by the {@link java.lang.Class} are
83-
* accessible at runtime for those classes.
85+
* Registers the provided classes for reflection at run time, if the {@code condition} is
86+
* satisfied. This means all reflection methods defined by the {@link java.lang.Class} (except
87+
* {@link Class#arrayType()}) are accessible at run time for those classes.
8488
* <p>
85-
* If a class is not registered for reflection at runtime, {@link Class#forName} will throw
86-
* {@link ClassNotFoundException}.
89+
* If a class is not registered for reflection at run time, the following methods will throw
90+
* {@link ClassNotFoundException}:
91+
* <ul>
92+
* <li>{@link Class#forName(String)}</li>
93+
* <li>{@link Class#forName(Module, String)}</li>
94+
* <li>{@link Class#forName(String, boolean, ClassLoader)}</li>
95+
* <li>{@link ClassLoader#loadClass(String)}</li>
96+
* </ul>
8797
*
8898
* @since 25.0
8999
*/
90100
void register(AccessCondition condition, Class<?>... classes);
91101

92102
/**
93-
* Registers a class with the provided {@code className} for reflection at runtime, if the
103+
* Registers a class with the provided {@code className} for reflection at run time, if the
94104
* {@code condition} is satisfied. This method should be used when
95105
* {@code --exact-reachability-metadata} is set: it makes calls to
96106
* {@code Class.forName(className)} throw {@link ClassNotFoundException} instead of throwing
@@ -103,29 +113,20 @@ public interface ReflectiveAccess {
103113
void registerClassLookup(AccessCondition condition, String className);
104114

105115
/**
106-
* Registers the provided {@code classes} for unsafe allocation at runtime, if the
107-
* {@code condition} is satisfied. Unsafe allocation can happen via
108-
* {@link sun.misc.Unsafe#allocateInstance(Class)} or from native code via
109-
* {@code AllocObject(jClass)}.
110-
*
111-
* @since 25.0
112-
*/
113-
void registerUnsafeAllocation(AccessCondition condition, Class<?>... classes);
114-
115-
/**
116-
* Registers the provided {@code methods} for reflective invocation at runtime, if the
117-
* {@code condition} is satisfied. This method also registers the declaring classes of the
118-
* provided methods for reflection at runtime. The methods will be invocable at runtime via
116+
* Registers the provided {@code executables} (methods and constructors) for reflective
117+
* invocation at run time, if the {@code condition} is satisfied. This method also registers the
118+
* declaring classes of the provided executables for reflection at run time. The executables
119+
* will be invocable at run time via
119120
* {@link java.lang.reflect.Method#invoke(java.lang.Object, java.lang.Object...)}.
120121
*
121122
* @since 25.0
122123
*/
123-
void register(AccessCondition condition, Executable... methods);
124+
void register(AccessCondition condition, Executable... executables);
124125

125126
/**
126-
* Registers the provided {@code fields} for reflective access at runtime, if the
127+
* Registers the provided {@code fields} for reflective access at run time, if the
127128
* {@code condition} is satisfied. This method also registers the declaring classes of the
128-
* provided fields for reflection at runtime. The fields will be accessible at runtime via
129+
* provided fields for reflection at run time. The fields will be accessible at run time via
129130
* {@link java.lang.reflect.Field#set(java.lang.Object, java.lang.Object)} and
130131
* {@link java.lang.reflect.Field#get(Object)}.
131132
*
@@ -134,8 +135,8 @@ public interface ReflectiveAccess {
134135
void register(AccessCondition condition, Field... fields);
135136

136137
/**
137-
* Registers the provided classes for serialization at runtime, if the {@code condition} is
138-
* satisfied. This method also registers the provided classes for reflection at runtime.
138+
* Registers the provided classes for serialization at run time, if the {@code condition} is
139+
* satisfied. This method also registers the provided classes for reflection at run time.
139140
*
140141
* @since 25.0
141142
*/
@@ -145,13 +146,14 @@ public interface ReflectiveAccess {
145146
* Registers a {@link java.lang.reflect.Proxy} class in the system classloader that implements
146147
* the specified {@code interfaces}, if the {@code condition} is satisfied. The proxy class is
147148
* fully specified by the interfaces it implements, and proxy instances matching that
148-
* specification can be created at runtime. The returned proxy class can be used in registration
149-
* for reflection and serialization at runtime. <blockquote> <strong>NOTE</strong>: The order of
150-
* the interfaces provided in the {@code interfaces} parameter is significant; different
151-
* orderings will produce distinct proxy classes. </blockquote>
149+
* specification can be created at run time. The returned proxy class can be used in
150+
* registration for reflection and serialization at run time. <blockquote>
151+
* <strong>NOTE</strong>: The order of the interfaces provided in the {@code interfaces}
152+
* parameter is significant; different orderings will produce distinct proxy classes.
153+
* </blockquote>
152154
* <p>
153155
* <strong>Example</strong>:
154-
*
156+
*
155157
* <pre>{@code
156158
* Class<?> proxyClass = reflection.registerProxy(AccessCondition.unconditional(), Interface1.class, Interface2.class);
157159
* reflection.register(AccessCondition.unconditional(), proxyClass);
@@ -164,4 +166,14 @@ public interface ReflectiveAccess {
164166
* @since 25.0
165167
*/
166168
Class<?> registerProxy(AccessCondition condition, Class<?>... interfaces);
169+
170+
/**
171+
* Registers the provided {@code classes} for unsafe allocation at run time, if the
172+
* {@code condition} is satisfied. Unsafe allocation can happen via
173+
* {@link sun.misc.Unsafe#allocateInstance(Class)} or from native code via
174+
* {@code AllocObject(jClass)}.
175+
*
176+
* @since 25.0
177+
*/
178+
void registerForUnsafeAllocation(AccessCondition condition, Class<?>... classes);
167179
}

sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/dynamicaccess/ResourceAccess.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,33 +40,37 @@
4040
*/
4141
package org.graalvm.nativeimage.dynamicaccess;
4242

43-
import org.graalvm.nativeimage.hosted.Feature;
44-
4543
import java.util.ResourceBundle;
4644

45+
import org.graalvm.nativeimage.hosted.Feature;
46+
4747
/**
4848
* This interface is used to register Java resources and {@link java.util.ResourceBundle}s that
49-
* should be accessible at runtime. An instance of this interface is acquired via
49+
* should be accessible at run time. An instance of this interface is acquired via
5050
* {@link Feature.AfterRegistrationAccess#getResourceAccess()}.
5151
* <p>
5252
* All methods in {@link ResourceAccess} require a {@link AccessCondition} as their first parameter.
53-
* Resources will be registered for runtime access only if the specified condition is satisfied.
53+
* Resources will be registered for run-time access only if the specified <code>condition</code> is
54+
* satisfied.
5455
*
5556
* <h3>How to use</h3>
5657
*
57-
* {@link ResourceAccess} should only be used during {@link Feature#afterRegistration}. Any attempt
58-
* to register metadata in any other phase will result in an error.
58+
* {@link ResourceAccess} should only be used during {@link Feature#afterRegistration}. Attempts to
59+
* register metadata in any other phase of the {@link Feature} API will result in an error.
5960
* <p>
6061
* <strong>Example:</strong>
6162
*
62-
* <pre>{@code
63-
* @Override
63+
* <pre>{@code @Override
6464
* public void afterRegistration(AfterRegistrationAccess access) {
6565
* ResourceAccess resources = access.getResourceAccess();
6666
* AccessCondition condition = AccessCondition.typeReached(ConditionType.class);
67+
*
6768
* resources.register(condition, "example/**");
69+
*
6870
* resources.register(AccessCondition.unconditional(), "example/directory/concreteResource");
71+
*
6972
* resources.register(condition, "example/concreteResourceStar\\*");
73+
*
7074
* try {
7175
* ResourceBundle bundle2 = ResourceBundle.getBundle("bundlePath1");
7276
* resources.registerResourceBundle(condition, bundle1, bundle2);
@@ -82,7 +86,7 @@ public interface ResourceAccess {
8286

8387
/**
8488
* Registers resources matching the specified {@code glob} in the provided {@code module} for
85-
* runtime access, if the {@code condition} is satisfied.
89+
* run-time access, if the {@code condition} is satisfied.
8690
* <p>
8791
* The {@code glob} uses a subset of
8892
* <a href="https://en.wikipedia.org/wiki/Glob_(programming)">glob-pattern</a> rules for
@@ -142,7 +146,7 @@ public interface ResourceAccess {
142146
void register(AccessCondition condition, Module module, String glob);
143147

144148
/**
145-
* Registers resources matching the specified {@code glob} from the classpath for runtime
149+
* Registers resources matching the specified {@code glob} from the classpath for run-time
146150
* access, if the {@code condition} is satisfied.
147151
* <p>
148152
* The {@code glob} uses a subset of
@@ -202,15 +206,14 @@ default void register(AccessCondition condition, String glob) {
202206
}
203207

204208
/**
205-
* Registers the provided {@link java.util.ResourceBundle}s for runtime access, if the
209+
* Registers the provided {@link java.util.ResourceBundle}s for run-time access, if the
206210
* {@code condition} is satisfied. Each {@link java.util.ResourceBundle} must be obtained using
207211
* any version of the {@link java.util.ResourceBundle#getBundle} method. A bundle created in any
208212
* other way will result in an error.
209213
*
210214
* <blockquote> <strong>Recommended use to avoid crashes during image building:</strong>
211215
*
212-
* <pre>{@code
213-
* @Override
216+
* <pre>{@code @Override
214217
* public void afterRegistration(AfterRegistrationAccess access) {
215218
* ResourceAccess resources = access.getResourceAccess();
216219
* AccessCondition condition = AccessCondition.typeReached(ConditionType.class);

0 commit comments

Comments
 (0)