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.RuleContext;
8   import net.sourceforge.pmd.ast.ASTLocalVariableDeclaration;
9   import net.sourceforge.pmd.ast.ASTVariableDeclaratorId;
10  import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
11  
12  import java.text.MessageFormat;
13  import java.util.Iterator;
14  
15  public class UnusedLocalVariableRule extends AbstractRule {
16      public Object visit(ASTVariableDeclaratorId node, Object data) {
17          if (node.jjtGetParent().jjtGetParent() instanceof ASTLocalVariableDeclaration) {
18              RuleContext ctx = (RuleContext) data;
19              for (Iterator i = node.getScope().getVariableDeclarations(false).keySet().iterator(); i.hasNext();) {
20                  VariableNameDeclaration decl = (VariableNameDeclaration) i.next();
21                  ctx.getReport().addRuleViolation(createRuleViolation(ctx, decl.getLine(), MessageFormat.format(getMessage(), new Object[]{decl.getImage()})));
22              }
23          }
24          return data;
25      }
26  }