Add empty modifier list for pattern variables

This commit is contained in:
Clément Fournier
2020-03-02 22:42:18 +01:00
parent f98fec8a44
commit fa58ac98d2
4 changed files with 33 additions and 6 deletions

View File

@ -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() :
<IDENTIFIER> { setLastTokenImage(jjtThis); }
}
void BindingVarId() #VariableDeclaratorId:
{ pushEmptyModifierList(); }
{
<IDENTIFIER> { 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();

View File

@ -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<JModifier> effective) {
// resources are implicitly final
if (node.isPatternBinding()) {
effective.add(FINAL);
}
}
@Override
public void visit(ASTLocalVariableDeclaration node, Set<JModifier> effective) {
// resources are implicitly final
@ -217,7 +226,6 @@ public final class ASTModifierList extends AbstractJavaNode {
}
}
@Override
public void visit(ASTEnumConstant node, Set<JModifier> effective) {
effective.add(PUBLIC);

View File

@ -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();
}

View File

@ -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<ASTPatternExpression> {
it::getPattern shouldBe child<ASTTypeTestPattern> {
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
}
}
}
}