1
2
3
4
5
6
7 package net.sourceforge.pmd.rules.design;
8
9 import net.sourceforge.pmd.AbstractRule;
10 import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
11 import net.sourceforge.pmd.ast.ASTConstructorDeclaration;
12 import net.sourceforge.pmd.ast.SimpleNode;
13 import net.sourceforge.pmd.symboltable.NameOccurrence;
14 import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
15
16 import java.util.Iterator;
17 import java.util.List;
18 import java.util.Map;
19
20
21 /***
22 * @author Eric Olander
23 */
24 public class AssignmentToNonFinalStatic extends AbstractRule {
25
26 public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
27 Map vars = node.getScope().getVariableDeclarations();
28 for (Iterator i = vars.keySet().iterator(); i.hasNext();) {
29 VariableNameDeclaration decl = (VariableNameDeclaration) i.next();
30 if (!decl.getAccessNodeParent().isStatic() || decl.getAccessNodeParent().isFinal()) {
31 continue;
32 }
33
34 if (initializedInConstructor((List) vars.get(decl))) {
35 addViolation(data, decl.getNode(), decl.getImage());
36 }
37 }
38 return super.visit(node, data);
39 }
40
41 private boolean initializedInConstructor(List usages) {
42 boolean initInConstructor = false;
43
44 for (Iterator j = usages.iterator(); j.hasNext();) {
45 NameOccurrence occ = (NameOccurrence) j.next();
46 if (occ.isOnLeftHandSide()) {
47 SimpleNode node = occ.getLocation();
48 SimpleNode constructor = (SimpleNode) node.getFirstParentOfType(ASTConstructorDeclaration.class);
49 if (constructor != null) {
50 initInConstructor = true;
51 }
52 }
53 }
54
55 return initInConstructor;
56 }
57
58 }