01: /**
02: * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
03: */package net.sourceforge.pmd.rules;
04:
05: import java.util.Map;
06:
07: import net.sourceforge.pmd.AbstractRule;
08: import net.sourceforge.pmd.PropertyDescriptor;
09: import net.sourceforge.pmd.ast.ASTCompilationUnit;
10: import net.sourceforge.pmd.ast.ASTIfStatement;
11: import net.sourceforge.pmd.properties.IntegerProperty;
12:
13: public class AvoidDeeplyNestedIfStmtsRule extends AbstractRule {
14:
15: private int depth;
16: private int depthLimit;
17:
18: private static final PropertyDescriptor problemDepthDescriptor = new IntegerProperty(
19: "problemDepth", "Maximum allowable statement depth", 0,
20: 1.0f);
21:
22: private static final Map<String, PropertyDescriptor> propertyDescriptorsByName = asFixedMap(problemDepthDescriptor);
23:
24: public Object visit(ASTCompilationUnit node, Object data) {
25: depth = 0;
26: depthLimit = getIntProperty(problemDepthDescriptor);
27: return super .visit(node, data);
28: }
29:
30: public Object visit(ASTIfStatement node, Object data) {
31: if (!node.hasElse()) {
32: depth++;
33: }
34: super .visit(node, data);
35: if (depth == depthLimit) {
36: addViolation(data, node);
37: }
38: depth--;
39: return data;
40: }
41:
42: /**
43: * @return Map
44: */
45: protected Map<String, PropertyDescriptor> propertiesByName() {
46: return propertyDescriptorsByName;
47: }
48: }
|