forked from daanvdh/JavaDataFlow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeCall.java
More file actions
255 lines (212 loc) · 7.42 KB
/
NodeCall.java
File metadata and controls
255 lines (212 loc) · 7.42 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
/*
* 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.Objects;
import java.util.Optional;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.javaparser.ast.Node;
/**
* Represents a call to a node (method, constructor or other code block). This node will be owned by the calling method. This class groups all in/output data
* from one method to another.
*
* @author Daan
*/
public class NodeCall extends OwnedNode<Node> {
private static final Logger LOG = LoggerFactory.getLogger(NodeCall.class);
/**
* The {@link ParameterList}s that contain the {@link DataFlowNode}s that where used for a specific {@link DataFlowMethod} call to the owner
* {@link DataFlowMethod}. Can be null if method does not have any input.
*/
private ParameterList in;
/** The method/constructor/codeBlock from which the method is called */
private OwnedNode<?> owner;
/** The called method, this can be null in case that the given method is not parsed. */
private DataFlowMethod calledMethod;
/**
* The return Node of a node call, this will be null if method is void. If the return value is not read, the outgoing edges of this node will be empty. There
* should only be a single incoming edge from the return node of the called method. This NodeCall is the owner of the returnNode.
*/
private DataFlowNode returnNode;
/**
* The {@link DataFlowNode} on which this {@link NodeCall} was called. Can be null if this {@link NodeCall} was a static call. Will be "this" if the method
* was called on the same instance as the method to which the call belongs to.
*/
private DataFlowNode instance;
private String claz;
private String peckage;
public NodeCall(OwnedNode<?> owner) {
this.owner = owner;
}
private NodeCall(Builder builder) {
super(builder);
if (builder.in != null) {
this.setIn(builder.in);
}
this.owner = builder.owner == null ? this.owner : builder.owner;
this.calledMethod = builder.calledMethod == null ? this.calledMethod : builder.calledMethod;
this.claz = builder.claz == null ? this.claz : builder.claz;
this.peckage = builder.peckage == null ? this.peckage : builder.peckage;
this.returnNode = builder.returnNode == null ? this.returnNode : builder.returnNode;
this.instance = builder.instance == null ? this.instance : builder.instance;
}
@Override
public Optional<OwnedNode<?>> getOwner() {
return Optional.of(owner);
}
public Optional<ParameterList> getIn() {
return Optional.ofNullable(in);
}
public final void setIn(ParameterList in) {
this.in = in;
in.setOwnerAndName(this);
}
public Optional<DataFlowMethod> getCalledMethod() {
return Optional.ofNullable(calledMethod);
}
public void setCalledMethod(DataFlowMethod calledMethod) {
this.calledMethod = calledMethod;
this.in.connectTo(calledMethod.getParameters());
if (this.returnNode != null) {
if (calledMethod.getReturnNode().isPresent()) {
calledMethod.getReturnNode().get().addEdgeTo(returnNode);
} else {
LOG.warn("Could not connect method return node to NodeCall return Node because return node was not present in method {}", calledMethod);
}
}
}
public String getClaz() {
return claz;
}
public void setClaz(String claz) {
this.claz = claz;
}
public String getPeckage() {
return peckage;
}
public void setPeckage(String peckage) {
this.peckage = peckage;
}
public void setOwner(OwnedNode<?> owner) {
this.owner = owner;
}
public Optional<DataFlowNode> getReturnNode() {
return Optional.ofNullable(returnNode);
}
public void setReturnNode(DataFlowNode returnNode) {
this.returnNode = returnNode;
}
public Optional<DataFlowNode> getInstance() {
return Optional.ofNullable(instance);
}
public void setInstance(DataFlowNode instance) {
this.instance = instance;
}
/**
* @return True if the return value is used, false otherwise.
*/
public boolean isReturnRead() {
return returnNode != null && (
// If it has outgoing edges, it's value is read
!returnNode.getOut().isEmpty() ||
// If the owner is a methodCall, that means that this nodeCall's return is the direct input to another method, example a(b());
(owner != null && owner instanceof NodeCall));
}
/**
* Creates builder to build {@link NodeCall}.
*
* @return created builder
*/
public static Builder builder() {
return new Builder();
}
@Override
public boolean equals(Object obj) {
boolean equals = false;
if (this == obj) {
equals = true;
} else if (obj != null && getClass() == obj.getClass()) {
NodeCall other = (NodeCall) obj;
equals = new EqualsBuilder().appendSuper(super.equals(obj)).append(in, other.in).append(calledMethod, other.calledMethod).append(claz, other.claz)
.append(peckage, other.peckage).isEquals();
}
return equals;
}
@Override
public int hashCode() {
return Objects.hash(in, calledMethod, claz, peckage);
}
@Override
public String toString() {
return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).appendSuper(super.toString()).append("in", in).append("calledMethod", calledMethod)
.append("returnNode", returnNode).append("class", claz).append("package", peckage).build();
}
/**
* Builder to build {@link NodeCall}.
*/
public static final class Builder extends NodeRepresenter.Builder<Node, NodeCall.Builder> {
private ParameterList in;
private OwnedNode<?> owner;
private DataFlowMethod calledMethod;
private String claz;
private String peckage;
private DataFlowNode returnNode;
private DataFlowNode instance;
private Builder() {
// Builder should only be constructed via the parent class
}
public Builder in(ParameterList in) {
this.in = in;
return this;
}
public Builder in(DataFlowNode... inputNodes) {
this.in = ParameterList.builder().nodes(inputNodes).build();
return this;
}
public Builder owner(OwnedNode<?> owner) {
this.owner = owner;
return this;
}
public Builder calledMethod(DataFlowMethod calledMethod) {
this.calledMethod = calledMethod;
return this;
}
public Builder claz(String claz) {
this.claz = claz;
return this;
}
public Builder peckage(String peckage) {
this.peckage = peckage;
return this;
}
public Builder instance(DataFlowNode instance) {
this.instance = instance;
return this;
}
public Builder returnNode(DataFlowNode node) {
this.returnNode = node;
return this;
}
public NodeCall build() {
return new NodeCall(this);
}
}
}