forked from mahnooshshd/extract_method
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource_code_parser.py
More file actions
433 lines (331 loc) · 12.1 KB
/
source_code_parser.py
File metadata and controls
433 lines (331 loc) · 12.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
import glob
import re
from pathlib import Path
import networkx as nx
import matplotlib.pyplot as plt
control_dep_weight = 1
data_dep_weight = 2
def get_file_path(class_name, project_path):
if '$' in class_name:
class_name = class_name.split('$')[0]
file_path = Path(project_path).rglob(class_name + '.java')
for f in file_path:
return f.absolute()
return None
def count_loc(class_name, method_name, project_path):
file_path = get_file_path(class_name, project_path)
if file_path is None:
return None
source_lines = open(file_path, 'r').readlines()
method_found = False
for line in source_lines:
if '//' in line:
line = line.split('//')[0]
method_def = re.findall('(public|private|protected) .* %s\(.*'%method_name, line)
if 'abstract' in line:
method_def = ''
if len(method_def) > 0:
method_found = True
scope_stack = []
line_counter = 0
end_of_define = False
if method_found:
try:
if not end_of_define and ')' in line:
end_of_define = True
if not end_of_define:
continue
if '{' in line and re.search(r"(?<=').*?{.*?(?=')", line) is None and re.search(r'(?<=").*?{.*?(?=")', line) is None:
scope_stack.append('{')
if '}' in line and re.search(r"(?<=').*?}.*?(?=')", line) is None and re.search(r'(?<=").*?}.*?(?=")', line) is None:
scope_stack.pop()
if len(line.strip().replace('\n', '')) > 0:
line_counter += 1
if len(scope_stack) == 0 and line_counter > 1:
return line_counter
except Exception as e:
pass
def get_statement(line):
if 'else if' in line:
return 'else if'
if ('if(' in line or 'if ' in line) and not ';' in line:
return 'if'
for_search = re.search('for ?\(.+;.*;.*\)', line)
if for_search:
return 'for'
else_search = else_search = re.match('( |})*else( |{)*', line)
if else_search:
return 'else'
try_search = re.match('( |})*try( |{)*', line)
if try_search:
return 'try'
catch_search = re.match('( |})*catch *\(.*\)', line)
if catch_search:
return 'catch'
while_search = re.match('( |})*while *\(.*\)', line)
if while_search:
return 'while'
if '=' in line and not '==' in line and not '=<' in line and not '>=' in line:
return 'assignment'
if 'return ' in line:
return 'return'
def is_final_var(line, var):
try:
is_final = re.findall('final %s'%var, line)
if len(is_final) > 0:
return True
return False
except:
return False
def get_if_vars(line):
parts = re.split('&&|\|\|', line)
variables = []
for part in parts:
clean_part = part.replace('if','').strip()
inner_parts = re.split('==|>=|<=|!=|>|<', clean_part)
for inner_part in inner_parts:
detected_names = re.match('\(?\s*(\w+)[^.|(]*\.?\w*(\(.*\))?.*', inner_part.replace('!',''))
if detected_names:
obj_name = detected_names.group(1)
args_names = detected_names.group(2)
if args_names is not None:
arg_names = args_names.replace('(', '').replace(')', '').split(',')
for arg_name in arg_names:
if '"' in arg_name or "'" in arg_name:
continue
clean_name = re.split('\.|\[', arg_name)[0].strip()
if len(clean_name) > 0 and clean_name != 'null' and not clean_name.isdigit():
variables.append(clean_name)
clean_name = obj_name.strip()
if len(clean_name) > 0 and clean_name != 'null' and not clean_name.isdigit():
variables.append(clean_name)
return variables
def get_assignment_vars(line):
variables = []
parts = line.split('=')
left_part = parts[0]
if len(left_part) < len(line):
def_var = left_part.strip().split(' ')[-1]
if '.' in def_var:
def_var = def_var.split('.')[0]
if '[' in def_var:
def_var = def_var.split('[')[0]
right_part = '='.join(parts[1:]).strip()
else:
right_part = line.strip()
inner_parts = re.split('==|>=|<=|!=|>|<|\+|\-|\/|\*|\%', right_part)
for inner_part in inner_parts:
detected_names = re.match('\(?\s*(\w+)[^.|(]*\.?\w*(\(.*\))?.*', inner_part.replace('!',''))
if detected_names:
obj_name = detected_names.group(1)
args_names = detected_names.group(2)
if args_names is not None:
arg_names = args_names.replace('(', '').replace(')', '').split(',')
for arg_name in arg_names:
if '"' in arg_name or "'" in arg_name:
continue
clean_name = re.split('\.|\[', arg_name)[0].strip()
if len(clean_name) > 0 and clean_name != 'null' and not clean_name.isdigit():
variables.append(clean_name)
clean_name = obj_name.strip()
if len(clean_name) > 0 and clean_name != 'null' and not clean_name.isdigit():
variables.append(clean_name)
return def_var, variables
def get_for_vars(line):
variables = []
parts = line.replace('for', '').strip().split(';')
iterator_var = parts[0].replace('(', '').split('=')[0]
iterator_var = iterator_var.replace('int', '').strip()
variables.append(iterator_var)
cond_vars = re.split('==|>=|<=|!=|>|<', parts[1])
if cond_vars[0].strip() == iterator_var:
variables.append(cond_vars[1].split('.')[0].strip())
else:
variables.append(cond_vars[0].split('.')[0].strip())
return variables
def get_while_vars(line):
variables = []
clean_line = line.replace('while','').replace('(', '').replace(')', '').strip()
detected_names = re.match('\!?(\w+)([^a-zA-z]*(\w*)\.?(\w*)?.*(==|>=|<=|!=|>|<)[^a-zA-Z]*(\w*)\.?(\w*)?.*)?', clean_line)
if detected_names:
if detected_names.group(1):
variables.append(detected_names.group(1).strip())
if detected_names.group(3):
variables.append(detected_names.group(3).strip())
if detected_names.group(6):
variables.append(detected_names.group(6).strip())
return variables
def get_return_vars(line):
variables = []
right_part = line.replace('return', '').strip()
detected_names = re.match('\(?\s*(\w+)[^.|(]*\.?\w*(\(.*\))?.*', right_part)
if detected_names:
obj_name = detected_names.group(1)
args_names = detected_names.group(2)
if args_names is not None:
arg_names = args_names.replace('(', '').replace(')', '').split(',')
for arg_name in arg_names:
if '"' in arg_name or "'" in arg_name:
continue
clean_name = re.split('\.|\[', arg_name)[0].strip()
if len(clean_name) > 0 and clean_name != 'null' and not clean_name.isdigit():
variables.append(clean_name)
clean_name = obj_name.strip()
if len(clean_name) > 0 and clean_name != 'null' and not clean_name.isdigit():
variables.append(clean_name)
return variables
def get_code_graph(method_name, class_name, project_path):
file_path = get_file_path(class_name, project_path)
if file_path is None:
print('sorry! this class can not be refactored %s'%class_name)
return None
# print(file_path)
source_lines = open(file_path, 'r').readlines()
method_found = False
graph = nx.DiGraph()
dependent_blocks = []
has_start_func = False
private_vulnerability = False
final_vulnerability = False
for line in source_lines:
method_def = re.findall('(public|private|protected) .*%s\(.*\).*'%method_name, line)
if len(method_def) > 0:
if not 'private' in line:
private_vulnerability = True
method_found = True
scope_stack = []
data_def = {}
code_lines = []
line_counter = 0
scope_stack.append(line_counter)
if '{' in line:
has_start_func = True
vul_obj = {'final': final_vulnerability, 'private': private_vulnerability}
graph.add_node(line_counter, content=line.strip(), vulnerability=vul_obj)
code_lines.append(line)
input_part = re.search(method_name+'\((.*($\n)?.*)\)', line)
if input_part:
input_part = input_part.group(1).replace('\n', '')
args = input_part.split(',')
for arg in args:
var_name = arg.split(' ')[-1].strip()
# print(var_name)
data_def[var_name] = line_counter
continue
if method_found and not has_start_func:
if '{' in line:
has_start_func = True
continue
if method_found and has_start_func:
if len(scope_stack) == 0:
data = [(u, v) for (u, v, d) in graph.edges(data=True) if d["weight"] == 2]
control = [(u, v) for (u, v, d) in graph.edges(data=True) if d["weight"] == 1]
labels = nx.get_edge_attributes(graph, "weight")
options = {
'node_color': 'green',
'node_size': 500,
'width': 1,
'arrowstyle': '-|>',
'arrowsize': 10,
'with_labels': True,
'edge_color':labels
}
pos = nx.circular_layout(graph)
nx.draw_networkx_nodes(graph, pos, node_size=700)
nx.draw_networkx_edges(graph, pos, edgelist=control, width=3)
nx.draw_networkx_edges(
graph, pos, edgelist=data, width=3, alpha=0.5, edge_color="b", style="dashed"
)
nx.draw_networkx_labels(graph, pos, font_size=20, font_family="sans-serif")
plt.savefig("graph.png")
return graph
if len(line.strip().replace('\n', '')) > 0:
var_list = []
line_counter += 1
code_lines.append(line)
vul_obj = {'final': final_vulnerability, 'private': private_vulnerability}
graph.add_node(line_counter, content=line.strip(), vulnerability=vul_obj)
for parent in scope_stack:
graph.add_weighted_edges_from([(line_counter, parent, control_dep_weight)])
statement = get_statement(line)
if statement == 'if':
#determine variables
var_list = get_if_vars(line)
dependent_blocks.append(('if', line_counter))
if statement == 'else if':
var_list = get_if_vars(line.replace('else ', ''))
if_line_number = dependent_blocks[-1][1]
if dependent_blocks[-1][0] != 'if':
if_line_number = dependent_blocks[-2][1]
graph.add_weighted_edges_from([(line_counter, if_line_number, control_dep_weight)])
if statement == 'else':
#connet to corresponding if condition with control dep edge
if_line_number = dependent_blocks[-1][1]
if dependent_blocks[-1][0] != 'if':
if_line_number = dependent_blocks[-2][1]
dependent_blocks = dependent_blocks[:-2] + [dependent_blocks[-1]]
else:
dependent_blocks = dependent_blocks[:-1]
graph.add_weighted_edges_from([(line_counter, if_line_number, control_dep_weight)])
if statement == 'for':
var_list = get_for_vars(line)
#detemine iterator var and condition var
if statement == 'while':
var_list = get_while_vars(line)
#detemine iterator var and condition var
if statement == 'try':
dependent_blocks.append(('try', line_counter))
#hold for connecting to catch
if statement == 'catch':
try_line_number = dependent_blocks[-1][1]
if dependent_blocks[-1][0] != 'try':
try_line_number = dependent_blocks[-2][1]
graph.add_weighted_edges_from([(line_counter, try_line_number, control_dep_weight)])
if statement == 'assignment':
defined_var, var_list = get_assignment_vars(line)
data_def[defined_var] = line_counter
if is_final_var(line, defined_var):
final_vulnerability = True
#determine vars
if statement == 'return':
var_list = get_return_vars(line)
pass
for var in var_list:
def_line = data_def.get(var)
if def_line is not None:
graph.add_weighted_edges_from([(line_counter, def_line, data_dep_weight)])
if '{' in line and re.search(r"(?<=').*?{.*?(?=')", line) is None and re.search(r'(?<=").*?{.*?(?=")', line) is None:
if len(line.strip()) > 1:
scope_stack.append(line_counter)
else:
scope_stack.append(line_counter - 1)
if '}' in line and re.search(r"(?<=').*?}.*?(?=')", line) is None and re.search(r'(?<=").*?}.*?(?=")', line) is None:
scope_stack.pop()
def get_method_name(lines, graph):
input_vars = []
for index in lines:
line = graph.nodes[index]['content']
statement = get_statement(line)
if statement == 'return':
return_vars = get_return_vars(line)
if len(return_vars) > 0:
return 'get_%s'%return_vars[0]
if statement == 'if':
#determine variables
var_list = get_if_vars(line)
if len(var_list) > 0:
input_vars.append(var_list[0])
if statement == 'for':
var_list = get_for_vars(line)
if len(var_list) > 0:
input_vars.append(var_list[0])
if statement == 'while':
var_list = get_while_vars(line)
if len(var_list) > 0:
input_vars.append(var_list[0])
if statement == 'assignment':
defined_var, var_list = get_assignment_vars(line)
if len(var_list) > 0:
input_vars.append(var_list[0])
if len(input_vars) > 0:
return 'handle_%s'%input_vars[0]