Clover coverage report - PMD - 3.9 Coverage timestamp: Tue Dec 19 2006 09:38:44 EST FRAMES NO FRAMES |
file stats: | LOC: | 174 |  | Methods: | 17
| NCLOC: | 143 |  | Classes: | 1
Source file | Conditionals | Statements | Methods | TOTAL
DataFlowNode.java | 62.5% | 58.1% | 94.1% | 64.3% |

1 |  | package net.sourceforge.pmd.dfa;
2 |  |
3 |  | import net.sourceforge.pmd.ast.ASTConstructorDeclaration;
4 |  | import net.sourceforge.pmd.ast.ASTMethodDeclaration;
5 |  | import net.sourceforge.pmd.ast.SimpleNode;
6 |  |
7 |  | import java.util.ArrayList;
8 |  | import java.util.BitSet;
9 |  | import java.util.HashMap;
10 |  | import java.util.LinkedList;
11 |  | import java.util.List;
12 |  | import java.util.Map;
13 |  | import java.util.StringTokenizer;
14 |  |
15 |  | /**
16 |  | * @author raik
17 |  | * <p/>
18 |  | * Each data flow contains a set of DataFlowNodes.
19 |  | */
20 |  | public class DataFlowNode implements IDataFlowNode {
21 |  |
22 |  | private SimpleNode node;
23 |  | private Map typeMap = new HashMap();
24 |  |
25 |  | protected List parents = new ArrayList();
26 |  | protected List children = new ArrayList();
27 |  | protected BitSet type = new BitSet();
28 |  | protected List variableAccess = new ArrayList();
29 |  | protected LinkedList dataFlow;
30 |  | protected int line;
31 |  |
32 | 101 | protected DataFlowNode() {
33 |  | }
34 |  |
35 | 230 | public DataFlowNode(SimpleNode node, LinkedList dataFlow) {
36 | 230 | this.dataFlow = dataFlow;
37 | 230 | this.node = node;
38 |  |
39 | 230 | node.setDataFlowNode(this);
40 | 230 | this.line = node.getBeginLine();
41 |  |
42 | 230 | if (!this.dataFlow.isEmpty()) {
43 | 229 | DataFlowNode parent = (DataFlowNode) this.dataFlow.getLast();
44 | 229 | parent.addPathToChild(this);
45 |  | }
46 | 230 | this.dataFlow.addLast(this);
47 |  | }
48 |  |
49 | 417 | public void addPathToChild(IDataFlowNode child) {
50 | 417 | DataFlowNode thisChild = (DataFlowNode) child;
51 |  | // TODO - throw an exception if already contained in children list?
52 | 417 | if (!this.children.contains(thisChild) || this.equals(thisChild)) {
53 | 417 | this.children.add(thisChild);
54 | 417 | thisChild.parents.add(this);
55 |  | }
56 |  | }
57 |  |
58 | 86 | public boolean removePathToChild(IDataFlowNode child) {
59 | 86 | DataFlowNode thisChild = (DataFlowNode) child;
60 | 86 | thisChild.parents.remove(this);
61 | 86 | return this.children.remove(thisChild);
62 |  | }
63 |  |
64 | 29 | public void reverseParentPathsTo(IDataFlowNode destination) {
65 | 29 | while (!parents.isEmpty()) {
66 | 37 | DataFlowNode parent = (DataFlowNode) parents.get(0);
67 | 37 | parent.removePathToChild(this);
68 | 37 | parent.addPathToChild(destination);
69 |  | }
70 |  | }
71 |  |
72 | 72 | public int getLine() {
73 | 72 | return this.line;
74 |  | }
75 |  |
76 | 217 | public void setType(int type) {
77 | 217 | this.type.set(type);
78 |  | }
79 |  |
80 | 69 | public boolean isType(int intype) {
81 | 69 | try {
82 | 69 | return type.get(intype);
83 |  | } catch (IndexOutOfBoundsException e) {
84 | 0 | e.printStackTrace();
85 |  | }
86 | 0 | return false;
87 |  | }
88 |  |
89 | 1665 | public SimpleNode getSimpleNode() {
90 | 1665 | return this.node;
91 |  | }
92 |  |
93 | 300 | public List getChildren() {
94 | 300 | return this.children;
95 |  | }
96 |  |
97 | 8 | public List getParents() {
98 | 8 | return this.parents;
99 |  | }
100 |  |
101 | 1073 | public List getFlow() {
102 | 1073 | return this.dataFlow;
103 |  | }
104 |  |
105 | 262 | public int getIndex() {
106 | 262 | return this.dataFlow.indexOf(this);
107 |  | }
108 |  |
109 | 251 | public void setVariableAccess(List variableAccess) {
110 | 251 | if (this.variableAccess.isEmpty()) {
111 | 250 | this.variableAccess = variableAccess;
112 |  | } else {
113 | 1 | this.variableAccess.addAll(variableAccess);
114 |  | }
115 |  | }
116 |  |
117 | 180 | public List getVariableAccess() {
118 | 180 | return this.variableAccess;
119 |  | }
120 |  |
121 | 4 | public String toString() {
122 | 4 | String res = "DataFlowNode: line " + this.getLine() + ", ";
123 | 4 | if (node instanceof ASTMethodDeclaration || node instanceof ASTConstructorDeclaration) {
124 | 1 | res += (node instanceof ASTMethodDeclaration) ? "(method)" : "(constructor)";
125 |  | } else {
126 | 3 | String tmp = type.toString();
127 | 3 | String newTmp = "";
128 | 3 | for (int i = 0; i < tmp.length(); i++) {
129 | 6 | if (tmp.charAt(i) != '{' && tmp.charAt(i) != '}' && tmp.charAt(i) != ' ') {
130 | 0 | newTmp += tmp.charAt(i);
131 |  | }
132 |  | }
133 | 3 | for (StringTokenizer st = new StringTokenizer(newTmp, ","); st.hasMoreTokens();) {
134 | 0 | int newTmpInt = Integer.parseInt(st.nextToken());
135 | 0 | res += "(" + stringFromType(newTmpInt) + ")";
136 |  | }
137 | 3 | res += ", " + this.node.getClass().getName().substring(node.getClass().getName().lastIndexOf('.') + 1);
138 | 3 | res += (node.getImage() == null ? "" : "(" + this.node.getImage() + ")");
139 |  | }
140 | 4 | return res;
141 |  | }
142 |  |
143 | 0 | private String stringFromType(int intype) {
144 | 0 | if (typeMap.isEmpty()) {
145 | 0 | typeMap.put(new Integer(NodeType.IF_EXPR), "IF_EXPR");
146 | 0 | typeMap.put(new Integer(NodeType.IF_LAST_STATEMENT), "IF_LAST_STATEMENT");
147 | 0 | typeMap.put(new Integer(NodeType.IF_LAST_STATEMENT_WITHOUT_ELSE), "IF_LAST_STATEMENT_WITHOUT_ELSE");
148 | 0 | typeMap.put(new Integer(NodeType.ELSE_LAST_STATEMENT), "ELSE_LAST_STATEMENT");
149 | 0 | typeMap.put(new Integer(NodeType.WHILE_LAST_STATEMENT), "WHILE_LAST_STATEMENT");
150 | 0 | typeMap.put(new Integer(NodeType.WHILE_EXPR), "WHILE_EXPR");
151 | 0 | typeMap.put(new Integer(NodeType.SWITCH_START), "SWITCH_START");
152 | 0 | typeMap.put(new Integer(NodeType.CASE_LAST_STATEMENT), "CASE_LAST_STATEMENT");
153 | 0 | typeMap.put(new Integer(NodeType.SWITCH_LAST_DEFAULT_STATEMENT), "SWITCH_LAST_DEFAULT_STATEMENT");
154 | 0 | typeMap.put(new Integer(NodeType.SWITCH_END), "SWITCH_END");
155 | 0 | typeMap.put(new Integer(NodeType.FOR_INIT), "FOR_INIT");
156 | 0 | typeMap.put(new Integer(NodeType.FOR_EXPR), "FOR_EXPR");
157 | 0 | typeMap.put(new Integer(NodeType.FOR_UPDATE), "FOR_UPDATE");
158 | 0 | typeMap.put(new Integer(NodeType.FOR_BEFORE_FIRST_STATEMENT), "FOR_BEFORE_FIRST_STATEMENT");
159 | 0 | typeMap.put(new Integer(NodeType.FOR_END), "FOR_END");
160 | 0 | typeMap.put(new Integer(NodeType.DO_BEFORE_FIRST_STATEMENT), "DO_BEFORE_FIRST_STATEMENT");
161 | 0 | typeMap.put(new Integer(NodeType.DO_EXPR), "DO_EXPR");
162 | 0 | typeMap.put(new Integer(NodeType.RETURN_STATEMENT), "RETURN_STATEMENT");
163 | 0 | typeMap.put(new Integer(NodeType.BREAK_STATEMENT), "BREAK_STATEMENT");
164 | 0 | typeMap.put(new Integer(NodeType.CONTINUE_STATEMENT), "CONTINUE_STATEMENT");
165 | 0 | typeMap.put(new Integer(NodeType.LABEL_STATEMENT), "LABEL_STATEMENT");
166 | 0 | typeMap.put(new Integer(NodeType.LABEL_LAST_STATEMENT), "LABEL_END");
167 |  | }
168 | 0 | if (!typeMap.containsKey(new Integer(intype))) {
169 | 0 | throw new RuntimeException("Couldn't find type id " + intype);
170 |  | }
171 | 0 | return (String) typeMap.get(new Integer(intype));
172 |  | }
173 |  |
174 |  | }