01: /**
02: * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
03: */package net.sourceforge.pmd.rules.optimization;
04:
05: import java.util.List;
06: import java.util.Map;
07:
08: import net.sourceforge.pmd.ast.ASTConstructorDeclaration;
09: import net.sourceforge.pmd.ast.ASTFormalParameter;
10: import net.sourceforge.pmd.ast.ASTMethodDeclaration;
11: import net.sourceforge.pmd.ast.AccessNode;
12: import net.sourceforge.pmd.symboltable.NameOccurrence;
13: import net.sourceforge.pmd.symboltable.Scope;
14: import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
15:
16: public class MethodArgumentCouldBeFinal extends
17: AbstractOptimizationRule {
18:
19: @Override
20: public Object visit(ASTMethodDeclaration meth, Object data) {
21: if (meth.isNative() || meth.isAbstract()) {
22: return data;
23: }
24: this .lookForViolation(meth.getScope(), data);
25: return super .visit(meth, data);
26: }
27:
28: private void lookForViolation(Scope scope, Object data) {
29: Map<VariableNameDeclaration, List<NameOccurrence>> decls = scope
30: .getVariableDeclarations();
31: for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : decls
32: .entrySet()) {
33: VariableNameDeclaration var = entry.getKey();
34: AccessNode node = var.getAccessNodeParent();
35: if (!node.isFinal() && (node instanceof ASTFormalParameter)
36: && !assigned(entry.getValue())) {
37: addViolation(data, node, var.getImage());
38: }
39: }
40: }
41:
42: @Override
43: public Object visit(ASTConstructorDeclaration constructor,
44: Object data) {
45: this.lookForViolation(constructor.getScope(), data);
46: return super.visit(constructor, data);
47: }
48:
49: }
|