KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > pmd > rules > UselessAssignment


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 JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.Map JavaDoc;
15
16 public class UselessAssignment extends AbstractRule implements Executable {
17
18     private RuleContext rc;
19
20     public Object JavaDoc visit(ASTMethodDeclaration node, Object JavaDoc data) {
21         this.rc = (RuleContext) data;
22
23 /*
24         IDataFlowNode n1 = node.getDataFlowNode();
25         List f = n1.getFlow();
26         for (Iterator i = f.iterator(); i.hasNext();) {
27             DataFlowNode dfan = (DataFlowNode)i.next();
28             System.out.println(dfan);
29             List va = dfan.getVariableAccess();
30             for (Iterator j = va.iterator(); j.hasNext();) {
31                 VariableAccess o = (VariableAccess)j.next();
32                 System.out.println(o);
33             }
34         }
35 */

36
37         DAAPathFinder a = new DAAPathFinder((IDataFlowNode) node.getDataFlowNode().getFlow().get(0), this);
38         a.run();
39
40         return data;
41     }
42
43     private static class Usage {
44         public int accessType;
45         public IDataFlowNode node;
46
47         public Usage(int accessType, IDataFlowNode node) {
48             this.accessType = accessType;
49             this.node = node;
50         }
51
52         public String JavaDoc toString() {
53             return "accessType = " + accessType + ", line = " + node.getLine();
54         }
55     }
56
57     public void execute(CurrentPath path) {
58         Map JavaDoc hash = new HashMap JavaDoc();
59         //System.out.println("path size is " + path.size());
60
for (Iterator JavaDoc i = path.iterator(); i.hasNext();) {
61             //System.out.println("i = " + i);
62
IDataFlowNode inode = (IDataFlowNode) i.next();
63             if (inode.getVariableAccess() == null) {
64                 continue;
65             }
66             for (int j = 0; j < inode.getVariableAccess().size(); j++) {
67                 VariableAccess va = (VariableAccess) inode.getVariableAccess().get(j);
68                 //System.out.println("inode = " + inode + ", va = " + va);
69
Object JavaDoc o = hash.get(va.getVariableName());
70                 if (o != null) {
71                     Usage u = (Usage) o;
72                     // At some point investigate and possibly reintroduce this line2 thing
73
//int line2 = ((Integer) array.get(1)).intValue();
74

75                     // DD - definition followed by another definition
76
// FIXME need to check for assignment as well!
77
if (va.isDefinition() && va.accessTypeMatches(u.accessType)) {
78                         //System.out.println(va.getVariableName() + ":" + u);
79
addViolation(rc, u.node.getSimpleNode(), va.getVariableName());
80                     }
81 /* // UR - ??
82                   else if (last == VariableAccess.UNDEFINITION && va.isReference()) {
83                         //this.rc.getReport().addRuleViolation(createRuleViolation(rc, inode.getSimpleNode(), va.getVariableName(), "UR"));
84                     }
85                     // DU - variable is defined and then goes out of scope
86                     // i.e., unused parameter
87                     else if (last == VariableAccess.DEFINITION && va.isUndefinition()) {
88                         if (inode.getSimpleNode() != null) {
89                             this.rc.getReport().addRuleViolation(createRuleViolation(rc, tmp, va.getVariableName(), "DU"));
90                         }
91                     }
92 */

93                 }
94                 Usage u = new Usage(va.getAccessType(), inode);
95                 hash.put(va.getVariableName(), u);
96             }
97         }
98     }
99 }
100
Popular Tags