View Javadoc

    1   /***
    2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
    3    */
    4   package net.sourceforge.pmd.rules.optimization;
    5
    6   import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
    7   import net.sourceforge.pmd.ast.ASTLocalVariableDeclaration;
    8   import net.sourceforge.pmd.ast.ASTMethodDeclaration;
    9
    10  public class LocalVariableCouldBeFinal extends AbstractOptimizationRule {
    11
    12      public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
    13          if (node.isInterface()) {
    14              return data;
    15          }
    16          return super.visit(node, data);
    17      }
    18
    19      public Object visit(ASTLocalVariableDeclaration node, Object data) {
    20          if (node.isFinal()) {
    21              return data;
    22          }
    23          final String varName = getVarName(node);
    24
    25          ASTMethodDeclaration md = (ASTMethodDeclaration) node.getFirstParentOfType(ASTMethodDeclaration.class);
    26          if (md != null) {
    27              if (!isVarWritterInMethod(varName, md)) {
    28                  addViolation(data, node);
    29              }
    30          }
    31          return data;
    32      }
    33
    34  }