blob: d84d76b538f001523377a459a4d80065c52bfebc [file] [log] [blame]
Yotam Harchol0b9befe2013-08-22 11:00:13 -07001# Copyright 2013, Big Switch Networks, Inc.
2#
3# LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with
4# the following special exception:
5#
6# LOXI Exception
7#
8# As a special exception to the terms of the EPL, you may distribute libraries
9# generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided
10# that copyright and licensing notices generated by LoxiGen are not altered or removed
11# from the LoxiGen Libraries and the notice provided below is (i) included in
12# the LoxiGen Libraries, if distributed in source code form and (ii) included in any
13# documentation for the LoxiGen Libraries, if distributed in binary form.
14#
15# Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler."
16#
17# You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain
18# a copy of the EPL at:
19#
20# http://www.eclipse.org/legal/epl-v10.html
21#
22# Unless required by applicable law or agreed to in writing, software
23# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
25# EPL for the specific language governing permissions and limitations
26# under the EPL.
27
28# A utility for naively cleaning of redundant 'import' statements in java source file.
29# This utility searches the name of the imported class in the code and if not found,
30# removes the corresponding import statement.
31# This utility assumes class/package naming that follows java naming conventions.
Yotam Harchol466b3212013-08-15 12:14:46 -070032
33import sys
34import re
35
36class ImportLine:
37 def __init__(self, line):
38 self.line = line
39 class_name = None
40 if line[len(line) - 1] == '*':
41 class_name = '*'
42 else:
43 i = 7
44 while i < len(line) - 1:
45 if re.match('\.[A-Z][\..]*$', line[i - 1 : len(line) - 1]):
46 class_name = line[i : len(line) - 1]
47 break
48 i = i + 1
49 if class_name is None:
50 class_name = line[line.rfind('.') + 1 : len(line) - 1]
51 self.class_name = class_name
52
53
54class ImportCleaner:
55 def __init__(self, path):
56 f = open(path)
57 self.imp_lines = []
58 self.code_lines = []
59 self.imports_first_line = -1
60 i = 0
61 for line in f:
62 if len(line) > 6 and re.match('^[ \t]*import ', line):
63 self.imp_lines.append(ImportLine(line.rstrip()))
64 if self.imports_first_line == -1:
65 self.imports_first_line = i
66 else:
67 self.code_lines.append(line.rstrip())
68 i = i + 1
69 f.close()
70
71 def find_used_imports(self):
72 self.used_imports = []
73 for line in self.code_lines:
74 temp = []
75 for imp in self.imp_lines:
76 if imp.class_name == '*' or line.find(imp.class_name) > -1:
77 temp.append(imp)
78 for x in temp:
79 self.imp_lines.remove(x)
80 self.used_imports.append(x)
81
82 def rewrite_file(self, path):
83 f = open(path, 'w')
84 imports_written = False
85 for i in range(len(self.code_lines)):
86 if not imports_written and self.imports_first_line == i:
87 # Put all imports
88 for imp in self.used_imports:
89 f.write(imp.line + '\n')
90 imports_written = True
91 # Put next code line
92 f.write(self.code_lines[i] + '\n')
93 f.close()
94
95def main(argv):
96 if len(argv) != 2:
97 print 'Usage: ImportCleaner <java file>'
98 return
99
100 filename = argv[1]
101 print 'Cleaning imports from file %s' % (filename)
102 cleaner = ImportCleaner(filename)
103 cleaner.find_used_imports()
104 cleaner.rewrite_file(filename)
105
106if __name__ == '__main__':
107 main(sys.argv)