diff --git a/pmd-java/etc/grammar/Java.jjt b/pmd-java/etc/grammar/Java.jjt index 3df3167efa..8eac02eeeb 100644 --- a/pmd-java/etc/grammar/Java.jjt +++ b/pmd-java/etc/grammar/Java.jjt @@ -302,6 +302,10 @@ class JavaParserImpl { return isRecordTypeSupported(); } + private boolean isSealedClassSupported() { + return jdkVersion == 16 && preview || jdkVersion >= 17; + } + /** * Keeps track during tree construction, whether we are currently building a switch label. * A switch label must not contain a LambdaExpression. @@ -2446,16 +2450,11 @@ void SwitchLabel() : void CaseLabelElement(ASTSwitchLabel label) #void: {} { - "default" {label.setDefault(); checkForDefaultCaseLabel();} + "default" {label.setDefault();} | - LOOKAHEAD(Pattern()) Pattern() {checkForPatternMatchingInSwitch();} + LOOKAHEAD(Pattern()) Pattern() | - ConditionalExpression() #Expression - { - if ("null".equals(((ASTExpression) jjtree.peekNode()).jjtGetFirstToken().getImage())) { - checkForNullCaseLabel(); - } - } + ConditionalExpression() } void YieldStatement() : diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/internal/LanguageLevelChecker.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/internal/LanguageLevelChecker.java index 906d277119..3b00a27ca9 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/internal/LanguageLevelChecker.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/internal/LanguageLevelChecker.java @@ -29,7 +29,9 @@ import net.sourceforge.pmd.lang.java.ast.ASTLambdaExpression; import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTMethodReference; import net.sourceforge.pmd.lang.java.ast.ASTModuleDeclaration; +import net.sourceforge.pmd.lang.java.ast.ASTNullLiteral; import net.sourceforge.pmd.lang.java.ast.ASTNumericLiteral; +import net.sourceforge.pmd.lang.java.ast.ASTPattern; import net.sourceforge.pmd.lang.java.ast.ASTReceiverParameter; import net.sourceforge.pmd.lang.java.ast.ASTRecordDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTResource; @@ -542,6 +544,15 @@ public class LanguageLevelChecker { if (IteratorUtil.count(node.iterator()) > 1) { check(node, RegularLanguageFeature.COMPOSITE_CASE_LABEL, data); } + if (node.isDefault() && "case".equals(node.getFirstToken().getImage())) { + check(node, PreviewFeature.PATTERN_MATCHING_FOR_SWITCH, data); + } + if (node.getFirstChild() instanceof ASTPattern) { + check(node, PreviewFeature.PATTERN_MATCHING_FOR_SWITCH, data); + } + if (node.getFirstChild() instanceof ASTNullLiteral) { + check(node, PreviewFeature.PATTERN_MATCHING_FOR_SWITCH, data); + } return null; } diff --git a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ASTLiteralTest.kt b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ASTLiteralTest.kt index e829bbdc1e..c2829ae510 100644 --- a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ASTLiteralTest.kt +++ b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ASTLiteralTest.kt @@ -46,7 +46,7 @@ class ASTLiteralTest : ParserTestSpec({ } } - parserTest("Text block literal", javaVersions = since(J15__PREVIEW)) { + parserTest("Text block literal", javaVersions = since(J15)) { val delim = "\"\"\""