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 import results |
11 from rules import Rule | 12 from rules import Rule |
12 | 13 |
13 | 14 |
14 class JavaChecker(object): | 15 class JavaChecker(object): |
15 """Import checker for Java files. | 16 """Import checker for Java files. |
16 | 17 |
17 The CheckFile method uses real filesystem paths, but Java imports work in | 18 The CheckFile method uses real filesystem paths, but Java imports work in |
18 terms of package names. To deal with this, we have an extra "prescan" pass | 19 terms of package names. To deal with this, we have an extra "prescan" pass |
19 that reads all the .java files and builds a mapping of class name -> filepath. | 20 that reads all the .java files and builds a mapping of class name -> filepath. |
20 In CheckFile, we convert each import statement into a real filepath, and check | 21 In CheckFile, we convert each import statement into a real filepath, and check |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 print | 77 print |
77 else: | 78 else: |
78 self._classmap[full_class_name] = filepath | 79 self._classmap[full_class_name] = filepath |
79 return | 80 return |
80 print 'WARNING: no package definition found in %s' % filepath | 81 print 'WARNING: no package definition found in %s' % filepath |
81 | 82 |
82 def CheckFile(self, rules, filepath): | 83 def CheckFile(self, rules, filepath): |
83 if self._verbose: | 84 if self._verbose: |
84 print 'Checking: ' + filepath | 85 print 'Checking: ' + filepath |
85 | 86 |
86 result = '' | 87 dependee_status = results.DependeeStatus(filepath) |
87 with codecs.open(filepath, encoding='utf-8') as f: | 88 with codecs.open(filepath, encoding='utf-8') as f: |
88 for line in f: | 89 for line in f: |
89 for clazz in re.findall('^import\s+(?:static\s+)?([\w\.]+)\s*;', line): | 90 for clazz in re.findall('^import\s+(?:static\s+)?([\w\.]+)\s*;', line): |
90 if clazz not in self._classmap: | 91 if clazz not in self._classmap: |
91 # Importing a class from outside the Chromium tree. That's fine -- | 92 # Importing a class from outside the Chromium tree. That's fine -- |
92 # it's probably a Java or Android system class. | 93 # it's probably a Java or Android system class. |
93 continue | 94 continue |
94 include_path = os.path.relpath( | 95 include_path = os.path.relpath( |
95 self._classmap[clazz], self._base_directory) | 96 self._classmap[clazz], self._base_directory) |
96 # Convert Windows paths to Unix style, as used in DEPS files. | 97 # Convert Windows paths to Unix style, as used in DEPS files. |
97 include_path = include_path.replace(os.path.sep, '/') | 98 include_path = include_path.replace(os.path.sep, '/') |
98 (allowed, why_failed) = rules.DirAllowed(include_path) | 99 rule = rules.RuleApplyingTo(include_path) |
99 if allowed == Rule.DISALLOW: | 100 if rule.allow == Rule.DISALLOW: |
100 if self._verbose: | 101 dependee_status.AddViolation( |
101 result += '\nFor %s' % rules | 102 results.DependencyViolation(include_path, rule, rules)) |
102 result += 'Illegal include: "%s"\n Because of %s\n' % ( | |
103 include_path, why_failed) | |
104 if '{' in line: | 103 if '{' in line: |
105 # This is code, so we're finished reading imports for this file. | 104 # This is code, so we're finished reading imports for this file. |
106 break | 105 break |
107 | 106 |
108 return result | 107 return dependee_status |
OLD | NEW |