Skip to content

[GR-60238] Include JNI reachability metadata with reflection #11066

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

Conversation

graalvmbot
Copy link
Collaborator

This PR merges the "jni" section of reachability-metadata.json into the "reflection" section.

Semantics

JNI access registration relies on a single "jniAccessible" at the class level. When this flag is set, the type as well as any method or field registered for reflection on the type are registered for JNI access as well.

On the other hand, classes, fields and methods that are registered for JNI access are now unconditionally registered for reflective access as well. This has no impact on reachability and only adds a limited amount of metadata to the image, which is acceptable.

The metadata file parsers of course still support the "jni" section, and the new semantics of "reflection", as well as the new "jniAccessible" flag, will be backported to the previous LTS releases.

Example

The following reflection and JNI registration:

{
    "reflection": [
        {
            "type": "fully.qualified.ClassName",
            "allDeclaredFields": true,
            "methods": [
                {
                    "name": "method",
                    "parameterTypes": []
                }
            ]
        }
    ],
    "jni": [
        {
            "type": "fully.qualified.ClassName",
            "fields": [
                {
                    "name": "field"
                }
            ],
            "methods": [
                {
                    "name": "method",
                    "parameterTypes": []
                }
            ]
        }
    ]
}

can now be expressed with the much simpler:

{
    "reflection": [
        {
            "type": "fully.qualified.ClassName",
            "allDeclaredFields": true,
            "jniAccessible": true,
            "methods": [
                {
                    "name": "method",
                    "parameterTypes": []
                }
            ]
        }
    ]
}

Configuration merging

Configuration merging will work by first converting any legacy "jni" section of reachability-metadata.json or jni-config.json file to the "reflection" section of reachability-metadata.json, and then merging according to the current rules. "jniAccessible" will be set on a merged configuration type if the flag was set on any of the source types.

As an example, merging the following reachability-metadata.json files:

{
    "reflection": [
        {
            "type": "fully.qualified.ClassName",
            "allDeclaredFields": true
        }
    ]
}
{
    "jni": [
        {
            "type": "fully.qualified.ClassName",
            "allDeclaredMethods": true,
            "fields": [
                {
                    "name": "field"
                }
            ]
        }
    ]
}

will result in the following reachability-metadata.json:

{
    "reflection": [
        {
            "type": "fully.qualified.ClassName",
            "allDeclaredFields": true,
            "allDeclaredMethods": true,
            "jniAccessible": true
        }
    ]
}

PR organization

The PR is organized in two commits, which are best reviewed separately:

  • "Clarify type name handling from different sources" introduces a systematic distinction between three types of class naming formats (human-readable names, Class.forName-compliant names and names used for JNI queries) and and where each of them is expected in the Native Image configuration handling code, centered around the new ClassNameSupport class.
  • "Include JNI reachability metadata with reflection" is the actual meat of the PR, incorporating JNI into reflection configuration both on the parsing side (around ReflectionMetadataParser) and in the internal data structures (in ConfigurationSet and related classes).

@oracle-contributor-agreement oracle-contributor-agreement bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Apr 23, 2025
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