Skip to content

Conversation

graalvmbot
Copy link
Collaborator

@graalvmbot graalvmbot commented Apr 23, 2025

Motivation

The current API for dynamic-access registration supports conditional registrations only through reachability handlers. This means that the program semantics (i.e., if something is registered or not) depends on the results of static analysis.

Since static analysis are not deterministic (affected by the classpath, compiler optimizations, usage layers or not) and not available in regular Java execution, we want to move away from the analysis results as the driver of semantics.

In addition, the current dynamic-access registration API was built for fine-grained reflection which is unnecessarily complex. Take, for example, the code needed to register all metadata for a single type:

        RuntimeReflection.registerAllDeclaredFields(clazz);
        RuntimeReflection.registerAllFields(clazz);
        RuntimeReflection.registerAllDeclaredMethods(clazz);
        RuntimeReflection.registerAllMethods(clazz);
        RuntimeReflection.registerAllDeclaredConstructors(clazz);
        RuntimeReflection.registerAllConstructors(clazz);
        RuntimeReflection.registerAllClasses(clazz);
        RuntimeReflection.registerAllRecordComponents(clazz);
        RuntimeReflection.registerAllPermittedSubclasses(clazz);
        RuntimeReflection.registerAllNestMembers(clazz);
        RuntimeReflection.registerAllSigners(clazz);

The revised API

This PR introduces a simplified API for metadata registration based on conditions, that has similar semantics as reachability-metadata.json. The key differences of the revised API are:

  1. Each method in the new API takes a AccessCondition as its first argument. Conditions for programmatic metadata registration are explained in the Access Condition documentation.

  2. The API should be independent of Native Image as we want to be able to use it in Crema. Therefore, it can be used only during Feature#afterRegistration as this phase of the Feature API is Native Image agnostic. Any attempt to register metadata after Feature#afterRegistration throws an error.

  3. The users acquire an instance of the revised API via getter methods on Feature#AfterRegistrationAccess. This guides the users to use the feature during Feature#afterRegistration.

  4. Similarly to reachability-metadata.json, the all reflection registrations of individual elements (e.g., methods and fields) also register the declaring class metadata.

Naming decisions

  1. Ideally we want to avoid binding the name to the build-time paradigm and the hosted vs non-hosted distinction, thus we are proposing to move the revised API from the hosted to the new dynamicaccess package.
  2. Unified the names for APIs to end with Access: ReflectiveAccess, ResourceAccess, ForeignAccess, JNIAccess.
  3. The name of the API that represents access conditions is AccessCondition, as they guard access at run time.
  4. Factory method for creating the always satisfiedAccessCondition is unconditional.

Examples

 // Current API
 RuntimeReflection.register(Foo.class);
 RuntimeReflection.registerAllClasses(Foo.class);
 RuntimeReflection.registerAllDeclaredClasses(Foo.class);
 RuntimeReflection.registerAllMethods(Foo.class);
 RuntimeReflection.registerAllFields(Foo.class);
 // New API
 public void afterRegistration(AfterRegistrationAccess access) {
     var reflection = access.getReflectiveAccess();
     reflection.register(AccessCondition.typeReached(Bar.class), Foo.class);
 }

An example of Micronaut internal feature adapted to the new API

Deprecation of current API

The existing API (e.g, RuntimeReflection, RuntimeResourceAccess, RuntimeSerialization, etc.) for metadata registration will remain available during a transition to the revised API. Therefore we are introducing an optional flag TrackDeprecatedRegistationUsage that emits runtime warnings when the legacy API is invoked, enabling developers to identify and remove deprecated usage.

@oracle-contributor-agreement oracle-contributor-agreement bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Apr 23, 2025
@graalvmbot graalvmbot force-pushed the mm/GR-62940-new-API-for-metadata-registration branch 2 times, most recently from caafa0a to 21d1b60 Compare April 23, 2025 22:02
@graalvmbot graalvmbot changed the title [GR-62940] Introduce new API for metadata registration [WIP][GR-62940] Introduce new API for metadata registration Apr 24, 2025
@graalvmbot graalvmbot force-pushed the mm/GR-62940-new-API-for-metadata-registration branch 5 times, most recently from c56713c to b759354 Compare April 29, 2025 12:14
@graalvmbot graalvmbot force-pushed the mm/GR-62940-new-API-for-metadata-registration branch 6 times, most recently from 9df5a56 to f95421a Compare May 12, 2025 09:54
@graalvmbot graalvmbot force-pushed the mm/GR-62940-new-API-for-metadata-registration branch 6 times, most recently from 7be6081 to c149086 Compare May 21, 2025 13:53
@graalvmbot graalvmbot force-pushed the mm/GR-62940-new-API-for-metadata-registration branch 3 times, most recently from 65fbdc3 to 409b6c4 Compare May 28, 2025 08:53
@graalvmbot graalvmbot changed the title [WIP][GR-62940] Introduce new API for metadata registration [GR-62940] Introduce new API for metadata registration May 28, 2025
@graalvmbot graalvmbot force-pushed the mm/GR-62940-new-API-for-metadata-registration branch 5 times, most recently from 9351a95 to 778f204 Compare June 2, 2025 22:48
@graalvmbot graalvmbot force-pushed the mm/GR-62940-new-API-for-metadata-registration branch 4 times, most recently from 2d139dd to 0a84886 Compare June 30, 2025 13:15
@graalvmbot graalvmbot force-pushed the mm/GR-62940-new-API-for-metadata-registration branch 5 times, most recently from a44fe0e to ac60b06 Compare July 7, 2025 17:10
@graalvmbot graalvmbot force-pushed the mm/GR-62940-new-API-for-metadata-registration branch from ac60b06 to b88862f Compare July 10, 2025 16:50
@graalvmbot graalvmbot force-pushed the mm/GR-62940-new-API-for-metadata-registration branch 2 times, most recently from 3493105 to 8472517 Compare July 21, 2025 14:11
@graalvmbot graalvmbot force-pushed the mm/GR-62940-new-API-for-metadata-registration branch 5 times, most recently from b590846 to faacfaf Compare August 14, 2025 11:55
@graalvmbot graalvmbot force-pushed the mm/GR-62940-new-API-for-metadata-registration branch from faacfaf to 471bfd5 Compare August 19, 2025 18:43
@graalvmbot graalvmbot force-pushed the mm/GR-62940-new-API-for-metadata-registration branch from 471bfd5 to e8e79ed Compare August 27, 2025 22:26
@graalvmbot graalvmbot changed the title [GR-62940] API for metadata registration that uses conditions [GR-62940] API for dynamic-access registration based on access conditions Aug 28, 2025
@graalvmbot graalvmbot force-pushed the mm/GR-62940-new-API-for-metadata-registration branch from 02f862f to f9e6cd4 Compare August 29, 2025 12:56
@graalvmbot graalvmbot force-pushed the mm/GR-62940-new-API-for-metadata-registration branch 2 times, most recently from 7473039 to 55b2e97 Compare September 8, 2025 11:07
ConfigurationCondition is divided into AccessCondition(API) and TypeReachabilityCondition(implementation).

Replaced all usages of ConfigurationCondition.
@graalvmbot graalvmbot force-pushed the mm/GR-62940-new-API-for-metadata-registration branch from 55b2e97 to ba933e2 Compare September 9, 2025 16:48
@graalvmbot graalvmbot force-pushed the mm/GR-62940-new-API-for-metadata-registration branch from ba933e2 to 7c78f1a Compare September 11, 2025 13:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OCA Verified All contributors have signed the Oracle Contributor Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants