Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # Copyright (C) 2014 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the 'License'); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an 'AS IS' BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | """ |
| 18 | Enforces common Android public API design patterns. It ignores lint messages from |
| 19 | a previous API level, if provided. |
| 20 | |
| 21 | Usage: apilint.py current.txt |
| 22 | Usage: apilint.py current.txt previous.txt |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 23 | |
| 24 | You can also splice in blame details like this: |
| 25 | $ git blame api/current.txt -t -e > /tmp/currentblame.txt |
| 26 | $ apilint.py /tmp/currentblame.txt previous.txt --no-color |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 27 | """ |
| 28 | |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 29 | import re, sys, collections, traceback |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 30 | |
| 31 | |
| 32 | BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8) |
| 33 | |
| 34 | def format(fg=None, bg=None, bright=False, bold=False, dim=False, reset=False): |
| 35 | # manually derived from http://en.wikipedia.org/wiki/ANSI_escape_code#Codes |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 36 | if "--no-color" in sys.argv: return "" |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 37 | codes = [] |
| 38 | if reset: codes.append("0") |
| 39 | else: |
| 40 | if not fg is None: codes.append("3%d" % (fg)) |
| 41 | if not bg is None: |
| 42 | if not bright: codes.append("4%d" % (bg)) |
| 43 | else: codes.append("10%d" % (bg)) |
| 44 | if bold: codes.append("1") |
| 45 | elif dim: codes.append("2") |
| 46 | else: codes.append("22") |
| 47 | return "\033[%sm" % (";".join(codes)) |
| 48 | |
| 49 | |
| 50 | class Field(): |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 51 | def __init__(self, clazz, line, raw, blame): |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 52 | self.clazz = clazz |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 53 | self.line = line |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 54 | self.raw = raw.strip(" {;") |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 55 | self.blame = blame |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 56 | |
| 57 | raw = raw.split() |
| 58 | self.split = list(raw) |
| 59 | |
| 60 | for r in ["field", "volatile", "transient", "public", "protected", "static", "final", "deprecated"]: |
| 61 | while r in raw: raw.remove(r) |
| 62 | |
| 63 | self.typ = raw[0] |
| 64 | self.name = raw[1].strip(";") |
| 65 | if len(raw) >= 4 and raw[2] == "=": |
| 66 | self.value = raw[3].strip(';"') |
| 67 | else: |
| 68 | self.value = None |
| 69 | |
Jeff Sharkey | 037458a | 2014-09-04 15:46:20 -0700 | [diff] [blame] | 70 | self.ident = self.raw.replace(" deprecated ", " ") |
| 71 | |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 72 | def __repr__(self): |
| 73 | return self.raw |
| 74 | |
| 75 | |
| 76 | class Method(): |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 77 | def __init__(self, clazz, line, raw, blame): |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 78 | self.clazz = clazz |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 79 | self.line = line |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 80 | self.raw = raw.strip(" {;") |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 81 | self.blame = blame |
| 82 | |
| 83 | # drop generics for now |
| 84 | raw = re.sub("<.+?>", "", raw) |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 85 | |
| 86 | raw = re.split("[\s(),;]+", raw) |
| 87 | for r in ["", ";"]: |
| 88 | while r in raw: raw.remove(r) |
| 89 | self.split = list(raw) |
| 90 | |
| 91 | for r in ["method", "public", "protected", "static", "final", "deprecated", "abstract"]: |
| 92 | while r in raw: raw.remove(r) |
| 93 | |
| 94 | self.typ = raw[0] |
| 95 | self.name = raw[1] |
| 96 | self.args = [] |
| 97 | for r in raw[2:]: |
| 98 | if r == "throws": break |
| 99 | self.args.append(r) |
| 100 | |
Jeff Sharkey | 037458a | 2014-09-04 15:46:20 -0700 | [diff] [blame] | 101 | # identity for compat purposes |
| 102 | ident = self.raw |
| 103 | ident = ident.replace(" deprecated ", " ") |
| 104 | ident = ident.replace(" synchronized ", " ") |
| 105 | ident = re.sub("<.+?>", "", ident) |
| 106 | if " throws " in ident: |
| 107 | ident = ident[:ident.index(" throws ")] |
| 108 | self.ident = ident |
| 109 | |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 110 | def __repr__(self): |
| 111 | return self.raw |
| 112 | |
| 113 | |
| 114 | class Class(): |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 115 | def __init__(self, pkg, line, raw, blame): |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 116 | self.pkg = pkg |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 117 | self.line = line |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 118 | self.raw = raw.strip(" {;") |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 119 | self.blame = blame |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 120 | self.ctors = [] |
| 121 | self.fields = [] |
| 122 | self.methods = [] |
| 123 | |
| 124 | raw = raw.split() |
| 125 | self.split = list(raw) |
| 126 | if "class" in raw: |
| 127 | self.fullname = raw[raw.index("class")+1] |
| 128 | elif "interface" in raw: |
| 129 | self.fullname = raw[raw.index("interface")+1] |
Jeff Sharkey | 037458a | 2014-09-04 15:46:20 -0700 | [diff] [blame] | 130 | else: |
| 131 | raise ValueError("Funky class type %s" % (self.raw)) |
| 132 | |
| 133 | if "extends" in raw: |
| 134 | self.extends = raw[raw.index("extends")+1] |
Jeff Sharkey | 047d7f0 | 2015-02-25 11:27:55 -0800 | [diff] [blame^] | 135 | self.extends_path = self.extends.split(".") |
Jeff Sharkey | 037458a | 2014-09-04 15:46:20 -0700 | [diff] [blame] | 136 | else: |
| 137 | self.extends = None |
Jeff Sharkey | 047d7f0 | 2015-02-25 11:27:55 -0800 | [diff] [blame^] | 138 | self.extends_path = [] |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 139 | |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 140 | self.fullname = self.pkg.name + "." + self.fullname |
Jeff Sharkey | 047d7f0 | 2015-02-25 11:27:55 -0800 | [diff] [blame^] | 141 | self.fullname_path = self.fullname.split(".") |
| 142 | |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 143 | self.name = self.fullname[self.fullname.rindex(".")+1:] |
| 144 | |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 145 | def __repr__(self): |
| 146 | return self.raw |
| 147 | |
| 148 | |
| 149 | class Package(): |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 150 | def __init__(self, line, raw, blame): |
| 151 | self.line = line |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 152 | self.raw = raw.strip(" {;") |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 153 | self.blame = blame |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 154 | |
| 155 | raw = raw.split() |
| 156 | self.name = raw[raw.index("package")+1] |
Jeff Sharkey | 047d7f0 | 2015-02-25 11:27:55 -0800 | [diff] [blame^] | 157 | self.name_path = self.name.split(".") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 158 | |
| 159 | def __repr__(self): |
| 160 | return self.raw |
| 161 | |
| 162 | |
Jeff Sharkey | a18a2e3 | 2015-02-22 15:54:32 -0800 | [diff] [blame] | 163 | def _parse_stream(f, clazz_cb=None): |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 164 | line = 0 |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 165 | api = {} |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 166 | pkg = None |
| 167 | clazz = None |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 168 | blame = None |
| 169 | |
| 170 | re_blame = re.compile("^([a-z0-9]{7,}) \(<([^>]+)>.+?\) (.+?)$") |
Jeff Sharkey | a18a2e3 | 2015-02-22 15:54:32 -0800 | [diff] [blame] | 171 | for raw in f: |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 172 | line += 1 |
| 173 | raw = raw.rstrip() |
| 174 | match = re_blame.match(raw) |
| 175 | if match is not None: |
| 176 | blame = match.groups()[0:2] |
| 177 | raw = match.groups()[2] |
| 178 | else: |
| 179 | blame = None |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 180 | |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 181 | if raw.startswith("package"): |
| 182 | pkg = Package(line, raw, blame) |
| 183 | elif raw.startswith(" ") and raw.endswith("{"): |
Jeff Sharkey | a18a2e3 | 2015-02-22 15:54:32 -0800 | [diff] [blame] | 184 | # When provided with class callback, we treat as incremental |
| 185 | # parse and don't build up entire API |
| 186 | if clazz and clazz_cb: |
| 187 | clazz_cb(clazz) |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 188 | clazz = Class(pkg, line, raw, blame) |
Jeff Sharkey | a18a2e3 | 2015-02-22 15:54:32 -0800 | [diff] [blame] | 189 | if not clazz_cb: |
| 190 | api[clazz.fullname] = clazz |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 191 | elif raw.startswith(" ctor"): |
| 192 | clazz.ctors.append(Method(clazz, line, raw, blame)) |
| 193 | elif raw.startswith(" method"): |
| 194 | clazz.methods.append(Method(clazz, line, raw, blame)) |
| 195 | elif raw.startswith(" field"): |
| 196 | clazz.fields.append(Field(clazz, line, raw, blame)) |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 197 | |
Jeff Sharkey | a18a2e3 | 2015-02-22 15:54:32 -0800 | [diff] [blame] | 198 | # Handle last trailing class |
| 199 | if clazz and clazz_cb: |
| 200 | clazz_cb(clazz) |
| 201 | |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 202 | return api |
| 203 | |
| 204 | |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 205 | class Failure(): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 206 | def __init__(self, sig, clazz, detail, error, rule, msg): |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 207 | self.sig = sig |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 208 | self.error = error |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 209 | self.rule = rule |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 210 | self.msg = msg |
| 211 | |
| 212 | if error: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 213 | self.head = "Error %s" % (rule) if rule else "Error" |
| 214 | dump = "%s%s:%s %s" % (format(fg=RED, bg=BLACK, bold=True), self.head, format(reset=True), msg) |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 215 | else: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 216 | self.head = "Warning %s" % (rule) if rule else "Warning" |
| 217 | dump = "%s%s:%s %s" % (format(fg=YELLOW, bg=BLACK, bold=True), self.head, format(reset=True), msg) |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 218 | |
| 219 | self.line = clazz.line |
| 220 | blame = clazz.blame |
| 221 | if detail is not None: |
| 222 | dump += "\n in " + repr(detail) |
| 223 | self.line = detail.line |
| 224 | blame = detail.blame |
| 225 | dump += "\n in " + repr(clazz) |
| 226 | dump += "\n in " + repr(clazz.pkg) |
| 227 | dump += "\n at line " + repr(self.line) |
| 228 | if blame is not None: |
| 229 | dump += "\n last modified by %s in %s" % (blame[1], blame[0]) |
| 230 | |
| 231 | self.dump = dump |
| 232 | |
| 233 | def __repr__(self): |
| 234 | return self.dump |
| 235 | |
| 236 | |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 237 | failures = {} |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 238 | |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 239 | def _fail(clazz, detail, error, rule, msg): |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 240 | """Records an API failure to be processed later.""" |
| 241 | global failures |
| 242 | |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 243 | sig = "%s-%s-%s" % (clazz.fullname, repr(detail), msg) |
| 244 | sig = sig.replace(" deprecated ", " ") |
| 245 | |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 246 | failures[sig] = Failure(sig, clazz, detail, error, rule, msg) |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 247 | |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 248 | |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 249 | def warn(clazz, detail, rule, msg): |
| 250 | _fail(clazz, detail, False, rule, msg) |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 251 | |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 252 | def error(clazz, detail, rule, msg): |
| 253 | _fail(clazz, detail, True, rule, msg) |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 254 | |
| 255 | |
| 256 | def verify_constants(clazz): |
| 257 | """All static final constants must be FOO_NAME style.""" |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 258 | if re.match("android\.R\.[a-z]+", clazz.fullname): return |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 259 | |
| 260 | for f in clazz.fields: |
| 261 | if "static" in f.split and "final" in f.split: |
| 262 | if re.match("[A-Z0-9_]+", f.name) is None: |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 263 | error(clazz, f, "C2", "Constant field names must be FOO_NAME") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 264 | |
| 265 | |
| 266 | def verify_enums(clazz): |
| 267 | """Enums are bad, mmkay?""" |
| 268 | if "extends java.lang.Enum" in clazz.raw: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 269 | error(clazz, None, "F5", "Enums are not allowed") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 270 | |
| 271 | |
| 272 | def verify_class_names(clazz): |
| 273 | """Try catching malformed class names like myMtp or MTPUser.""" |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 274 | if clazz.fullname.startswith("android.opengl"): return |
| 275 | if clazz.fullname.startswith("android.renderscript"): return |
| 276 | if re.match("android\.R\.[a-z]+", clazz.fullname): return |
| 277 | |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 278 | if re.search("[A-Z]{2,}", clazz.name) is not None: |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 279 | warn(clazz, None, "S1", "Class names with acronyms should be Mtp not MTP") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 280 | if re.match("[^A-Z]", clazz.name): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 281 | error(clazz, None, "S1", "Class must start with uppercase char") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 282 | |
| 283 | |
| 284 | def verify_method_names(clazz): |
| 285 | """Try catching malformed method names, like Foo() or getMTU().""" |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 286 | if clazz.fullname.startswith("android.opengl"): return |
| 287 | if clazz.fullname.startswith("android.renderscript"): return |
| 288 | if clazz.fullname == "android.system.OsConstants": return |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 289 | |
| 290 | for m in clazz.methods: |
| 291 | if re.search("[A-Z]{2,}", m.name) is not None: |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 292 | warn(clazz, m, "S1", "Method names with acronyms should be getMtu() instead of getMTU()") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 293 | if re.match("[^a-z]", m.name): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 294 | error(clazz, m, "S1", "Method name must start with lowercase char") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 295 | |
| 296 | |
| 297 | def verify_callbacks(clazz): |
| 298 | """Verify Callback classes. |
| 299 | All callback classes must be abstract. |
| 300 | All methods must follow onFoo() naming style.""" |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 301 | if clazz.fullname == "android.speech.tts.SynthesisCallback": return |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 302 | |
| 303 | if clazz.name.endswith("Callbacks"): |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 304 | error(clazz, None, "L1", "Callback class names should be singular") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 305 | if clazz.name.endswith("Observer"): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 306 | warn(clazz, None, "L1", "Class should be named FooCallback") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 307 | |
| 308 | if clazz.name.endswith("Callback"): |
| 309 | if "interface" in clazz.split: |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 310 | error(clazz, None, "CL3", "Callbacks must be abstract class to enable extension in future API levels") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 311 | |
| 312 | for m in clazz.methods: |
| 313 | if not re.match("on[A-Z][a-z]*", m.name): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 314 | error(clazz, m, "L1", "Callback method names must be onFoo() style") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 315 | |
| 316 | |
| 317 | def verify_listeners(clazz): |
| 318 | """Verify Listener classes. |
| 319 | All Listener classes must be interface. |
| 320 | All methods must follow onFoo() naming style. |
| 321 | If only a single method, it must match class name: |
| 322 | interface OnFooListener { void onFoo() }""" |
| 323 | |
| 324 | if clazz.name.endswith("Listener"): |
| 325 | if " abstract class " in clazz.raw: |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 326 | error(clazz, None, "L1", "Listeners should be an interface, or otherwise renamed Callback") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 327 | |
| 328 | for m in clazz.methods: |
| 329 | if not re.match("on[A-Z][a-z]*", m.name): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 330 | error(clazz, m, "L1", "Listener method names must be onFoo() style") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 331 | |
| 332 | if len(clazz.methods) == 1 and clazz.name.startswith("On"): |
| 333 | m = clazz.methods[0] |
| 334 | if (m.name + "Listener").lower() != clazz.name.lower(): |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 335 | error(clazz, m, "L1", "Single listener method name must match class name") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 336 | |
| 337 | |
| 338 | def verify_actions(clazz): |
| 339 | """Verify intent actions. |
| 340 | All action names must be named ACTION_FOO. |
| 341 | All action values must be scoped by package and match name: |
| 342 | package android.foo { |
| 343 | String ACTION_BAR = "android.foo.action.BAR"; |
| 344 | }""" |
| 345 | for f in clazz.fields: |
| 346 | if f.value is None: continue |
| 347 | if f.name.startswith("EXTRA_"): continue |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 348 | if f.name == "SERVICE_INTERFACE" or f.name == "PROVIDER_INTERFACE": continue |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 349 | |
| 350 | if "static" in f.split and "final" in f.split and f.typ == "java.lang.String": |
| 351 | if "_ACTION" in f.name or "ACTION_" in f.name or ".action." in f.value.lower(): |
| 352 | if not f.name.startswith("ACTION_"): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 353 | error(clazz, f, "C3", "Intent action constant name must be ACTION_FOO") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 354 | else: |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 355 | if clazz.fullname == "android.content.Intent": |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 356 | prefix = "android.intent.action" |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 357 | elif clazz.fullname == "android.provider.Settings": |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 358 | prefix = "android.settings" |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 359 | elif clazz.fullname == "android.app.admin.DevicePolicyManager" or clazz.fullname == "android.app.admin.DeviceAdminReceiver": |
| 360 | prefix = "android.app.action" |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 361 | else: |
| 362 | prefix = clazz.pkg.name + ".action" |
| 363 | expected = prefix + "." + f.name[7:] |
| 364 | if f.value != expected: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 365 | error(clazz, f, "C4", "Inconsistent action value; expected %s" % (expected)) |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 366 | |
| 367 | |
| 368 | def verify_extras(clazz): |
| 369 | """Verify intent extras. |
| 370 | All extra names must be named EXTRA_FOO. |
| 371 | All extra values must be scoped by package and match name: |
| 372 | package android.foo { |
| 373 | String EXTRA_BAR = "android.foo.extra.BAR"; |
| 374 | }""" |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 375 | if clazz.fullname == "android.app.Notification": return |
| 376 | if clazz.fullname == "android.appwidget.AppWidgetManager": return |
| 377 | |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 378 | for f in clazz.fields: |
| 379 | if f.value is None: continue |
| 380 | if f.name.startswith("ACTION_"): continue |
| 381 | |
| 382 | if "static" in f.split and "final" in f.split and f.typ == "java.lang.String": |
| 383 | if "_EXTRA" in f.name or "EXTRA_" in f.name or ".extra" in f.value.lower(): |
| 384 | if not f.name.startswith("EXTRA_"): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 385 | error(clazz, f, "C3", "Intent extra must be EXTRA_FOO") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 386 | else: |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 387 | if clazz.pkg.name == "android.content" and clazz.name == "Intent": |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 388 | prefix = "android.intent.extra" |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 389 | elif clazz.pkg.name == "android.app.admin": |
| 390 | prefix = "android.app.extra" |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 391 | else: |
| 392 | prefix = clazz.pkg.name + ".extra" |
| 393 | expected = prefix + "." + f.name[6:] |
| 394 | if f.value != expected: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 395 | error(clazz, f, "C4", "Inconsistent extra value; expected %s" % (expected)) |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 396 | |
| 397 | |
| 398 | def verify_equals(clazz): |
| 399 | """Verify that equals() and hashCode() must be overridden together.""" |
| 400 | methods = [ m.name for m in clazz.methods ] |
| 401 | eq = "equals" in methods |
| 402 | hc = "hashCode" in methods |
| 403 | if eq != hc: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 404 | error(clazz, None, "M8", "Must override both equals and hashCode; missing one") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 405 | |
| 406 | |
| 407 | def verify_parcelable(clazz): |
| 408 | """Verify that Parcelable objects aren't hiding required bits.""" |
| 409 | if "implements android.os.Parcelable" in clazz.raw: |
| 410 | creator = [ i for i in clazz.fields if i.name == "CREATOR" ] |
| 411 | write = [ i for i in clazz.methods if i.name == "writeToParcel" ] |
| 412 | describe = [ i for i in clazz.methods if i.name == "describeContents" ] |
| 413 | |
| 414 | if len(creator) == 0 or len(write) == 0 or len(describe) == 0: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 415 | error(clazz, None, "FW3", "Parcelable requires CREATOR, writeToParcel, and describeContents; missing one") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 416 | |
| 417 | |
| 418 | def verify_protected(clazz): |
Jeff Sharkey | b46a969 | 2015-02-17 17:19:41 -0800 | [diff] [blame] | 419 | """Verify that no protected methods or fields are allowed.""" |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 420 | for m in clazz.methods: |
| 421 | if "protected" in m.split: |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 422 | error(clazz, m, "M7", "Protected methods not allowed; must be public") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 423 | for f in clazz.fields: |
| 424 | if "protected" in f.split: |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 425 | error(clazz, f, "M7", "Protected fields not allowed; must be public") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 426 | |
| 427 | |
| 428 | def verify_fields(clazz): |
| 429 | """Verify that all exposed fields are final. |
| 430 | Exposed fields must follow myName style. |
| 431 | Catch internal mFoo objects being exposed.""" |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 432 | |
| 433 | IGNORE_BARE_FIELDS = [ |
| 434 | "android.app.ActivityManager.RecentTaskInfo", |
| 435 | "android.app.Notification", |
| 436 | "android.content.pm.ActivityInfo", |
| 437 | "android.content.pm.ApplicationInfo", |
| 438 | "android.content.pm.FeatureGroupInfo", |
| 439 | "android.content.pm.InstrumentationInfo", |
| 440 | "android.content.pm.PackageInfo", |
| 441 | "android.content.pm.PackageItemInfo", |
| 442 | "android.os.Message", |
| 443 | "android.system.StructPollfd", |
| 444 | ] |
| 445 | |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 446 | for f in clazz.fields: |
| 447 | if not "final" in f.split: |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 448 | if clazz.fullname in IGNORE_BARE_FIELDS: |
| 449 | pass |
| 450 | elif clazz.fullname.endswith("LayoutParams"): |
| 451 | pass |
| 452 | elif clazz.fullname.startswith("android.util.Mutable"): |
| 453 | pass |
| 454 | else: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 455 | error(clazz, f, "F2", "Bare fields must be marked final, or add accessors if mutable") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 456 | |
| 457 | if not "static" in f.split: |
| 458 | if not re.match("[a-z]([a-zA-Z]+)?", f.name): |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 459 | error(clazz, f, "S1", "Non-static fields must be named using myField style") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 460 | |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 461 | if re.match("[ms][A-Z]", f.name): |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 462 | error(clazz, f, "F1", "Internal objects must not be exposed") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 463 | |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 464 | if re.match("[A-Z_]+", f.name): |
| 465 | if "static" not in f.split or "final" not in f.split: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 466 | error(clazz, f, "C2", "Constants must be marked static final") |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 467 | |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 468 | |
| 469 | def verify_register(clazz): |
| 470 | """Verify parity of registration methods. |
| 471 | Callback objects use register/unregister methods. |
| 472 | Listener objects use add/remove methods.""" |
| 473 | methods = [ m.name for m in clazz.methods ] |
| 474 | for m in clazz.methods: |
| 475 | if "Callback" in m.raw: |
| 476 | if m.name.startswith("register"): |
| 477 | other = "unregister" + m.name[8:] |
| 478 | if other not in methods: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 479 | error(clazz, m, "L2", "Missing unregister method") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 480 | if m.name.startswith("unregister"): |
| 481 | other = "register" + m.name[10:] |
| 482 | if other not in methods: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 483 | error(clazz, m, "L2", "Missing register method") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 484 | |
| 485 | if m.name.startswith("add") or m.name.startswith("remove"): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 486 | error(clazz, m, "L3", "Callback methods should be named register/unregister") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 487 | |
| 488 | if "Listener" in m.raw: |
| 489 | if m.name.startswith("add"): |
| 490 | other = "remove" + m.name[3:] |
| 491 | if other not in methods: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 492 | error(clazz, m, "L2", "Missing remove method") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 493 | if m.name.startswith("remove") and not m.name.startswith("removeAll"): |
| 494 | other = "add" + m.name[6:] |
| 495 | if other not in methods: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 496 | error(clazz, m, "L2", "Missing add method") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 497 | |
| 498 | if m.name.startswith("register") or m.name.startswith("unregister"): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 499 | error(clazz, m, "L3", "Listener methods should be named add/remove") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 500 | |
| 501 | |
| 502 | def verify_sync(clazz): |
| 503 | """Verify synchronized methods aren't exposed.""" |
| 504 | for m in clazz.methods: |
| 505 | if "synchronized" in m.split: |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 506 | error(clazz, m, "M5", "Internal locks must not be exposed") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 507 | |
| 508 | |
| 509 | def verify_intent_builder(clazz): |
| 510 | """Verify that Intent builders are createFooIntent() style.""" |
| 511 | if clazz.name == "Intent": return |
| 512 | |
| 513 | for m in clazz.methods: |
| 514 | if m.typ == "android.content.Intent": |
| 515 | if m.name.startswith("create") and m.name.endswith("Intent"): |
| 516 | pass |
| 517 | else: |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 518 | error(clazz, m, "FW1", "Methods creating an Intent must be named createFooIntent()") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 519 | |
| 520 | |
| 521 | def verify_helper_classes(clazz): |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 522 | """Verify that helper classes are named consistently with what they extend. |
| 523 | All developer extendable methods should be named onFoo().""" |
| 524 | test_methods = False |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 525 | if "extends android.app.Service" in clazz.raw: |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 526 | test_methods = True |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 527 | if not clazz.name.endswith("Service"): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 528 | error(clazz, None, "CL4", "Inconsistent class name; should be FooService") |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 529 | |
| 530 | found = False |
| 531 | for f in clazz.fields: |
| 532 | if f.name == "SERVICE_INTERFACE": |
| 533 | found = True |
| 534 | if f.value != clazz.fullname: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 535 | error(clazz, f, "C4", "Inconsistent interface constant; expected %s" % (clazz.fullname)) |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 536 | |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 537 | if "extends android.content.ContentProvider" in clazz.raw: |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 538 | test_methods = True |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 539 | if not clazz.name.endswith("Provider"): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 540 | error(clazz, None, "CL4", "Inconsistent class name; should be FooProvider") |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 541 | |
| 542 | found = False |
| 543 | for f in clazz.fields: |
| 544 | if f.name == "PROVIDER_INTERFACE": |
| 545 | found = True |
| 546 | if f.value != clazz.fullname: |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 547 | error(clazz, f, "C4", "Inconsistent interface constant; expected %s" % (clazz.fullname)) |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 548 | |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 549 | if "extends android.content.BroadcastReceiver" in clazz.raw: |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 550 | test_methods = True |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 551 | if not clazz.name.endswith("Receiver"): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 552 | error(clazz, None, "CL4", "Inconsistent class name; should be FooReceiver") |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 553 | |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 554 | if "extends android.app.Activity" in clazz.raw: |
| 555 | test_methods = True |
| 556 | if not clazz.name.endswith("Activity"): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 557 | error(clazz, None, "CL4", "Inconsistent class name; should be FooActivity") |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 558 | |
| 559 | if test_methods: |
| 560 | for m in clazz.methods: |
| 561 | if "final" in m.split: continue |
| 562 | if not re.match("on[A-Z]", m.name): |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 563 | if "abstract" in m.split: |
Jeff Sharkey | b46a969 | 2015-02-17 17:19:41 -0800 | [diff] [blame] | 564 | warn(clazz, m, None, "Methods implemented by developers should be named onFoo()") |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 565 | else: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 566 | warn(clazz, m, None, "If implemented by developer, should be named onFoo(); otherwise consider marking final") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 567 | |
| 568 | |
| 569 | def verify_builder(clazz): |
| 570 | """Verify builder classes. |
| 571 | Methods should return the builder to enable chaining.""" |
| 572 | if " extends " in clazz.raw: return |
| 573 | if not clazz.name.endswith("Builder"): return |
| 574 | |
| 575 | if clazz.name != "Builder": |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 576 | warn(clazz, None, None, "Builder should be defined as inner class") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 577 | |
| 578 | has_build = False |
| 579 | for m in clazz.methods: |
| 580 | if m.name == "build": |
| 581 | has_build = True |
| 582 | continue |
| 583 | |
| 584 | if m.name.startswith("get"): continue |
| 585 | if m.name.startswith("clear"): continue |
| 586 | |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 587 | if m.name.startswith("with"): |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 588 | warn(clazz, m, None, "Builder methods names should use setFoo() style") |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 589 | |
| 590 | if m.name.startswith("set"): |
| 591 | if not m.typ.endswith(clazz.fullname): |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 592 | warn(clazz, m, "M4", "Methods must return the builder object") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 593 | |
| 594 | if not has_build: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 595 | warn(clazz, None, None, "Missing build() method") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 596 | |
| 597 | |
| 598 | def verify_aidl(clazz): |
| 599 | """Catch people exposing raw AIDL.""" |
Jeff Sharkey | 932a07c | 2014-08-28 16:16:02 -0700 | [diff] [blame] | 600 | if "extends android.os.Binder" in clazz.raw or "implements android.os.IInterface" in clazz.raw: |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 601 | error(clazz, None, None, "Raw AIDL interfaces must not be exposed") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 602 | |
| 603 | |
Jeff Sharkey | 932a07c | 2014-08-28 16:16:02 -0700 | [diff] [blame] | 604 | def verify_internal(clazz): |
| 605 | """Catch people exposing internal classes.""" |
| 606 | if clazz.pkg.name.startswith("com.android"): |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 607 | error(clazz, None, None, "Internal classes must not be exposed") |
Jeff Sharkey | 932a07c | 2014-08-28 16:16:02 -0700 | [diff] [blame] | 608 | |
| 609 | |
| 610 | def verify_layering(clazz): |
| 611 | """Catch package layering violations. |
| 612 | For example, something in android.os depending on android.app.""" |
| 613 | ranking = [ |
| 614 | ["android.service","android.accessibilityservice","android.inputmethodservice","android.printservice","android.appwidget","android.webkit","android.preference","android.gesture","android.print"], |
| 615 | "android.app", |
| 616 | "android.widget", |
| 617 | "android.view", |
| 618 | "android.animation", |
| 619 | "android.provider", |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 620 | ["android.content","android.graphics.drawable"], |
Jeff Sharkey | 932a07c | 2014-08-28 16:16:02 -0700 | [diff] [blame] | 621 | "android.database", |
| 622 | "android.graphics", |
| 623 | "android.text", |
| 624 | "android.os", |
| 625 | "android.util" |
| 626 | ] |
| 627 | |
| 628 | def rank(p): |
| 629 | for i in range(len(ranking)): |
| 630 | if isinstance(ranking[i], list): |
| 631 | for j in ranking[i]: |
| 632 | if p.startswith(j): return i |
| 633 | else: |
| 634 | if p.startswith(ranking[i]): return i |
| 635 | |
| 636 | cr = rank(clazz.pkg.name) |
| 637 | if cr is None: return |
| 638 | |
| 639 | for f in clazz.fields: |
| 640 | ir = rank(f.typ) |
| 641 | if ir and ir < cr: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 642 | warn(clazz, f, "FW6", "Field type violates package layering") |
Jeff Sharkey | 932a07c | 2014-08-28 16:16:02 -0700 | [diff] [blame] | 643 | |
| 644 | for m in clazz.methods: |
| 645 | ir = rank(m.typ) |
| 646 | if ir and ir < cr: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 647 | warn(clazz, m, "FW6", "Method return type violates package layering") |
Jeff Sharkey | 932a07c | 2014-08-28 16:16:02 -0700 | [diff] [blame] | 648 | for arg in m.args: |
| 649 | ir = rank(arg) |
| 650 | if ir and ir < cr: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 651 | warn(clazz, m, "FW6", "Method argument type violates package layering") |
Jeff Sharkey | 932a07c | 2014-08-28 16:16:02 -0700 | [diff] [blame] | 652 | |
| 653 | |
Jeff Sharkey | a18a2e3 | 2015-02-22 15:54:32 -0800 | [diff] [blame] | 654 | def verify_boolean(clazz): |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 655 | """Verifies that boolean accessors are named correctly. |
| 656 | For example, hasFoo() and setHasFoo().""" |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 657 | |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 658 | def is_get(m): return len(m.args) == 0 and m.typ == "boolean" |
| 659 | def is_set(m): return len(m.args) == 1 and m.args[0] == "boolean" |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 660 | |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 661 | gets = [ m for m in clazz.methods if is_get(m) ] |
| 662 | sets = [ m for m in clazz.methods if is_set(m) ] |
| 663 | |
| 664 | def error_if_exists(methods, trigger, expected, actual): |
| 665 | for m in methods: |
| 666 | if m.name == actual: |
| 667 | error(clazz, m, "M6", "Symmetric method for %s must be named %s" % (trigger, expected)) |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 668 | |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 669 | for m in clazz.methods: |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 670 | if is_get(m): |
| 671 | if re.match("is[A-Z]", m.name): |
| 672 | target = m.name[2:] |
| 673 | expected = "setIs" + target |
| 674 | error_if_exists(sets, m.name, expected, "setHas" + target) |
| 675 | elif re.match("has[A-Z]", m.name): |
| 676 | target = m.name[3:] |
| 677 | expected = "setHas" + target |
| 678 | error_if_exists(sets, m.name, expected, "setIs" + target) |
| 679 | error_if_exists(sets, m.name, expected, "set" + target) |
| 680 | elif re.match("get[A-Z]", m.name): |
| 681 | target = m.name[3:] |
| 682 | expected = "set" + target |
| 683 | error_if_exists(sets, m.name, expected, "setIs" + target) |
| 684 | error_if_exists(sets, m.name, expected, "setHas" + target) |
| 685 | |
| 686 | if is_set(m): |
| 687 | if re.match("set[A-Z]", m.name): |
| 688 | target = m.name[3:] |
| 689 | expected = "get" + target |
| 690 | error_if_exists(sets, m.name, expected, "is" + target) |
| 691 | error_if_exists(sets, m.name, expected, "has" + target) |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 692 | |
| 693 | |
| 694 | def verify_collections(clazz): |
| 695 | """Verifies that collection types are interfaces.""" |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 696 | if clazz.fullname == "android.os.Bundle": return |
| 697 | |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 698 | bad = ["java.util.Vector", "java.util.LinkedList", "java.util.ArrayList", "java.util.Stack", |
| 699 | "java.util.HashMap", "java.util.HashSet", "android.util.ArraySet", "android.util.ArrayMap"] |
| 700 | for m in clazz.methods: |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 701 | if m.typ in bad: |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 702 | error(clazz, m, "CL2", "Return type is concrete collection; must be higher-level interface") |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 703 | for arg in m.args: |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 704 | if arg in bad: |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 705 | error(clazz, m, "CL2", "Argument is concrete collection; must be higher-level interface") |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 706 | |
| 707 | |
| 708 | def verify_flags(clazz): |
| 709 | """Verifies that flags are non-overlapping.""" |
| 710 | known = collections.defaultdict(int) |
| 711 | for f in clazz.fields: |
| 712 | if "FLAG_" in f.name: |
| 713 | try: |
| 714 | val = int(f.value) |
| 715 | except: |
| 716 | continue |
| 717 | |
| 718 | scope = f.name[0:f.name.index("FLAG_")] |
| 719 | if val & known[scope]: |
Jeff Sharkey | b46a969 | 2015-02-17 17:19:41 -0800 | [diff] [blame] | 720 | warn(clazz, f, "C1", "Found overlapping flag constant value") |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 721 | known[scope] |= val |
| 722 | |
| 723 | |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 724 | def verify_exception(clazz): |
| 725 | """Verifies that methods don't throw generic exceptions.""" |
| 726 | for m in clazz.methods: |
| 727 | if "throws java.lang.Exception" in m.raw or "throws java.lang.Throwable" in m.raw or "throws java.lang.Error" in m.raw: |
| 728 | error(clazz, m, "S1", "Methods must not throw generic exceptions") |
| 729 | |
| 730 | |
| 731 | def verify_google(clazz): |
| 732 | """Verifies that APIs never reference Google.""" |
| 733 | |
| 734 | if re.search("google", clazz.raw, re.IGNORECASE): |
| 735 | error(clazz, None, None, "Must never reference Google") |
| 736 | |
| 737 | test = [] |
| 738 | test.extend(clazz.ctors) |
| 739 | test.extend(clazz.fields) |
| 740 | test.extend(clazz.methods) |
| 741 | |
| 742 | for t in test: |
| 743 | if re.search("google", t.raw, re.IGNORECASE): |
| 744 | error(clazz, t, None, "Must never reference Google") |
| 745 | |
| 746 | |
| 747 | def verify_bitset(clazz): |
| 748 | """Verifies that we avoid using heavy BitSet.""" |
| 749 | |
| 750 | for f in clazz.fields: |
| 751 | if f.typ == "java.util.BitSet": |
| 752 | error(clazz, f, None, "Field type must not be heavy BitSet") |
| 753 | |
| 754 | for m in clazz.methods: |
| 755 | if m.typ == "java.util.BitSet": |
| 756 | error(clazz, m, None, "Return type must not be heavy BitSet") |
| 757 | for arg in m.args: |
| 758 | if arg == "java.util.BitSet": |
| 759 | error(clazz, m, None, "Argument type must not be heavy BitSet") |
| 760 | |
| 761 | |
| 762 | def verify_manager(clazz): |
| 763 | """Verifies that FooManager is only obtained from Context.""" |
| 764 | |
| 765 | if not clazz.name.endswith("Manager"): return |
| 766 | |
| 767 | for c in clazz.ctors: |
Jeff Sharkey | 047d7f0 | 2015-02-25 11:27:55 -0800 | [diff] [blame^] | 768 | error(clazz, c, None, "Managers must always be obtained from Context; no direct constructors") |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 769 | |
| 770 | |
| 771 | def verify_boxed(clazz): |
| 772 | """Verifies that methods avoid boxed primitives.""" |
| 773 | |
Jeff Sharkey | b46a969 | 2015-02-17 17:19:41 -0800 | [diff] [blame] | 774 | boxed = ["java.lang.Number","java.lang.Byte","java.lang.Double","java.lang.Float","java.lang.Integer","java.lang.Long","java.lang.Short"] |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 775 | |
| 776 | for c in clazz.ctors: |
| 777 | for arg in c.args: |
| 778 | if arg in boxed: |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 779 | error(clazz, c, "M11", "Must avoid boxed primitives") |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 780 | |
| 781 | for f in clazz.fields: |
| 782 | if f.typ in boxed: |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 783 | error(clazz, f, "M11", "Must avoid boxed primitives") |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 784 | |
| 785 | for m in clazz.methods: |
| 786 | if m.typ in boxed: |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 787 | error(clazz, m, "M11", "Must avoid boxed primitives") |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 788 | for arg in m.args: |
| 789 | if arg in boxed: |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 790 | error(clazz, m, "M11", "Must avoid boxed primitives") |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 791 | |
| 792 | |
| 793 | def verify_static_utils(clazz): |
| 794 | """Verifies that helper classes can't be constructed.""" |
| 795 | if clazz.fullname.startswith("android.opengl"): return |
| 796 | if re.match("android\.R\.[a-z]+", clazz.fullname): return |
| 797 | |
| 798 | if len(clazz.fields) > 0: return |
| 799 | if len(clazz.methods) == 0: return |
| 800 | |
| 801 | for m in clazz.methods: |
| 802 | if "static" not in m.split: |
| 803 | return |
| 804 | |
| 805 | # At this point, we have no fields, and all methods are static |
| 806 | if len(clazz.ctors) > 0: |
| 807 | error(clazz, None, None, "Fully-static utility classes must not have constructor") |
| 808 | |
| 809 | |
Jeff Sharkey | b46a969 | 2015-02-17 17:19:41 -0800 | [diff] [blame] | 810 | def verify_overload_args(clazz): |
| 811 | """Verifies that method overloads add new arguments at the end.""" |
| 812 | if clazz.fullname.startswith("android.opengl"): return |
| 813 | |
| 814 | overloads = collections.defaultdict(list) |
| 815 | for m in clazz.methods: |
| 816 | if "deprecated" in m.split: continue |
| 817 | overloads[m.name].append(m) |
| 818 | |
Jeff Sharkey | a18a2e3 | 2015-02-22 15:54:32 -0800 | [diff] [blame] | 819 | for name, methods in overloads.items(): |
Jeff Sharkey | b46a969 | 2015-02-17 17:19:41 -0800 | [diff] [blame] | 820 | if len(methods) <= 1: continue |
| 821 | |
| 822 | # Look for arguments common across all overloads |
| 823 | def cluster(args): |
| 824 | count = collections.defaultdict(int) |
| 825 | res = set() |
| 826 | for i in range(len(args)): |
| 827 | a = args[i] |
| 828 | res.add("%s#%d" % (a, count[a])) |
| 829 | count[a] += 1 |
| 830 | return res |
| 831 | |
| 832 | common_args = cluster(methods[0].args) |
| 833 | for m in methods: |
| 834 | common_args = common_args & cluster(m.args) |
| 835 | |
| 836 | if len(common_args) == 0: continue |
| 837 | |
| 838 | # Require that all common arguments are present at start of signature |
| 839 | locked_sig = None |
| 840 | for m in methods: |
| 841 | sig = m.args[0:len(common_args)] |
| 842 | if not common_args.issubset(cluster(sig)): |
| 843 | warn(clazz, m, "M2", "Expected common arguments [%s] at beginning of overloaded method" % (", ".join(common_args))) |
| 844 | elif not locked_sig: |
| 845 | locked_sig = sig |
| 846 | elif locked_sig != sig: |
| 847 | error(clazz, m, "M2", "Expected consistent argument ordering between overloads: %s..." % (", ".join(locked_sig))) |
| 848 | |
| 849 | |
| 850 | def verify_callback_handlers(clazz): |
| 851 | """Verifies that methods adding listener/callback have overload |
| 852 | for specifying delivery thread.""" |
| 853 | |
Jeff Sharkey | 047d7f0 | 2015-02-25 11:27:55 -0800 | [diff] [blame^] | 854 | # Ignore UI packages which assume main thread |
Jeff Sharkey | b46a969 | 2015-02-17 17:19:41 -0800 | [diff] [blame] | 855 | skip = [ |
Jeff Sharkey | 047d7f0 | 2015-02-25 11:27:55 -0800 | [diff] [blame^] | 856 | "animation", |
| 857 | "view", |
| 858 | "graphics", |
| 859 | "transition", |
| 860 | "widget", |
| 861 | "webkit", |
Jeff Sharkey | b46a969 | 2015-02-17 17:19:41 -0800 | [diff] [blame] | 862 | ] |
| 863 | for s in skip: |
Jeff Sharkey | 047d7f0 | 2015-02-25 11:27:55 -0800 | [diff] [blame^] | 864 | if s in clazz.pkg.name_path: return |
| 865 | if s in clazz.extends_path: return |
Jeff Sharkey | b46a969 | 2015-02-17 17:19:41 -0800 | [diff] [blame] | 866 | |
Jeff Sharkey | 047d7f0 | 2015-02-25 11:27:55 -0800 | [diff] [blame^] | 867 | # Ignore UI classes which assume main thread |
| 868 | if "app" in clazz.pkg.name_path or "app" in clazz.extends_path: |
| 869 | for s in ["ActionBar","Dialog","Application","Activity","Fragment","Loader"]: |
| 870 | if s in clazz.fullname: return |
| 871 | if "content" in clazz.pkg.name_path or "content" in clazz.extends_path: |
| 872 | for s in ["Loader"]: |
| 873 | if s in clazz.fullname: return |
Jeff Sharkey | b46a969 | 2015-02-17 17:19:41 -0800 | [diff] [blame] | 874 | |
| 875 | found = {} |
| 876 | by_name = collections.defaultdict(list) |
| 877 | for m in clazz.methods: |
| 878 | if m.name.startswith("unregister"): continue |
| 879 | if m.name.startswith("remove"): continue |
| 880 | if re.match("on[A-Z]+", m.name): continue |
| 881 | |
| 882 | by_name[m.name].append(m) |
| 883 | |
| 884 | for a in m.args: |
| 885 | if a.endswith("Listener") or a.endswith("Callback") or a.endswith("Callbacks"): |
| 886 | found[m.name] = m |
| 887 | |
| 888 | for f in found.values(): |
| 889 | takes_handler = False |
| 890 | for m in by_name[f.name]: |
| 891 | if "android.os.Handler" in m.args: |
| 892 | takes_handler = True |
| 893 | if not takes_handler: |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 894 | warn(clazz, f, "L1", "Registration methods should have overload that accepts delivery Handler") |
Jeff Sharkey | b46a969 | 2015-02-17 17:19:41 -0800 | [diff] [blame] | 895 | |
| 896 | |
| 897 | def verify_context_first(clazz): |
| 898 | """Verifies that methods accepting a Context keep it the first argument.""" |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 899 | examine = clazz.ctors + clazz.methods |
| 900 | for m in examine: |
| 901 | if len(m.args) > 1 and m.args[0] != "android.content.Context": |
Jeff Sharkey | b46a969 | 2015-02-17 17:19:41 -0800 | [diff] [blame] | 902 | if "android.content.Context" in m.args[1:]: |
| 903 | error(clazz, m, "M3", "Context is distinct, so it must be the first argument") |
| 904 | |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 905 | |
| 906 | def verify_listener_last(clazz): |
| 907 | """Verifies that methods accepting a Listener or Callback keep them as last arguments.""" |
| 908 | examine = clazz.ctors + clazz.methods |
| 909 | for m in examine: |
| 910 | if "Listener" in m.name or "Callback" in m.name: continue |
| 911 | found = False |
| 912 | for a in m.args: |
| 913 | if a.endswith("Callback") or a.endswith("Callbacks") or a.endswith("Listener"): |
| 914 | found = True |
| 915 | elif found and a != "android.os.Handler": |
| 916 | warn(clazz, m, "M3", "Listeners should always be at end of argument list") |
| 917 | |
| 918 | |
| 919 | def verify_resource_names(clazz): |
| 920 | """Verifies that resource names have consistent case.""" |
| 921 | if not re.match("android\.R\.[a-z]+", clazz.fullname): return |
| 922 | |
| 923 | # Resources defined by files are foo_bar_baz |
| 924 | if clazz.name in ["anim","animator","color","dimen","drawable","interpolator","layout","transition","menu","mipmap","string","plurals","raw","xml"]: |
| 925 | for f in clazz.fields: |
| 926 | if re.match("[a-z1-9_]+$", f.name): continue |
| 927 | error(clazz, f, None, "Expected resource name in this class to be foo_bar_baz style") |
| 928 | |
| 929 | # Resources defined inside files are fooBarBaz |
| 930 | if clazz.name in ["array","attr","id","bool","fraction","integer"]: |
| 931 | for f in clazz.fields: |
| 932 | if re.match("config_[a-z][a-zA-Z1-9]*$", f.name): continue |
| 933 | if re.match("layout_[a-z][a-zA-Z1-9]*$", f.name): continue |
| 934 | if re.match("state_[a-z_]*$", f.name): continue |
| 935 | |
| 936 | if re.match("[a-z][a-zA-Z1-9]*$", f.name): continue |
| 937 | error(clazz, f, "C7", "Expected resource name in this class to be fooBarBaz style") |
| 938 | |
| 939 | # Styles are FooBar_Baz |
| 940 | if clazz.name in ["style"]: |
| 941 | for f in clazz.fields: |
| 942 | if re.match("[A-Z][A-Za-z1-9]+(_[A-Z][A-Za-z1-9]+?)*$", f.name): continue |
| 943 | error(clazz, f, "C7", "Expected resource name in this class to be FooBar_Baz style") |
Jeff Sharkey | b46a969 | 2015-02-17 17:19:41 -0800 | [diff] [blame] | 944 | |
| 945 | |
Jeff Sharkey | a18a2e3 | 2015-02-22 15:54:32 -0800 | [diff] [blame] | 946 | def examine_clazz(clazz): |
| 947 | """Find all style issues in the given class.""" |
| 948 | if clazz.pkg.name.startswith("java"): return |
| 949 | if clazz.pkg.name.startswith("junit"): return |
| 950 | if clazz.pkg.name.startswith("org.apache"): return |
| 951 | if clazz.pkg.name.startswith("org.xml"): return |
| 952 | if clazz.pkg.name.startswith("org.json"): return |
| 953 | if clazz.pkg.name.startswith("org.w3c"): return |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 954 | |
Jeff Sharkey | a18a2e3 | 2015-02-22 15:54:32 -0800 | [diff] [blame] | 955 | verify_constants(clazz) |
| 956 | verify_enums(clazz) |
| 957 | verify_class_names(clazz) |
| 958 | verify_method_names(clazz) |
| 959 | verify_callbacks(clazz) |
| 960 | verify_listeners(clazz) |
| 961 | verify_actions(clazz) |
| 962 | verify_extras(clazz) |
| 963 | verify_equals(clazz) |
| 964 | verify_parcelable(clazz) |
| 965 | verify_protected(clazz) |
| 966 | verify_fields(clazz) |
| 967 | verify_register(clazz) |
| 968 | verify_sync(clazz) |
| 969 | verify_intent_builder(clazz) |
| 970 | verify_helper_classes(clazz) |
| 971 | verify_builder(clazz) |
| 972 | verify_aidl(clazz) |
| 973 | verify_internal(clazz) |
| 974 | verify_layering(clazz) |
| 975 | verify_boolean(clazz) |
| 976 | verify_collections(clazz) |
| 977 | verify_flags(clazz) |
| 978 | verify_exception(clazz) |
| 979 | verify_google(clazz) |
| 980 | verify_bitset(clazz) |
| 981 | verify_manager(clazz) |
| 982 | verify_boxed(clazz) |
| 983 | verify_static_utils(clazz) |
| 984 | verify_overload_args(clazz) |
| 985 | verify_callback_handlers(clazz) |
| 986 | verify_context_first(clazz) |
| 987 | verify_listener_last(clazz) |
| 988 | verify_resource_names(clazz) |
| 989 | |
| 990 | |
| 991 | def examine_stream(stream): |
| 992 | """Find all style issues in the given API stream.""" |
| 993 | global failures |
| 994 | failures = {} |
| 995 | _parse_stream(stream, examine_clazz) |
| 996 | return failures |
| 997 | |
| 998 | |
| 999 | def examine_api(api): |
| 1000 | """Find all style issues in the given parsed API.""" |
| 1001 | global failures |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 1002 | failures = {} |
| 1003 | for key in sorted(api.keys()): |
Jeff Sharkey | a18a2e3 | 2015-02-22 15:54:32 -0800 | [diff] [blame] | 1004 | examine_clazz(api[key]) |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 1005 | return failures |
| 1006 | |
| 1007 | |
Jeff Sharkey | 037458a | 2014-09-04 15:46:20 -0700 | [diff] [blame] | 1008 | def verify_compat(cur, prev): |
| 1009 | """Find any incompatible API changes between two levels.""" |
| 1010 | global failures |
| 1011 | |
| 1012 | def class_exists(api, test): |
| 1013 | return test.fullname in api |
| 1014 | |
| 1015 | def ctor_exists(api, clazz, test): |
| 1016 | for m in clazz.ctors: |
| 1017 | if m.ident == test.ident: return True |
| 1018 | return False |
| 1019 | |
| 1020 | def all_methods(api, clazz): |
| 1021 | methods = list(clazz.methods) |
| 1022 | if clazz.extends is not None: |
| 1023 | methods.extend(all_methods(api, api[clazz.extends])) |
| 1024 | return methods |
| 1025 | |
| 1026 | def method_exists(api, clazz, test): |
| 1027 | methods = all_methods(api, clazz) |
| 1028 | for m in methods: |
| 1029 | if m.ident == test.ident: return True |
| 1030 | return False |
| 1031 | |
| 1032 | def field_exists(api, clazz, test): |
| 1033 | for f in clazz.fields: |
| 1034 | if f.ident == test.ident: return True |
| 1035 | return False |
| 1036 | |
| 1037 | failures = {} |
| 1038 | for key in sorted(prev.keys()): |
| 1039 | prev_clazz = prev[key] |
| 1040 | |
| 1041 | if not class_exists(cur, prev_clazz): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 1042 | error(prev_clazz, None, None, "Class removed or incompatible change") |
Jeff Sharkey | 037458a | 2014-09-04 15:46:20 -0700 | [diff] [blame] | 1043 | continue |
| 1044 | |
| 1045 | cur_clazz = cur[key] |
| 1046 | |
| 1047 | for test in prev_clazz.ctors: |
| 1048 | if not ctor_exists(cur, cur_clazz, test): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 1049 | error(prev_clazz, prev_ctor, None, "Constructor removed or incompatible change") |
Jeff Sharkey | 037458a | 2014-09-04 15:46:20 -0700 | [diff] [blame] | 1050 | |
| 1051 | methods = all_methods(prev, prev_clazz) |
| 1052 | for test in methods: |
| 1053 | if not method_exists(cur, cur_clazz, test): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 1054 | error(prev_clazz, test, None, "Method removed or incompatible change") |
Jeff Sharkey | 037458a | 2014-09-04 15:46:20 -0700 | [diff] [blame] | 1055 | |
| 1056 | for test in prev_clazz.fields: |
| 1057 | if not field_exists(cur, cur_clazz, test): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 1058 | error(prev_clazz, test, None, "Field removed or incompatible change") |
Jeff Sharkey | 037458a | 2014-09-04 15:46:20 -0700 | [diff] [blame] | 1059 | |
| 1060 | return failures |
| 1061 | |
| 1062 | |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 1063 | if __name__ == "__main__": |
Jeff Sharkey | a18a2e3 | 2015-02-22 15:54:32 -0800 | [diff] [blame] | 1064 | with open(sys.argv[1]) as f: |
| 1065 | cur_fail = examine_stream(f) |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 1066 | |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 1067 | if len(sys.argv) > 2: |
Jeff Sharkey | a18a2e3 | 2015-02-22 15:54:32 -0800 | [diff] [blame] | 1068 | with open(sys.argv[2]) as f: |
| 1069 | prev_fail = examine_stream(f) |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 1070 | |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 1071 | # ignore errors from previous API level |
| 1072 | for p in prev_fail: |
| 1073 | if p in cur_fail: |
| 1074 | del cur_fail[p] |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 1075 | |
Jeff Sharkey | a18a2e3 | 2015-02-22 15:54:32 -0800 | [diff] [blame] | 1076 | """ |
| 1077 | # NOTE: disabled because of memory pressure |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 1078 | # look for compatibility issues |
| 1079 | compat_fail = verify_compat(cur, prev) |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 1080 | |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 1081 | print "%s API compatibility issues %s\n" % ((format(fg=WHITE, bg=BLUE, bold=True), format(reset=True))) |
| 1082 | for f in sorted(compat_fail): |
| 1083 | print compat_fail[f] |
| 1084 | print |
Jeff Sharkey | a18a2e3 | 2015-02-22 15:54:32 -0800 | [diff] [blame] | 1085 | """ |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 1086 | |
| 1087 | print "%s API style issues %s\n" % ((format(fg=WHITE, bg=BLUE, bold=True), format(reset=True))) |
| 1088 | for f in sorted(cur_fail): |
| 1089 | print cur_fail[f] |
Jeff Sharkey | 037458a | 2014-09-04 15:46:20 -0700 | [diff] [blame] | 1090 | print |