1 |
| package net.sourceforge.pmd.rules; |
2 |
| |
3 |
| import net.sourceforge.pmd.AbstractRule; |
4 |
| import net.sourceforge.pmd.RuleContext; |
5 |
| import net.sourceforge.pmd.ast.ASTMethodDeclaration; |
6 |
| import net.sourceforge.pmd.dfa.IDataFlowNode; |
7 |
| import net.sourceforge.pmd.dfa.pathfinder.CurrentPath; |
8 |
| import net.sourceforge.pmd.dfa.pathfinder.DAAPathFinder; |
9 |
| import net.sourceforge.pmd.dfa.pathfinder.Executable; |
10 |
| import net.sourceforge.pmd.dfa.variableaccess.VariableAccess; |
11 |
| |
12 |
| import java.util.HashMap; |
13 |
| import java.util.Iterator; |
14 |
| import java.util.Map; |
15 |
| |
16 |
| public class UselessAssignment extends AbstractRule implements Executable { |
17 |
| |
18 |
| private RuleContext rc; |
19 |
| |
20 |
0
| public Object visit(ASTMethodDeclaration node, Object data) {
|
21 |
0
| this.rc = (RuleContext) data;
|
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
0
| DAAPathFinder a = new DAAPathFinder((IDataFlowNode) node.getDataFlowNode().getFlow().get(0), this);
|
38 |
0
| a.run();
|
39 |
| |
40 |
0
| return data;
|
41 |
| } |
42 |
| |
43 |
| private static class Usage { |
44 |
| public int accessType; |
45 |
| public IDataFlowNode node; |
46 |
| |
47 |
0
| public Usage(int accessType, IDataFlowNode node) {
|
48 |
0
| this.accessType = accessType;
|
49 |
0
| this.node = node;
|
50 |
| } |
51 |
| |
52 |
0
| public String toString() {
|
53 |
0
| return "accessType = " + accessType + ", line = " + node.getLine();
|
54 |
| } |
55 |
| } |
56 |
| |
57 |
0
| public void execute(CurrentPath path) {
|
58 |
0
| Map hash = new HashMap();
|
59 |
| |
60 |
0
| for (Iterator i = path.iterator(); i.hasNext();) {
|
61 |
| |
62 |
0
| IDataFlowNode inode = (IDataFlowNode) i.next();
|
63 |
0
| if (inode.getVariableAccess() == null) {
|
64 |
0
| continue;
|
65 |
| } |
66 |
0
| for (int j = 0; j < inode.getVariableAccess().size(); j++) {
|
67 |
0
| VariableAccess va = (VariableAccess) inode.getVariableAccess().get(j);
|
68 |
| |
69 |
0
| Object o = hash.get(va.getVariableName());
|
70 |
0
| if (o != null) {
|
71 |
0
| Usage u = (Usage) o;
|
72 |
| |
73 |
| |
74 |
| |
75 |
| |
76 |
| |
77 |
0
| if (va.isDefinition() && va.accessTypeMatches(u.accessType)) {
|
78 |
| |
79 |
0
| addViolation(rc, u.node.getSimpleNode(), va.getVariableName());
|
80 |
| } |
81 |
| |
82 |
| |
83 |
| |
84 |
| |
85 |
| |
86 |
| |
87 |
| |
88 |
| |
89 |
| |
90 |
| |
91 |
| |
92 |
| |
93 |
| } |
94 |
0
| Usage u = new Usage(va.getAccessType(), inode);
|
95 |
0
| hash.put(va.getVariableName(), u);
|
96 |
| } |
97 |
| } |
98 |
| } |
99 |
| } |