From fa58ac98d2874a6fc48bc600c6368f1d68b25184 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Mon, 2 Mar 2020 22:42:18 +0100 Subject: [PATCH] Add empty modifier list for pattern variables --- pmd-java/etc/grammar/Java.jjt | 10 ++++++++-- .../pmd/lang/java/ast/ASTModifierList.java | 14 +++++++++++--- .../pmd/lang/java/ast/ASTVariableDeclaratorId.java | 8 ++++++++ .../pmd/lang/java/ast/ASTPatternTest.kt | 7 ++++++- 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/pmd-java/etc/grammar/Java.jjt b/pmd-java/etc/grammar/Java.jjt index 9d27e3726e..7b42758705 100644 --- a/pmd-java/etc/grammar/Java.jjt +++ b/pmd-java/etc/grammar/Java.jjt @@ -329,7 +329,7 @@ class JavaParserImpl { emptyMods.setDeclaredModifiers(Collections.emptySet()); - JavaccToken tok = getToken(1); + JavaccToken tok = JavaccToken.implicitBefore(getToken(1)); emptyMods.jjtSetFirstToken(tok); emptyMods.jjtSetLastToken(tok); @@ -1022,6 +1022,12 @@ void VariableDeclaratorId() : { setLastTokenImage(jjtThis); } } +void BindingVarId() #VariableDeclaratorId: +{ pushEmptyModifierList(); } +{ + { setLastTokenImage(jjtThis); } +} + void VariableIdWithDims() #VariableDeclaratorId : {} { @@ -1500,7 +1506,7 @@ void InstanceOfExpression() #void: RelationalExpression() [ LOOKAHEAD(1) ("instanceof" - AnnotatedRefType() [ VariableDeclaratorId() #TypeTestPattern(2) ] + AnnotatedRefType() [ BindingVarId() #TypeTestPattern(2) ] { jjtThis.setOp(BinaryOp.INSTANCEOF); AbstractJavaNode top = jjtree.popNode(); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTModifierList.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTModifierList.java index 31d2119062..fff367dde2 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTModifierList.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTModifierList.java @@ -111,8 +111,9 @@ public final class ASTModifierList extends AbstractJavaNode { return effectiveModifiers; } - public JavaNode getOwner() { - return getParent(); // TODO + /** Returns the node owning this modifier list. */ + public AccessNode getOwner() { + return (AccessNode) getParent(); // TODO } /** @@ -209,6 +210,14 @@ public final class ASTModifierList extends AbstractJavaNode { } } + @Override + public void visit(ASTVariableDeclaratorId node, Set effective) { + // resources are implicitly final + if (node.isPatternBinding()) { + effective.add(FINAL); + } + } + @Override public void visit(ASTLocalVariableDeclaration node, Set effective) { // resources are implicitly final @@ -217,7 +226,6 @@ public final class ASTModifierList extends AbstractJavaNode { } } - @Override public void visit(ASTEnumConstant node, Set effective) { effective.add(PUBLIC); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTVariableDeclaratorId.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTVariableDeclaratorId.java index dabfebcafb..d34426f8af 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTVariableDeclaratorId.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTVariableDeclaratorId.java @@ -6,6 +6,7 @@ package net.sourceforge.pmd.lang.java.ast; import java.util.List; +import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; import net.sourceforge.pmd.annotation.Experimental; @@ -91,8 +92,15 @@ public final class ASTVariableDeclaratorId extends AbstractTypedSymbolDeclarator return children(ASTArrayDimensions.class).first(); } + @NonNull @Override public ASTModifierList getModifiers() { + if (isPatternBinding()) { + JavaNode firstChild = getFirstChild(); + assert firstChild != null : "Binding variable has no modifiers!"; + return (ASTModifierList) firstChild; + } + // delegates modifiers return getModifierOwnerParent().getModifiers(); } diff --git a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ASTPatternTest.kt b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ASTPatternTest.kt index 959a8bc6b0..f95fdc5feb 100644 --- a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ASTPatternTest.kt +++ b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ASTPatternTest.kt @@ -1,6 +1,7 @@ package net.sourceforge.pmd.lang.java.ast import io.kotlintest.matchers.string.shouldContain +import io.kotlintest.shouldBe import net.sourceforge.pmd.lang.ast.test.shouldBe import net.sourceforge.pmd.lang.java.ast.JavaVersion.J14__PREVIEW import java.io.IOException @@ -28,7 +29,11 @@ class ASTPatternTest : ParserTestSpec({ child { it::getPattern shouldBe child { it::getTypeNode shouldBe classType("Class") - it::getVarId shouldBe variableId("c") + it::getVarId shouldBe variableId("c") { + it::getModifiers shouldBe modifiers { } // dummy modifier list + it.hasExplicitModifiers(JModifier.FINAL) shouldBe false + it.hasModifiers(JModifier.FINAL) shouldBe true + } } } }