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 |
211
| public DataFlowNode(SimpleNode node, LinkedList dataFlow) {
|
35 |
211
| this.dataFlow = dataFlow;
|
36 |
211
| this.node = node;
|
37 |
| |
38 |
211
| node.setDataFlowNode(this);
|
39 |
211
| this.line = node.getBeginLine();
|
40 |
| |
41 |
211
| if (!this.dataFlow.isEmpty()) {
|
42 |
210
| DataFlowNode parent = (DataFlowNode) this.dataFlow.getLast();
|
43 |
210
| parent.addPathToChild(this);
|
44 |
| } |
45 |
211
| this.dataFlow.addLast(this);
|
46 |
| } |
47 |
| |
48 |
389
| public void addPathToChild(IDataFlowNode child) {
|
49 |
389
| DataFlowNode thisChild = (DataFlowNode) child;
|
50 |
| |
51 |
389
| if (!this.children.contains(thisChild) || this.equals(thisChild)) {
|
52 |
389
| this.children.add(thisChild);
|
53 |
389
| thisChild.parents.add(this);
|
54 |
| } |
55 |
| } |
56 |
| |
57 |
84
| public boolean removePathToChild(IDataFlowNode child) {
|
58 |
84
| DataFlowNode thisChild = (DataFlowNode) child;
|
59 |
84
| thisChild.parents.remove(this);
|
60 |
84
| return this.children.remove(thisChild);
|
61 |
| } |
62 |
| |
63 |
29
| public void reverseParentPathsTo(IDataFlowNode destination) {
|
64 |
29
| while (!parents.isEmpty()) {
|
65 |
37
| DataFlowNode parent = (DataFlowNode) parents.get(0);
|
66 |
37
| parent.removePathToChild(this);
|
67 |
37
| parent.addPathToChild(destination);
|
68 |
| } |
69 |
| } |
70 |
| |
71 |
1282
| public int getLine() {
|
72 |
1282
| return this.line;
|
73 |
| } |
74 |
| |
75 |
204
| public void setType(int type) {
|
76 |
204
| this.type.set(type);
|
77 |
| } |
78 |
| |
79 |
38
| public boolean isType(int intype) {
|
80 |
38
| try {
|
81 |
38
| return type.get(intype);
|
82 |
| } catch (IndexOutOfBoundsException e) { |
83 |
0
| e.printStackTrace();
|
84 |
| } |
85 |
0
| return false;
|
86 |
| } |
87 |
| |
88 |
290
| public SimpleNode getSimpleNode() {
|
89 |
290
| return this.node;
|
90 |
| } |
91 |
| |
92 |
132
| public List getChildren() {
|
93 |
132
| return this.children;
|
94 |
| } |
95 |
| |
96 |
8
| public List getParents() {
|
97 |
8
| return this.parents;
|
98 |
| } |
99 |
| |
100 |
949
| public List getFlow() {
|
101 |
949
| return this.dataFlow;
|
102 |
| } |
103 |
| |
104 |
226
| public int getIndex() {
|
105 |
226
| return this.dataFlow.indexOf(this);
|
106 |
| } |
107 |
| |
108 |
343
| public void setVariableAccess(List variableAccess) {
|
109 |
343
| if (this.variableAccess.isEmpty()) {
|
110 |
232
| this.variableAccess = variableAccess;
|
111 |
| } else { |
112 |
111
| this.variableAccess.addAll(variableAccess);
|
113 |
| } |
114 |
| } |
115 |
| |
116 |
6
| public List getVariableAccess() {
|
117 |
6
| return this.variableAccess;
|
118 |
| } |
119 |
| |
120 |
4
| public String toString() {
|
121 |
4
| String res = "DataFlowNode: line " + this.getLine() + ", ";
|
122 |
4
| if (node instanceof ASTMethodDeclaration || node instanceof ASTConstructorDeclaration) {
|
123 |
1
| res += (node instanceof ASTMethodDeclaration) ? "(method)" : "(constructor)";
|
124 |
| } else { |
125 |
3
| String tmp = type.toString();
|
126 |
3
| String newTmp = "";
|
127 |
3
| for (int i=0; i<tmp.length(); i++) {
|
128 |
6
| if (tmp.charAt(i) != '{' && tmp.charAt(i) != '}'&& tmp.charAt(i) != ' ') {
|
129 |
0
| newTmp += tmp.charAt(i);
|
130 |
| } |
131 |
| } |
132 |
3
| for (StringTokenizer st = new StringTokenizer(newTmp, ","); st.hasMoreTokens();) {
|
133 |
0
| int newTmpInt = Integer.parseInt(st.nextToken());
|
134 |
0
| res += "(" + stringFromType(newTmpInt) + ")";
|
135 |
| } |
136 |
3
| res += ", " + this.node.getClass().getName().substring(node.getClass().getName().lastIndexOf('.')+1);
|
137 |
3
| res += (node.getImage() == null ? "" : "(" + this.node.getImage() + ")");
|
138 |
| } |
139 |
4
| return res;
|
140 |
| } |
141 |
| |
142 |
0
| private String stringFromType(int intype) {
|
143 |
0
| if (typeMap.isEmpty()) {
|
144 |
0
| typeMap.put(new Integer(NodeType.IF_EXPR), "IF_EXPR");
|
145 |
0
| typeMap.put(new Integer(NodeType.IF_LAST_STATEMENT), "IF_LAST_STATEMENT");
|
146 |
0
| typeMap.put(new Integer(NodeType.IF_LAST_STATEMENT_WITHOUT_ELSE), "IF_LAST_STATEMENT_WITHOUT_ELSE");
|
147 |
0
| typeMap.put(new Integer(NodeType.ELSE_LAST_STATEMENT), "ELSE_LAST_STATEMENT");
|
148 |
0
| typeMap.put(new Integer(NodeType.WHILE_LAST_STATEMENT), "WHILE_LAST_STATEMENT");
|
149 |
0
| typeMap.put(new Integer(NodeType.WHILE_EXPR), "WHILE_EXPR");
|
150 |
0
| typeMap.put(new Integer(NodeType.SWITCH_START), "SWITCH_START");
|
151 |
0
| typeMap.put(new Integer(NodeType.CASE_LAST_STATEMENT), "CASE_LAST_STATEMENT");
|
152 |
0
| typeMap.put(new Integer(NodeType.SWITCH_LAST_DEFAULT_STATEMENT), "SWITCH_LAST_DEFAULT_STATEMENT");
|
153 |
0
| typeMap.put(new Integer(NodeType.SWITCH_END), "SWITCH_END");
|
154 |
0
| typeMap.put(new Integer(NodeType.FOR_INIT), "FOR_INIT");
|
155 |
0
| typeMap.put(new Integer(NodeType.FOR_EXPR), "FOR_EXPR");
|
156 |
0
| typeMap.put(new Integer(NodeType.FOR_UPDATE), "FOR_UPDATE");
|
157 |
0
| typeMap.put(new Integer(NodeType.FOR_BEFORE_FIRST_STATEMENT), "FOR_BEFORE_FIRST_STATEMENT");
|
158 |
0
| typeMap.put(new Integer(NodeType.FOR_END), "FOR_END");
|
159 |
0
| typeMap.put(new Integer(NodeType.DO_BEFORE_FIRST_STATEMENT), "DO_BEFORE_FIRST_STATEMENT");
|
160 |
0
| typeMap.put(new Integer(NodeType.DO_EXPR), "DO_EXPR");
|
161 |
0
| typeMap.put(new Integer(NodeType.RETURN_STATEMENT), "RETURN_STATEMENT");
|
162 |
0
| typeMap.put(new Integer(NodeType.BREAK_STATEMENT), "BREAK_STATEMENT");
|
163 |
0
| typeMap.put(new Integer(NodeType.CONTINUE_STATEMENT), "CONTINUE_STATEMENT");
|
164 |
| } |
165 |
0
| if (!typeMap.containsKey(new Integer(intype))) {
|
166 |
0
| throw new RuntimeException("Couldn't find type id " + intype);
|
167 |
| } |
168 |
0
| return (String)typeMap.get(new Integer(intype));
|
169 |
| } |
170 |
| |
171 |
| } |