From 36f6f50c0ffe6d48294660a610ba7e996e05ee7d Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Thu, 1 Jul 2021 20:29:07 +0200 Subject: [PATCH] [java] Handle default case in switch labels --- pmd-java/etc/grammar/Java.jjt | 7 +++---- .../pmd/lang/java/ast/Java17PreviewTreeDumpTest.java | 10 +++++----- .../ast/jdkversiontests/java17p/DealingWithNull.txt | 2 +- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/pmd-java/etc/grammar/Java.jjt b/pmd-java/etc/grammar/Java.jjt index 79ddeffb01..b067a3678f 100644 --- a/pmd-java/etc/grammar/Java.jjt +++ b/pmd-java/etc/grammar/Java.jjt @@ -2259,21 +2259,20 @@ void SwitchLabel() : { { inSwitchLabel = true; } ( - "case" CaseLabelElement() ( {checkForMultipleCaseLabels();} "," CaseLabelElement() )* + "case" CaseLabelElement(jjtThis) ( {checkForMultipleCaseLabels();} "," CaseLabelElement(jjtThis) )* | "default" {jjtThis.setDefault();} ) { inSwitchLabel = false; } } -void CaseLabelElement() #void: +void CaseLabelElement(ASTSwitchLabel label) #void: {} { LOOKAHEAD(1) NullLiteral() {checkForNullCaseLabel();} | - "default" {checkForDefaultCaseLabel();} + "default" {label.setDefault(); checkForDefaultCaseLabel();} | - // LOOKAHEAD(Type() ) Type() VariableDeclaratorId() #TypePattern(2) {checkForPatternMatchingInSwitch();} LOOKAHEAD(Pattern()) Pattern() {checkForPatternMatchingInSwitch();} | ConditionalExpression() #Expression diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/Java17PreviewTreeDumpTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/Java17PreviewTreeDumpTest.java index 5aa20f9a62..194634aebf 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/Java17PreviewTreeDumpTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/ast/Java17PreviewTreeDumpTest.java @@ -32,11 +32,6 @@ public class Java17PreviewTreeDumpTest extends BaseTreeDumpTest { java17.parseResource("PatternsInSwitchLabels.java"); } - @Test(expected = ParseException.class) - public void dealingWithNullBeforeJava17Preview() { - java17.parseResource("DealingWithNull.java"); - } - @Test public void patternMatchingForSwitch() { doTest("PatternsInSwitchLabels"); @@ -52,6 +47,11 @@ public class Java17PreviewTreeDumpTest extends BaseTreeDumpTest { doTest("ScopeOfPatternVariableDeclarations"); } + @Test(expected = ParseException.class) + public void dealingWithNullBeforeJava17Preview() { + java17.parseResource("DealingWithNull.java"); + } + @Test public void dealingWithNull() { doTest("DealingWithNull"); diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java17p/DealingWithNull.txt b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java17p/DealingWithNull.txt index 93b4de740b..c6d7db6831 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java17p/DealingWithNull.txt +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/ast/jdkversiontests/java17p/DealingWithNull.txt @@ -275,7 +275,7 @@ | | +- PrimaryPrefix[@SuperModifier = false, @ThisModifier = false] | | +- Name[@Image = "o"] | +- SwitchLabeledExpression[] - | +- SwitchLabel[@Default = false] + | +- SwitchLabel[@Default = true] | | +- NullLiteral[] | +- Expression[@StandAlonePrimitive = false] | +- PrimaryExpression[]