View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.vm.rule.basic;
5   
6   import net.sourceforge.pmd.lang.rule.properties.IntegerProperty;
7   import net.sourceforge.pmd.lang.vm.ast.ASTElseIfStatement;
8   import net.sourceforge.pmd.lang.vm.ast.ASTIfStatement;
9   import net.sourceforge.pmd.lang.vm.ast.ASTprocess;
10  import net.sourceforge.pmd.lang.vm.ast.AbstractVmNode;
11  import net.sourceforge.pmd.lang.vm.rule.AbstractVmRule;
12  
13  public class AvoidDeeplyNestedIfStmtsRule extends AbstractVmRule {
14  
15  	private int depth;
16      private int depthLimit;
17      
18      private static final IntegerProperty PROBLEM_DEPTH_DESCRIPTOR = new IntegerProperty(
19      		"problemDepth", 
20      		"The if statement depth reporting threshold",
21      		1, 25,
22      		3,
23      		1.0f
24      		);
25      
26      public AvoidDeeplyNestedIfStmtsRule() {
27      	definePropertyDescriptor(PROBLEM_DEPTH_DESCRIPTOR);
28      }
29      
30      public Object visit(ASTprocess node, Object data) {
31          depth = 0;
32          depthLimit = getProperty(PROBLEM_DEPTH_DESCRIPTOR);
33          return super.visit(node, data);
34      }
35      
36      public Object visit(ASTIfStatement node, Object data) {
37          return handleIf(node, data);
38      }
39      
40      public Object visit(ASTElseIfStatement node, Object data) {
41          return handleIf(node, data);
42      }
43  
44  
45  	private Object handleIf(AbstractVmNode node, Object data) {
46  		depth++;
47          super.visit(node, data);
48          if (depth == depthLimit) {
49              addViolation(data, node);
50          }
51          depth--;
52          return data;
53  	}
54  	
55  }