RFR: 8372382: Invalid RuntimeVisibleTypeAnnotations for compact record constructor
Vicente Romero
vromero at openjdk.org
Thu Feb 26 03:13:27 UTC 2026
On Thu, 26 Feb 2026 03:06:00 GMT, Vicente Romero <vromero 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
src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java line 4433:
> 4431: .VarDef(F.Modifiers(Flags.PARAMETER | Flags.GENERATED_MEMBER | Flags.MANDATED | param.mods.flags & Flags.VARARGS,
> 4432: copier.copy(param.mods.annotations)),
> 4433: param.name, copier.copy(param.vartype), null));
we need to copy the record component's vartype too as it can also contain annotations
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/29928#discussion_r2856646151
More information about the compiler-dev
mailing list