OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Checks Java files for illegal imports.""" | 5 """Checks Java files for illegal imports.""" |
6 | 6 |
7 import codecs | 7 import codecs |
8 import os | 8 import os |
9 import re | 9 import re |
10 | 10 |
| 11 from rules import Rule |
| 12 |
11 | 13 |
12 class JavaChecker(object): | 14 class JavaChecker(object): |
13 """Import checker for Java files. | 15 """Import checker for Java files. |
14 | 16 |
15 The CheckFile method uses real filesystem paths, but Java imports work in | 17 The CheckFile method uses real filesystem paths, but Java imports work in |
16 terms of package names. To deal with this, we have an extra "prescan" pass | 18 terms of package names. To deal with this, we have an extra "prescan" pass |
17 that reads all the .java files and builds a mapping of class name -> filepath. | 19 that reads all the .java files and builds a mapping of class name -> filepath. |
18 In CheckFile, we convert each import statement into a real filepath, and check | 20 In CheckFile, we convert each import statement into a real filepath, and check |
19 that against the rules in the DEPS files. | 21 that against the rules in the DEPS files. |
20 | 22 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 for clazz in re.findall('^import\s+(?:static\s+)?([\w\.]+)\s*;', line): | 89 for clazz in re.findall('^import\s+(?:static\s+)?([\w\.]+)\s*;', line): |
88 if clazz not in self._classmap: | 90 if clazz not in self._classmap: |
89 # Importing a class from outside the Chromium tree. That's fine -- | 91 # Importing a class from outside the Chromium tree. That's fine -- |
90 # it's probably a Java or Android system class. | 92 # it's probably a Java or Android system class. |
91 continue | 93 continue |
92 include_path = os.path.relpath( | 94 include_path = os.path.relpath( |
93 self._classmap[clazz], self._base_directory) | 95 self._classmap[clazz], self._base_directory) |
94 # Convert Windows paths to Unix style, as used in DEPS files. | 96 # Convert Windows paths to Unix style, as used in DEPS files. |
95 include_path = include_path.replace(os.path.sep, '/') | 97 include_path = include_path.replace(os.path.sep, '/') |
96 (allowed, why_failed) = rules.DirAllowed(include_path) | 98 (allowed, why_failed) = rules.DirAllowed(include_path) |
97 if not allowed: | 99 if allowed == Rule.DISALLOW: |
98 if self._verbose: | 100 if self._verbose: |
99 result += '\nFor %s' % rules | 101 result += '\nFor %s' % rules |
100 result += 'Illegal include: "%s"\n Because of %s\n' % ( | 102 result += 'Illegal include: "%s"\n Because of %s\n' % ( |
101 include_path, why_failed) | 103 include_path, why_failed) |
102 if '{' in line: | 104 if '{' in line: |
103 # This is code, so we're finished reading imports for this file. | 105 # This is code, so we're finished reading imports for this file. |
104 break | 106 break |
105 | 107 |
106 return result | 108 return result |
OLD | NEW |