-
Notifications
You must be signed in to change notification settings - Fork 0
/
RedundantImportDetector.py
executable file
·54 lines (44 loc) · 1.91 KB
/
RedundantImportDetector.py
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
"""RedundantImportDetector.py
Discover redundant java imports using brute force.
Requires Python 2.3"""
import os, sys, re
from glob import glob
reportFile = file("RedundantImports.txt", 'w')
startDir = 'D:\\aaa-TIJ4\\code'
# Regular expression to find the block of import statements:
findImports = re.compile("\n(?:import .*?\n)+")
baseDir = os.path.abspath(".")
print "basDir:", baseDir
def main():
for javaFile in glob("*.java") + glob("**/*.java"):
print javaFile
checkImports(os.path.join(baseDir, javaFile))
def checkImports(javaFile):
java = file(javaFile).read()
imports = findImports.search(java)
if imports:
imports = [f for f in imports.group(0).split('\n') if f != '']
fileParts = findImports.split(java)
assert len(fileParts) == 2
for mutated in mutateImports(imports):
file(javaFile, 'w').write(fileParts[0] + mutated + fileParts[1])
print "changing to", os.path.dirname(javaFile)
os.chdir(os.path.dirname(javaFile))
if os.system("javac " + os.path.basename(javaFile)) == 0:
print >>reportFile, javaFile + "\n" + mutated
redundantRemoved = "\n".join(
[m for m in mutated.split("\n")
if not m.startswith("//")])
print >>reportFile, redundantRemoved
file(javaFile, 'w').write(fileParts[0] +
redundantRemoved + fileParts[1])
return # No further attempts
file(javaFile, 'w').write(java) # Restore original file
def mutateImports(imports):
'''Generates different versions of imports, each with a
different line commented out'''
for i in range(len(imports)):
mutated = imports[:]
mutated[i] = '//' + mutated[i]
yield "\n".join([''] + mutated + [''])
if __name__ == "__main__": main()