forked from tushartushar/DesigniteJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSM_Type.java
More file actions
404 lines (341 loc) · 11.7 KB
/
SM_Type.java
File metadata and controls
404 lines (341 loc) · 11.7 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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package Designite.SourceModel;
import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.ImportDeclaration;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.Name;
import org.eclipse.jdt.core.dom.Type;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import Designite.InputArgs;
import Designite.metrics.MethodMetrics;
import Designite.smells.implementationSmells.ImplementationSmellDetector;
import Designite.smells.models.ImplementationCodeSmell;
import Designite.utils.CSVUtils;
import Designite.utils.Constants;
import Designite.utils.models.Edge;
import Designite.utils.models.Vertex;
import Designite.visitors.StaticFieldAccessVisitor;
//TODO check EnumDeclaration, AnnotationTypeDeclaration and nested classes
public class SM_Type extends SM_SourceItem implements Vertex {
private boolean isAbstract = false;
private boolean isInterface = false;
private SM_Package parentPkg;
private TypeDeclaration typeDeclaration;
private TypeDeclaration containerClass;
private boolean nestedClass;
private List<SM_Type> superTypes = new ArrayList<>();
private List<SM_Type> subTypes = new ArrayList<>();
private List<SM_Type> referencedTypeList = new ArrayList<>();
private List<SM_Type> typesThatReferenceThisList = new ArrayList<>();
private List<SM_Type> nestedTypesList = new ArrayList<>();
private List<ImportDeclaration> importList = new ArrayList<>();
private List<SM_Method> methodList = new ArrayList<>();
private List<SM_Field> fieldList = new ArrayList<>();
private List<Name> staticFieldAccesses = new ArrayList<>();
private List<SM_Type> staticFieldAccessList = new ArrayList<>();
private List<SM_Type> staticMethodInvocations = new ArrayList<>();
private Map<SM_Method, MethodMetrics> metricsMapping = new HashMap<>();
private Map<SM_Method, List<ImplementationCodeSmell>> smellMapping = new HashMap<>();
private InputArgs inputArgs;
public SM_Type(TypeDeclaration typeDeclaration, CompilationUnit compilationUnit, SM_Package pkg, InputArgs inputArgs) {
parentPkg = pkg;
if (typeDeclaration == null || compilationUnit == null)
throw new NullPointerException();
name = typeDeclaration.getName().toString();
this.typeDeclaration = typeDeclaration;
this.inputArgs = inputArgs;
setTypeInfo();
setAccessModifier(typeDeclaration.getModifiers());
setImportList(compilationUnit);
}
public List<SM_Type> getSuperTypes() {
return superTypes;
}
public List<SM_Type> getSubTypes() {
return subTypes;
}
public List<SM_Type> getReferencedTypeList() {
return referencedTypeList;
}
public List<SM_Type> getTypesThatReferenceThis() {
return typesThatReferenceThisList;
}
public TypeDeclaration getTypeDeclaration() {
return typeDeclaration;
}
public void addReferencedTypeList(SM_Type type) {
referencedTypeList.add(type);
}
public void addStaticMethodInvocation(SM_Type type) {
if (!this.staticMethodInvocations.contains(type)){
this.staticMethodInvocations.add(type);
}
}
public void addNestedClass(SM_Type type) {
if (!this.nestedTypesList.contains(type)) {
this.nestedTypesList.add(type);
}
}
public SM_Type getNestedTypeFromName(String typeName) {
for(SM_Type nestedType : this.nestedTypesList) {
if(nestedType.name.equals(typeName)) {
return nestedType;
}
}
return null;
}
public List<SM_Type> getNestedTypes() {
return this.nestedTypesList;
}
public boolean containsTypeInReferencedTypeList(SM_Type type) {
return referencedTypeList.contains(type);
}
public void addTypesThatReferenceThisList(SM_Type type) {
typesThatReferenceThisList.add(type);
}
public boolean containsTypeInTypesThatReferenceThisList(SM_Type type) {
return typesThatReferenceThisList.contains(type);
}
private void setTypeInfo() {
int modifier = typeDeclaration.getModifiers();
if (Modifier.isAbstract(modifier)) {
isAbstract = true;
}
if (typeDeclaration.isInterface()) {
isInterface = true;
}
}
public boolean isAbstract() {
return isAbstract;
}
public boolean isInterface() {
return isInterface;
}
public void setNestedClass(TypeDeclaration referredClass) {
nestedClass = true;
this.containerClass = referredClass;
}
public boolean isNestedClass() {
return nestedClass;
}
private void setImportList(CompilationUnit unit) {
ImportVisitor importVisitor = new ImportVisitor();
unit.accept(importVisitor);
List<ImportDeclaration> imports = importVisitor.getImports();
if (imports.size() > 0)
importList.addAll(imports);
}
public List<ImportDeclaration> getImportList() {
return importList;
}
private void setSuperTypes() {
setSuperClass();
setSuperInterface();
}
private void setSuperClass() {
Type superclass = typeDeclaration.getSuperclassType();
if (superclass != null)
{
SM_Type inferredType = (new Resolver()).resolveType(superclass, parentPkg.getParentProject());
if(inferredType != null) {
superTypes.add(inferredType);
inferredType.addThisAsChildToSuperType(this);
}
}
}
private void setSuperInterface() {
List<Type> superInterfaces = typeDeclaration.superInterfaceTypes();
if (superInterfaces != null)
{
for (Type superInterface : superInterfaces) {
SM_Type inferredType = (new Resolver()).resolveType(superInterface, parentPkg.getParentProject());
if(inferredType != null) {
superTypes.add(inferredType);
inferredType.addThisAsChildToSuperType(this);
}
}
}
}
private void addThisAsChildToSuperType(SM_Type child) {
if (!subTypes.contains(child)) {
subTypes.add(child);
}
}
public List<SM_Method> getMethodList() {
return methodList;
}
public List<SM_Field> getFieldList() {
return fieldList;
}
public SM_Package getParentPkg() {
return parentPkg;
}
private void parseMethods() {
for (SM_Method method : methodList) {
method.parse();
}
}
private void parseFields() {
for (SM_Field field : fieldList) {
field.parse();
}
}
@Override
public void printDebugLog(PrintWriter writer) {
print(writer, "\tType: " + name);
print(writer, "\tPackage: " + this.getParentPkg().getName());
print(writer, "\tAccess: " + accessModifier);
print(writer, "\tInterface: " + isInterface);
print(writer, "\tAbstract: " + isAbstract);
print(writer, "\tSupertypes: " + ((getSuperTypes().size() != 0) ? getSuperTypes().get(0).getName() : "Object"));
print(writer, "\tNested class: " + nestedClass);
if (nestedClass)
print(writer, "\tContainer class: " + containerClass.getName());
print(writer, "\tReferenced types: ");
for (SM_Type type:referencedTypeList)
print(writer, "\t\t" + type.getName());
for (SM_Field field : fieldList)
field.printDebugLog(writer);
for (SM_Method method : methodList)
method.printDebugLog(writer);
print(writer, "\t----");
}
@Override
public void parse() {
MethodVisitor methodVisitor = new MethodVisitor(typeDeclaration, this);
typeDeclaration.accept(methodVisitor);
List<SM_Method> mList = methodVisitor.getMethods();
if (mList.size() > 0)
methodList.addAll(mList);
parseMethods();
FieldVisitor fieldVisitor = new FieldVisitor(this);
typeDeclaration.accept(fieldVisitor);
List<SM_Field> fList = fieldVisitor.getFields();
if (fList.size() > 0)
fieldList.addAll(fList);
parseFields();
StaticFieldAccessVisitor fieldAccessVisitor = new StaticFieldAccessVisitor();
typeDeclaration.accept(fieldAccessVisitor);
staticFieldAccesses = fieldAccessVisitor.getStaticFieldAccesses();
}
@Override
public void resolve() {
for (SM_Method method : methodList)
method.resolve();
for (SM_Field field : fieldList)
field.resolve();
setStaticAccessList();
setReferencedTypes();
setTypesThatReferenceThis();
setSuperTypes();
updateHierarchyGraph();
updateDependencyGraph();
}
private void setStaticAccessList() {
staticFieldAccessList = (new Resolver()).inferStaticAccess(staticFieldAccesses, this);
}
private void setReferencedTypes() {
for (SM_Field field:fieldList)
if(!field.isPrimitiveType()) {
addUniqueReference(this, field.getType(), false);
}
for (SM_Method method:methodList) {
for (SM_Type refType:method.getReferencedTypeList()) {
addUniqueReference(this, refType, false);
}
}
for (SM_Type staticAccessType : staticFieldAccessList) {
addUniqueReference(this, staticAccessType, false);
}
for (SM_Type methodInvocation : staticMethodInvocations){
addUniqueReference(this, methodInvocation, false);
}
}
private void setTypesThatReferenceThis() {
for (SM_Type refType : referencedTypeList) {
addUniqueReference(refType, this, true);
}
}
private void updateHierarchyGraph() {
if (superTypes.size() > 0) {
for (SM_Type superType : superTypes) {
getParentPkg().getParentProject().getHierarchyGraph().addEdge(
new Edge(this, superType));
}
}
getParentPkg().getParentProject().getHierarchyGraph().addVertex(this);
}
private void updateDependencyGraph() {
if (getReferencedTypeList().size() > 0) {
for (SM_Type dependency : getReferencedTypeList()) {
getParentPkg().getParentProject().getDependencyGraph().addEdge(
new Edge(this, dependency));
}
}
getParentPkg().getParentProject().getDependencyGraph().addVertex(this);
}
private void addUniqueReference(SM_Type type, SM_Type typeToAdd, boolean invardReference) {
if(typeToAdd == null)
return;
if (invardReference) {
if (!type.containsTypeInTypesThatReferenceThisList(typeToAdd)) {
type.addTypesThatReferenceThisList(typeToAdd);//FAN-IN?
}
} else {
if (!type.containsTypeInReferencedTypeList(typeToAdd)) {
type.addReferencedTypeList(typeToAdd);//FAN-OUT?
}
}
}
public void extractMethodMetrics() {
for (SM_Method method : methodList) {
MethodMetrics metrics = new MethodMetrics(method);
metrics.extractMetrics();
metricsMapping.put(method, metrics);
exportMethodMetricsToCSV(metrics, method.getName());
}
}
public MethodMetrics getMetricsFromMethod(SM_Method method) {
return metricsMapping.get(method);
}
public void exportMethodMetricsToCSV(MethodMetrics metrics, String methodName) {
String path = inputArgs.getOutputFolder()
+ File.separator + Constants.METHOD_METRICS_PATH_SUFFIX;
CSVUtils.addToCSVFile(path, getMetricsAsARow(metrics, methodName));
}
private String getMetricsAsARow(MethodMetrics metrics, String methodName) {
return getParentPkg().getParentProject().getName()
+ "," + getParentPkg().getName()
+ "," + getName()
+ "," + methodName
+ "," + metrics.getNumOfLines()
+ "," + metrics.getCyclomaticComplexity()
+ "," + metrics.getNumOfParameters()
+ "\n";
}
public void extractCodeSmells() {
for (SM_Method method : methodList) {
ImplementationSmellDetector detector = new ImplementationSmellDetector(metricsMapping.get(method)
, new SourceItemInfo(getParentPkg().getParentProject().getName()
, getParentPkg().getName()
, getName()
, method.getName()));
smellMapping.put(method, detector.detectCodeSmells());
exportDesignSmellsToCSV(method);
}
}
private void exportDesignSmellsToCSV(SM_Method method) {
CSVUtils.addAllToCSVFile(inputArgs.getOutputFolder()
+ File.separator + Constants.IMPLEMENTATION_CODE_SMELLS_PATH_SUFFIX
, smellMapping.get(method));
}
@Override
public String toString() {
return "Type="+name;
}
}