Corrections from PR review (#908), make the metrics visitors extendable again

This commit is contained in:
Andreas Dangel
2018-03-10 20:46:33 +01:00
parent 8bd1e3b486
commit d6ccc58f20
7 changed files with 11 additions and 34 deletions

View File

@ -195,15 +195,13 @@ public final class Helper {
static boolean isSystemLevelClass(ASTUserClass node) {
List<TypeRef> interfaces = node.getNode().getDefiningType().getCodeUnitDetails().getInterfaceTypeRefs();
boolean hasWhitelistedInterfaces = false;
for (TypeRef intObject : interfaces) {
if (isWhitelisted(intObject.getNames())) {
hasWhitelistedInterfaces = true;
break;
return true;
}
}
return hasWhitelistedInterfaces;
return false;
}
private static boolean isWhitelisted(List<Identifier> ids) {

View File

@ -37,16 +37,9 @@ class BenchmarkResult implements Comparable<BenchmarkResult> {
@Override
public int compareTo(BenchmarkResult benchmarkResult) {
int cmp = type.index - benchmarkResult.type.index;
int cmp = Integer.compare(type.index, benchmarkResult.type.index);
if (cmp == 0) {
long delta = this.time - benchmarkResult.time;
if (delta > 0) {
cmp = 1;
} else if (delta < 0) {
cmp = -1;
} else {
cmp = 0;
}
cmp = Long.compare(this.time, benchmarkResult.time);
}
return cmp;
}

View File

@ -6,6 +6,7 @@ package net.sourceforge.pmd.lang.java.metrics.impl.visitors;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.mutable.MutableInt;
import net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression;
@ -15,7 +16,6 @@ import net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression;
import net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix;
import net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix;
import net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter;
import net.sourceforge.pmd.util.StringUtil;
/**
* Computes Atfd.
@ -41,7 +41,7 @@ public class AtfdBaseVisitor extends JavaParserVisitorAdapter {
String methodOrAttributeName = getMethodOrAttributeName(node);
return methodOrAttributeName != null && StringUtil.startsWithAny(methodOrAttributeName, "get", "is", "set");
return methodOrAttributeName != null && StringUtils.startsWithAny(methodOrAttributeName, "get", "is", "set");
}

View File

@ -25,15 +25,11 @@ import net.sourceforge.pmd.lang.java.ast.JavaParserControllessVisitorAdapter;
* @author Clément Fournier
* @see net.sourceforge.pmd.lang.java.metrics.impl.CycloMetric
*/
public final class CycloBaseVisitor extends JavaParserControllessVisitorAdapter {
public class CycloBaseVisitor extends JavaParserControllessVisitorAdapter {
/** Instance. */
public static final CycloBaseVisitor INSTANCE = new CycloBaseVisitor();
private CycloBaseVisitor() {
}
@Override
public Object visit(ASTSwitchStatement node, Object data) {
int childCount = node.jjtGetNumChildren();

View File

@ -41,16 +41,11 @@ import net.sourceforge.pmd.lang.java.ast.JavaParserControllessVisitorAdapter;
* @author Clément Fournier
* @see net.sourceforge.pmd.lang.java.metrics.impl.NcssMetric
*/
public final class NcssBaseVisitor extends JavaParserControllessVisitorAdapter {
public class NcssBaseVisitor extends JavaParserControllessVisitorAdapter {
/** Instance. */
public static final NcssBaseVisitor INSTANCE = new NcssBaseVisitor();
private NcssBaseVisitor() {
}
@Override
public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
((MutableInt) data).increment();

View File

@ -28,14 +28,11 @@ import net.sourceforge.pmd.lang.java.metrics.impl.CycloMetric;
* @author Clément Fournier
* @author Jason Bennett
*/
public final class NpathBaseVisitor extends JavaParserVisitorReducedAdapter {
public class NpathBaseVisitor extends JavaParserVisitorReducedAdapter {
/** Instance. */
public static final NpathBaseVisitor INSTANCE = new NpathBaseVisitor();
private NpathBaseVisitor() {
}
/* Multiplies the complexity of the children of this node. */
private int multiplyChildrenComplexities(JavaNode node, Object data) {
int product = 1;

View File

@ -397,7 +397,6 @@ public class ForLoopCanBeForeachRule extends AbstractJavaRule {
return false;
}
boolean onlyCallingNext = true;
for (NameOccurrence occ : indexInfo.getValue()) {
ScopedNode location = occ.getLocation();
boolean isCallingNext = location instanceof ASTName
@ -405,11 +404,10 @@ public class ForLoopCanBeForeachRule extends AbstractJavaRule {
|| location.hasImageEqualTo(indexName + ".next"));
if (!isCallingNext) {
onlyCallingNext = false;
break;
return false;
}
}
return onlyCallingNext;
return true;
}
private boolean isIterableModifiedInsideLoop(Entry<VariableNameDeclaration, List<NameOccurrence>> iterableInfo,