Finished rule. Only switch case labels with a following block statement

are counted as decision points.  A case with only a break counts as 1.
A case with an expression but no break counts as 1.  A case with an
expression and a break counts as 1.


git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@1383 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Don Leckie
2003-01-28 03:21:55 +00:00
parent adeaa6b5d5
commit bb30b7e839

View File

@ -3,6 +3,7 @@ package net.sourceforge.pmd.rules;
import net.sourceforge.pmd.AbstractRule;
import net.sourceforge.pmd.RuleContext;
import net.sourceforge.pmd.RuleViolation;
import net.sourceforge.pmd.ast.ASTBlockStatement;
import net.sourceforge.pmd.ast.ASTConstructorDeclaration;
import net.sourceforge.pmd.ast.ASTForStatement;
import net.sourceforge.pmd.ast.ASTIfStatement;
@ -10,6 +11,7 @@ import net.sourceforge.pmd.ast.ASTInterfaceDeclaration;
import net.sourceforge.pmd.ast.ASTMethodDeclaration;
import net.sourceforge.pmd.ast.ASTMethodDeclarator;
import net.sourceforge.pmd.ast.ASTSwitchLabel;
import net.sourceforge.pmd.ast.ASTSwitchStatement;
import net.sourceforge.pmd.ast.ASTUnmodifiedClassDeclaration;
import net.sourceforge.pmd.ast.ASTWhileStatement;
import net.sourceforge.pmd.ast.Node;
@ -70,13 +72,28 @@ public class CyclomaticComplexityRule extends AbstractRule
*
* @return
*/
public Object visit(ASTSwitchLabel node, Object data)
public Object visit(ASTSwitchStatement node, Object data)
{
Entry entry = (Entry) m_entryStack.peek();
// *******
// Needs work: don't count label if there is no block under it.
entry.m_decisionPoints++;
int childCount = node.jjtGetNumChildren();
int lastIndex = childCount - 1;
for (int n = 0; n < lastIndex; n++)
{
Node childNode = node.jjtGetChild(n);
if (childNode instanceof ASTSwitchLabel)
{
childNode = node.jjtGetChild(n + 1);
if (childNode instanceof ASTBlockStatement)
{
entry.m_decisionPoints++;
}
}
}
super.visit(node, data);
return data;