From f813961ec510181348d4325e6a939dd036d377cf Mon Sep 17 00:00:00 2001 From: Edward Klimoshenko Date: Fri, 19 Aug 2022 10:07:54 +0000 Subject: [PATCH] Build variable declarations --- .../lang/apex/ast/ASTVariableDeclaration.java | 23 ++++---------- .../ast/ASTVariableDeclarationStatements.java | 11 ++++--- .../pmd/lang/apex/ast/ApexTreeBuilder.kt | 30 ++++++++++++++++++- 3 files changed, 42 insertions(+), 22 deletions(-) diff --git a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/ASTVariableDeclaration.java b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/ASTVariableDeclaration.java index 23ff7c7e96..98996077a5 100644 --- a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/ASTVariableDeclaration.java +++ b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/ASTVariableDeclaration.java @@ -4,15 +4,16 @@ package net.sourceforge.pmd.lang.apex.ast; -import com.google.summit.ast.Node; import net.sourceforge.pmd.Rule; import net.sourceforge.pmd.annotation.InternalApi; -public class ASTVariableDeclaration extends AbstractApexNode.Single implements CanSuppressWarnings { +import com.google.summit.ast.declaration.VariableDeclaration; + +public class ASTVariableDeclaration extends AbstractApexNode.Single implements CanSuppressWarnings { @Deprecated @InternalApi - public ASTVariableDeclaration(Node variableDeclaration) { + public ASTVariableDeclaration(VariableDeclaration variableDeclaration) { super(variableDeclaration); } @@ -23,13 +24,7 @@ public class ASTVariableDeclaration extends AbstractApexNode.Single implem @Override public String getImage() { - /* - if (node.getLocalInfo() != null) { - return node.getLocalInfo().getName(); - } - */ - // TODO(b/239648780) - return null; + return node.getId().getString(); } @Override @@ -47,12 +42,6 @@ public class ASTVariableDeclaration extends AbstractApexNode.Single implem } public String getType() { - /* - if (node.getLocalInfo() != null) { - return node.getLocalInfo().getType().getApexName(); - } - */ - // TODO(b/239648780) - return null; + return node.getType().asCodeString(); } } diff --git a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/ASTVariableDeclarationStatements.java b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/ASTVariableDeclarationStatements.java index f10e09abef..0adf680101 100644 --- a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/ASTVariableDeclarationStatements.java +++ b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/ASTVariableDeclarationStatements.java @@ -4,15 +4,18 @@ package net.sourceforge.pmd.lang.apex.ast; -import com.google.summit.ast.Node; +import java.util.List; + import net.sourceforge.pmd.annotation.InternalApi; -public class ASTVariableDeclarationStatements extends AbstractApexNode.Single { +import com.google.summit.ast.declaration.VariableDeclaration; + +public class ASTVariableDeclarationStatements extends AbstractApexNode.Many { @Deprecated @InternalApi - public ASTVariableDeclarationStatements(Node variableDeclarationStatements) { - super(variableDeclarationStatements); + public ASTVariableDeclarationStatements(List variableDeclarations) { + super(variableDeclarations); } @Override diff --git a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/ApexTreeBuilder.kt b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/ApexTreeBuilder.kt index bacbf48a21..0f5c7279d0 100644 --- a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/ApexTreeBuilder.kt +++ b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/ApexTreeBuilder.kt @@ -22,6 +22,7 @@ import com.google.summit.ast.declaration.MethodDeclaration import com.google.summit.ast.declaration.PropertyDeclaration import com.google.summit.ast.declaration.TriggerDeclaration import com.google.summit.ast.declaration.TypeDeclaration +import com.google.summit.ast.declaration.VariableDeclaration import com.google.summit.ast.expression.ArrayExpression import com.google.summit.ast.expression.AssignExpression import com.google.summit.ast.expression.BinaryExpression @@ -48,6 +49,7 @@ import com.google.summit.ast.statement.CompoundStatement import com.google.summit.ast.statement.ExpressionStatement import com.google.summit.ast.statement.IfStatement import com.google.summit.ast.statement.Statement +import com.google.summit.ast.statement.VariableDeclarationStatement @Deprecated("internal") @InternalApi @@ -121,6 +123,8 @@ class ApexTreeBuilder(val sourceCode: String, val parserOptions: ApexParserOptio is SizedArrayInitializer -> buildSizedArrayInitializer(node) is DmlStatement -> buildDmlStatement(node) is IfStatement -> buildIfStatement(node) + is VariableDeclarationStatement -> buildVariableDeclarations(node.variableDeclarations) + is VariableDeclaration -> buildVariableDeclaration(node) is Identifier, is KeywordModifier, is TypeRef -> null @@ -353,7 +357,7 @@ class ApexTreeBuilder(val sourceCode: String, val parserOptions: ApexParserOptio components: List, receiver: Node?, referenceType: ReferenceType, - isSafe: Boolean + isSafe: Boolean = false ) = if (receiver == null && components.isEmpty()) { ASTEmptyReferenceExpression() @@ -482,6 +486,30 @@ class ApexTreeBuilder(val sourceCode: String, val parserOptions: ApexParserOptio FlatIfStatement(ifBlocks, elseBlock = node) } + /** Builds an [ASTVariableDeclarationStatements] for the [VariableDeclaration] list. */ + private fun buildVariableDeclarations(declarations: List) = + ASTVariableDeclarationStatements(declarations).apply { + if (declarations.isNotEmpty()) { + // Modifiers are duplicated between all declarations - use any + buildModifiers(declarations.first().modifiers).also { it.setParent(this) } + } + declarations.forEach { buildAndSetParent(it, parent = this) } + } + + /** Builds an [ASTVariableDeclaration] wrapper for the [VariableDeclaration]. */ + private fun buildVariableDeclaration(node: VariableDeclaration) = + ASTVariableDeclaration(node).apply { + // Exclude modifiers - built in ASTVariableDeclarationStatements + buildChildren(node, parent = this, exclude = { it in node.modifiers }) + + ASTVariableExpression(node.id) + .apply { + buildReferenceExpression(components = emptyList(), receiver = null, ReferenceType.NONE) + .also { it.setParent(this) } + } + .also { it.setParent(this) } + } + /** Builds an [ASTStandardCondition] wrapper for the [condition]. */ private fun buildCondition(condition: Node?) = ASTStandardCondition(condition).apply { buildAndSetParent(condition, this) }