01: /**
02: * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
03: */package net.sourceforge.pmd.rules;
04:
05: import java.util.List;
06: import java.util.Map;
07:
08: import net.sourceforge.pmd.AbstractJavaRule;
09: import net.sourceforge.pmd.ast.ASTConstructorDeclaration;
10: import net.sourceforge.pmd.ast.ASTMethodDeclarator;
11: import net.sourceforge.pmd.symboltable.NameOccurrence;
12: import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
13:
14: public class AvoidReassigningParameters extends AbstractJavaRule {
15:
16: @Override
17: public Object visit(ASTMethodDeclarator node, Object data) {
18: Map<VariableNameDeclaration, List<NameOccurrence>> params = node
19: .getScope().getVariableDeclarations();
20: this .lookForViolation(params, data);
21: return super .visit(node, data);
22: }
23:
24: private void lookForViolation(
25: Map<VariableNameDeclaration, List<NameOccurrence>> params,
26: Object data) {
27: for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : params
28: .entrySet()) {
29: VariableNameDeclaration decl = entry.getKey();
30: List<NameOccurrence> usages = entry.getValue();
31: for (NameOccurrence occ : usages) {
32: if ((occ.isOnLeftHandSide() || occ.isSelfAssignment())
33: && occ.getNameForWhichThisIsAQualifier() == null
34: && (!occ.useThisOrSuper())
35: && (!decl.isArray() || occ.getLocation()
36: .jjtGetParent().jjtGetParent()
37: .jjtGetNumChildren() == 1)) {
38: // not an array or no primary suffix to access the array values
39: addViolation(data, decl.getNode(), decl.getImage());
40: }
41: }
42: }
43: }
44:
45: @Override
46: public Object visit(ASTConstructorDeclaration node, Object data) {
47: Map<VariableNameDeclaration, List<NameOccurrence>> params = node
48: .getScope().getVariableDeclarations();
49: this.lookForViolation(params, data);
50: return super.visit(node, data);
51: }
52: }
|