| # Copyright 2013, Big Switch Networks, Inc. |
| # |
| # LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with |
| # the following special exception: |
| # |
| # LOXI Exception |
| # |
| # As a special exception to the terms of the EPL, you may distribute libraries |
| # generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided |
| # that copyright and licensing notices generated by LoxiGen are not altered or removed |
| # from the LoxiGen Libraries and the notice provided below is (i) included in |
| # the LoxiGen Libraries, if distributed in source code form and (ii) included in any |
| # documentation for the LoxiGen Libraries, if distributed in binary form. |
| # |
| # Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler." |
| # |
| # You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain |
| # a copy of the EPL at: |
| # |
| # http://www.eclipse.org/legal/epl-v10.html |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| # EPL for the specific language governing permissions and limitations |
| # under the EPL. |
| |
| # A utility for naively cleaning of redundant 'import' statements in java source file. |
| # This utility searches the name of the imported class in the code and if not found, |
| # removes the corresponding import statement. |
| # This utility assumes class/package naming that follows java naming conventions. |
| |
| 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) |