forked from MatteoBosco89/UnityCodeSmellAnalyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetaExtractor.cs
More file actions
484 lines (465 loc) · 20.1 KB
/
MetaExtractor.cs
File metadata and controls
484 lines (465 loc) · 20.1 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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json.Linq;
using System.Reflection;
using System.Drawing.Printing;
namespace MetaSmellDetector
{
public class MetaExtractor
{
/// <summary>
/// Get the names of all the mthods that extract smells
/// </summary>
/// <returns>A list containing the names of the methods</returns>
public static List<string> SmellsMethods()
{
List<string> methods = new List<string>();
MetaExtractor m = new MetaExtractor();
MethodInfo[] mList = m.GetType().GetMethods();
foreach(MethodInfo mi in mList)
{
if (mi.ReturnType != typeof(JObject)) continue;
if (mi.Name == "InvokeMethods") continue;
methods.Add(mi.Name);
}
return methods;
}
/// <summary>
/// This method uses reflection for invoke specificed methods for search smells
/// </summary>
/// <param name="lines"> The line contanning the parameters to invoche the method</param>
/// <param name="data"> The dataset to analyze</param>
public static JObject InvokeMethods(string lines, JArray data)
{
string[] s = lines.Split(',');
string name = s[0];
MetaExtractor ex = new MetaExtractor();
MethodInfo m = ex.GetType().GetMethod(name);
if(m!= null)
{
List<string> param = new List<string>();
for(int i=1; i < s.Length; i++)
{
param.Add(s[i]);
}
object[] args = { data,param };
JObject result = m.Invoke(ex, args) as JObject;
return result;
}
return null;
}
/// <summary>
/// This method searches for the smell "Heavy Physics Computation". The smell is present if an object uses a Rigidbody with m_CollisionDetection set
/// to 1 or 2
/// </summary>
/// <param name="data">The dataset to analyze</param>
/// <param name="paramList">The list of parameters</param>
public static JObject HeavyPhysics(JArray data, List<string> paramList)
{
Logger.Log(Logger.LogLevel.Debug, "Heavy Physics Computation...");
JArray smells = new JArray();
JObject result = new JObject();
result.Add("Name", "Heavy Physics Computation ");
if (paramList.Count == 5)
{
foreach (JObject c in data)
{
var token = c.SelectTokens($"$..COMPONENTS[?(@..{paramList[0]})]");
if (token.Count() > 0)
{
token = c.SelectTokens($"$..COMPONENTS[?(@..{paramList[1]} {paramList[2]} '{paramList[3]}')]");
if (token.Count() > 0)
{
JObject jo = new JObject();
jo.Add("FilePath", c["file_path"].ToString());
jo.Add("Name", c["name"].ToString());
smells.Add(jo);
}
token = c.SelectTokens($"$..COMPONENTS[?(@..{paramList[1]} {paramList[2]} '{paramList[4]}')]");
if (token.Count() > 0)
{
JObject jo = new JObject();
jo.Add("FilePath", c["file_path"].ToString());
jo.Add("Name", c["name"].ToString());
smells.Add(jo);
}
}
}
}
Logger.Log(Logger.LogLevel.Debug, "Done!!");
result.Add("Occurrency", smells.Count());
result.Add("Smells", smells);
return result;
}
/// <summary>
/// This method searches for the smell "SubOptimal", this method search all smell with activate rbaked light if object have Animator component (is dynamic)
/// </summary>
/// <param name="data">The dataset to analyze</param>
/// <param name="paramList">The list of parameters</param>
public static JObject SubOptimal(JArray data, List<string> paramList)
{
Logger.Log(Logger.LogLevel.Debug, "Searching SubOptimal Expensive Lights...");
JArray smells = new JArray();
JObject result = new JObject();
result.Add("Name", "SubOptimal Expensive Lights");
if (paramList.Count == 4)
{
foreach (JObject c in data)
{
var token = c.SelectTokens($"$..COMPONENTS[?(@..{paramList[0]})]");
if (token.Count() > 0)
{
token = c.SelectTokens($"$..COMPONENTS[?(@..{paramList[1]}{paramList[2]}{paramList[3]})]");
if (token.Count() > 0)
{
JObject jo = new JObject();
jo.Add("FilePath", c["file_path"].ToString());
jo.Add("Name", c["name"].ToString());
smells.Add(jo);
}
}
}
}
Logger.Log(Logger.LogLevel.Debug,"Done!!");
result.Add("Occurrency", smells.Count());
result.Add("Smells", smells);
return result;
}
/// <summary>
/// This method searches for the smell "SubOptimal1", this function search all smell with activate real-time light if doesn't have animator (is static)
/// </summary>
/// <param name="data">The dataset to analyze</param>
/// <param name="paramList">The list of parameters</param>
public static JObject SubOptimal1(JArray data, List<string> paramList)
{
Logger.Log(Logger.LogLevel.Debug, "Searching SubOptimal Expensive Lights with enable LightRealTime...");
JArray smells = new JArray();
JObject result = new JObject();
result.Add("Name", "SubOptimal Expensive Lights with enable LightRealTime");
if (paramList.Count == 4)
{
foreach (JObject c in data)
{
var token = c.SelectTokens($"$..COMPONENTS[?(@..{paramList[0]})]");
if (token.Count() <= 0)
{
token = c.SelectTokens($"$..COMPONENTS[?(@..{paramList[1]}{paramList[2]}{paramList[3]})]");
if (token.Count() > 0)
{
JObject jo = new JObject();
jo.Add("FilePath", c["file_path"].ToString());
jo.Add("Name", c["name"].ToString());
smells.Add(jo);
}
}
}
}
Logger.Log(Logger.LogLevel.Debug, "Done!!");
result.Add("Occurrency", smells.Count());
result.Add("Smells", smells);
return result;
}
/// <summary>
/// This method searches the smell related to light of type real time activated
/// </summary>
/// <param name="data">The dataset to analyze</param>
/// <param name="paramList">The list of parameters</param>
public static JObject LightSmell(JArray data, List<string> paramList)
{
Logger.Log(Logger.LogLevel.Debug, "Searching Lack of optimization when drawing-rendering...");
JArray smells = new JArray();
JObject result = new JObject();
result.Add("Name", "Lack of optimization when drawing-rendering");
if (paramList.Count == 3)
{
foreach (JObject c in data)
{
var token = c.SelectTokens($"$..COMPONENTS[?(@..{paramList[0]} {paramList[1]} {paramList[2]})]");
foreach (JToken p in token)
{
JObject jo = new JObject();
jo.Add("FilePath", c["file_path"].ToString());
jo.Add("Name", c["name"].ToString());
smells.Add(jo);
}
}
}
Logger.Log(Logger.LogLevel.Debug, "Done!!");
result.Add("Occurrency", smells.Count());
result.Add("Smells", smells);
return result;
}
/// <summary>
/// This method searches for smell Multiple Animator
/// </summary>
/// <param name="data">The dataset to analyze</param>
/// <param name="paramList">The list of parameters</param>
public static JObject MultipleAnimator(JArray data, List<string> paramList)
{
Logger.Log(Logger.LogLevel.Debug, "Searching Multiple animators for a single object smells...");
JArray smells = new JArray();
JObject result = new JObject();
result.Add("Name", "Multiple animators for a single object");
if (paramList.Count == 1)
{
foreach (JObject c in data)
{
List<string> st = new List<string>();
var token = c.SelectTokens($"$..COMPONENTS[?(@..{paramList[0]})]");
foreach (JToken p in token)
{
st.Add(p.ToString());
}
if (st.Count() > 1)
{
JObject jo = new JObject();
jo.Add("FilePath", c["file_path"].ToString());
jo.Add("Name", c["name"].ToString());
jo.Add("NumAnimator", st.Count());
smells.Add(jo);
}
}
}
Logger.Log(Logger.LogLevel.Debug, "Done!!");
result.Add("Occurrency", smells.Count());
result.Add("Smells", smells);
return result;
}
/// <summary>
/// This method searches for smell "Static coupling" with guid associated with other gameobjects
/// </summary>
/// <param name="data">The dataset to analyze</param>
/// <param name="paramList">The list of parameters</param>
public static JObject InstanceCounter(JArray data, List<string> paramList)
{
Logger.Log(Logger.LogLevel.Debug, "Searching Static Coupling Smells...");
JArray smells = new JArray();
JObject result = new JObject();
result.Add("Name", "Static Coupling Smells");
if (paramList.Count == 2)
{
int threshold = 0;
try
{
threshold = int.Parse(paramList[1]);
}
catch (Exception)
{
Logger.Log(Logger.LogLevel.Debug, "Parameter not a Int");
}
foreach (JObject c in data)
{
List<string> st = new List<string>();
var token = c.SelectTokens($"$..COMPONENTS[?(@..{paramList[0]})]..{paramList[0]}");
JArray reference = new JArray();
foreach (JToken p in token)
{
JObject res = Utility.SearchData(data, paramList[0], p.ToString());
if(res != null)
{
JObject stat = new JObject();
stat["FilePath"] = res["file_path"];
stat["Name"] = res["name"];
stat["guid"] = res["guid"];
stat["Type"] = res["type"];
reference.Add(stat);
}
}
if(reference.Count > threshold)
{
JObject jo = new JObject();
jo.Add("FilePath", c["file_path"].ToString());
jo.Add("Name", c["name"].ToString());
jo.Add("NumReference", reference.Count());
jo.Add("Reference", reference);
smells.Add(jo);
}
}
}
Logger.Log(Logger.LogLevel.Debug, "Done!!");
result.Add("Occurrency", smells.Count());
result.Add("Smells", smells);
return result;
}
/// <summary>
/// This method searches for the smell with Anystate in the animator
/// </summary>
/// <param name="data">The dataset to analyze</param>
/// <param name="paramList">The list of parameters</param>
public static JObject AnyState(JArray data, List<string> paramList)
{
Logger.Log(Logger.LogLevel.Debug, "Searching AnyState Smells...");
JArray smells = new JArray();
JObject result = new JObject();
result.Add("Name", "Anystate Smells");
if (paramList.Count == 1) {
foreach (JObject c in data)
{
bool found = false;
var token = c.SelectTokens($"$..COMPONENTS[?(@..{paramList[0]})]..{paramList[0]}");
foreach (JToken p in token)
{
if (found) break;
if (p is JArray)
{
if (p.Count() > 0)
{
JObject jo = new JObject();
jo.Add("FilePath", c["file_path"].ToString());
jo.Add("Name", c["name"].ToString());
smells.Add(jo);
found = true;
}
}
}
}
}
Logger.Log(Logger.LogLevel.Debug, "Done!!");
result.Add("Occurrency", smells.Count());
result.Add("Smells", smells);
return result;
}
/// <summary>
/// This method searches num_components or file_size greather than a given treshold
/// </summary>
/// <param name="data">The dataset to analyze</param>
/// <param name="paramList">The list of parameters</param>
public static JObject QuerySearch(JArray data, List<string> paramList)
{
Logger.Log(Logger.LogLevel.Debug, "Searching Bloated Assets Smells...");
JArray smells = new JArray();
JObject result = new JObject();
result.Add("Name", "Bloated Assets Smells");
if (paramList.Count == 3)
{
try
{
int result1 = Int32.Parse(paramList[2]);
foreach (JToken token in data)
{
string svalue = token[paramList[0]].ToString();
int val = int.Parse(svalue);
if(val > result1)
{
JObject jo = new JObject();
jo.Add("FilePath", token["file_path"].ToString());
jo.Add("Name", token["name"].ToString());
jo.Add("NumComponents", token["num_components"].ToString());
smells.Add(jo);
}
}
/*List<JToken> tokens = data.SelectTokens($"$.[?(@.{paramList[0]} {paramList[1]} {result1})]").ToList();
foreach (JToken c in tokens)
{
JObject jo = new JObject();
jo.Add("FilePath", c["file_path"].ToString());
jo.Add("Name", c["name"].ToString());
jo.Add("NumComponents", c["num_components"].ToString());
smells.Add(jo);
}*/
}
catch (FormatException)
{
Logger.Log(Logger.LogLevel.Debug, "Parameter is not an int");
}
}
Logger.Log(Logger.LogLevel.Debug, "Done!!");
result.Add("Occurrency", smells.Count());
result.Add("Smells", smells);
return result;
}
/// <summary>
/// This method searches for components containing a meshcollider
/// </summary>
/// <param name="data">The dataset to analyze</param>
/// <param name="paramList">The list of parameters</param>
public static JObject SearchSmellByParam(JArray data, List<string> paramList)
{
Logger.Log(Logger.LogLevel.Debug, "Search Mesh Collider Smells...");
JArray smells = new JArray();
JObject result = new JObject();
result.Add("Name", "Mesh Collider Smells");
if (paramList.Count == 1)
{
var res = data.SelectTokens($"$.[?(@..{paramList[0]})]");
JArray results = new JArray(res);
foreach (JToken obj in results)
{
JObject s = new JObject();
s.Add("FileName", obj["file_path"]);
s.Add("Name", obj["name"]);
s.Add("Type", obj["type"]);
smells.Add(s);
}
}
Logger.Log(Logger.LogLevel.Debug, "Done!!");
result.Add("Occurrency", smells.Count());
result.Add("Smells", smells);
return result;
}
/// <summary>
/// This method search animation with too many key frames
/// </summary>
/// <param name="data">The dataset to analyze</param>
/// <param name="paramList">The list of parameters</param>
public static JObject TooManyKeyFrames(JArray data, List<string> paramList)
{
Logger.Log(Logger.LogLevel.Debug, "Search Too Many Key Frames Smell...");
JArray smells = new JArray();
JObject result = new JObject();
result.Add("Name", "Too Many Key Frames");
if(paramList.Count == 1)
{
int threshold = 0;
try
{
threshold = int.Parse(paramList[0]);
}
catch (Exception)
{
Logger.Log(Logger.LogLevel.Debug, "Threshold is not a number, no threshold set");
}
var res = data.SelectTokens("$..[?(@.type == 'anim')]");
foreach (JToken obj in res)
{
res = obj.SelectTokens("$..m_Curve");
if (res.Count() > 0)
{
JToken animData = res.ElementAt(0);
var res1 = animData.SelectTokens("$..time");
int numKey = res1.Count();
if(numKey >= threshold)
{
JObject s = new JObject();
s.Add("FileName", obj["file_path"]);
s.Add("Name", obj["name"]);
s.Add("Type", obj["type"]);
s.Add("KeyFrames", numKey);
smells.Add(s);
}
}
}
}
Logger.Log(Logger.LogLevel.Debug, "Done!!");
result.Add("Occurrency", smells.Count());
result.Add("Smells", smells);
return result;
}
}
public static class Utility
{
public static JObject SearchData(JArray data, string param, string value)
{
foreach (JToken tok in data)
{
var p_val = tok[param].ToString();
if(p_val == value)
{
return tok as JObject;
}
}
return null;
}
}
}