RFR: 8372382: Invalid RuntimeVisibleTypeAnnotations for compact record constructor

Vicente Romero vromero at openjdk.org
Thu Feb 26 03:13:27 UTC 2026


This test case shows the issue:

import java.lang.annotation.*;

class Test {
    class Foo {}
    record R(Test. at Anno Foo foo) {
        public R{}
    }
    @Target(ElementType.TYPE_USE)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface Anno {}
}


when compiling this code and inspecting the class file for record R we see this for it's constructor:


public Test$R(Test$Foo);
    descriptor: (LTest$Foo;)V
    flags: (0x0001) ACC_PUBLIC
    Code:
      stack=2, locals=2, args_size=2
         0: aload_0
         1: invokespecial #1                  // Method java/lang/Record."<init>":()V
         4: aload_0
         5: aload_1
         6: putfield      #7                  // Field foo:LTest$Foo;
         9: return
      LineNumberTable:
        line 11: 0
    MethodParameters:
      Name                           Flags
      foo                            mandated
    RuntimeVisibleTypeAnnotations:
      0: #26(): FIELD, location=[INNER_TYPE]
        Test$Anno

The position of the type annotation applied to the parameter for the record constructor is incorrect, it should be:

RuntimeVisibleTypeAnnotations:
      0: #26(): METHOD_FORMAL_PARAMETER, param_index=0, location=[INNER_TYPE]
        Test$Anno

The problem here is that the generated record field and argument generated for the compact constructor compare the same annotation, so the type annotation position of the method argument is overwritten as the field is visited after the constructor at TypeAnnotations.TypeAnnotationPositions. The proposal here is to copy all the elements that can contain annotations when filling the parameters of compact constructors.

TIA

-------------

Commit messages:
 - 8372382: invalid RuntimeVisibleTypeAnnotations for compact record constructor

Changes: https://git.openjdk.org/jdk/pull/29928/files
  Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=29928&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8372382
  Stats: 17 lines in 2 files changed: 15 ins; 0 del; 2 mod
  Patch: https://git.openjdk.org/jdk/pull/29928.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/29928/head:pull/29928

PR: https://git.openjdk.org/jdk/pull/29928


More information about the compiler-dev mailing list