forked from tushartushar/DesigniteJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSM_Method.java
More file actions
292 lines (251 loc) · 8.6 KB
/
SM_Method.java
File metadata and controls
292 lines (251 loc) · 8.6 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
package Designite.SourceModel;
import java.io.PrintWriter;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jdt.core.dom.FieldAccess;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
import org.eclipse.jdt.core.dom.Type;
import Designite.utils.models.Vertex;
import Designite.visitors.DirectAceessFieldVisitor;
import Designite.visitors.InstanceOfVisitor;
import Designite.visitors.ThrowVisitor;
public class SM_Method extends SM_SourceItem implements Vertex {
private boolean abstractMethod;
private boolean finalMethod;
private boolean staticMethod;
private boolean isConstructor;
private boolean throwsException;
private SM_Type parentType;
private MethodDeclaration methodDeclaration;
private List<SM_Method> calledMethodsList = new ArrayList<SM_Method>();
private List<SM_Parameter> parameterList = new ArrayList<SM_Parameter>();
private List<SM_LocalVar> localVarList = new ArrayList<SM_LocalVar>();
private List<MethodInvocation> calledMethods = new ArrayList<MethodInvocation>();
private List<SM_Type> referencedTypeList = new ArrayList<SM_Type>();
private List<SimpleName> namesInMethod = new ArrayList<>();
private List<FieldAccess> thisAccessesInMethod = new ArrayList<>();
private List<SM_Field> directFieldAccesses = new ArrayList<>();
private List<Type> typesInInstanceOf = new ArrayList<>();
private List<SM_Type> smTypesInInstanceOf = new ArrayList<>();
public SM_Method(MethodDeclaration methodDeclaration, SM_Type typeObj) {
name = methodDeclaration.getName().toString();
this.parentType = typeObj;
this.methodDeclaration = methodDeclaration;
setMethodInfo(methodDeclaration);
setAccessModifier(methodDeclaration.getModifiers());
}
public void setMethodInfo(MethodDeclaration method) {
int modifiers = method.getModifiers();
if (Modifier.isAbstract(modifiers))
abstractMethod = true;
if (Modifier.isFinal(modifiers))
finalMethod = true;
if (Modifier.isStatic(modifiers))
staticMethod = true;
if (method.isConstructor())
isConstructor = true;
}
public boolean isAbstract() {
return this.abstractMethod;
}
public boolean isStatic() {
return this.staticMethod;
}
public boolean isFinal() {
return this.finalMethod;
}
public boolean isConstructor() {
return this.isConstructor;
}
public SM_Type getParentType() {
return parentType;
}
public boolean throwsException() {
return throwsException;
}
public boolean hasBody() {
return this.methodDeclaration.getBody() != null;
}
public List<SM_Parameter> getParameterList() {
return parameterList;
}
public List<SM_LocalVar> getLocalVarList() {
return localVarList;
}
public List<SM_Method> getCalledMethods() {
return calledMethodsList;
}
public MethodDeclaration getMethodDeclaration() {
return methodDeclaration;
}
private void parseParameters() {
for (SM_Parameter param : parameterList) {
param.parse();
}
}
private void parseLocalVar() {
for (SM_LocalVar var : localVarList) {
var.parse();
}
}
public String getMethodBody() {
if (this.hasBody())
return this.getMethodDeclaration().getBody().toString();
else
return "";
}
@Override
public void printDebugLog(PrintWriter writer) {
print(writer, "\t\tMethod: " + name);
print(writer, "\t\tParent type: " + this.getParentType().getName());
print(writer, "\t\tConstructor: " + isConstructor);
print(writer, "\t\tReturns: " + methodDeclaration.getReturnType2());
print(writer, "\t\tAccess: " + accessModifier);
print(writer, "\t\tAbstract: " + abstractMethod);
print(writer, "\t\tFinal: " + finalMethod);
print(writer, "\t\tStatic: " + staticMethod);
print(writer, "\t\tCalled methods: ");
for(SM_Method method:getCalledMethods())
print(writer, "\t\t\t" + method.getName());
for (SM_Parameter param : parameterList)
param.printDebugLog(writer);
for (SM_LocalVar var : localVarList)
var.printDebugLog(writer);
print(writer, "\t\t----");
}
//TODO: Modularize parser with private functions
@Override
public void parse() {
MethodInvVisitor invVisitor = new MethodInvVisitor(methodDeclaration);
methodDeclaration.accept(invVisitor);
List<MethodInvocation> invList = invVisitor.getCalledMethods();
if (invList.size() > 0) {
calledMethods.addAll(invList);
}
List<SingleVariableDeclaration> variableList = methodDeclaration.parameters();
for (SingleVariableDeclaration var : variableList) {
VariableVisitor parameterVisitor = new VariableVisitor(this);
// methodDeclaration.accept(parameterVisitor);
var.accept(parameterVisitor);
List<SM_Parameter> pList = parameterVisitor.getParameterList();
if (pList.size() > 0) {
parameterList.addAll(pList);
}
parseParameters();
}
LocalVarVisitor localVarVisitor = new LocalVarVisitor(this);
methodDeclaration.accept(localVarVisitor);
List<SM_LocalVar> lList = localVarVisitor.getLocalVarList();
if (lList.size() > 0) {
localVarList.addAll(lList);
}
parseLocalVar();
DirectAceessFieldVisitor directAceessFieldVisitor = new DirectAceessFieldVisitor();
methodDeclaration.accept(directAceessFieldVisitor);
List<SimpleName> names = directAceessFieldVisitor.getNames();
List<FieldAccess> thisAccesses = directAceessFieldVisitor.getThisAccesses();
if (names.size() > 0) {
namesInMethod.addAll(names);
}
if (thisAccesses.size() > 0) {
thisAccessesInMethod.addAll(thisAccesses);
}
InstanceOfVisitor instanceOfVisitor = new InstanceOfVisitor();
methodDeclaration.accept(instanceOfVisitor);
List<Type> instanceOfTypes = instanceOfVisitor.getTypesInInstanceOf();
if (instanceOfTypes.size() > 0) {
typesInInstanceOf.addAll(instanceOfTypes);
}
ThrowVisitor throwVisithor = new ThrowVisitor();
methodDeclaration.accept(throwVisithor);
throwsException = throwVisithor.throwsException();
}
@Override
public void resolve() {
for (SM_Parameter param : parameterList) {
param.resolve();
}
for (SM_LocalVar localVar : localVarList) {
localVar.resolve();
}
calledMethodsList = (new Resolver()).inferCalledMethods(calledMethods, parentType);
setReferencedTypes();
setDirectFieldAccesses();
setSMTypesInInstanceOf();
}
private void setReferencedTypes() {
for (SM_Parameter param : parameterList) {
if (!param.isPrimitiveType()) {
addunique(param.getType());
}
}
for (SM_LocalVar localVar : localVarList) {
if (!localVar.isPrimitiveType()) {
addunique(localVar.getType());
}
}
for (SM_Method methodCall : calledMethodsList) {
if (methodCall.isStatic()) {
addunique(methodCall.getParentType());
}
}
}
private void setDirectFieldAccesses() {
for (FieldAccess thisAccess : thisAccessesInMethod) {
SM_Field sameField = getFieldWithSameName(thisAccess.getName().toString());
if (sameField != null && !directFieldAccesses.contains(sameField)) {
directFieldAccesses.add(sameField);
}
}
for (SimpleName name : namesInMethod) {
if (!existsAsNameInLocalVars(name.toString())) {
SM_Field sameField = getFieldWithSameName(name.toString());
if (sameField != null && !directFieldAccesses.contains(sameField)) {
directFieldAccesses.add(sameField);
}
}
}
}
private boolean existsAsNameInLocalVars(String name) {
for (SM_LocalVar localVar : localVarList) {
if (name.equals(localVar.getName())) {
return true;
}
}
return false;
}
private SM_Field getFieldWithSameName(String name) {
for (SM_Field field : parentType.getFieldList()) {
if (name.equals(field.getName())) {
return field;
}
}
return null;
}
private void setSMTypesInInstanceOf() {
Resolver resolver = new Resolver();
for (Type type : typesInInstanceOf) {
SM_Type smType = resolver.resolveType(type, parentType.getParentPkg().getParentProject());
if (smType != null && !smTypesInInstanceOf.contains(smType)) {
smTypesInInstanceOf.add(smType);
}
}
}
private void addunique(SM_Type variableType) {
if (!referencedTypeList.contains(variableType))
referencedTypeList.add(variableType);
}
public List<SM_Type> getReferencedTypeList() {
return referencedTypeList;
}
public List<SM_Field> getDirectFieldAccesses() {
return directFieldAccesses;
}
public List<SM_Type> getSMTypesInInstanceOf() {
return smTypesInInstanceOf;
}
}