Skip to content

[GR-62284] Introduce RegistrationCondition and TypeReachabilityCondition #11139

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 1 commit 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
4 changes: 4 additions & 0 deletions sdk/src/org.graalvm.nativeimage/snapshot.sigtest
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,10 @@ CLSS public abstract interface org.graalvm.nativeimage.hosted.FieldValueTransfor
meth public abstract java.lang.Object transform(java.lang.Object,java.lang.Object)
meth public boolean isAvailable()

CLSS public abstract interface org.graalvm.nativeimage.hosted.RegistrationCondition
meth public static org.graalvm.nativeimage.hosted.RegistrationCondition alwaysInclude()
meth public static org.graalvm.nativeimage.hosted.RegistrationCondition typeReached(java.lang.Class<?>)

CLSS public final org.graalvm.nativeimage.hosted.RuntimeClassInitialization
meth public !varargs static void initializeAtBuildTime(java.lang.Class<?>[])
meth public !varargs static void initializeAtBuildTime(java.lang.String[])
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.TypeReachabilityCondition;

/**
* Condition that must be satisfied for inclusion of elements for dynamic access (e.g., runtime
* reflection, serialization, JNI access, and resource access at runtime).
* {@link RegistrationCondition} is used together with {@link Feature} for programmatic registration
* of metadata.
* <p>
* Currently, there is only one type of condition: {@link #typeReached} 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.
* </p>
*
* Conditions can be created via {@link #always} and {@link #typeReached} factory methods.
*/
public interface RegistrationCondition {

/**
* 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 RegistrationCondition always() {
return TypeReachabilityCondition.JAVA_LANG_OBJECT_REACHED;
}

/**
* Creates the default 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 RegistrationCondition typeReached(Class<?> type) {
return TypeReachabilityCondition.create(type, true);
}
}
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(RegistrationCondition.always(), 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(RegistrationCondition.always(), 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(RegistrationCondition.always(), 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(RegistrationCondition.always(), 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(RegistrationCondition.always(), 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(RegistrationCondition.always(), 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(RegistrationCondition.always(), interfaces);
}

private RuntimeProxyCreation() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,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.RuntimeReflectionSupport;

//Checkstyle: allow reflection
Expand All @@ -69,7 +68,7 @@ public final class RuntimeReflection {
* @since 19.0
*/
public static void register(Class<?>... classes) {
ImageSingletons.lookup(RuntimeReflectionSupport.class).register(ConfigurationCondition.alwaysTrue(), classes);
ImageSingletons.lookup(RuntimeReflectionSupport.class).register(RegistrationCondition.always(), classes);
}

/**
Expand All @@ -80,7 +79,7 @@ public static void register(Class<?>... classes) {
* @since 23.0
*/
public static void registerClassLookup(String className) {
ImageSingletons.lookup(RuntimeReflectionSupport.class).registerClassLookup(ConfigurationCondition.alwaysTrue(), className);
ImageSingletons.lookup(RuntimeReflectionSupport.class).registerClassLookup(RegistrationCondition.always(), className);
}

/**
Expand All @@ -91,7 +90,7 @@ public static void registerClassLookup(String className) {
* @since 19.0
*/
public static void register(Executable... methods) {
ImageSingletons.lookup(RuntimeReflectionSupport.class).register(ConfigurationCondition.alwaysTrue(), false, methods);
ImageSingletons.lookup(RuntimeReflectionSupport.class).register(RegistrationCondition.always(), false, methods);
}

/**
Expand All @@ -103,7 +102,7 @@ public static void register(Executable... methods) {
* @since 21.3
*/
public static void registerAsQueried(Executable... methods) {
ImageSingletons.lookup(RuntimeReflectionSupport.class).register(ConfigurationCondition.alwaysTrue(), true, methods);
ImageSingletons.lookup(RuntimeReflectionSupport.class).register(RegistrationCondition.always(), true, methods);
}

/**
Expand All @@ -116,7 +115,7 @@ public static void registerAsQueried(Executable... methods) {
* @since 23.0
*/
public static void registerMethodLookup(Class<?> declaringClass, String methodName, Class<?>... parameterTypes) {
ImageSingletons.lookup(RuntimeReflectionSupport.class).registerMethodLookup(ConfigurationCondition.alwaysTrue(), declaringClass, methodName, parameterTypes);
ImageSingletons.lookup(RuntimeReflectionSupport.class).registerMethodLookup(RegistrationCondition.always(), declaringClass, methodName, parameterTypes);
}

/**
Expand All @@ -130,7 +129,7 @@ public static void registerMethodLookup(Class<?> declaringClass, String methodNa
* @since 23.0
*/
public static void registerConstructorLookup(Class<?> declaringClass, Class<?>... parameterTypes) {
ImageSingletons.lookup(RuntimeReflectionSupport.class).registerConstructorLookup(ConfigurationCondition.alwaysTrue(), declaringClass, parameterTypes);
ImageSingletons.lookup(RuntimeReflectionSupport.class).registerConstructorLookup(RegistrationCondition.always(), declaringClass, parameterTypes);
}

/**
Expand All @@ -141,7 +140,7 @@ public static void registerConstructorLookup(Class<?> declaringClass, Class<?>..
* @since 19.0
*/
public static void register(Field... fields) {
ImageSingletons.lookup(RuntimeReflectionSupport.class).register(ConfigurationCondition.alwaysTrue(), false, fields);
ImageSingletons.lookup(RuntimeReflectionSupport.class).register(RegistrationCondition.always(), false, fields);
}

/**
Expand All @@ -153,7 +152,7 @@ public static void register(Field... fields) {
* @since 19.0
*/
public static void registerFieldLookup(Class<?> declaringClass, String fieldName) {
ImageSingletons.lookup(RuntimeReflectionSupport.class).registerFieldLookup(ConfigurationCondition.alwaysTrue(), declaringClass, fieldName);
ImageSingletons.lookup(RuntimeReflectionSupport.class).registerFieldLookup(RegistrationCondition.always(), declaringClass, fieldName);
}

/**
Expand All @@ -162,7 +161,7 @@ public static void registerFieldLookup(Class<?> declaringClass, String fieldName
* @since 23.0
*/
public static void registerAllClasses(Class<?> declaringClass) {
ImageSingletons.lookup(RuntimeReflectionSupport.class).registerAllClassesQuery(ConfigurationCondition.alwaysTrue(), declaringClass);
ImageSingletons.lookup(RuntimeReflectionSupport.class).registerAllClassesQuery(RegistrationCondition.always(), declaringClass);
}

/**
Expand All @@ -171,7 +170,7 @@ public static void registerAllClasses(Class<?> declaringClass) {
* @since 23.0
*/
public static void registerAllDeclaredClasses(Class<?> declaringClass) {
ImageSingletons.lookup(RuntimeReflectionSupport.class).registerAllDeclaredClassesQuery(ConfigurationCondition.alwaysTrue(), declaringClass);
ImageSingletons.lookup(RuntimeReflectionSupport.class).registerAllDeclaredClassesQuery(RegistrationCondition.always(), declaringClass);
}

/**
Expand All @@ -181,7 +180,7 @@ public static void registerAllDeclaredClasses(Class<?> declaringClass) {
* @since 23.0
*/
public static void registerAllMethods(Class<?> declaringClass) {
ImageSingletons.lookup(RuntimeReflectionSupport.class).registerAllMethodsQuery(ConfigurationCondition.alwaysTrue(), true, declaringClass);
ImageSingletons.lookup(RuntimeReflectionSupport.class).registerAllMethodsQuery(RegistrationCondition.always(), true, declaringClass);
}

/**
Expand All @@ -191,7 +190,7 @@ public static void registerAllMethods(Class<?> declaringClass) {
* @since 23.0
*/
public static void registerAllDeclaredMethods(Class<?> declaringClass) {
ImageSingletons.lookup(RuntimeReflectionSupport.class).registerAllDeclaredMethodsQuery(ConfigurationCondition.alwaysTrue(), true, declaringClass);
ImageSingletons.lookup(RuntimeReflectionSupport.class).registerAllDeclaredMethodsQuery(RegistrationCondition.always(), true, declaringClass);
}

/**
Expand All @@ -201,7 +200,7 @@ public static void registerAllDeclaredMethods(Class<?> declaringClass) {
* @since 23.0
*/
public static void registerAllConstructors(Class<?> declaringClass) {
ImageSingletons.lookup(RuntimeReflectionSupport.class).registerAllConstructorsQuery(ConfigurationCondition.alwaysTrue(), true, declaringClass);
ImageSingletons.lookup(RuntimeReflectionSupport.class).registerAllConstructorsQuery(RegistrationCondition.always(), true, declaringClass);
}

/**
Expand All @@ -211,7 +210,7 @@ public static void registerAllConstructors(Class<?> declaringClass) {
* @since 23.0
*/
public static void registerAllDeclaredConstructors(Class<?> declaringClass) {
ImageSingletons.lookup(RuntimeReflectionSupport.class).registerAllDeclaredConstructorsQuery(ConfigurationCondition.alwaysTrue(), true, declaringClass);
ImageSingletons.lookup(RuntimeReflectionSupport.class).registerAllDeclaredConstructorsQuery(RegistrationCondition.always(), true, declaringClass);
}

/**
Expand All @@ -221,7 +220,7 @@ public static void registerAllDeclaredConstructors(Class<?> declaringClass) {
* @since 23.0
*/
public static void registerAllFields(Class<?> declaringClass) {
ImageSingletons.lookup(RuntimeReflectionSupport.class).registerAllFields(ConfigurationCondition.alwaysTrue(), declaringClass);
ImageSingletons.lookup(RuntimeReflectionSupport.class).registerAllFields(RegistrationCondition.always(), declaringClass);
}

/**
Expand All @@ -231,7 +230,7 @@ public static void registerAllFields(Class<?> declaringClass) {
* @since 23.0
*/
public static void registerAllDeclaredFields(Class<?> declaringClass) {
ImageSingletons.lookup(RuntimeReflectionSupport.class).registerAllDeclaredFields(ConfigurationCondition.alwaysTrue(), declaringClass);
ImageSingletons.lookup(RuntimeReflectionSupport.class).registerAllDeclaredFields(RegistrationCondition.always(), declaringClass);
}

/**
Expand All @@ -240,7 +239,7 @@ public static void registerAllDeclaredFields(Class<?> declaringClass) {
* @since 23.0
*/
public static void registerAllNestMembers(Class<?> declaringClass) {
ImageSingletons.lookup(RuntimeReflectionSupport.class).registerAllNestMembersQuery(ConfigurationCondition.alwaysTrue(), declaringClass);
ImageSingletons.lookup(RuntimeReflectionSupport.class).registerAllNestMembersQuery(RegistrationCondition.always(), declaringClass);
}

/**
Expand All @@ -249,7 +248,7 @@ public static void registerAllNestMembers(Class<?> declaringClass) {
* @since 23.0
*/
public static void registerAllPermittedSubclasses(Class<?> declaringClass) {
ImageSingletons.lookup(RuntimeReflectionSupport.class).registerAllPermittedSubclassesQuery(ConfigurationCondition.alwaysTrue(), declaringClass);
ImageSingletons.lookup(RuntimeReflectionSupport.class).registerAllPermittedSubclassesQuery(RegistrationCondition.always(), declaringClass);
}

/**
Expand All @@ -258,7 +257,7 @@ public static void registerAllPermittedSubclasses(Class<?> declaringClass) {
* @since 23.0
*/
public static void registerAllRecordComponents(Class<?> declaringClass) {
ImageSingletons.lookup(RuntimeReflectionSupport.class).registerAllRecordComponentsQuery(ConfigurationCondition.alwaysTrue(), declaringClass);
ImageSingletons.lookup(RuntimeReflectionSupport.class).registerAllRecordComponentsQuery(RegistrationCondition.always(), declaringClass);
}

/**
Expand All @@ -267,7 +266,7 @@ public static void registerAllRecordComponents(Class<?> declaringClass) {
* @since 23.0
*/
public static void registerAllSigners(Class<?> declaringClass) {
ImageSingletons.lookup(RuntimeReflectionSupport.class).registerAllSignersQuery(ConfigurationCondition.alwaysTrue(), declaringClass);
ImageSingletons.lookup(RuntimeReflectionSupport.class).registerAllSignersQuery(RegistrationCondition.always(), declaringClass);
}

/**
Expand Down
Loading
Loading