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.ASTMethodDeclarator;
9 import net.sourceforge.pmd.symboltable.NameOccurrence;
10 import net.sourceforge.pmd.symboltable.Scope;
11 import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
12
13 import java.text.MessageFormat;
14 import java.util.Iterator;
15 import java.util.List;
16 import java.util.Map;
17
18 public class AvoidReassigningParametersRule extends AbstractRule {
19
20 public Object visit(ASTMethodDeclarator node, Object data) {
21 Scope scope = node.getScope();
22 Map params = scope.getVariableDeclarations(true);
23 for (Iterator i = params.keySet().iterator(); i.hasNext();) {
24 VariableNameDeclaration decl = (VariableNameDeclaration) i.next();
25 List usages = (List) params.get(decl);
26 for (Iterator j = usages.iterator(); j.hasNext();) {
27 NameOccurrence occ = (NameOccurrence) j.next();
28 if (occ.isOnLeftHandSide() && occ.getNameForWhichThisIsAQualifier() == null && !decl.isArray()) {
29 RuleContext ctx = (RuleContext) data;
30 String msg = MessageFormat.format(getMessage(), new Object[]{decl.getImage()});
31 ctx.getReport().addRuleViolation(createRuleViolation(ctx, decl.getLine(), msg));
32 }
33 }
34 }
35 return super.visit(node, data);
36 }
37 }
This page was automatically generated by Maven