Skip to content

[GR-62516] Changing typeReachable conditions into typeReached in the codebase #11138

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 2 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
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 always()
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,127 @@
/*
* 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;

/**
* A condition that must be satisfied to register elements for dynamic access (e.g., reflection,
* serialization, JNI access, resource access, and foreign access at runtime). Conditions prevent
* unnecessary growth of the native binary size.
* <p>
* There are currently two types of conditions:
* <ul>
* <li>{@link #typeReached} - satisfied when the type is both reachable by static analysis at build
* time, and reached at run time.</li>
* <li>{@link #always} - a condition that is always satisfied.</li>
* </ul>
* <p>
* Conditions can be created via the {@link #always} and {@link #typeReached} factory methods.
*
* @since 25.0
*/
public interface RegistrationCondition {

/**
* Creates the condition that is always satisfied. Any metadata that is predicated with this
* condition will always be included.
*
* @return instance of the condition
*
* @since 25.0
*/
static RegistrationCondition always() {
return TypeReachabilityCondition.JAVA_LANG_OBJECT_REACHED;
}

/**
* Creates the {@code typeReached} condition that is satisfied when the type is reached at
* runtime. A type is reached at runtime, right before the class-initialization routine starts
* for that type, or any of the type's subtypes are reached. Metadata predicated with this
* condition is only included if the condition is satisfied.
* <p>
* <strong>Example:</strong>
*
* <pre>{@code
* class SuperType {
* static {
* // ConditionType reached (subtype reached) => metadata is available
* }
* }
*
* class ConditionType extends SuperType {
* static {
* // ConditionType reached (before static initializer) => metadata is available
* }
*
* static ConditionType singleton() {
* // ConditionType reached (already initialized) => metadata is available
* }
* }
*
* public class App {
* public static void main(String[] args) {
* // ConditionType not reached => metadata is not available
* Class<?> clazz = ConditionType.class;
* // ConditionType not reached (ConditionType.class doesn't start class initialization)
* // => metadata is not available
* ConditionType.singleton();
* // ConditionType reached (already initialized) => metadata is available
* }
* }
* }</pre>
* <p>
* Type is also reached, if it is marked as {@code --initialize-at-build-time} or any of its
* subtypes are marked as {@code --initialize-at-build-time} and they exist on the classpath.
* Array types are never marked as reached and therefore cannot be used as the type in a
* condition.
*
* @param type the type that has to be reached for this condition to be satisfied, must not be
* {@code null}
*
* @return instance of the condition
*
* @since 25.0
*/
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
Loading