From bd6c72e0f5f36a7fb5291d46b0e4f2bbdd4b1575 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Wed, 8 Sep 2021 20:11:49 +0200 Subject: [PATCH] Fix code duplications --- .../pmd/lang/apex/rule/AbstractApexRule.java | 8 ++++ .../AvoidDmlStatementsInLoopsRule.java | 2 + .../pmd/lang/apex/rule/security/Helper.java | 39 +------------------ .../lang/ecmascript/ast/ASTFunctionCall.java | 18 +-------- .../lang/ecmascript/ast/ASTNewExpression.java | 18 +-------- .../ast/AbstractFunctionCallNode.java | 31 +++++++++++++++ .../rule/AbstractEcmascriptRule.java | 8 ++-- .../pmd/lang/vf/rule/AbstractVfRule.java | 20 +++++++--- 8 files changed, 64 insertions(+), 80 deletions(-) create mode 100644 pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/AbstractFunctionCallNode.java diff --git a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/AbstractApexRule.java b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/AbstractApexRule.java index a931dbc4b5..b068f09989 100644 --- a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/AbstractApexRule.java +++ b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/AbstractApexRule.java @@ -155,6 +155,12 @@ public abstract class AbstractApexRule extends AbstractRule return null; } + // + // The following APIs are identical to those in ApexParserVisitorAdapter. + // Due to Java single inheritance, it is preferred to extend from the more + // complex Rule base class instead of from relatively simple Visitor. + // + // CPD-OFF @Override public Object visit(ApexNode node, Object data) { @@ -648,4 +654,6 @@ public abstract class AbstractApexRule extends AbstractRule public Object visit(ASTEmptyReferenceExpression node, Object data) { return visit((ApexNode) node, data); } + + // CPD-ON } diff --git a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/performance/AvoidDmlStatementsInLoopsRule.java b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/performance/AvoidDmlStatementsInLoopsRule.java index 00b5831988..d7c0360a31 100644 --- a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/performance/AvoidDmlStatementsInLoopsRule.java +++ b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/performance/AvoidDmlStatementsInLoopsRule.java @@ -24,6 +24,7 @@ public class AvoidDmlStatementsInLoopsRule extends AbstractAvoidNodeInLoopsRule setProperty(CODECLIMATE_BLOCK_HIGHLIGHTING, false); } + // CPD-OFF - the same visits are in the replacement rule OperationWithLimitsInLoopRule @Override public Object visit(ASTDmlDeleteStatement node, Object data) { return checkForViolation(node, data); @@ -53,4 +54,5 @@ public class AvoidDmlStatementsInLoopsRule extends AbstractAvoidNodeInLoopsRule public Object visit(ASTDmlUpsertStatement node, Object data) { return checkForViolation(node, data); } + // CPD-ON } diff --git a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/security/Helper.java b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/security/Helper.java index b51c0c2f68..7a9af5e824 100644 --- a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/security/Helper.java +++ b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/security/Helper.java @@ -4,15 +4,8 @@ package net.sourceforge.pmd.lang.apex.rule.security; -import java.util.Arrays; import java.util.List; -import net.sourceforge.pmd.lang.apex.ast.ASTDmlDeleteStatement; -import net.sourceforge.pmd.lang.apex.ast.ASTDmlInsertStatement; -import net.sourceforge.pmd.lang.apex.ast.ASTDmlMergeStatement; -import net.sourceforge.pmd.lang.apex.ast.ASTDmlUndeleteStatement; -import net.sourceforge.pmd.lang.apex.ast.ASTDmlUpdateStatement; -import net.sourceforge.pmd.lang.apex.ast.ASTDmlUpsertStatement; import net.sourceforge.pmd.lang.apex.ast.ASTField; import net.sourceforge.pmd.lang.apex.ast.ASTFieldDeclaration; import net.sourceforge.pmd.lang.apex.ast.ASTMethodCallExpression; @@ -62,17 +55,7 @@ public final class Helper { * @return true if found DML operations in node descendants */ static boolean foundAnyDML(final ApexNode node) { - - final List dmlUpsertStatement = node.findDescendantsOfType(ASTDmlUpsertStatement.class); - final List dmlUpdateStatement = node.findDescendantsOfType(ASTDmlUpdateStatement.class); - final List dmlUndeleteStatement = node - .findDescendantsOfType(ASTDmlUndeleteStatement.class); - final List dmlMergeStatement = node.findDescendantsOfType(ASTDmlMergeStatement.class); - final List dmlInsertStatement = node.findDescendantsOfType(ASTDmlInsertStatement.class); - final List dmlDeleteStatement = node.findDescendantsOfType(ASTDmlDeleteStatement.class); - - return !dmlUpsertStatement.isEmpty() || !dmlUpdateStatement.isEmpty() || !dmlUndeleteStatement.isEmpty() - || !dmlMergeStatement.isEmpty() || !dmlInsertStatement.isEmpty() || !dmlDeleteStatement.isEmpty(); + return net.sourceforge.pmd.lang.apex.rule.internal.Helper.foundAnyDML(node); } static boolean isMethodName(final ASTMethodCallExpression methodNode, final String className, @@ -89,25 +72,7 @@ public final class Helper { } static boolean isMethodCallChain(final ASTMethodCallExpression methodNode, final String... methodNames) { - String methodName = methodNames[methodNames.length - 1]; - if (Helper.isMethodName(methodNode, methodName)) { - final ASTReferenceExpression reference = methodNode.getFirstChildOfType(ASTReferenceExpression.class); - if (reference != null) { - final ASTMethodCallExpression nestedMethod = reference - .getFirstChildOfType(ASTMethodCallExpression.class); - if (nestedMethod != null) { - String[] newMethodNames = Arrays.copyOf(methodNames, methodNames.length - 1); - return isMethodCallChain(nestedMethod, newMethodNames); - } else { - String[] newClassName = Arrays.copyOf(methodNames, methodNames.length - 1); - if (newClassName.length == 1) { - return Helper.isMethodName(methodNode, newClassName[0], methodName); - } - } - } - } - - return false; + return net.sourceforge.pmd.lang.apex.rule.internal.Helper.isMethodCallChain(methodNode, methodNames); } static String getFQVariableName(final ASTVariableExpression variable) { diff --git a/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/ASTFunctionCall.java b/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/ASTFunctionCall.java index b527d45747..1b6ad642c0 100644 --- a/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/ASTFunctionCall.java +++ b/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/ASTFunctionCall.java @@ -8,7 +8,7 @@ import org.mozilla.javascript.ast.FunctionCall; import net.sourceforge.pmd.annotation.InternalApi; -public class ASTFunctionCall extends AbstractEcmascriptNode { +public class ASTFunctionCall extends AbstractFunctionCallNode { @Deprecated @InternalApi public ASTFunctionCall(FunctionCall functionCall) { @@ -19,20 +19,4 @@ public class ASTFunctionCall extends AbstractEcmascriptNode { public Object jjtAccept(EcmascriptParserVisitor visitor, Object data) { return visitor.visit(this, data); } - - public EcmascriptNode getTarget() { - return (EcmascriptNode) getChild(0); - } - - public int getNumArguments() { - return node.getArguments().size(); - } - - public EcmascriptNode getArgument(int index) { - return (EcmascriptNode) getChild(index + 1); - } - - public boolean hasArguments() { - return getNumArguments() != 0; - } } diff --git a/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/ASTNewExpression.java b/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/ASTNewExpression.java index 71b193a31a..a550a40059 100644 --- a/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/ASTNewExpression.java +++ b/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/ASTNewExpression.java @@ -8,7 +8,7 @@ import org.mozilla.javascript.ast.NewExpression; import net.sourceforge.pmd.annotation.InternalApi; -public class ASTNewExpression extends AbstractEcmascriptNode { +public class ASTNewExpression extends AbstractFunctionCallNode { @Deprecated @InternalApi public ASTNewExpression(NewExpression newExpression) { @@ -20,22 +20,6 @@ public class ASTNewExpression extends AbstractEcmascriptNode { return visitor.visit(this, data); } - public EcmascriptNode getTarget() { - return (EcmascriptNode) getChild(0); - } - - public int getNumArguments() { - return node.getArguments().size(); - } - - public EcmascriptNode getArgument(int index) { - return (EcmascriptNode) getChild(index + 1); - } - - public boolean hasArguments() { - return getNumArguments() != 0; - } - public boolean hasInitializer() { return node.getInitializer() != null; } diff --git a/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/AbstractFunctionCallNode.java b/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/AbstractFunctionCallNode.java new file mode 100644 index 0000000000..7ebed0d75e --- /dev/null +++ b/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/AbstractFunctionCallNode.java @@ -0,0 +1,31 @@ +/* + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +package net.sourceforge.pmd.lang.ecmascript.ast; + +import org.mozilla.javascript.ast.FunctionCall; + +class AbstractFunctionCallNode extends AbstractEcmascriptNode { + + AbstractFunctionCallNode(T node) { + super(node); + } + + public EcmascriptNode getTarget() { + return (EcmascriptNode) getChild(0); + } + + public int getNumArguments() { + return node.getArguments().size(); + } + + public EcmascriptNode getArgument(int index) { + return (EcmascriptNode) getChild(index + 1); + } + + public boolean hasArguments() { + return getNumArguments() != 0; + } + +} diff --git a/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/rule/AbstractEcmascriptRule.java b/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/rule/AbstractEcmascriptRule.java index 30e5754cd6..c711333d52 100644 --- a/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/rule/AbstractEcmascriptRule.java +++ b/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/rule/AbstractEcmascriptRule.java @@ -103,11 +103,11 @@ public abstract class AbstractEcmascriptRule extends AbstractRule } // - // The following APIs are identical to those in - // EcmascriptParserVisitorAdapter. - // Due to Java single inheritance, it preferred to extend from the more + // The following APIs are identical to those in EcmascriptParserVisitorAdapter. + // Due to Java single inheritance, it is preferred to extend from the more // complex Rule base class instead of from relatively simple Visitor. // + // CPD-OFF @Override public Object visit(EcmascriptNode node, Object data) { @@ -359,4 +359,6 @@ public abstract class AbstractEcmascriptRule extends AbstractRule public Object visit(ASTXmlString node, Object data) { return visit((EcmascriptNode) node, data); } + + // CPD-ON } diff --git a/pmd-visualforce/src/main/java/net/sourceforge/pmd/lang/vf/rule/AbstractVfRule.java b/pmd-visualforce/src/main/java/net/sourceforge/pmd/lang/vf/rule/AbstractVfRule.java index 7eb1eb01c6..08fdbeae5c 100644 --- a/pmd-visualforce/src/main/java/net/sourceforge/pmd/lang/vf/rule/AbstractVfRule.java +++ b/pmd-visualforce/src/main/java/net/sourceforge/pmd/lang/vf/rule/AbstractVfRule.java @@ -52,10 +52,17 @@ public abstract class AbstractVfRule extends AbstractRule implements VfParserVis } } + // + // The following APIs are identical to those in EcmascriptParserVisitorAdapter. + // Due to Java single inheritance, it is preferred to extend from the more + // complex Rule base class instead of from relatively simple Visitor. + // + // CPD-OFF + @Override public Object visit(VfNode node, Object data) { node.childrenAccept(this, data); - return null; + return data; } @Override @@ -68,11 +75,6 @@ public abstract class AbstractVfRule extends AbstractRule implements VfParserVis return visit((VfNode) node, data); } - @Override - public Object visit(ASTAttributeValue node, Object data) { - return visit((VfNode) node, data); - } - @Override public Object visit(ASTElExpression node, Object data) { return visit((VfNode) node, data); @@ -93,6 +95,11 @@ public abstract class AbstractVfRule extends AbstractRule implements VfParserVis return visit((VfNode) node, data); } + @Override + public Object visit(ASTAttributeValue node, Object data) { + return visit((VfNode) node, data); + } + @Override public Object visit(ASTDeclaration node, Object data) { return visit((VfNode) node, data); @@ -148,4 +155,5 @@ public abstract class AbstractVfRule extends AbstractRule implements VfParserVis return visit((VfNode) node, data); } + // CPD-ON }