01: package org.acm.seguin.pmd.rules;
02:
03: import org.acm.seguin.pmd.AbstractRule;
04: import org.acm.seguin.pmd.RuleContext;
05: import net.sourceforge.jrefactory.ast.ASTMethodDeclarator;
06: import org.acm.seguin.pmd.symboltable.NameOccurrence;
07: import org.acm.seguin.pmd.symboltable.Scope;
08: import org.acm.seguin.pmd.symboltable.VariableNameDeclaration;
09:
10: import java.text.MessageFormat;
11: import java.util.Iterator;
12: import java.util.List;
13: import java.util.Map;
14:
15: public class AvoidReassigningParametersRule extends AbstractRule {
16: public Object visit(ASTMethodDeclarator node, Object data) {
17: Scope scope = (Scope) node.getScope();
18: Map params = scope.getVariableDeclarations(true);
19: for (Iterator i = params.keySet().iterator(); i.hasNext();) {
20: VariableNameDeclaration decl = (VariableNameDeclaration) i
21: .next();
22: List usages = (List) params.get(decl);
23: for (Iterator j = usages.iterator(); j.hasNext();) {
24: NameOccurrence occ = (NameOccurrence) j.next();
25: if (occ.isOnLeftHandSide()
26: && !occ.isArrayAccess()
27: && (occ.getNameForWhichThisIsAQualifier() == null)) {
28: RuleContext ctx = (RuleContext) data;
29: String msg = MessageFormat.format(getMessage(),
30: new Object[] { decl.getImage() });
31: ctx.getReport().addRuleViolation(
32: createRuleViolation(ctx, decl.getLine(),
33: msg));
34: }
35: }
36: }
37: return super.visit(node, data);
38: }
39: }
|