RFR: 8372382: Invalid RuntimeVisibleTypeAnnotations for compact record constructor
Vicente Romero
vromero at openjdk.org
Thu Feb 26 14:10:49 UTC 2026
On Thu, 26 Feb 2026 03:18:47 GMT, Chen Liang <liach at openjdk.org> wrote:
>> 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
>
> Looks reasonable
@liach thanks for the review
-------------
PR Comment: https://git.openjdk.org/jdk/pull/29928#issuecomment-3966863200
More information about the compiler-dev
mailing list