-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlicer.java
More file actions
494 lines (466 loc) · 24.5 KB
/
Slicer.java
File metadata and controls
494 lines (466 loc) · 24.5 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
484
485
486
487
488
489
490
491
492
493
494
package AnalyzerPackage;
import javafx.scene.shape.Line;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Slicer {
private String code;
private String[] lines;
Slicer(ArrayList<String> filePaths) {
FilesUtil file = new FilesUtil();
code = file.readFiles(filePaths) + "\n";
lines = code.split("\n");
}
public int getLinesCount() {
return lines.length;
}
public boolean findStringToRegex(String stringToBeMatched, String regex) {
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(stringToBeMatched);
return matcher.find();
}
public boolean lookingAtStringToRegex(String stringToBeMatched, String regex) {
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(stringToBeMatched);
return matcher.lookingAt();
}
//a simple class that holds every line with its index and the comparing between to objects of that class is a compare between the indexes only.
private class LineOfCode implements Comparable {
public int index;
public String line;
public LineOfCode(int index, String line) {
this.index = index;
this.line = line;
}
@Override
public int compareTo(Object o) {
return index - ((LineOfCode) o).index;
}
}
//a simple class that holds every variable with its line of code
private class VarLine {
public LineOfCode line;
public String variableName;
public VarLine(LineOfCode line, String variableName) {
this.line = line;
this.variableName = variableName;
}
}
//This function checks if a specific variable already existed in the array "variablesAttachedTo" or not.
public boolean isFoundInVarlineSet(Set<VarLine> variablesAttachedTo, String varName) {
for (VarLine varLine : variablesAttachedTo) {
if (varLine.variableName.equals(varName)) {
return true;
}
}
return false;
}
public String staticSlicing(int endLine, String variableName) {
//array list holding teh final result of the slice
ArrayList<LineOfCode> result = new ArrayList<>();
//array list holding the bodies of the if conditions found in the specified slice
ArrayList<LineOfCode> ifConditions = new ArrayList<>();
//array list holding the variables that needs a double check to get any variables attached to the main variable that was selected by the user
ArrayList<String> revisit = new ArrayList<>();
//a set holding the variables attached to the variable selected by the user
Set<VarLine> variablesAttachedTo = new HashSet<>();
for (int i = 0; i < endLine; i++) {
lines[i] = lines[i].trim();
if ((lines[i].length() > 1 && lines[i].charAt(0) == '/' && lines[i].charAt(1) == '/')) {
continue;
}
String words[] = lines[i].trim().split(" ");
//searching for any if or while or for using regex and adding the bodies to if conditions
if (lookingAtStringToRegex(lines[i], "(if|for|while)(\\s)?\\(")) {
Deque<Character> stack = new ArrayDeque<Character>();
int temp = i;
String ifCondition = "";
stack.push('F');
while (!stack.isEmpty() && i < endLine) {
if (lines[i].contains("{")) {
stack.push('{');
ifCondition += lines[i] + "\n";
} else if (lines[i].contains(variableName)) {
if (!ifCondition.equals("")) {
ifCondition += String.valueOf(i + 1) + ": " + lines[i] + "\n";
} else {
ifCondition += lines[i] + "\n";
}
}
if (lines[i].contains("}")) {
stack.pop();
if (!stack.isEmpty() && (stack.getFirst() == 'F')) {
stack.pop();
}
if (!lines[i].contains(variableName)) {
ifCondition += "\n}";
}
}
i++;
}
ifConditions.add(new LineOfCode(i, String.valueOf(temp) + ' ' + ifCondition));
i = temp + 1;
}
//searching for the variable selected by the user here in all the cases
words = lines[i].trim().split(" ");
if (findStringToRegex(lines[i], "(\\w+|.)(\\[\\])? \\w+\\s?(=|;)\\s?") && !lines[i].contains("{") && !lines[i].contains("}") && !(lines[i].charAt(0) == '/') && !(lines[i].charAt(1) == '/')) {
if (words[1].equals("static")) {
if (words[3].contains(";")) {
words[3] = words[3].substring(0, words[3].indexOf(';'));
}
if (!isFoundInVarlineSet(variablesAttachedTo, words[3])) {
variablesAttachedTo.add(new VarLine(new LineOfCode(i, lines[i]), words[3]));
}
} else if (words.length == 3) {
if (words[2].contains(";")) {
words[2] = words[2].substring(0, words[2].indexOf(';'));
}
if (!isFoundInVarlineSet(variablesAttachedTo, words[2])) {
variablesAttachedTo.add(new VarLine(new LineOfCode(i, lines[i]), words[2]));
}
} else if (words.length == 2) {
if (words[1].contains(";")) {
words[1] = words[1].substring(0, words[1].indexOf(';'));
}
if (!isFoundInVarlineSet(variablesAttachedTo, words[1])) {
variablesAttachedTo.add(new VarLine(new LineOfCode(i, lines[i]), words[1]));
}
} else if (!words[1].equals("=")) {
if (words[0].equals("private") || words[0].equals("public")) {
if (words[2].contains(";")) {
words[2] = words[2].substring(0, words[2].indexOf(';'));
}
if (!isFoundInVarlineSet(variablesAttachedTo, words[2])) {
variablesAttachedTo.add(new VarLine(new LineOfCode(i, lines[i]), words[2]));
}
} else {
if (words[1].contains(";")) {
words[1] = words[1].substring(0, words[1].indexOf(';'));
}
if (!isFoundInVarlineSet(variablesAttachedTo, words[1])) {
variablesAttachedTo.add(new VarLine(new LineOfCode(i, lines[i]), words[1]));
}
}
}
}
if (!lines[i].contains(variableName)) {
continue;
}
if (words[0].equals(variableName) || (words.length > 1 && words[1].equals(variableName)) || (words.length > 2 && words[2].equals(variableName))) {
//result.add(new LineOfCode(i, lines[i]));
boolean foundCondition = false;
for (int j = 0; j < ifConditions.size(); j++) {
if (i < ifConditions.get(j).index && i > Integer.parseInt(ifConditions.get(j).line.split(" ")[0])) {
result.add(new LineOfCode(Integer.parseInt(ifConditions.get(j).line.split(" ")[0]), ifConditions.get(j).line.substring(2)));
foundCondition = true;
}
}
if (!foundCondition) {
result.add(new LineOfCode(i, lines[i]));
}
} else if (lines[i].contains(variableName)) {
for (int j = 0; j < words.length; j++) {
if ((words[j].contains(variableName) && (words[j].contains(variableName + "[") || words[j].contains(variableName + ".") || words[j].contains(variableName + ";") || words[j].equals(variableName)) && words[j].substring(0, variableName.length()).equals(variableName)) || words[j].equals("(" + variableName)) {
//result.add(new LineOfCode(i, lines[i]));
boolean foundCondition = false;
if (!lookingAtStringToRegex(lines[i], "(if|for|while)(\\s)?\\(")) {
revisit.add(lines[i]);
}
for (int k = 0; k < ifConditions.size(); k++) {
if (i < ifConditions.get(k).index && i > Integer.parseInt(ifConditions.get(k).line.split(" ")[0])) {
result.add(new LineOfCode(Integer.parseInt(ifConditions.get(k).line.split(" ")[0]), ifConditions.get(k).line.substring(2)));
foundCondition = true;
}
}
if (!foundCondition) {
result.add(new LineOfCode(i, lines[i]));
}
}
}
}
}
//that for loop searches for the initialization of the variables attached to the selected variable.
for (VarLine varLine : variablesAttachedTo) {
for (int i = 0; i < revisit.size(); i++) {
if (revisit.get(i).contains(varLine.variableName) && revisit.get(i).contains(variableName) && !varLine.variableName.equals(variableName)) {
result.add(varLine.line);
break;
}
}
}
// Hard coded values for the example given.
//result.add(new LineOfCode(0,"int n;"));
//result.add(new LineOfCode(1,"int i = 1;"));
Collections.sort(result);
String resultString = "";
for (int i = 0; i < result.size(); i++) {
if (i > 0 && result.get(i - 1).index == result.get(i).index) {
continue;
}
resultString += (result.get(i).index + 1) + ": " + result.get(i).line + "\n";
}
return resultString;
}
public String dynamicSlicing(int endLine, String variableName, int varValue) {
ArrayList<LineOfCode> result = new ArrayList<>();
ArrayList<LineOfCode> revisit = new ArrayList<>();
ArrayList<String> revisitVariables = new ArrayList<>();
ArrayList<String> ifConditions = new ArrayList<>();
ArrayList<LineOfCode> ifConditions2 = new ArrayList<>();
Deque<Character> stack = new ArrayDeque<Character>();
Set<VarLine> variablesAttachedTo = new HashSet<>();
for (int i = 0; i < endLine; i++) {
lines[i] = lines[i].trim();
if ((lines[i].length() > 1 && lines[i].charAt(0) == '/' && lines[i].charAt(1) == '/')) {
continue;
}
String words[] = lines[i].trim().split(" ");
if (lookingAtStringToRegex(lines[i], "(if|for|while)(\\s)?\\(") && !lines[i].contains(variableName)) {
int temp = i;
String ifCondition = "";
stack.push('F');
while (!stack.isEmpty() && i < endLine) {
if (lines[i].contains("{")) {
stack.push('{');
ifCondition += lines[i] + "\n";
}
if (lines[i].contains("}")) {
stack.pop();
if (!stack.isEmpty() && (stack.getFirst() == 'F')) {
stack.pop();
}
if (!lines[i].contains(variableName)) {
ifCondition += "\n}";
}
}
if (lines[i].contains(variableName)) {
if (!ifCondition.equals("")) {
ifCondition += String.valueOf(i + 1) + ": " + lines[i] + "\n";
} else {
ifCondition += lines[i] + "\n";
}
}
i++;
}
ifConditions2.add(new LineOfCode(i, String.valueOf(temp) + ' ' + ifCondition));
i = temp;
}
if (findStringToRegex(lines[i], "(\\w+|.)(\\[\\])? \\w+\\s?(=|;)\\s?") && !lines[i].contains("{") && !lines[i].contains("}") && !(lines[i].charAt(0) == '/') && !(lines[i].charAt(1) == '/')) {
if (words[1].equals("static")) {
if (words[3].contains(";")) {
words[3] = words[3].substring(0, words[3].indexOf(';'));
}
if (words[3] == variableName) {
result.add(new LineOfCode(i, lines[i]));
} else {
if (!isFoundInVarlineSet(variablesAttachedTo, words[3])) {
variablesAttachedTo.add(new VarLine(new LineOfCode(i, lines[i]), words[3]));
}
}
} else if (words.length == 3 && !words[1].equals("=")) {
if (words[2].contains(";")) {
words[2] = words[2].substring(0, words[2].indexOf(';'));
}
if (words[2] == variableName) {
result.add(new LineOfCode(i, lines[i]));
} else {
if (!isFoundInVarlineSet(variablesAttachedTo, words[2])) {
variablesAttachedTo.add(new VarLine(new LineOfCode(i, lines[i]), words[2]));
}
}
} else if (!words[1].equals("=")) {
if (words[0].equals("private") || words[0].equals("public")) {
if (words[2].contains(";")) {
words[2] = words[2].substring(0, words[2].indexOf(';'));
}
if (words[2] == variableName) {
result.add(new LineOfCode(i, lines[i]));
} else {
if (!isFoundInVarlineSet(variablesAttachedTo, words[2])) {
variablesAttachedTo.add(new VarLine(new LineOfCode(i, lines[i]), words[2]));
}
}
} else {
if (words[1].contains(";")) {
words[1] = words[1].substring(0, words[1].indexOf(';'));
}
if (words[1] == variableName) {
result.add(new LineOfCode(i, lines[i]));
} else {
if (!isFoundInVarlineSet(variablesAttachedTo, words[1])) {
variablesAttachedTo.add(new VarLine(new LineOfCode(i, lines[i]), words[1]));
}
}
}
}
}
if (!lines[i].contains(variableName)) {
continue;
}
if (words[0].equals(variableName) || (words.length > 1 && words[1].equals(variableName)) || (words.length > 2 && words[2].equals(variableName))) {
//result.add(new LineOfCode(i, lines[i]));
boolean foundCondition = false;
//if (lookingAtStringToRegex(lines[i], "(if|for|while)(\\s)?\\(")) {
for (int k = 0; k < ifConditions2.size(); k++) {
if (i < ifConditions2.get(k).index && i > Integer.parseInt(ifConditions2.get(k).line.split(" ")[0]) && !ifConditions2.get(k).line.split("\n")[0].contains(variableName)) {
result.add(new LineOfCode(Integer.parseInt(ifConditions2.get(k).line.split(" ")[0]), ifConditions2.get(k).line.substring(2)));
foundCondition = true;
}
else if (ifConditions2.get(k).line.split("\n")[0].contains(variableName))
{
foundCondition = true;
}
}
//}
if (!foundCondition) {
result.add(new LineOfCode(i, lines[i]));
}
} else if (lines[i].contains(variableName)) {
if (lookingAtStringToRegex(lines[i], "(if|for|while)(\\s)?\\(") || lookingAtStringToRegex(lines[i], "(else if)(\\s)?\\(")) {
revisit.add(new LineOfCode(i, lines[i]));
String ifCondition = "";
stack.push('F');
while (!stack.isEmpty() && i < endLine) {
if (lines[i].contains("{")) {
stack.push('{');
}
if (lines[i].contains("}")) {
stack.pop();
if (!stack.isEmpty() && (stack.getFirst() == 'F')) {
stack.pop();
}
if (!lines[i].contains(variableName)) {
ifCondition += "\n}";
}
}
if (lines[i].contains(variableName)) {
if (!ifCondition.equals("")) {
ifCondition += String.valueOf(i + 1) + ": " + lines[i] + "\n";
} else {
ifCondition += lines[i] + "\n";
}
revisitVariables.add(String.valueOf(i + 1) + ": " + lines[i]);
}
i++;
}
ifConditions.add(ifCondition);
i--;
} else {
for (int j = 0; j < words.length; j++) {
if ((words[j].contains(variableName) && (words[j].contains(variableName + "[") || words[j].contains(variableName + ".") || words[j].contains(variableName + ";") || words[j].equals(variableName)) && words[j].substring(0, variableName.length()).equals(variableName)) || words[j].equals("(" + variableName)) {
//result.add(new LineOfCode(i, lines[i]));
boolean foundCondition = false;
//if (lookingAtStringToRegex(lines[i], "(if|for|while)(\\s)?\\(")) {
for (int k = 0; k < ifConditions2.size(); k++) {
if (i < ifConditions2.get(k).index && i > Integer.parseInt(ifConditions2.get(k).line.split(" ")[0])) {
result.add(new LineOfCode(Integer.parseInt(ifConditions2.get(k).line.split(" ")[0]), ifConditions2.get(k).line.substring(2)));
foundCondition = true;
}
}
//}
if (!foundCondition) {
result.add(new LineOfCode(i, lines[i]));
}
}
}
}
}
}
//the for loop below checks if the given value passes the conditions that includes the selected variable.
for (int i = 0; i < revisit.size(); i++) {
String[] split, split2;
String condition = revisit.get(i).line;
condition = condition.trim();
ArrayList<String> operators = new ArrayList<>();
operators.add(">");
operators.add("<");
operators.add(">=");
operators.add("<=");
operators.add("==");
operators.add("!=");
for (String operator : operators) {
condition = condition.replace('(', ' ');
condition = condition.replace(')', ' ');
condition = condition.trim();
split = condition.split(operator);
if (split.length > 1 && condition.charAt(0) != '/' && condition.charAt(1) != '/') {
for (int j = 0; j < split.length - 1; j++) {
split2 = split[j].split(" ");
if (split2.length > 1) {
if (!split2[split2.length - 1].equals(variableName)) {
if (operator.equals(">")) {
if (varValue < Integer.parseInt(split2[split2.length - 1])) {
revisit.get(i).line = ifConditions.get(i);
result.add(revisit.get(i));
}
} else if (operator.equals("<")) {
if (varValue > Integer.parseInt(split2[split2.length - 1])) {
revisit.get(i).line = ifConditions.get(i);
result.add(revisit.get(i));
}
} else if (operator.equals("==") || operator.equals("<=") || operator.equals(">=")) {
if (varValue == Integer.parseInt(split2[split2.length - 1])) {
revisit.get(i).line = ifConditions.get(i);
result.add(revisit.get(i));
}
} else if (operator.equals("!=")) {
if (varValue != Integer.parseInt(split2[split2.length - 1])) {
revisit.get(i).line = ifConditions.get(i);
result.add(revisit.get(i));
}
}
}
}
split2 = split[j + 1].split(" ");
if (split2.length > 1) {
if (!split2[1].equals(variableName)) {
if (operator.equals(">")) {
if (varValue > Integer.parseInt(split2[1])) {
revisit.get(i).line = ifConditions.get(i);
result.add(revisit.get(i));
}
} else if (operator.equals("<")) {
if (varValue < Integer.parseInt(split2[1])) {
revisit.get(i).line = ifConditions.get(i);
result.add(revisit.get(i));
}
} else if (operator.equals("==") || operator.equals("<=") || operator.equals(">=")) {
if (varValue == Integer.parseInt(split2[1])) {
revisit.get(i).line = ifConditions.get(i);
result.add(revisit.get(i));
}
} else if (operator.equals("!=")) {
if (varValue != Integer.parseInt(split2[1])) {
revisit.get(i).line = ifConditions.get(i);
result.add(revisit.get(i));
}
}
}
}
}
}
}
}
for (VarLine varLine : variablesAttachedTo) {
for (int i = 0; i < revisitVariables.size(); i++) {
if (revisitVariables.get(i).contains(varLine.variableName) && revisitVariables.get(i).contains(variableName) && !varLine.variableName.equals(variableName)) {
result.add(varLine.line);
break;
}
}
}
// Hard coded values for the example given.
// result.add(new LineOfCode(0,"int n;"));
// result.add(new LineOfCode(1,"int i = 1;"));
Collections.sort(result);
Collections.sort(result);
String resultString = "";
for (int i = 0; i < result.size(); i++) {
if (i > 0 && result.get(i - 1).index == result.get(i).index) {
continue;
}
resultString += (result.get(i).index + 1) + ": " + result.get(i).line + "\n";
}
return resultString;
}
}