|
| 1 | +# |
| 2 | +# Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. |
| 3 | +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | +# |
| 5 | +# This code is free software; you can redistribute it and/or modify it |
| 6 | +# under the terms of the GNU General Public License version 2 only, as |
| 7 | +# published by the Free Software Foundation. |
| 8 | +# |
| 9 | +# This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | +# version 2 for more details (a copy is included in the LICENSE file that |
| 13 | +# accompanied this code). |
| 14 | +# |
| 15 | +# You should have received a copy of the GNU General Public License version |
| 16 | +# 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | +# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | +# |
| 19 | +# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | +# or visit www.oracle.com if you need additional information or have any |
| 21 | +# questions. |
| 22 | +# |
| 23 | + |
| 24 | +"""Provides import ordering capabilities for .java files.""" |
| 25 | + |
| 26 | +from glob import iglob |
| 27 | + |
| 28 | +# If a given line is an import, it will end with this suffix. |
| 29 | +# Used to strip this suffix for faster string comparison. |
| 30 | +SUFFIX = ";\n" |
| 31 | + |
| 32 | +STATIC_PREFIX = "import static " |
| 33 | +REGULAR_PREFIX = "import " |
| 34 | + |
| 35 | +def verify_order(path, prefix_order): |
| 36 | + """ |
| 37 | + Verifies import order of java files under the given path. |
| 38 | +
|
| 39 | + Iterates over all '.java' files in the given path, recursively over its subfolders. |
| 40 | + It then checks that imports in these files are ordered. |
| 41 | +
|
| 42 | + Here are the rules: |
| 43 | + 1. All imports that starts with a suffix that appears in this list must |
| 44 | + be found before any other import with a suffix that appears later in |
| 45 | + this list. |
| 46 | + 2. All imports with a given suffix must be in lexicographic order within |
| 47 | + all other imports with the same prefix. |
| 48 | + 3. Static imports must appear before regular imports. |
| 49 | +
|
| 50 | + :param path: Where to look for the java files. |
| 51 | + :param prefix_order: An ordered list of expected suffixes. |
| 52 | + :return: The list of files violating the specified order. |
| 53 | + """ |
| 54 | + |
| 55 | + # Validate the prefixes |
| 56 | + err = validate_format(prefix_order) |
| 57 | + if err: |
| 58 | + # Failure is represented with a non-empty list |
| 59 | + return [err] |
| 60 | + |
| 61 | + # Start building definitive list of import prefixes |
| 62 | + static_prefixes = [] |
| 63 | + regular_prefixes = [] |
| 64 | + |
| 65 | + for prefix in prefix_order: |
| 66 | + if prefix: |
| 67 | + # If prefix is "abc", add "import static abc" |
| 68 | + static_prefixes.append(STATIC_PREFIX + prefix + '.') |
| 69 | + # If prefix is "abc", add "import abc" |
| 70 | + regular_prefixes.append(REGULAR_PREFIX + prefix + '.') |
| 71 | + else: |
| 72 | + # Empty prefix means everything will match. |
| 73 | + # Empty prefix is added manually below. |
| 74 | + break |
| 75 | + |
| 76 | + # Ensure we have the empty prefix |
| 77 | + # Add "import static " |
| 78 | + static_prefixes.append(STATIC_PREFIX) |
| 79 | + # Add "import " |
| 80 | + regular_prefixes.append(REGULAR_PREFIX) |
| 81 | + |
| 82 | + # Ensures static imports are before regular imports. |
| 83 | + prefix_format = static_prefixes + regular_prefixes |
| 84 | + |
| 85 | + invalid_files = [] |
| 86 | + |
| 87 | + def is_sorted(li): |
| 88 | + if len(li) <= 1: |
| 89 | + return True |
| 90 | + return all(li[i] <= li[i + 1] for i in range(len(li) - 1)) |
| 91 | + |
| 92 | + def check_file(to_check, prefix_format): |
| 93 | + imports, prefix_ordered = get_imports(to_check, prefix_format) |
| 94 | + |
| 95 | + if not prefix_ordered: |
| 96 | + return False |
| 97 | + |
| 98 | + for import_list in imports: |
| 99 | + if not is_sorted(import_list): |
| 100 | + return False |
| 101 | + |
| 102 | + return True |
| 103 | + |
| 104 | + for file in iglob(path + '/**/*.java', recursive=True): |
| 105 | + if not check_file(file, prefix_format): |
| 106 | + invalid_files.append(file) |
| 107 | + |
| 108 | + return invalid_files |
| 109 | + |
| 110 | +def validate_format(prefix_order): |
| 111 | + """ |
| 112 | + Validates a given ordered list of prefix for import order verification. |
| 113 | +
|
| 114 | + Returns the reason for failure of validation if any, or an empty string |
| 115 | + if the prefixes are well-formed. |
| 116 | + """ |
| 117 | + for prefix in prefix_order: |
| 118 | + if prefix.endswith('.'): |
| 119 | + return "Invalid format for the ordered prefixes: \n'" + prefix + "' must not end with a '.'" |
| 120 | + return "" |
| 121 | + |
| 122 | +def get_imports(file, prefix_format): |
| 123 | + """ |
| 124 | + Obtains list of imports list, each corresponding to each specified prefix. |
| 125 | + Also returns whether the found prefixes were ordered. |
| 126 | +
|
| 127 | + In case the prefixes where not ordered, the last element of the returned list will contain |
| 128 | + every import after the violating line |
| 129 | + """ |
| 130 | + def add_import(li, value, prefix, suf): |
| 131 | + to_add = value[len(prefix):] |
| 132 | + if to_add.endswith(suf): |
| 133 | + to_add = to_add[:len(to_add) - len(suf)] |
| 134 | + li.append(to_add) |
| 135 | + |
| 136 | + def enter_fail_state(imports, prefix_format, cur_prefix_imports): |
| 137 | + if cur_prefix_imports: |
| 138 | + imports.append(cur_prefix_imports) |
| 139 | + return False, len(prefix_format), "" |
| 140 | + |
| 141 | + with open(file) as f: |
| 142 | + imports = [] |
| 143 | + prefix_ordered = True |
| 144 | + |
| 145 | + cur_prefix_idx = 0 |
| 146 | + cur_prefix = prefix_format[cur_prefix_idx] |
| 147 | + |
| 148 | + cur_prefix_imports = [] |
| 149 | + |
| 150 | + for line in f.readlines(): |
| 151 | + ignore = not line.startswith("import") |
| 152 | + if ignore: |
| 153 | + # start of class declaration, we can stop looking for imports. |
| 154 | + end = 'class ' in line or 'interface ' in line or 'enum ' in line or 'record ' in line |
| 155 | + if end: |
| 156 | + break |
| 157 | + continue |
| 158 | + |
| 159 | + if line.startswith(cur_prefix): |
| 160 | + # If we are still ensuring prefix ordering, ensure that this line does not belong |
| 161 | + # to a previous prefix. |
| 162 | + if prefix_ordered: |
| 163 | + for i in range(cur_prefix_idx): |
| 164 | + if line.startswith(prefix_format[i]): |
| 165 | + # A match for a previous prefix was found: enter fail state |
| 166 | + prefix_ordered, cur_prefix_idx, cur_prefix = enter_fail_state(imports, prefix_format, cur_prefix_imports) |
| 167 | + cur_prefix_imports = [] |
| 168 | + add_import(cur_prefix_imports, line, cur_prefix, SUFFIX) |
| 169 | + else: |
| 170 | + # cur_prefix not found, advance to next prefix if found, report failure if not. |
| 171 | + for i in range(cur_prefix_idx + 1, len(prefix_format)): |
| 172 | + if line.startswith(prefix_format[i]): |
| 173 | + # Report imports for current prefix, |
| 174 | + if cur_prefix_imports: |
| 175 | + imports.append(cur_prefix_imports) |
| 176 | + # Set state to next prefix. |
| 177 | + cur_prefix = prefix_format[i] |
| 178 | + cur_prefix_idx = i |
| 179 | + cur_prefix_imports = [] |
| 180 | + add_import(cur_prefix_imports, line, cur_prefix, SUFFIX) |
| 181 | + break |
| 182 | + else: |
| 183 | + # On failure, dump remaining lines into the last cur_prefix_imports. |
| 184 | + prefix_ordered, cur_prefix_idx, cur_prefix = enter_fail_state(imports, prefix_format, cur_prefix_imports) |
| 185 | + cur_prefix_imports = [] |
| 186 | + add_import(cur_prefix_imports, line, cur_prefix, SUFFIX) |
| 187 | + |
| 188 | + if cur_prefix_imports: |
| 189 | + imports.append(cur_prefix_imports) |
| 190 | + |
| 191 | + return imports, prefix_ordered |
0 commit comments