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

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