JDK-8359041 : Wrong index for annotations on enum constructor parameters
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang:reflect
  • Affected Version: 11,25
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: generic
  • CPU: generic
  • Submitted: 2025-06-08
  • Updated: 2025-06-09
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdUnresolved
Related Reports
Relates :  
Description
ADDITIONAL SYSTEM INFORMATION :
Is reproducible in at least: 21.0.7, 24.0.1, 17.0.15

output of java --version:
openjdk 24.0.1 2025-04-15
OpenJDK Runtime Environment (build 24.0.1+11)
OpenJDK 64-Bit Server VM (build 24.0.1+11, mixed mode, sharing)

MacBook pro M1Max, 32GB, macOS 15.5
output of `uname -a:
Darwin MacBook-Pro-von-Axel.local 24.5.0 Darwin Kernel Version 24.5.0: Tue Apr 22 19:54:49 PDT 2025; root:xnu-11417.121.6~2/RELEASE_ARM64_T6000 arm64


A DESCRIPTION OF THE PROBLEM :
In attached sample code, the E constructor takes two parameters, the second of which is annotated with @Annotation that has target TYPE_USE. The compiler introduces two additional synthetic parameters for ordinal and name. If I query the annotations, the annotation is present on the second synthetic parameter instead of the second non-synthetic parameter.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Save the example code as Main.java, then run using `java Main`.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Constructor signature: private E(java.lang.String,int,int,java.lang.String)
Parameter count: 4
Parameter[0]: String arg0 - annotations: none
Parameter[1]: int arg1 - annotations: none
Parameter[2]: int arg2 - annotations: none
Parameter[3]: String arg3 - annotations:: @Annotation()

ACTUAL -
Constructor signature: private E(java.lang.String,int,int,java.lang.String)
Parameter count: 4
Parameter[0]: String arg0 - annotations: none
Parameter[1]: int arg1 - annotations: @Annotation()
Parameter[2]: int arg2 - annotations: none
Parameter[3]: String arg3 - annotations: none


---------- BEGIN SOURCE ----------
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Parameter;

public class Main {
    public static void main(String[] args) throws Exception {
        // Get the enum constructor
        var constructor = E.class.getDeclaredConstructors()[0];
        Parameter[] parameters = constructor.getParameters();

        System.out.println("Constructor signature: " + constructor);
        System.out.println("Parameter count: " + parameters.length);

        for (int i = 0; i < parameters.length; i++) {
            Parameter param = parameters[i];
            var annotations = param.getAnnotatedType().getDeclaredAnnotations();

            System.out.printf("Parameter[%d]: %s %s - annotations: %s%n",
                    i, param.getType().getSimpleName(), param.getName(),
                    annotations.length > 0 ? annotations[0] : "none");
        }
    }
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE_USE)
@interface Annotation {
}

enum E {
    VALUE1(42, "hello"),
    VALUE2(99, null);

    E(int number, @Annotation String text) {
    }
}

---------- END SOURCE ----------


Comments
The patch for JDK-8180892 should be able to fix this issue. Note that there is an ad-hoc fix for this for declaration annotations on enum constructors in Constructor::handleParameterNumberMismatch, which was required before MethodParameters attribute was consistently emitted since JDK 21.
09-06-2025

Please evaluate
09-06-2025

Impact -> M (Somewhere in-between the extremes) Likelihood -> M (for @Annotation in enum constructor) Workaround -> L (avoid problematic use) Priority -> P4
09-06-2025

Observation on Windows 11 ---------------------------------------- JDK 11.0.26 : Failed JDK 17.0.15 : Failed JDK 21.0.7 : Failed JDK 25ea : Failed
09-06-2025