Fix code duplications

This commit is contained in:
Andreas Dangel
2021-09-08 20:11:49 +02:00
parent 64ef8957bc
commit bd6c72e0f5
8 changed files with 64 additions and 80 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -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<ASTDmlUpsertStatement> dmlUpsertStatement = node.findDescendantsOfType(ASTDmlUpsertStatement.class);
final List<ASTDmlUpdateStatement> dmlUpdateStatement = node.findDescendantsOfType(ASTDmlUpdateStatement.class);
final List<ASTDmlUndeleteStatement> dmlUndeleteStatement = node
.findDescendantsOfType(ASTDmlUndeleteStatement.class);
final List<ASTDmlMergeStatement> dmlMergeStatement = node.findDescendantsOfType(ASTDmlMergeStatement.class);
final List<ASTDmlInsertStatement> dmlInsertStatement = node.findDescendantsOfType(ASTDmlInsertStatement.class);
final List<ASTDmlDeleteStatement> 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) {

View File

@ -8,7 +8,7 @@ import org.mozilla.javascript.ast.FunctionCall;
import net.sourceforge.pmd.annotation.InternalApi;
public class ASTFunctionCall extends AbstractEcmascriptNode<FunctionCall> {
public class ASTFunctionCall extends AbstractFunctionCallNode<FunctionCall> {
@Deprecated
@InternalApi
public ASTFunctionCall(FunctionCall functionCall) {
@ -19,20 +19,4 @@ public class ASTFunctionCall extends AbstractEcmascriptNode<FunctionCall> {
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;
}
}

View File

@ -8,7 +8,7 @@ import org.mozilla.javascript.ast.NewExpression;
import net.sourceforge.pmd.annotation.InternalApi;
public class ASTNewExpression extends AbstractEcmascriptNode<NewExpression> {
public class ASTNewExpression extends AbstractFunctionCallNode<NewExpression> {
@Deprecated
@InternalApi
public ASTNewExpression(NewExpression newExpression) {
@ -20,22 +20,6 @@ public class ASTNewExpression extends AbstractEcmascriptNode<NewExpression> {
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;
}

View File

@ -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<T extends FunctionCall> extends AbstractEcmascriptNode<T> {
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;
}
}

View File

@ -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
}

View File

@ -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
}