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 AvoidWritingToArrayParametersRule extends AbstractRule {
16:
17: public Object visit(ASTMethodDeclarator node, Object data) {
18: Scope scope = (Scope) node.getScope();
19: Map params = scope.getVariableDeclarations(true);
20: for (Iterator i = params.keySet().iterator(); i.hasNext();) {
21: VariableNameDeclaration decl = (VariableNameDeclaration) i
22: .next();
23: List usages = (List) params.get(decl);
24: for (Iterator j = usages.iterator(); j.hasNext();) {
25: NameOccurrence occ = (NameOccurrence) j.next();
26: if (occ.isOnLeftHandSide()
27: && occ.isArrayAccess()
28: && (occ.getNameForWhichThisIsAQualifier() == null)) {
29: RuleContext ctx = (RuleContext) data;
30: String msg = MessageFormat.format(getMessage(),
31: new Object[] { decl.getImage() });
32: ctx.getReport().addRuleViolation(
33: createRuleViolation(ctx, decl.getLine(),
34: msg));
35: }
36: }
37: }
38: return super.visit(node, data);
39: }
40: }
|