View Javadoc

1   /*
2    * Created on 20.07.2004
3    */
4   package net.sourceforge.pmd.dfa;
5   
6   import net.sourceforge.pmd.AbstractRule;
7   import net.sourceforge.pmd.RuleContext;
8   import net.sourceforge.pmd.ast.ASTMethodDeclaration;
9   import net.sourceforge.pmd.dfa.pathfinder.CurrentPath;
10  import net.sourceforge.pmd.dfa.pathfinder.DAAPathFinder;
11  import net.sourceforge.pmd.dfa.pathfinder.Executable;
12  import net.sourceforge.pmd.dfa.variableaccess.VariableAccess;
13  
14  import java.util.ArrayList;
15  import java.util.Hashtable;
16  import java.util.Iterator;
17  import java.util.List;
18  
19  /***
20   * @author raik
21   *         <p/>
22   *         Starts path search for each method and runs code if found.
23   */
24  public class DaaRule extends AbstractRule implements Executable {
25  
26      private RuleContext rc;
27      private int counter;
28      private static final int MAX_PATHS = 5000;
29  
30      public Object visit(ASTMethodDeclaration node, Object data) {
31          this.rc = (RuleContext) data;
32          counter = 0;
33  
34          IDataFlowNode n = (IDataFlowNode) node.getDataFlowNode().getFlow().get(0);
35          System.out.println("In DaaRule, IDataFlowNode n = " + n);
36  
37          DAAPathFinder a = new DAAPathFinder(n, this);
38          a.run();
39  
40          super.visit(node, data);
41          return data;
42      }
43  
44      public void execute(CurrentPath path) {
45          Hashtable hash = new Hashtable();
46          counter++;
47          if (counter == 5000) {
48              System.out.print("|");
49              counter = 0;
50          }
51          for (Iterator d = path.iterator(); d.hasNext();) {
52              IDataFlowNode inode = (IDataFlowNode) d.next();
53              if (inode.getVariableAccess() != null) {
54                  for (int g = 0; g < inode.getVariableAccess().size(); g++) {
55                      VariableAccess va = (VariableAccess) inode.getVariableAccess().get(g);
56  
57                      Object o = hash.get(va.getVariableName());
58                      if (o != null) {
59                          List array = (List) o;
60                          int last = ((Integer) array.get(0)).intValue();
61                          // TODO - at some point investigate and possibly reintroduce this line2 thing
62                          //int line2 = ((Integer) array.get(1)).intValue();
63  /*
64                          if (va.accessTypeMatches(last) && va.isDefinition()) { // DD
65                              this.rc.getReport().addRuleViolation(createRuleViolation(rc, inode.getSimpleNode(), va.getVariableName(), "DD"));
66                          } else if (last == VariableAccess.UNDEFINITION && va.isReference()) { // UR
67                              this.rc.getReport().addRuleViolation(createRuleViolation(rc, inode.getSimpleNode(), va.getVariableName(), "UR"));
68                          } else if (last == VariableAccess.DEFINITION && va.isUndefinition()) { // DU
69                              this.rc.getReport().addRuleViolation(createRuleViolation(rc, inode.getSimpleNode(), va.getVariableName(), "DU"));
70                          }
71  */
72                      }
73                      List array = new ArrayList();
74                      array.add(new Integer(va.getAccessType()));
75                      array.add(new Integer(inode.getLine()));
76                      hash.put(va.getVariableName(), array);
77                  }
78              }
79          }
80      }
81  }