View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.java.rule;
5   
6   import java.util.HashMap;
7   import java.util.Iterator;
8   import java.util.Map;
9   
10  import net.sourceforge.pmd.RuleContext;
11  import net.sourceforge.pmd.lang.dfa.DataFlowNode;
12  import net.sourceforge.pmd.lang.dfa.VariableAccess;
13  import net.sourceforge.pmd.lang.dfa.pathfinder.CurrentPath;
14  import net.sourceforge.pmd.lang.dfa.pathfinder.DAAPathFinder;
15  import net.sourceforge.pmd.lang.dfa.pathfinder.Executable;
16  import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
17  
18  //FUTURE This is not referenced by any RuleSet?
19  public class UselessAssignment extends AbstractJavaRule implements Executable {
20  
21      private RuleContext rc;
22  
23      public Object visit(ASTMethodDeclaration node, Object data) {
24          this.rc = (RuleContext) data;
25  
26  /*
27          IDataFlowNode n1 = node.getDataFlowNode();
28          List f = n1.getFlow();
29          for (Iterator i = f.iterator(); i.hasNext();) {
30              DataFlowNode dfan = (DataFlowNode)i.next();
31              System.out.println(dfan);
32              List va = dfan.getVariableAccess();
33              for (Iterator j = va.iterator(); j.hasNext();) {
34                  VariableAccess o = (VariableAccess)j.next();
35                  System.out.println(o);
36              }
37          }
38  */
39  
40          DAAPathFinder a = new DAAPathFinder(node.getDataFlowNode().getFlow().get(0), this);
41          a.run();
42  
43          return data;
44      }
45  
46      private static class Usage {
47          public int accessType;
48          public DataFlowNode node;
49  
50          public Usage(int accessType, DataFlowNode node) {
51              this.accessType = accessType;
52              this.node = node;
53          }
54  
55          public String toString() {
56              return "accessType = " + accessType + ", line = " + node.getLine();
57          }
58      }
59  
60      public void execute(CurrentPath path) {
61          Map<String, Usage> hash = new HashMap<>();
62          //System.out.println("path size is " + path.size());
63          for (Iterator<DataFlowNode> i = path.iterator(); i.hasNext();) {
64              //System.out.println("i = " + i);
65              DataFlowNode inode = i.next();
66              if (inode.getVariableAccess() == null) {
67                  continue;
68              }
69              for (int j = 0; j < inode.getVariableAccess().size(); j++) {
70                  VariableAccess va = inode.getVariableAccess().get(j);
71                  //System.out.println("inode = " + inode + ", va = " + va);
72                  Usage u = hash.get(va.getVariableName());
73                  if (u != null) {
74                      // At some point investigate and possibly reintroduce this line2 thing
75                      //int line2 = ((Integer) array.get(1)).intValue();
76  
77                      // DD - definition followed by another definition
78                      // FIXME need to check for assignment as well!
79                      if (va.isDefinition() && va.accessTypeMatches(u.accessType)) {
80                          //System.out.println(va.getVariableName() + ":" + u);
81                          addViolation(rc, u.node.getNode(), va.getVariableName());
82                      }
83  /*                        // UR - ??
84                    else if (last == VariableAccess.UNDEFINITION && va.isReference()) {
85                          //this.rc.getReport().addRuleViolation(createRuleViolation(rc, inode.getNode(), va.getVariableName(), "UR"));
86                      }
87                      // DU - variable is defined and then goes out of scope
88                      // i.e., unused parameter
89                      else if (last == VariableAccess.DEFINITION && va.isUndefinition()) {
90                          if (inode.getNode() != null) {
91                              this.rc.getReport().addRuleViolation(createRuleViolation(rc, tmp, va.getVariableName(), "DU"));
92                          }
93                      }
94  */
95                  }
96                  u = new Usage(va.getAccessType(), inode);
97                  hash.put(va.getVariableName(), u);
98              }
99          }
100     }
101 }