forked from daanvdh/JavaDataFlow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataFlowGraph.java
More file actions
283 lines (235 loc) · 8.49 KB
/
DataFlowGraph.java
File metadata and controls
283 lines (235 loc) · 8.49 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
/*
* 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.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
/**
* Graph representing the data flow within a single class. The {@link DataFlowNode}s represent variables. An {@link DataFlowEdge} goes from node a to b iff a
* influences the state of b. Conditional statements are not supported in the current implementation.
*
* @author Daan
*/
public class DataFlowGraph extends OwnerNode<ClassOrInterfaceDeclaration> {
/** The package of the class that this {@link DataFlowGraph} represents. */
private String classPackage;
/** This fields within the represented class */
private List<DataFlowNode> fields = new ArrayList<>();
/** This Constructors within the represented class */
private List<DataFlowMethod> constructors = new ArrayList<>();
/** This Methods within the represented class */
private Map<Node, DataFlowMethod> methods = new HashMap<>();
/**
* All nodes defined within the class: fields and method/constructor parameters and return values. Does not contain method/constructor in-between variables.
*/
private Map<Node, DataFlowNode> nodes = new HashMap<>();
/**
* List containing all external DataFlowGraphs that this {@link DataFlowGraph} depends on. The contained dfg's are not complete graphs they only contain the
* signatures of methods called from this class. The keys are the package and class name concatenated with a dot.
*/
private Map<String, DataFlowGraph> dependedGraphs = new HashMap<>();
/** In case that this {@link DataFlowGraph} represents an inner class, the owner graph represents the class outer class. */
private DataFlowGraph ownerGraph;
public DataFlowGraph() {
// empty constructor which would otherwise be invisible due to the constructor receiving the builder.
}
private DataFlowGraph(Builder builder) {
super(builder);
this.classPackage = builder.classPackage == null ? this.classPackage : builder.classPackage;
this.fields.clear();
this.addFields(builder.fields);
this.constructors.clear();
this.constructors.addAll(builder.constructors);
this.methods = builder.methods == null ? this.methods : builder.methods;
this.nodes = builder.nodes == null ? this.nodes : builder.nodes;
this.dependedGraphs = builder.dependedGraphs == null ? this.dependedGraphs : builder.dependedGraphs;
}
public List<DataFlowNode> getFields() {
return fields;
}
public void setFields(List<DataFlowNode> fields) {
this.fields = fields;
}
public List<DataFlowMethod> getConstructors() {
return constructors;
}
public void setConstructors(List<DataFlowMethod> constructors) {
this.constructors = constructors;
}
public Collection<DataFlowMethod> getMethods() {
return methods.values();
}
public Map<Node, DataFlowMethod> getMethodMap() {
return this.methods;
}
public void setMethods(List<DataFlowMethod> methods) {
this.methods.clear();
methods.forEach(this::addMethod);
}
public void addMethod(DataFlowMethod method) {
if (method.getRepresentedNode() == null) {
throw new NullPointerException("The representedNode may not be null, this risks overriding existing methods.");
}
this.methods.put(method.getRepresentedNode(), method);
method.setGraph(this);
}
public DataFlowMethod getMethod(Node node) {
return methods.get(node);
}
public final void addField(DataFlowNode node) {
this.fields.add(node);
if (node.getRepresentedNode() == null) {
throw new NullPointerException("The representedNode may not be null, this risks overriding existing fields.");
}
this.nodes.put(node.getRepresentedNode(), node);
node.setOwner(this);
}
public final void addFields(DataFlowNode... fields) {
Stream.of(fields).forEach(this::addField);
}
private final void addFields(List<DataFlowNode> fields) {
fields.forEach(this::addField);
}
public void addNodes(List<DataFlowNode> nodes) {
nodes.forEach(this::addNode);
}
public void addNode(DataFlowNode node) {
this.nodes.put(node.getRepresentedNode(), node);
}
public DataFlowNode getNode(Node node) {
return nodes.get(node);
}
public Map<Node, DataFlowNode> getNodes() {
return this.nodes;
}
public Map<String, DataFlowGraph> getDependedGraphs() {
return dependedGraphs;
}
public DataFlowGraph getDependedGraph(String path) {
return this.dependedGraphs.get(path);
}
public void setDependedGraphs(Map<String, DataFlowGraph> dependedGraphs) {
this.dependedGraphs = dependedGraphs;
}
public void addDependedGraph(DataFlowGraph graph) {
this.dependedGraphs.put(graph.getClassPackage() + "." + graph.getName(), graph);
}
public String getClassPackage() {
return classPackage;
}
public void setClassPackage(String classPackage) {
this.classPackage = classPackage;
}
@Override
public Optional<OwnedNode<?>> getOwner() {
return Optional.ofNullable(this.ownerGraph);
}
@Override
Collection<OwnerNode<?>> getOwnedOwners() {
// streaming and collecting needed for casting.
return this.methods.values().stream().collect(Collectors.toList());
}
@Override
Collection<DataFlowNode> getDirectOwnedNodes() {
return this.fields;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(super.toString());
sb.append("fields{");
fields.forEach(f -> sb.append("\n->").append(f.toStringForward(1)));
sb.append("\n");
fields.forEach(f -> sb.append("\n<-").append(f.toStringBackward(1)));
sb.append("\n}\n");
sb.append("methods{\n");
for (DataFlowMethod m : methods.values()) {
sb.append(m.toString());
}
sb.append("\n}");
return sb.toString();
}
/**
* Creates builder to build {@link DataFlowGraph}.
*
* @return created builder
*/
public static Builder builder() {
return new Builder();
}
/**
* Builder to build {@link DataFlowGraph}.
*/
public static final class Builder extends NodeRepresenter.Builder<ClassOrInterfaceDeclaration, DataFlowGraph.Builder> {
private String classPackage;
private List<DataFlowNode> fields = new ArrayList<>();
private List<DataFlowMethod> constructors = new ArrayList<>();
private Map<Node, DataFlowMethod> methods = new HashMap<>();
private Map<Node, DataFlowNode> nodes;
private Map<String, DataFlowGraph> dependedGraphs;
private Builder() {
// Builder should only be constructed via the parent class
}
public Builder classPackage(String classPackage) {
this.classPackage = classPackage;
return this;
}
public Builder fields(List<DataFlowNode> fields) {
this.fields.clear();
this.fields.addAll(fields);
return this;
}
public Builder fields(DataFlowNode... fields) {
this.fields.clear();
this.fields.addAll(Arrays.asList(fields));
return this;
}
public Builder constructors(List<DataFlowMethod> constructors) {
this.constructors.clear();
this.constructors.addAll(constructors);
return this;
}
public Builder methods(Map<Node, DataFlowMethod> methods) {
this.methods = methods;
return this;
}
public Builder methods(DataFlowMethod... methods) {
Stream.of(methods).forEach(m -> this.methods.put(m.getRepresentedNode(), m));
return this;
}
public Builder nodes(Map<Node, DataFlowNode> nodes) {
this.nodes = nodes;
return this;
}
public Builder dependedGraphs(Map<String, DataFlowGraph> dependedGraphs) {
this.dependedGraphs = dependedGraphs;
return this;
}
public DataFlowGraph build() {
return new DataFlowGraph(this);
}
}
}