[GR-62940] API for dynamic-access registration based on access conditions #11075
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
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: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.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 afterFeature#afterRegistration
throws an error.The users acquire an instance of the revised API via getter methods on
Feature#AfterRegistrationAccess
. This guides the users to use the feature duringFeature#afterRegistration
.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
hosted
to the newdynamicaccess
package.Access
:ReflectiveAccess
,ResourceAccess
,ForeignAccess
,JNIAccess
.AccessCondition
, as they guard access at run time.AccessCondition
isunconditional
.Examples
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 flagTrackDeprecatedRegistationUsage
that emits runtime warnings when the legacy API is invoked, enabling developers to identify and remove deprecated usage.