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 d3539b53541b3..cf26d76c85acf 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 @@ -5427,7 +5427,10 @@ private JCExpression insertAnnotationsToMostInner( */ protected JCVariableDecl formalParameter(boolean lambdaParameter, boolean recordComponent) { JCModifiers mods = !recordComponent ? optFinal(Flags.PARAMETER) : modifiersOpt(); - if (recordComponent && mods.flags != 0) { + if (recordComponent && mods.flags != 0 && mods.flags != Flags.DEPRECATED) { + /* for record components we only allow the DEPRECATED flag, which can be set with a + * @deprecated javadoc tag + */ log.error(mods.pos, Errors.RecordCantDeclareFieldModifiers); } if (recordComponent) { diff --git a/test/langtools/tools/javac/records/RecordCompilationTests.java b/test/langtools/tools/javac/records/RecordCompilationTests.java index 1de0ec48f0dbd..7347abe4f9006 100644 --- a/test/langtools/tools/javac/records/RecordCompilationTests.java +++ b/test/langtools/tools/javac/records/RecordCompilationTests.java @@ -2169,4 +2169,58 @@ record R(@SafeVarargs String... s) { """ ); } + + @Test + void testDeprecatedJavadoc() { + assertOK( + """ + record R( + /** + * @deprecated + */ + @Deprecated + int i + ) {} + """ + ); + assertOK( + """ + record R( + @Deprecated + int i + ) {} + """ + ); + assertOKWithWarning("compiler.warn.missing.deprecated.annotation", + """ + record R( + /** + * @deprecated + */ + int i + ) {} + """ + ); + String[] previousOptions = getCompileOptions(); + try { + setCompileOptions(new String[] {"-Xlint:deprecation"}); + assertOKWithWarning("compiler.warn.has.been.deprecated", + """ + record R( + /** + * @deprecated + */ + @Deprecated + int i + ) {} + class Client { + R r; + int j = r.i(); + } + """ + ); + } finally { + setCompileOptions(previousOptions); + } + } }