Skip to content

[svm] Forward QUERIED-to-ACCESSED upgrades of annotation methods to annotation proxy methods - #14912

Draft
MarkusBernhardt wants to merge 1 commit into
oracle:masterfrom
MarkusBernhardt:reflection-annotation-proxy-upgrade
Draft

MarkusBernhardt wants to merge 1 commit into
oracle:masterfrom
MarkusBernhardt:reflection-annotation-proxy-upgrade

Conversation

@MarkusBernhardt

@MarkusBernhardt MarkusBernhardt commented Nov 5, 2026 •

Copy link
Copy Markdown

Problem

ReflectionDataBuilder.registerMethod mirrors each registration of an annotation method onto the matching method of the annotation's proxy class (processAnnotationMethod). The mirroring is gated on previous == null || !previous.includes(QUERIED), so it only runs the first time the annotation method becomes at least QUERIED.

As a result, the final accessibility of the proxy method depends on the order in which the annotation method's registrations are processed:

Order of registrations for A.value() Proxy method $ProxyN.value() ends up
ACCESSED, then QUERIED ACCESSED
QUERIED, then ACCESSED QUERIED (the upgrade is dropped)

Both orders happen in practice, depending on analysis task scheduling:

  • Registering an annotation type for reflection (registerClass(ACCESSED, …) → registerTypesForTypeQuery) queues a QUERIED registration of all its methods.
  • Registering an element that carries the annotation, or having the hub of an annotated class in the image heap (registerHeapDynamicHub → registerTypesForAnnotations → registerType), creates the annotation proxy class through RuntimeProxyCreation, and DynamicProxySupport.createProxyClass registers the annotation interface's methods as ACCESSED.
  • AnnotationFeature.registerDeclaredMethods issues both kinds of registration for annotation objects in the image heap.

Each of these runs as its own task posted through runConditionalTask, which has two consequences:

  • The reflection metadata of the proxy class differs between otherwise identical builds. We found this while making native-image builds bit-for-bit reproducible.
  • In the losing order, annotation.getClass().getDeclaredMethod("value").invoke(annotation) throws MissingReflectionRegistrationError, even though A.value() is registered for invocation.

Fix

Forward the registration whenever it raises the annotation method's accessibility: previous == null || !previous.includes(accessibility). The hiding-method check keeps its existing gate (!previous.includes(QUERIED)), so it still runs once per method.

processAnnotationField uses the same gate, but proxy classes do not declare the constant fields of the annotation interface (getUniqueDeclaredField(true, proxyType, name) finds no such field on the proxy). The field path therefore has no observable effect and is left unchanged.

Test

New com.oracle.svm.test.reflect.AnnotationProxyMethodRegistrationTest (plus AnnotationProxyMethodRegistrationTarget), run with its own feature via @NativeImageBuildArgs. It forces the losing order deterministically:

  • In the first analysis iteration, only the annotation type is registered, so the annotation methods and the proxy methods become QUERIED.
  • In the second iteration, the feature registers the annotation method for invocation and the annotated class. Registering the annotated class creates the proxy class, which registers the annotation methods as ACCESSED.

The annotated class must not be reachable in the first iteration, otherwise its annotations would already create the proxy there. So it is a top-level class, because the test harness registers the test class and with it all its nest members. The test also looks it up with Class.forName and a non-constant name, because a class literal would put its hub into the image heap. The test then invokes the proxy method reflectively.

On darwin-aarch64 (labsjdk 25.0.5.1, JVMCI 25.6-b27):

  • On master 3810b9fb0f4 without the fix, the test fails in 4 of 4 runs with MissingReflectionRegistrationError: Cannot reflectively invoke method 'public final java.lang.String jdk.proxy4.$Proxy/….value()'.
  • With the fix, it passes in 3 of 3 runs.

mx checkstyle --primary passes. mx eclipseformat was not run.

Notes

🤖 Generated with Claude Code

ReflectionDataBuilder.registerMethod mirrors every registration of an
annotation method onto the corresponding method of the annotation proxy
class. The mirroring was gated on the annotation method becoming QUERIED
for the first time, so a later upgrade of the annotation method from
QUERIED to ACCESSED was never forwarded: the proxy method kept QUERIED
and stayed non-invocable. With the opposite order, ACCESSED first and
QUERIED second, the proxy method ends up ACCESSED.

The order is not fixed in practice. Registering an annotation type for
reflection queues a QUERIED registration of all its methods. Registering
an element that carries the annotation, which also happens for every
class whose hub is in the image heap, creates the annotation proxy
class, and DynamicProxySupport.createProxyClass registers the annotation
methods as ACCESSED. AnnotationFeature does both for annotation objects
in the image heap. All of these run as separate analysis tasks, so the
reflection metadata of the proxy class depended on task scheduling.

Forward the registration whenever it raises the accessibility of the
annotation method. The hiding-method check keeps its original gate.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@oracle-contributor-agreement

Copy link
Copy Markdown

Thank you for your pull request and welcome to our community! To contribute, please sign the Oracle Contributor Agreement (OCA).
The following contributors of this PR have not signed the OCA:

To sign the OCA, please create an Oracle account and sign the OCA in Oracle's Contributor Agreement Application.

When signing the OCA, please provide your GitHub username. After signing the OCA and getting an OCA approval from Oracle, this PR will be automatically updated.

If you are an Oracle employee, please make sure that you are a member of the main Oracle GitHub organization, and your membership in this organization is public.

@oracle-contributor-agreement oracle-contributor-agreement Bot added the OCA Required At least one contributor does not have an approved Oracle Contributor Agreement. label Nov 5, 2026
@oracle-contributor-agreement

Copy link
Copy Markdown

Thank you for signing the OCA.

@oracle-contributor-agreement oracle-contributor-agreement Bot added OCA Verified All contributors have signed the Oracle Contributor Agreement. and removed OCA Required At least one contributor does not have an approved Oracle Contributor Agreement. labels Nov 5, 2026

This branch has not been deployed

No deployments
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.

1 participant