01: package org.acm.seguin.pmd.rules;
02:
03: import org.acm.seguin.pmd.AbstractRule;
04: import org.acm.seguin.pmd.RuleContext;
05: import net.sourceforge.jrefactory.ast.ASTCompilationUnit;
06: import net.sourceforge.jrefactory.ast.ASTIfStatement;
07:
08: public class AvoidDeeplyNestedIfStmtsRule extends AbstractRule {
09:
10: private int depth;
11:
12: public Object visit(ASTCompilationUnit node, Object data) {
13: depth = 0;
14: return super .visit(node, data);
15: }
16:
17: public Object visit(ASTIfStatement node, Object data) {
18: if (!node.hasElse()) {
19: depth++;
20: }
21: super .visit(node, data);
22: if (depth == getIntProperty("problemDepth")) {
23: RuleContext ctx = (RuleContext) data;
24: ctx.getReport().addRuleViolation(
25: createRuleViolation(ctx, node.getBeginLine()));
26: }
27: depth--;
28: return data;
29: }
30:
31: }
|