From fc0b437ad7174b5f8e199d500ef91b341af0877f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Mon, 2 Mar 2020 19:00:59 +0100 Subject: [PATCH] Fix record ctor with throws --- pmd-java/etc/grammar/Java.jjt | 11 +++++++++-- .../lang/java/ast/jdkversiontests/java14/Records.java | 8 +++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/pmd-java/etc/grammar/Java.jjt b/pmd-java/etc/grammar/Java.jjt index 06c4b35c72..29ce5dd111 100644 --- a/pmd-java/etc/grammar/Java.jjt +++ b/pmd-java/etc/grammar/Java.jjt @@ -1152,9 +1152,15 @@ void RecordBody(): void RecordBodyDeclaration() #void : {} { - LOOKAHEAD(ClassOrInterfaceBodyDeclaration()) ClassOrInterfaceBodyDeclaration() + LOOKAHEAD(RecordCtorLookahead()) RecordConstructorDeclaration() | - RecordConstructorDeclaration() + ClassOrInterfaceBodyDeclaration() +} + +private void RecordCtorLookahead() #void: +{} +{ + Modifiers() [ TypeParameters() ] ("throws" | "{") } void RecordConstructorDeclaration(): @@ -1165,6 +1171,7 @@ void RecordConstructorDeclaration(): modifiers = Modifiers() { jjtThis.setModifiers(modifiers); } [TypeParameters()] { jjtThis.setImage(token.image); } + [ "throws" NameList() ] "{" ( BlockStatement() )* "}" } diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java14/Records.java b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java14/Records.java index 24be286303..a1be788be7 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java14/Records.java +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java14/Records.java @@ -1,3 +1,4 @@ +import java.io.IOException; import java.lang.annotation.Target; import java.lang.annotation.ElementType; @@ -54,5 +55,10 @@ public class Records { String lastName(); } public record PersonRecord(String firstName, String lastName) - implements Person, java.io.Serializable { } + implements Person, java.io.Serializable { + + PersonRecord throws IOException { // compact ctor with throws list + throw new IOException(); + } + } }