Skip to content

[WIP][GR-62940] Introduce new API for metadata registration #11075

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@ interface IsInConfigurationAccess extends FeatureAccess {
*/
@Platforms(Platform.HOSTED_ONLY.class)
interface AfterRegistrationAccess extends FeatureAccess {
/**
* Creates access for runtime registration. All registrations should happen in
* {@link Feature#afterRegistration}
*
*/
ReflectionDynamicAccess createReflectionDynamicAccess();

ResourceDynamicAccess createResourceDynamicAccess();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or
* data (collectively the "Software"), free of charge and under any and all
* copyright rights in the Software, and any and all patent rights owned or
* freely licensable by each licensor hereunder covering either (i) the
* unmodified Software as contributed to or provided by such licensor, or (ii)
* the Larger Works (as defined below), to deal in both
*
* (a) the Software, and
*
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
*
* The above copyright notice and either this complete permission notice or at a
* minimum a reference to the UPL must be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.graalvm.nativeimage.hosted;

import org.graalvm.nativeimage.impl.TypeCondition;

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

/**
* Creates the type-reached condition that is always satisfied. Any element that is predicated
* with this condition will always be included.
*
* @return instance of the condition
*/
static InclusionCondition alwaysInclude() {
return TypeCondition.JAVA_LANG_OBJECT_REACHED;
}

/**
* Creates the type-reached condition that is satisfied when the type is reached at runtime.
*
* @param type that has to be reached for this condition to be satisfied
* @return instance of the condition
*/
static InclusionCondition typeReached(Class<?> type) {
return TypeCondition.create(type, true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package org.graalvm.nativeimage.hosted;

import java.lang.reflect.Executable;
import java.lang.reflect.Field;

/**
* This class is used to register classes, methods, fields for reflection, serialization and JNI
* access at runtime.
*
* It should only be used at {@link Feature#afterRegistration}.
*/
public interface ReflectionDynamicAccess {

/**
* Makes the provided classes available for reflection at runtime and all of their accessible
* members available for reflection queries at run time. A call to {@link Class#forName} for the
* names of the classes will return the classes at run time.
*
* @param condition needs to be satisfied for inclusion of types for reflection at runtime
*/
void register(InclusionCondition condition, Class<?>... classes);

/**
* Makes the provided class available for reflection at runtime if {@code condition} is
* satisfied. A call to {@link Class#forName} for the name of the class will return the class
* (if it exists) or a {@link ClassNotFoundException} at run time.
*/
void registerClassLookup(InclusionCondition condition, String className);

/**
* Makes the provided methods available for reflection at runtime if {@code condition} is
* satisfied. The methods will be returned by {@link Class#getMethod},
* {@link Class#getDeclaredMethod(String, Class[])}, and all the other methods on {@link Class}
* that return a single method.
*/
void register(InclusionCondition condition, Executable... methods);

void register(InclusionCondition condition, Field... fields);

/**
* Makes the provided classes available for serialization and reflection at runtime if
* {@code condition} is satisfied.
*/
void registerForSerialization(InclusionCondition condition, Class<?>... classes);

/**
* Makes the provided classes available for JNI access at runtime if {@code condition} is
* satisfied. Needed when native code looks up Java classes via <a href=
* "https://docs.oracle.com/en/java/javase/17/docs/specs/jni/functions.html#findclass">FindClass</a>.
*/
void registerForJNIAccess(InclusionCondition condition, Class<?>... classes);

/**
* Makes the provided methods available for JNI access at runtime if {@code condition} is
* satisfied. Needed when native code looks up Java methods via <a href=
* "https://docs.oracle.com/en/java/javase/17/docs/specs/jni/functions.html#getmethodid">GetMethodID</a>
* or <a href=
* "https://docs.oracle.com/en/java/javase/17/docs/specs/jni/functions.html#getstaticmethodid">GetStaticMethodID</a>.
*/
void registerForJNIAccess(InclusionCondition condition, Executable... methods);

/**
* Makes the provided methods available for JNI access at runtime if {@code condition} is
* satisfied. Needed when native code looks up Java methods via <a href=
* "https://docs.oracle.com/en/java/javase/17/docs/specs/jni/functions.html#getmethodid">GetMethodID</a>
* or <a href=
* "https://docs.oracle.com/en/java/javase/17/docs/specs/jni/functions.html#getstaticmethodid">GetStaticMethodID</a>.
*/
void registerForJNIAccess(InclusionCondition condition, Field... fields);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.graalvm.nativeimage.hosted;

/**
* This class is used to register Java resources and ResourceBundles that should be accessible at
* runtime.
*/
public interface ResourceDynamicAccess {

/**
* If {@code pattern} contains any wildcard patterns, such as star(*) or globstar(**), pattern
* is treated as glob pattern from {@code module} and will be registered if {@code condition} is
* satisfied. All resources that match the glob are available at runtime. Otherwise pattern
* represents Java resource from {@code module} that will be available at run time if
* {@code condition} is satisfied.
*/
void register(InclusionCondition condition, Module module, String pattern);

default void register(InclusionCondition condition, String pattern) {
register(condition, null, pattern);
}

/**
* Make Java ResourceBundle that is specified by a {@code bundleName} from module {@code module}
* available at run time if {@code condition} is satisfied. If the given {@code module} is
* unnamed, the ResourceBundle is looked up on the classpath instead.
*/
void registerResourceBundle(InclusionCondition condition, Module module, String bundleName);

default void registerResourceBundle(InclusionCondition condition, String bundleName) {
registerResourceBundle(condition, null, bundleName);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -46,7 +46,6 @@
import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
import org.graalvm.nativeimage.impl.ConfigurationCondition;
import org.graalvm.nativeimage.impl.RuntimeForeignAccessSupport;

@Platforms(Platform.HOSTED_ONLY.class)
Expand All @@ -68,7 +67,7 @@ public final class RuntimeForeignAccess {
* @since 23.1
*/
public static void registerForDowncall(Object desc, Object... options) {
ImageSingletons.lookup(RuntimeForeignAccessSupport.class).registerForDowncall(ConfigurationCondition.alwaysTrue(), desc, options);
ImageSingletons.lookup(RuntimeForeignAccessSupport.class).registerForDowncall(InclusionCondition.alwaysInclude(), desc, options);
}

/**
Expand All @@ -86,7 +85,7 @@ public static void registerForDowncall(Object desc, Object... options) {
* @since 24.1
*/
public static void registerForUpcall(Object desc, Object... options) {
ImageSingletons.lookup(RuntimeForeignAccessSupport.class).registerForUpcall(ConfigurationCondition.alwaysTrue(), desc, options);
ImageSingletons.lookup(RuntimeForeignAccessSupport.class).registerForUpcall(InclusionCondition.alwaysInclude(), desc, options);
}

/**
Expand Down Expand Up @@ -114,7 +113,7 @@ public static void registerForUpcall(Object desc, Object... options) {
* @since 24.2
*/
public static void registerForDirectUpcall(MethodHandle target, Object desc, Object... options) {
ImageSingletons.lookup(RuntimeForeignAccessSupport.class).registerForDirectUpcall(ConfigurationCondition.alwaysTrue(), target, desc, options);
ImageSingletons.lookup(RuntimeForeignAccessSupport.class).registerForDirectUpcall(InclusionCondition.alwaysInclude(), target, desc, options);
}

private RuntimeForeignAccess() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -46,7 +46,6 @@
import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
import org.graalvm.nativeimage.impl.ConfigurationCondition;
import org.graalvm.nativeimage.impl.RuntimeJNIAccessSupport;

/**
Expand All @@ -66,7 +65,7 @@ public final class RuntimeJNIAccess {
* @since 22.3
*/
public static void register(Class<?>... classes) {
ImageSingletons.lookup(RuntimeJNIAccessSupport.class).register(ConfigurationCondition.alwaysTrue(), classes);
ImageSingletons.lookup(RuntimeJNIAccessSupport.class).register(InclusionCondition.alwaysInclude(), classes);
}

/**
Expand All @@ -79,7 +78,7 @@ public static void register(Class<?>... classes) {
* @since 22.3
*/
public static void register(Executable... methods) {
ImageSingletons.lookup(RuntimeJNIAccessSupport.class).register(ConfigurationCondition.alwaysTrue(), false, methods);
ImageSingletons.lookup(RuntimeJNIAccessSupport.class).register(InclusionCondition.alwaysInclude(), false, methods);
}

/**
Expand All @@ -92,7 +91,7 @@ public static void register(Executable... methods) {
* @since 22.3
*/
public static void register(Field... fields) {
ImageSingletons.lookup(RuntimeJNIAccessSupport.class).register(ConfigurationCondition.alwaysTrue(), false, fields);
ImageSingletons.lookup(RuntimeJNIAccessSupport.class).register(InclusionCondition.alwaysInclude(), false, fields);
}

private RuntimeJNIAccess() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -43,7 +43,6 @@
import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
import org.graalvm.nativeimage.impl.ConfigurationCondition;
import org.graalvm.nativeimage.impl.RuntimeProxyCreationSupport;

/**
Expand All @@ -62,7 +61,7 @@ public final class RuntimeProxyCreation {
* @since 22.3
*/
public static void register(Class<?>... interfaces) {
ImageSingletons.lookup(RuntimeProxyCreationSupport.class).addProxyClass(ConfigurationCondition.alwaysTrue(), interfaces);
ImageSingletons.lookup(RuntimeProxyCreationSupport.class).addProxyClass(InclusionCondition.alwaysInclude(), interfaces);
}

private RuntimeProxyCreation() {
Expand Down
Loading
Loading