forked from daanvdh/JavaDataFlow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataFlowNode.java
More file actions
326 lines (278 loc) · 9.76 KB
/
DataFlowNode.java
File metadata and controls
326 lines (278 loc) · 9.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
* Copyright 2018 by Daan van den Heuvel.
*
* This file is part of JavaDataFlow.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package model;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Node;
import util.GraphUtil;
/**
* A node inside the {@link DataFlowGraph} containing a {@link JavaParser} {@link Node}. The incoming {@link DataFlowEdge}s are {@link DataFlowNode}s that
* influence the state of this node. The outgoing {@link DataFlowEdge}s point to {@link DataFlowNode}s which state is influenced by this {@link DataFlowNode}.
*
* @author Daan
*/
public class DataFlowNode extends OwnedNode<Node> {
/** The {@link DataFlowEdge}s from {@link DataFlowNode}s that influence the state of this node */
private List<DataFlowEdge> in = new ArrayList<>();
/** The {@link DataFlowEdge}s to {@link DataFlowNode}s who's state is influenced by this node */
private List<DataFlowEdge> out = new ArrayList<>();
/**
* The type of the represented node. This is needed in the case that we need to create a {@link DataFlowNode} without a representedNode, for instance when the
* {@link CompilationUnit} of a dependend graph is not available while constructing a {@link DataFlowGraph}.
*/
private String type;
/**
* The {@link DataFlowMethod} that contains this {@link DataFlowNode}, this will be null in case this node was not defined within a method, for instance in
* case of a class field.
*/
private OwnedNode<?> owner;
/**
* If a method was called on the current {@link DataFlowNode} this {@link NodeCall} will represent that NodeCall. Each {@link DataFlowNode} can only have a
* single nodeCall, since each usage of a single variable is modeled to be a separate dataFlowNode. All method called on a given dataFlowNode can be collected
* by walking through the graph. This value will be null if no method was called.
*/
private NodeCall nodeCall;
public DataFlowNode(Node representedNode) {
super(representedNode);
}
public DataFlowNode(String name, Node representedNode) {
super(name, representedNode);
}
private DataFlowNode(Builder builder) {
super(builder);
this.in.clear();
this.in.addAll(builder.in);
this.out.clear();
this.out.addAll(builder.out);
this.setType(builder.type);
this.owner = builder.owner;
this.nodeCall = builder.nodeCall;
}
public List<DataFlowEdge> getIn() {
return in;
}
public void setIn(List<DataFlowEdge> in) {
this.in = in;
}
public List<DataFlowEdge> getOut() {
return out;
}
public void setOut(List<DataFlowEdge> out) {
this.out = out;
}
public void addEdgeTo(DataFlowNode to) {
DataFlowEdge edge = new DataFlowEdge(this, to);
this.addOutgoing(edge);
to.addIncoming(edge);
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Optional<NodeCall> getNodeCall() {
return Optional.ofNullable(nodeCall);
}
public void setNodeCall(NodeCall nodeCall) {
this.nodeCall = nodeCall;
}
public boolean isField() {
return this.getOwner().map(DataFlowGraph.class::isInstance).orElse(false);
}
public boolean isInputParameter() {
return this.getOwner().filter(ParameterList.class::isInstance).map(ParameterList.class::cast).filter(ParameterList::isInputParametersForMethod).isPresent();
}
/**
* Walks back over incoming edges until predicate is met or no incoming edges are present.
*
* @see GraphUtil#walkBackUntil(DataFlowNode, Predicate, Predicate)
* @param predicate The {@link Predicate} to meet
* @param scope The scope for the variable, the search is stopped as soon as the scope does not hold and an empty list is returned.
* @return {@link List} of {@link DataFlowNode}
*/
public List<DataFlowNode> walkBackUntil(Predicate<DataFlowNode> predicate, Predicate<DataFlowNode> scope) {
return GraphUtil.walkBackUntil(this, predicate, scope);
}
/**
* Returns all {@link NodeCall} that are called directly on this {@link DataFlowNode} or on any other {@link DataFlowNode} that has an {@link DataFlowEdge}
* resulting from this node. Only nodes within the defined scope are considered.
*
* @param scope The scope for searching for {@link NodeCall}s.
* @return List of {@link NodeCall}.
*/
public List<NodeCall> collectNodeCalls(Predicate<DataFlowNode> scope) {
if (!scope.test(this)) {
return new ArrayList<>();
}
List<NodeCall> collect =
this.getOut().stream().map(DataFlowEdge::getTo).map(dfn -> dfn.collectNodeCalls(scope)).flatMap(List::stream).collect(Collectors.toList());
if (this.nodeCall != null) {
collect.add(nodeCall);
}
return collect;
}
/**
* @param node The {@link DataFlowNode} to check.
* @return True if this node is equal to the given node or has a direct incoming edge from the input node, false otherwise.
*/
public boolean hasAsDirectInput(DataFlowNode node) {
return this.equals(node) || this.in.stream().map(DataFlowEdge::getFrom).filter(node::equals).findAny().isPresent();
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), in, out);
}
@Override
public boolean equals(Object obj) {
boolean equals = false;
if (this == obj) {
equals = true;
} else if (obj != null && getClass() == obj.getClass()) {
DataFlowNode other = (DataFlowNode) obj;
equals = new EqualsBuilder().appendSuper(super.equals(obj)).append(in, other.in).append(out, other.out).append(type, other.type).isEquals();
}
return equals;
}
private void addIncoming(DataFlowEdge edge) {
this.in.add(edge);
}
private void addOutgoing(DataFlowEdge edge) {
this.out.add(edge);
}
@Override
public String toString() {
return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).appendSuper(super.toString()).append("in", in).append("out", out).append("type", type)
.build();
}
public String toStringForward(int tabs) {
if (tabs > 10) {
return "TestDataFlowNode::toStringForward tabs>10";
}
return toStringForward(tabs, 0);
}
public String toStringForward(int tabs, int firstTabs) {
StringBuilder sb = new StringBuilder();
sb.append(tabs(firstTabs) + this.getName());
boolean first = true;
for (DataFlowEdge e : out) {
if (first) {
first = false;
sb.append("\t-> " + e.getTo().toStringForward(tabs + 1));
} else {
sb.append("\n" + tabs(firstTabs + tabs + 3) + "-> " + e.getTo().toStringForward(tabs + 1, firstTabs));
}
}
return sb.toString();
}
public String toStringBackward(int tabs) {
if (tabs > 10) {
return "TestDataFlowNode::toStringForward tabs>10";
}
return toStringBackward(tabs, 0);
}
private String toStringBackward(int tabs, int firstTabs) {
StringBuilder sb = new StringBuilder();
sb.append(this.getName());
boolean first = true;
for (DataFlowEdge e : in) {
if (first) {
first = false;
sb.append("\t<- " + e.getFrom().toStringBackward(tabs + 1));
} else {
sb.append("\n" + tabs(tabs + 1) + "<- " + e.getFrom().toStringBackward(tabs + 1, tabs + 1));
}
}
return sb.toString();
}
/**
* Creates builder to build {@link DataFlowNode}.
*
* @return created builder
*/
public static Builder builder() {
return new Builder();
}
private String tabs(int tabs) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < tabs; i++) {
sb.append("\t");
}
return sb.toString();
}
@Override
public Optional<OwnedNode<?>> getOwner() {
return Optional.ofNullable(this.owner);
}
public void setOwner(OwnedNode<?> owner) {
this.owner = owner;
}
/**
* Builder to build {@link DataFlowNode}.
*/
public static final class Builder extends NodeRepresenter.Builder<Node, DataFlowNode.Builder> {
private OwnedNode<?> owner;
private List<DataFlowEdge> in = new ArrayList<>();
private List<DataFlowEdge> out = new ArrayList<>();
private String type;
private NodeCall nodeCall;
private Builder() {
// Builder should only be constructed via the parent class
}
public Builder in(List<DataFlowEdge> in) {
this.in.clear();
this.in.addAll(in);
return this;
}
public Builder out(List<DataFlowEdge> out) {
this.out.clear();
this.out.addAll(out);
return this;
}
public Builder out(DataFlowEdge... out) {
this.out.clear();
this.out.addAll(Arrays.asList(out));
return this;
}
public Builder type(String type) {
this.type = type;
return this;
}
public Builder owner(OwnedNode<?> owner) {
this.owner = owner;
return this;
}
public Builder nodeCall(NodeCall nodeCall) {
this.nodeCall = nodeCall;
return this;
}
public DataFlowNode build() {
return new DataFlowNode(this);
}
}
}