From bb30b7e839fc54659fe02b916d31365051a7bcfa Mon Sep 17 00:00:00 2001 From: Don Leckie Date: Tue, 28 Jan 2003 03:21:55 +0000 Subject: [PATCH] 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 --- .../pmd/rules/CyclomaticComplexityRule.java | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/pmd/src/net/sourceforge/pmd/rules/CyclomaticComplexityRule.java b/pmd/src/net/sourceforge/pmd/rules/CyclomaticComplexityRule.java index 32473d5b44..55203a9829 100644 --- a/pmd/src/net/sourceforge/pmd/rules/CyclomaticComplexityRule.java +++ b/pmd/src/net/sourceforge/pmd/rules/CyclomaticComplexityRule.java @@ -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;