View Javadoc

    1   /***
    2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
    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      public Object visit(ASTCompilationUnit node, Object data) {
    15          depth = 0;
    16          return super.visit(node, data);
    17      }
    18
    19      public Object visit(ASTIfStatement node, Object data) {
    20          if (!node.hasElse()) {
    21              depth++;
    22          }
    23          super.visit(node, data);
    24          if (depth == getIntProperty("problemDepth")) {
    25              addViolation(data, node);
    26          }
    27          depth--;
    28          return data;
    29      }
    30
    31  }