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