Applied patch from Jason Bennett to enhance CyclomaticComplexity rule to account for conditional or/and nodes, do stmts, and catch blocks.

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4504 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Tom Copeland
2006-09-13 02:50:47 +00:00
parent e9ed53f161
commit fd46de18d8
4 changed files with 42 additions and 1 deletions

View File

@ -33,6 +33,7 @@ Added the image to the ASTEnumConstant nodes.
Added new XSLT stylesheet for CPD XML->HTML from Max Tardiveau.
Refactored UseIndexOfChar to extract common functionality into AbstractPoorMethodCall.
Improved CPD GUI and Designer look/functionality; thanks to Brian Remedios for the changes!
Applied patch from Jason Bennett to enhance CyclomaticComplexity rule to account for conditional or/and nodes, do stmts, and catch blocks.
Performance Refactoring, XPath rules re-written as Java:
BooleanInstantiation
UselessOperationOnImmutable

View File

@ -35,7 +35,7 @@ public class CyclomaticComplexityTest extends RuleTst {
runTestFromString(TEST2, rule, report);
Iterator i = report.iterator();
RuleViolation rv = (RuleViolation) i.next();
assertTrue(rv.getDescription().indexOf("Highest = 12") != -1);
assertTrue(rv.getDescription().indexOf("Highest = 11") != -1);
}
public void testConstructor() throws Throwable {

View File

@ -5,8 +5,13 @@ package net.sourceforge.pmd.rules;
import net.sourceforge.pmd.AbstractRule;
import net.sourceforge.pmd.ast.ASTBlockStatement;
import net.sourceforge.pmd.ast.ASTCatchStatement;
import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
import net.sourceforge.pmd.ast.ASTConditionalAndExpression;
import net.sourceforge.pmd.ast.ASTConditionalExpression;
import net.sourceforge.pmd.ast.ASTConditionalOrExpression;
import net.sourceforge.pmd.ast.ASTConstructorDeclaration;
import net.sourceforge.pmd.ast.ASTDoStatement;
import net.sourceforge.pmd.ast.ASTEnumDeclaration;
import net.sourceforge.pmd.ast.ASTForStatement;
import net.sourceforge.pmd.ast.ASTIfStatement;
@ -57,12 +62,24 @@ public class CyclomaticComplexity extends AbstractRule {
super.visit(node, data);
return data;
}
public Object visit(ASTCatchStatement node, Object data) {
((Entry) entryStack.peek()).bumpDecisionPoints();
super.visit(node, data);
return data;
}
public Object visit(ASTForStatement node, Object data) {
((Entry) entryStack.peek()).bumpDecisionPoints();
super.visit(node, data);
return data;
}
public Object visit(ASTDoStatement node, Object data) {
((Entry) entryStack.peek()).bumpDecisionPoints();
super.visit(node, data);
return data;
}
public Object visit(ASTSwitchStatement node, Object data) {
Entry entry = (Entry) entryStack.peek();
@ -71,10 +88,14 @@ public class CyclomaticComplexity extends AbstractRule {
for (int n = 0; n < lastIndex; n++) {
Node childNode = node.jjtGetChild(n);
if (childNode instanceof ASTSwitchLabel) {
// default is generally not considered a decision (same as "else")
ASTSwitchLabel sl = (ASTSwitchLabel) childNode;
if (!sl.isDefault()) {
childNode = node.jjtGetChild(n + 1);
if (childNode instanceof ASTBlockStatement) {
entry.bumpDecisionPoints();
}
}
}
}
super.visit(node, data);
@ -86,6 +107,24 @@ public class CyclomaticComplexity extends AbstractRule {
super.visit(node, data);
return data;
}
public Object visit(ASTConditionalExpression node, Object data) {
((Entry) entryStack.peek()).bumpDecisionPoints();
super.visit(node, data);
return data;
}
public Object visit(ASTConditionalAndExpression node, Object data) {
((Entry) entryStack.peek()).bumpDecisionPoints();
super.visit(node, data);
return data;
}
public Object visit(ASTConditionalOrExpression node, Object data) {
((Entry) entryStack.peek()).bumpDecisionPoints();
super.visit(node, data);
return data;
}
public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
if (node.isInterface()) {

View File

@ -48,6 +48,7 @@
</subsection>
<subsection name="Contributors">
<ul>
<li>Jason Bennett - Patch to improve CyclomaticComplexity rule</li>
<li>Wouter Zelle - Wrote BrokenNullCheck rule, fixed a false positive in InefficientStringBuffering, fixed a false positive in NonThreadSafeSingleton, a nice patch to clean up some of the Ant task properties and fix a TextRenderer bug, rewrote PositionLiteralsFirstInComparisons in XPath, Renderer improvement suggestions, wrote NonThreadSafeSingleton rule, wrote DefaultPackage rule, worked thru ASTMethodDeclaration.isSyntacticallyX design, reported docs bug 1292689 for UnnecessaryLocalBeforeReturn, reported leftover ExceptionTypeChecking source file, rewrote UselessOverridingMethod in Java, UselessOverridingMethod rule, ProperLogger rule, nifty code to allow variables in XPath rules, externalInfoUrl data for all rules in basic and unusedcode rulesets, some very nifty XSLT, improvements to UseCorrectExceptionLogging, designed and implemented the "externalInfoUrl" feature in the rule definitions, fixed a devious bug in RuleSetFactory, AvoidPrintStackTrace, initial implementation of SimplifyConditional</li>
<li>Sven Jacob - Some fixes for the DFA layer.</li>
<li>Max Tardiveau - A nice XML to HTML stylesheet for CPD.</li>