|
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 |
| |
|
19 |
| |
|
20 |
| |
|
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 |
0
| public Object visit(ASTMethodDeclaration node, Object data) {
|
|
29 |
0
| this.rc = (RuleContext) data;
|
|
30 |
0
| counter = 0;
|
|
31 |
| |
|
32 |
0
| IDataFlowNode n = (IDataFlowNode) node.getDataFlowNode().getFlow().get(0);
|
|
33 |
0
| System.out.println("In DaaRule, IDataFlowNode n = " + n);
|
|
34 |
| |
|
35 |
0
| DAAPathFinder a = new DAAPathFinder(n, this);
|
|
36 |
0
| a.run();
|
|
37 |
| |
|
38 |
0
| super.visit(node, data);
|
|
39 |
0
| return data;
|
|
40 |
| } |
|
41 |
| |
|
42 |
0
| public void execute(List path) {
|
|
43 |
0
| Hashtable hash = new Hashtable();
|
|
44 |
0
| counter++;
|
|
45 |
0
| if (counter == 5000) {
|
|
46 |
0
| System.out.print("|");
|
|
47 |
0
| counter = 0;
|
|
48 |
| } |
|
49 |
0
| for (int d = 0; d < path.size(); d++) {
|
|
50 |
0
| IDataFlowNode inode = (IDataFlowNode) path.get(d);
|
|
51 |
0
| if (inode.getVariableAccess() != null) {
|
|
52 |
0
| for (int g = 0; g < inode.getVariableAccess().size(); g++) {
|
|
53 |
0
| VariableAccess va = (VariableAccess) inode.getVariableAccess().get(g);
|
|
54 |
| |
|
55 |
0
| Object o = hash.get(va.getVariableName());
|
|
56 |
0
| if (o != null) {
|
|
57 |
0
| List array = (List) o;
|
|
58 |
0
| int last = ((Integer) array.get(0)).intValue();
|
|
59 |
| |
|
60 |
| |
|
61 |
0
| if (va.accessTypeMatches(last) && va.isDefinition()) {
|
|
62 |
0
| this.rc.getReport().addRuleViolation(createRuleViolation(rc, inode.getSimpleNode(), va.getVariableName(), "DD"));
|
|
63 |
0
| } else if (last == VariableAccess.UNDEFINITION && va.isReference()) {
|
|
64 |
0
| this.rc.getReport().addRuleViolation(createRuleViolation(rc, inode.getSimpleNode(), va.getVariableName(), "UR"));
|
|
65 |
0
| } else if (last == VariableAccess.DEFINITION && va.isUndefinition()) {
|
|
66 |
0
| this.rc.getReport().addRuleViolation(createRuleViolation(rc, inode.getSimpleNode(), va.getVariableName(), "DU"));
|
|
67 |
| } |
|
68 |
| } |
|
69 |
0
| List array = new ArrayList();
|
|
70 |
0
| array.add(new Integer(va.getAccessType()));
|
|
71 |
0
| array.add(new Integer(inode.getLine()));
|
|
72 |
0
| hash.put(va.getVariableName(), array);
|
|
73 |
| } |
|
74 |
| } |
|
75 |
| } |
|
76 |
| } |
|
77 |
| } |