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 |
| |
17 |
| |
18 |
| |
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 |
91
| protected DataFlowNode() {
|
33 |
| } |
34 |
| |
35 |
211
| public DataFlowNode(SimpleNode node, LinkedList dataFlow) {
|
36 |
211
| this.dataFlow = dataFlow;
|
37 |
211
| this.node = node;
|
38 |
| |
39 |
211
| node.setDataFlowNode(this);
|
40 |
211
| this.line = node.getBeginLine();
|
41 |
| |
42 |
211
| if (!this.dataFlow.isEmpty()) {
|
43 |
210
| DataFlowNode parent = (DataFlowNode) this.dataFlow.getLast();
|
44 |
210
| parent.addPathToChild(this);
|
45 |
| } |
46 |
211
| this.dataFlow.addLast(this);
|
47 |
| } |
48 |
| |
49 |
389
| public void addPathToChild(IDataFlowNode child) {
|
50 |
389
| DataFlowNode thisChild = (DataFlowNode) child;
|
51 |
| |
52 |
389
| if (!this.children.contains(thisChild) || this.equals(thisChild)) {
|
53 |
389
| this.children.add(thisChild);
|
54 |
389
| thisChild.parents.add(this);
|
55 |
| } |
56 |
| } |
57 |
| |
58 |
84
| public boolean removePathToChild(IDataFlowNode child) {
|
59 |
84
| DataFlowNode thisChild = (DataFlowNode) child;
|
60 |
84
| thisChild.parents.remove(this);
|
61 |
84
| 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 |
1282
| public int getLine() {
|
73 |
1282
| return this.line;
|
74 |
| } |
75 |
| |
76 |
204
| public void setType(int type) {
|
77 |
204
| this.type.set(type);
|
78 |
| } |
79 |
| |
80 |
38
| public boolean isType(int intype) {
|
81 |
38
| try {
|
82 |
38
| return type.get(intype);
|
83 |
| } catch (IndexOutOfBoundsException e) { |
84 |
0
| e.printStackTrace();
|
85 |
| } |
86 |
0
| return false;
|
87 |
| } |
88 |
| |
89 |
290
| public SimpleNode getSimpleNode() {
|
90 |
290
| return this.node;
|
91 |
| } |
92 |
| |
93 |
132
| public List getChildren() {
|
94 |
132
| return this.children;
|
95 |
| } |
96 |
| |
97 |
8
| public List getParents() {
|
98 |
8
| return this.parents;
|
99 |
| } |
100 |
| |
101 |
949
| public List getFlow() {
|
102 |
949
| return this.dataFlow;
|
103 |
| } |
104 |
| |
105 |
226
| public int getIndex() {
|
106 |
226
| return this.dataFlow.indexOf(this);
|
107 |
| } |
108 |
| |
109 |
343
| public void setVariableAccess(List variableAccess) {
|
110 |
343
| if (this.variableAccess.isEmpty()) {
|
111 |
232
| this.variableAccess = variableAccess;
|
112 |
| } else { |
113 |
111
| this.variableAccess.addAll(variableAccess);
|
114 |
| } |
115 |
| } |
116 |
| |
117 |
6
| public List getVariableAccess() {
|
118 |
6
| 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 |
| } |
166 |
0
| if (!typeMap.containsKey(new Integer(intype))) {
|
167 |
0
| throw new RuntimeException("Couldn't find type id " + intype);
|
168 |
| } |
169 |
0
| return (String) typeMap.get(new Integer(intype));
|
170 |
| } |
171 |
| |
172 |
| } |