View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.java.rule.codesize;
5   
6   import net.sourceforge.pmd.lang.java.ast.ASTConditionalExpression;
7   import net.sourceforge.pmd.lang.java.ast.ASTDoStatement;
8   import net.sourceforge.pmd.lang.java.ast.ASTExpression;
9   import net.sourceforge.pmd.lang.java.ast.ASTForStatement;
10  import net.sourceforge.pmd.lang.java.ast.ASTIfStatement;
11  import net.sourceforge.pmd.lang.java.ast.ASTSwitchStatement;
12  import net.sourceforge.pmd.lang.java.ast.ASTWhileStatement;
13  
14  /**
15   * @author Donald A. Leckie,
16   *
17   * @version $Revision: 5956 $, $Date: 2008-04-04 04:59:25 -0500 (Fri, 04 Apr 2008) $
18   * @since January 14, 2003
19   */
20  public class CyclomaticComplexityRule extends StdCyclomaticComplexityRule {
21  
22    @Override
23  public Object visit(ASTIfStatement node, Object data) {
24      super.visit( node, data );
25  
26      int boolCompIf = NPathComplexityRule.sumExpressionComplexity( node.getFirstChildOfType( ASTExpression.class ) );
27      entryStack.peek().bumpDecisionPoints( boolCompIf );
28      return data;
29    }
30  
31    @Override
32  public Object visit(ASTForStatement node, Object data) {
33      super.visit( node, data );
34  
35      int boolCompFor = NPathComplexityRule.sumExpressionComplexity( node.getFirstDescendantOfType( ASTExpression.class ) );
36      entryStack.peek().bumpDecisionPoints( boolCompFor );
37      return data;
38    }
39  
40    @Override
41  public Object visit(ASTDoStatement node, Object data) {
42      super.visit( node, data );
43  
44      int boolCompDo = NPathComplexityRule.sumExpressionComplexity( node.getFirstChildOfType( ASTExpression.class ) );
45      entryStack.peek().bumpDecisionPoints( boolCompDo );
46      return data;
47    }
48  
49    @Override
50  public Object visit(ASTSwitchStatement node, Object data) {
51      super.visit( node, data );
52  
53      int boolCompSwitch = NPathComplexityRule.sumExpressionComplexity( node.getFirstChildOfType( ASTExpression.class ) );
54      entryStack.peek().bumpDecisionPoints( boolCompSwitch );
55      return data;
56    }
57  
58    @Override
59  public Object visit(ASTWhileStatement node, Object data) {
60      super.visit( node, data );
61  
62      int boolCompWhile = NPathComplexityRule.sumExpressionComplexity( node.getFirstChildOfType( ASTExpression.class ) );
63      entryStack.peek().bumpDecisionPoints( boolCompWhile );
64      return data;
65    }
66  
67    @Override
68  public Object visit(ASTConditionalExpression node, Object data) {
69      super.visit( node, data );
70  
71      if ( node.isTernary() ) {
72        int boolCompTern = NPathComplexityRule.sumExpressionComplexity( node.getFirstChildOfType( ASTExpression.class ) );
73        entryStack.peek().bumpDecisionPoints( boolCompTern );
74      }
75      return data;
76    }
77  }