From 94de8982f99ff7fd5d955246a56a12bf2bf69785 Mon Sep 17 00:00:00 2001 From: Vicente Romero Date: Thu, 26 Feb 2026 15:21:31 +0000 Subject: [PATCH] 8372382: Invalid RuntimeVisibleTypeAnnotations for compact record constructor Reviewed-by: liach --- .../com/sun/tools/javac/parser/JavacParser.java | 5 +++-- .../TypeAnnotationsPositionsOnRecords.java | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java index d32b892eb54..df5da5cb954 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java @@ -4421,12 +4421,13 @@ public class JavacParser implements Parser { JCMethodDecl methDef = (JCMethodDecl) def; if (methDef.name == names.init && methDef.params.isEmpty() && (methDef.mods.flags & Flags.COMPACT_RECORD_CONSTRUCTOR) != 0) { ListBuffer tmpParams = new ListBuffer<>(); + TreeCopier copier = new TreeCopier<>(F); for (JCVariableDecl param : headerFields) { tmpParams.add(F.at(param) // we will get flags plus annotations from the record component .VarDef(F.Modifiers(Flags.PARAMETER | Flags.GENERATED_MEMBER | Flags.MANDATED | param.mods.flags & Flags.VARARGS, - param.mods.annotations), - param.name, param.vartype, null)); + copier.copy(param.mods.annotations)), + param.name, copier.copy(param.vartype), null)); } methDef.params = tmpParams.toList(); } diff --git a/test/langtools/tools/javac/annotations/typeAnnotations/TypeAnnotationsPositionsOnRecords.java b/test/langtools/tools/javac/annotations/typeAnnotations/TypeAnnotationsPositionsOnRecords.java index dfa266ef035..b8ee32fcacd 100644 --- a/test/langtools/tools/javac/annotations/typeAnnotations/TypeAnnotationsPositionsOnRecords.java +++ b/test/langtools/tools/javac/annotations/typeAnnotations/TypeAnnotationsPositionsOnRecords.java @@ -78,6 +78,17 @@ public class TypeAnnotationsPositionsOnRecords { record Record6(String t1, @Nullable String t2) { public Record6 {} } + + class Test2 { + @Target(ElementType.TYPE_USE) + @Retention(RetentionPolicy.RUNTIME) + public @interface Anno {} + + class Foo {} + record Record7(Test2.@Anno Foo foo) { + public Record7 {} // compact constructor + } + } """; public static void main(String... args) throws Exception { @@ -100,6 +111,8 @@ public class TypeAnnotationsPositionsOnRecords { "Record5.class").toUri()), 1); checkClassFile(new File(Paths.get(System.getProperty("user.dir"), "Record6.class").toUri()), 1); + checkClassFile(new File(Paths.get(System.getProperty("user.dir"), + "Test2$Record7.class").toUri()), 0); } void compileTestClass() throws Exception { @@ -110,6 +123,7 @@ public class TypeAnnotationsPositionsOnRecords { void checkClassFile(final File cfile, int... taPositions) throws Exception { ClassModel classFile = ClassFile.of().parse(cfile.toPath()); + System.err.println("-----------loading " + cfile.getPath()); int accessorPos = 0; int checkedAccessors = 0; for (MethodModel method : classFile.methods()) {