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