Added a naive import cleaning module that looks for imported class name in code and if not found removes
the import declaration (It's not perfect in removing everything like Eclipse but it cannot harm as it may
only miss a redundant import but can't remove a useful one).
Also, changed java model to support multiple test files for a single java class, test files should be:
c_name.data (first test file)
c_name.1.data (second test file)
c_name.2.data (third test file)
and so on.
diff --git a/java_gen/import_cleaner.py b/java_gen/import_cleaner.py
new file mode 100755
index 0000000..83897d4
--- /dev/null
+++ b/java_gen/import_cleaner.py
@@ -0,0 +1,77 @@
+#!/usr/bin/python
+
+import sys
+import re
+
+class ImportLine:
+ def __init__(self, line):
+ self.line = line
+ class_name = None
+ if line[len(line) - 1] == '*':
+ class_name = '*'
+ else:
+ i = 7
+ while i < len(line) - 1:
+ if re.match('\.[A-Z][\..]*$', line[i - 1 : len(line) - 1]):
+ class_name = line[i : len(line) - 1]
+ break
+ i = i + 1
+ if class_name is None:
+ class_name = line[line.rfind('.') + 1 : len(line) - 1]
+ self.class_name = class_name
+
+
+class ImportCleaner:
+ def __init__(self, path):
+ f = open(path)
+ self.imp_lines = []
+ self.code_lines = []
+ self.imports_first_line = -1
+ i = 0
+ for line in f:
+ if len(line) > 6 and re.match('^[ \t]*import ', line):
+ self.imp_lines.append(ImportLine(line.rstrip()))
+ if self.imports_first_line == -1:
+ self.imports_first_line = i
+ else:
+ self.code_lines.append(line.rstrip())
+ i = i + 1
+ f.close()
+
+ def find_used_imports(self):
+ self.used_imports = []
+ for line in self.code_lines:
+ temp = []
+ for imp in self.imp_lines:
+ if imp.class_name == '*' or line.find(imp.class_name) > -1:
+ temp.append(imp)
+ for x in temp:
+ self.imp_lines.remove(x)
+ self.used_imports.append(x)
+
+ def rewrite_file(self, path):
+ f = open(path, 'w')
+ imports_written = False
+ for i in range(len(self.code_lines)):
+ if not imports_written and self.imports_first_line == i:
+ # Put all imports
+ for imp in self.used_imports:
+ f.write(imp.line + '\n')
+ imports_written = True
+ # Put next code line
+ f.write(self.code_lines[i] + '\n')
+ f.close()
+
+def main(argv):
+ if len(argv) != 2:
+ print 'Usage: ImportCleaner <java file>'
+ return
+
+ filename = argv[1]
+ print 'Cleaning imports from file %s' % (filename)
+ cleaner = ImportCleaner(filename)
+ cleaner.find_used_imports()
+ cleaner.rewrite_file(filename)
+
+if __name__ == '__main__':
+ main(sys.argv)