1 |
| |
2 |
| |
3 |
| |
4 |
| package net.sourceforge.pmd.rules; |
5 |
| |
6 |
| import net.sourceforge.pmd.AbstractRule; |
7 |
| import net.sourceforge.pmd.ast.ASTCompilationUnit; |
8 |
| import net.sourceforge.pmd.ast.ASTIfStatement; |
9 |
| |
10 |
| public class AvoidDeeplyNestedIfStmtsRule extends AbstractRule { |
11 |
| |
12 |
| private int depth; |
13 |
| |
14 |
2
| public Object visit(ASTCompilationUnit node, Object data) {
|
15 |
2
| depth = 0;
|
16 |
2
| return super.visit(node, data);
|
17 |
| } |
18 |
| |
19 |
6
| public Object visit(ASTIfStatement node, Object data) {
|
20 |
6
| if (!node.hasElse()) {
|
21 |
3
| depth++;
|
22 |
| } |
23 |
6
| super.visit(node, data);
|
24 |
6
| if (depth == getIntProperty("problemDepth")) {
|
25 |
1
| addViolation(data, node);
|
26 |
| } |
27 |
6
| depth--;
|
28 |
6
| return data;
|
29 |
| } |
30 |
| |
31 |
| } |