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] |
| 135 | else: |
| 136 | self.extends = None |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 137 | |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 138 | self.fullname = self.pkg.name + "." + self.fullname |
| 139 | self.name = self.fullname[self.fullname.rindex(".")+1:] |
| 140 | |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 141 | def __repr__(self): |
| 142 | return self.raw |
| 143 | |
| 144 | |
| 145 | class Package(): |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 146 | def __init__(self, line, raw, blame): |
| 147 | self.line = line |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 148 | self.raw = raw.strip(" {;") |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 149 | self.blame = blame |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 150 | |
| 151 | raw = raw.split() |
| 152 | self.name = raw[raw.index("package")+1] |
| 153 | |
| 154 | def __repr__(self): |
| 155 | return self.raw |
| 156 | |
| 157 | |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 158 | def parse_api(f): |
| 159 | line = 0 |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 160 | api = {} |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 161 | pkg = None |
| 162 | clazz = None |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 163 | blame = None |
| 164 | |
| 165 | re_blame = re.compile("^([a-z0-9]{7,}) \(<([^>]+)>.+?\) (.+?)$") |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 166 | for raw in f.readlines(): |
| 167 | line += 1 |
| 168 | raw = raw.rstrip() |
| 169 | match = re_blame.match(raw) |
| 170 | if match is not None: |
| 171 | blame = match.groups()[0:2] |
| 172 | raw = match.groups()[2] |
| 173 | else: |
| 174 | blame = None |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 175 | |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 176 | if raw.startswith("package"): |
| 177 | pkg = Package(line, raw, blame) |
| 178 | elif raw.startswith(" ") and raw.endswith("{"): |
| 179 | clazz = Class(pkg, line, raw, blame) |
| 180 | api[clazz.fullname] = clazz |
| 181 | elif raw.startswith(" ctor"): |
| 182 | clazz.ctors.append(Method(clazz, line, raw, blame)) |
| 183 | elif raw.startswith(" method"): |
| 184 | clazz.methods.append(Method(clazz, line, raw, blame)) |
| 185 | elif raw.startswith(" field"): |
| 186 | clazz.fields.append(Field(clazz, line, raw, blame)) |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 187 | |
| 188 | return api |
| 189 | |
| 190 | |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 191 | def parse_api_file(fn): |
| 192 | with open(fn) as f: |
| 193 | return parse_api(f) |
| 194 | |
| 195 | |
| 196 | class Failure(): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 197 | def __init__(self, sig, clazz, detail, error, rule, msg): |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 198 | self.sig = sig |
| 199 | self.clazz = clazz |
| 200 | self.detail = detail |
| 201 | self.error = error |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 202 | self.rule = rule |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 203 | self.msg = msg |
| 204 | |
| 205 | if error: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 206 | self.head = "Error %s" % (rule) if rule else "Error" |
| 207 | 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] | 208 | else: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 209 | self.head = "Warning %s" % (rule) if rule else "Warning" |
| 210 | 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] | 211 | |
| 212 | self.line = clazz.line |
| 213 | blame = clazz.blame |
| 214 | if detail is not None: |
| 215 | dump += "\n in " + repr(detail) |
| 216 | self.line = detail.line |
| 217 | blame = detail.blame |
| 218 | dump += "\n in " + repr(clazz) |
| 219 | dump += "\n in " + repr(clazz.pkg) |
| 220 | dump += "\n at line " + repr(self.line) |
| 221 | if blame is not None: |
| 222 | dump += "\n last modified by %s in %s" % (blame[1], blame[0]) |
| 223 | |
| 224 | self.dump = dump |
| 225 | |
| 226 | def __repr__(self): |
| 227 | return self.dump |
| 228 | |
| 229 | |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 230 | failures = {} |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 231 | |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 232 | def _fail(clazz, detail, error, rule, msg): |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 233 | """Records an API failure to be processed later.""" |
| 234 | global failures |
| 235 | |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 236 | sig = "%s-%s-%s" % (clazz.fullname, repr(detail), msg) |
| 237 | sig = sig.replace(" deprecated ", " ") |
| 238 | |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 239 | failures[sig] = Failure(sig, clazz, detail, error, rule, msg) |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 240 | |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 241 | |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 242 | def warn(clazz, detail, rule, msg): |
| 243 | _fail(clazz, detail, False, rule, msg) |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 244 | |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 245 | def error(clazz, detail, rule, msg): |
| 246 | _fail(clazz, detail, True, rule, msg) |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 247 | |
| 248 | |
| 249 | def verify_constants(clazz): |
| 250 | """All static final constants must be FOO_NAME style.""" |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 251 | if re.match("android\.R\.[a-z]+", clazz.fullname): return |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 252 | |
| 253 | for f in clazz.fields: |
| 254 | if "static" in f.split and "final" in f.split: |
| 255 | if re.match("[A-Z0-9_]+", f.name) is None: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 256 | error(clazz, f, "C2", "Constant field names should be FOO_NAME") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 257 | |
| 258 | |
| 259 | def verify_enums(clazz): |
| 260 | """Enums are bad, mmkay?""" |
| 261 | if "extends java.lang.Enum" in clazz.raw: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 262 | error(clazz, None, "F5", "Enums are not allowed") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 263 | |
| 264 | |
| 265 | def verify_class_names(clazz): |
| 266 | """Try catching malformed class names like myMtp or MTPUser.""" |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 267 | if clazz.fullname.startswith("android.opengl"): return |
| 268 | if clazz.fullname.startswith("android.renderscript"): return |
| 269 | if re.match("android\.R\.[a-z]+", clazz.fullname): return |
| 270 | |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 271 | if re.search("[A-Z]{2,}", clazz.name) is not None: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 272 | warn(clazz, None, "S1", "Class name style should be Mtp not MTP") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 273 | if re.match("[^A-Z]", clazz.name): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 274 | error(clazz, None, "S1", "Class must start with uppercase char") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 275 | |
| 276 | |
| 277 | def verify_method_names(clazz): |
| 278 | """Try catching malformed method names, like Foo() or getMTU().""" |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 279 | if clazz.fullname.startswith("android.opengl"): return |
| 280 | if clazz.fullname.startswith("android.renderscript"): return |
| 281 | if clazz.fullname == "android.system.OsConstants": return |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 282 | |
| 283 | for m in clazz.methods: |
| 284 | if re.search("[A-Z]{2,}", m.name) is not None: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 285 | warn(clazz, m, "S1", "Method name style should be getMtu() instead of getMTU()") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 286 | if re.match("[^a-z]", m.name): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 287 | error(clazz, m, "S1", "Method name must start with lowercase char") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 288 | |
| 289 | |
| 290 | def verify_callbacks(clazz): |
| 291 | """Verify Callback classes. |
| 292 | All callback classes must be abstract. |
| 293 | All methods must follow onFoo() naming style.""" |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 294 | if clazz.fullname == "android.speech.tts.SynthesisCallback": return |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 295 | |
| 296 | if clazz.name.endswith("Callbacks"): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 297 | error(clazz, None, "L1", "Class name must not be plural") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 298 | if clazz.name.endswith("Observer"): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 299 | warn(clazz, None, "L1", "Class should be named FooCallback") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 300 | |
| 301 | if clazz.name.endswith("Callback"): |
| 302 | if "interface" in clazz.split: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 303 | error(clazz, None, "CL3", "Callback must be abstract class to enable extension in future API levels") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 304 | |
| 305 | for m in clazz.methods: |
| 306 | if not re.match("on[A-Z][a-z]*", m.name): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 307 | error(clazz, m, "L1", "Callback method names must be onFoo() style") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 308 | |
| 309 | |
| 310 | def verify_listeners(clazz): |
| 311 | """Verify Listener classes. |
| 312 | All Listener classes must be interface. |
| 313 | All methods must follow onFoo() naming style. |
| 314 | If only a single method, it must match class name: |
| 315 | interface OnFooListener { void onFoo() }""" |
| 316 | |
| 317 | if clazz.name.endswith("Listener"): |
| 318 | if " abstract class " in clazz.raw: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 319 | error(clazz, None, "L1", "Listener should be an interface, otherwise renamed Callback") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 320 | |
| 321 | for m in clazz.methods: |
| 322 | if not re.match("on[A-Z][a-z]*", m.name): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 323 | error(clazz, m, "L1", "Listener method names must be onFoo() style") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 324 | |
| 325 | if len(clazz.methods) == 1 and clazz.name.startswith("On"): |
| 326 | m = clazz.methods[0] |
| 327 | if (m.name + "Listener").lower() != clazz.name.lower(): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 328 | error(clazz, m, "L1", "Single listener method name should match class name") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 329 | |
| 330 | |
| 331 | def verify_actions(clazz): |
| 332 | """Verify intent actions. |
| 333 | All action names must be named ACTION_FOO. |
| 334 | All action values must be scoped by package and match name: |
| 335 | package android.foo { |
| 336 | String ACTION_BAR = "android.foo.action.BAR"; |
| 337 | }""" |
| 338 | for f in clazz.fields: |
| 339 | if f.value is None: continue |
| 340 | if f.name.startswith("EXTRA_"): continue |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 341 | if f.name == "SERVICE_INTERFACE" or f.name == "PROVIDER_INTERFACE": continue |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 342 | |
| 343 | if "static" in f.split and "final" in f.split and f.typ == "java.lang.String": |
| 344 | if "_ACTION" in f.name or "ACTION_" in f.name or ".action." in f.value.lower(): |
| 345 | if not f.name.startswith("ACTION_"): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 346 | error(clazz, f, "C3", "Intent action constant name must be ACTION_FOO") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 347 | else: |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 348 | if clazz.fullname == "android.content.Intent": |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 349 | prefix = "android.intent.action" |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 350 | elif clazz.fullname == "android.provider.Settings": |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 351 | prefix = "android.settings" |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 352 | elif clazz.fullname == "android.app.admin.DevicePolicyManager" or clazz.fullname == "android.app.admin.DeviceAdminReceiver": |
| 353 | prefix = "android.app.action" |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 354 | else: |
| 355 | prefix = clazz.pkg.name + ".action" |
| 356 | expected = prefix + "." + f.name[7:] |
| 357 | if f.value != expected: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 358 | error(clazz, f, "C4", "Inconsistent action value; expected %s" % (expected)) |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 359 | |
| 360 | |
| 361 | def verify_extras(clazz): |
| 362 | """Verify intent extras. |
| 363 | All extra names must be named EXTRA_FOO. |
| 364 | All extra values must be scoped by package and match name: |
| 365 | package android.foo { |
| 366 | String EXTRA_BAR = "android.foo.extra.BAR"; |
| 367 | }""" |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 368 | if clazz.fullname == "android.app.Notification": return |
| 369 | if clazz.fullname == "android.appwidget.AppWidgetManager": return |
| 370 | |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 371 | for f in clazz.fields: |
| 372 | if f.value is None: continue |
| 373 | if f.name.startswith("ACTION_"): continue |
| 374 | |
| 375 | if "static" in f.split and "final" in f.split and f.typ == "java.lang.String": |
| 376 | if "_EXTRA" in f.name or "EXTRA_" in f.name or ".extra" in f.value.lower(): |
| 377 | if not f.name.startswith("EXTRA_"): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 378 | error(clazz, f, "C3", "Intent extra must be EXTRA_FOO") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 379 | else: |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 380 | if clazz.pkg.name == "android.content" and clazz.name == "Intent": |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 381 | prefix = "android.intent.extra" |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 382 | elif clazz.pkg.name == "android.app.admin": |
| 383 | prefix = "android.app.extra" |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 384 | else: |
| 385 | prefix = clazz.pkg.name + ".extra" |
| 386 | expected = prefix + "." + f.name[6:] |
| 387 | if f.value != expected: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 388 | error(clazz, f, "C4", "Inconsistent extra value; expected %s" % (expected)) |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 389 | |
| 390 | |
| 391 | def verify_equals(clazz): |
| 392 | """Verify that equals() and hashCode() must be overridden together.""" |
| 393 | methods = [ m.name for m in clazz.methods ] |
| 394 | eq = "equals" in methods |
| 395 | hc = "hashCode" in methods |
| 396 | if eq != hc: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 397 | error(clazz, None, "M8", "Must override both equals and hashCode; missing one") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 398 | |
| 399 | |
| 400 | def verify_parcelable(clazz): |
| 401 | """Verify that Parcelable objects aren't hiding required bits.""" |
| 402 | if "implements android.os.Parcelable" in clazz.raw: |
| 403 | creator = [ i for i in clazz.fields if i.name == "CREATOR" ] |
| 404 | write = [ i for i in clazz.methods if i.name == "writeToParcel" ] |
| 405 | describe = [ i for i in clazz.methods if i.name == "describeContents" ] |
| 406 | |
| 407 | if len(creator) == 0 or len(write) == 0 or len(describe) == 0: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 408 | error(clazz, None, "FW3", "Parcelable requires CREATOR, writeToParcel, and describeContents; missing one") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 409 | |
| 410 | |
| 411 | def verify_protected(clazz): |
| 412 | """Verify that no protected methods are allowed.""" |
| 413 | for m in clazz.methods: |
| 414 | if "protected" in m.split: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 415 | error(clazz, m, "M7", "No protected methods; must be public") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 416 | for f in clazz.fields: |
| 417 | if "protected" in f.split: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 418 | error(clazz, f, "M7", "No protected fields; must be public") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 419 | |
| 420 | |
| 421 | def verify_fields(clazz): |
| 422 | """Verify that all exposed fields are final. |
| 423 | Exposed fields must follow myName style. |
| 424 | Catch internal mFoo objects being exposed.""" |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 425 | |
| 426 | IGNORE_BARE_FIELDS = [ |
| 427 | "android.app.ActivityManager.RecentTaskInfo", |
| 428 | "android.app.Notification", |
| 429 | "android.content.pm.ActivityInfo", |
| 430 | "android.content.pm.ApplicationInfo", |
| 431 | "android.content.pm.FeatureGroupInfo", |
| 432 | "android.content.pm.InstrumentationInfo", |
| 433 | "android.content.pm.PackageInfo", |
| 434 | "android.content.pm.PackageItemInfo", |
| 435 | "android.os.Message", |
| 436 | "android.system.StructPollfd", |
| 437 | ] |
| 438 | |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 439 | for f in clazz.fields: |
| 440 | if not "final" in f.split: |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 441 | if clazz.fullname in IGNORE_BARE_FIELDS: |
| 442 | pass |
| 443 | elif clazz.fullname.endswith("LayoutParams"): |
| 444 | pass |
| 445 | elif clazz.fullname.startswith("android.util.Mutable"): |
| 446 | pass |
| 447 | else: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 448 | 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] | 449 | |
| 450 | if not "static" in f.split: |
| 451 | if not re.match("[a-z]([a-zA-Z]+)?", f.name): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 452 | error(clazz, f, "S1", "Non-static fields must be named with myField style") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 453 | |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 454 | if re.match("[ms][A-Z]", f.name): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 455 | error(clazz, f, "F1", "Don't expose your internal objects") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 456 | |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 457 | if re.match("[A-Z_]+", f.name): |
| 458 | if "static" not in f.split or "final" not in f.split: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 459 | error(clazz, f, "C2", "Constants must be marked static final") |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 460 | |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 461 | |
| 462 | def verify_register(clazz): |
| 463 | """Verify parity of registration methods. |
| 464 | Callback objects use register/unregister methods. |
| 465 | Listener objects use add/remove methods.""" |
| 466 | methods = [ m.name for m in clazz.methods ] |
| 467 | for m in clazz.methods: |
| 468 | if "Callback" in m.raw: |
| 469 | if m.name.startswith("register"): |
| 470 | other = "unregister" + m.name[8:] |
| 471 | if other not in methods: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 472 | error(clazz, m, "L2", "Missing unregister method") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 473 | if m.name.startswith("unregister"): |
| 474 | other = "register" + m.name[10:] |
| 475 | if other not in methods: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 476 | error(clazz, m, "L2", "Missing register method") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 477 | |
| 478 | if m.name.startswith("add") or m.name.startswith("remove"): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 479 | error(clazz, m, "L3", "Callback methods should be named register/unregister") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 480 | |
| 481 | if "Listener" in m.raw: |
| 482 | if m.name.startswith("add"): |
| 483 | other = "remove" + m.name[3:] |
| 484 | if other not in methods: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 485 | error(clazz, m, "L2", "Missing remove method") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 486 | if m.name.startswith("remove") and not m.name.startswith("removeAll"): |
| 487 | other = "add" + m.name[6:] |
| 488 | if other not in methods: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 489 | error(clazz, m, "L2", "Missing add method") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 490 | |
| 491 | if m.name.startswith("register") or m.name.startswith("unregister"): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 492 | error(clazz, m, "L3", "Listener methods should be named add/remove") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 493 | |
| 494 | |
| 495 | def verify_sync(clazz): |
| 496 | """Verify synchronized methods aren't exposed.""" |
| 497 | for m in clazz.methods: |
| 498 | if "synchronized" in m.split: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 499 | error(clazz, m, "M5", "Internal lock exposed") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 500 | |
| 501 | |
| 502 | def verify_intent_builder(clazz): |
| 503 | """Verify that Intent builders are createFooIntent() style.""" |
| 504 | if clazz.name == "Intent": return |
| 505 | |
| 506 | for m in clazz.methods: |
| 507 | if m.typ == "android.content.Intent": |
| 508 | if m.name.startswith("create") and m.name.endswith("Intent"): |
| 509 | pass |
| 510 | else: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 511 | error(clazz, m, "FW1", "Methods creating an Intent should be named createFooIntent()") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 512 | |
| 513 | |
| 514 | def verify_helper_classes(clazz): |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 515 | """Verify that helper classes are named consistently with what they extend. |
| 516 | All developer extendable methods should be named onFoo().""" |
| 517 | test_methods = False |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 518 | if "extends android.app.Service" in clazz.raw: |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 519 | test_methods = True |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 520 | if not clazz.name.endswith("Service"): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 521 | error(clazz, None, "CL4", "Inconsistent class name; should be FooService") |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 522 | |
| 523 | found = False |
| 524 | for f in clazz.fields: |
| 525 | if f.name == "SERVICE_INTERFACE": |
| 526 | found = True |
| 527 | if f.value != clazz.fullname: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 528 | error(clazz, f, "C4", "Inconsistent interface constant; expected %s" % (clazz.fullname)) |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 529 | |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 530 | if "extends android.content.ContentProvider" in clazz.raw: |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 531 | test_methods = True |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 532 | if not clazz.name.endswith("Provider"): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 533 | error(clazz, None, "CL4", "Inconsistent class name; should be FooProvider") |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 534 | |
| 535 | found = False |
| 536 | for f in clazz.fields: |
| 537 | if f.name == "PROVIDER_INTERFACE": |
| 538 | found = True |
| 539 | if f.value != clazz.fullname: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 540 | error(clazz, f, "C4", "Inconsistent interface name; expected %s" % (clazz.fullname)) |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 541 | |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 542 | if "extends android.content.BroadcastReceiver" in clazz.raw: |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 543 | test_methods = True |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 544 | if not clazz.name.endswith("Receiver"): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 545 | error(clazz, None, "CL4", "Inconsistent class name; should be FooReceiver") |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 546 | |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 547 | if "extends android.app.Activity" in clazz.raw: |
| 548 | test_methods = True |
| 549 | if not clazz.name.endswith("Activity"): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 550 | error(clazz, None, "CL4", "Inconsistent class name; should be FooActivity") |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 551 | |
| 552 | if test_methods: |
| 553 | for m in clazz.methods: |
| 554 | if "final" in m.split: continue |
| 555 | if not re.match("on[A-Z]", m.name): |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 556 | if "abstract" in m.split: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 557 | error(clazz, m, None, "Methods implemented by developers must be named onFoo()") |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 558 | else: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 559 | 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] | 560 | |
| 561 | |
| 562 | def verify_builder(clazz): |
| 563 | """Verify builder classes. |
| 564 | Methods should return the builder to enable chaining.""" |
| 565 | if " extends " in clazz.raw: return |
| 566 | if not clazz.name.endswith("Builder"): return |
| 567 | |
| 568 | if clazz.name != "Builder": |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 569 | warn(clazz, None, None, "Builder should be defined as inner class") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 570 | |
| 571 | has_build = False |
| 572 | for m in clazz.methods: |
| 573 | if m.name == "build": |
| 574 | has_build = True |
| 575 | continue |
| 576 | |
| 577 | if m.name.startswith("get"): continue |
| 578 | if m.name.startswith("clear"): continue |
| 579 | |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 580 | if m.name.startswith("with"): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 581 | error(clazz, m, None, "Builder methods names must follow setFoo() style") |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 582 | |
| 583 | if m.name.startswith("set"): |
| 584 | if not m.typ.endswith(clazz.fullname): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 585 | warn(clazz, m, "M4", "Methods should return the builder") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 586 | |
| 587 | if not has_build: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 588 | warn(clazz, None, None, "Missing build() method") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 589 | |
| 590 | |
| 591 | def verify_aidl(clazz): |
| 592 | """Catch people exposing raw AIDL.""" |
Jeff Sharkey | 932a07c | 2014-08-28 16:16:02 -0700 | [diff] [blame] | 593 | if "extends android.os.Binder" in clazz.raw or "implements android.os.IInterface" in clazz.raw: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 594 | error(clazz, None, None, "Exposing raw AIDL interface") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 595 | |
| 596 | |
Jeff Sharkey | 932a07c | 2014-08-28 16:16:02 -0700 | [diff] [blame] | 597 | def verify_internal(clazz): |
| 598 | """Catch people exposing internal classes.""" |
| 599 | if clazz.pkg.name.startswith("com.android"): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 600 | error(clazz, None, None, "Exposing internal class") |
Jeff Sharkey | 932a07c | 2014-08-28 16:16:02 -0700 | [diff] [blame] | 601 | |
| 602 | |
| 603 | def verify_layering(clazz): |
| 604 | """Catch package layering violations. |
| 605 | For example, something in android.os depending on android.app.""" |
| 606 | ranking = [ |
| 607 | ["android.service","android.accessibilityservice","android.inputmethodservice","android.printservice","android.appwidget","android.webkit","android.preference","android.gesture","android.print"], |
| 608 | "android.app", |
| 609 | "android.widget", |
| 610 | "android.view", |
| 611 | "android.animation", |
| 612 | "android.provider", |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 613 | ["android.content","android.graphics.drawable"], |
Jeff Sharkey | 932a07c | 2014-08-28 16:16:02 -0700 | [diff] [blame] | 614 | "android.database", |
| 615 | "android.graphics", |
| 616 | "android.text", |
| 617 | "android.os", |
| 618 | "android.util" |
| 619 | ] |
| 620 | |
| 621 | def rank(p): |
| 622 | for i in range(len(ranking)): |
| 623 | if isinstance(ranking[i], list): |
| 624 | for j in ranking[i]: |
| 625 | if p.startswith(j): return i |
| 626 | else: |
| 627 | if p.startswith(ranking[i]): return i |
| 628 | |
| 629 | cr = rank(clazz.pkg.name) |
| 630 | if cr is None: return |
| 631 | |
| 632 | for f in clazz.fields: |
| 633 | ir = rank(f.typ) |
| 634 | if ir and ir < cr: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 635 | warn(clazz, f, "FW6", "Field type violates package layering") |
Jeff Sharkey | 932a07c | 2014-08-28 16:16:02 -0700 | [diff] [blame] | 636 | |
| 637 | for m in clazz.methods: |
| 638 | ir = rank(m.typ) |
| 639 | if ir and ir < cr: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 640 | warn(clazz, m, "FW6", "Method return type violates package layering") |
Jeff Sharkey | 932a07c | 2014-08-28 16:16:02 -0700 | [diff] [blame] | 641 | for arg in m.args: |
| 642 | ir = rank(arg) |
| 643 | if ir and ir < cr: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 644 | warn(clazz, m, "FW6", "Method argument type violates package layering") |
Jeff Sharkey | 932a07c | 2014-08-28 16:16:02 -0700 | [diff] [blame] | 645 | |
| 646 | |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 647 | def verify_boolean(clazz, api): |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 648 | """Catches people returning boolean from getFoo() style methods. |
| 649 | Ignores when matching setFoo() is present.""" |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 650 | |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 651 | methods = [ m.name for m in clazz.methods ] |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 652 | |
| 653 | builder = clazz.fullname + ".Builder" |
| 654 | builder_methods = [] |
| 655 | if builder in api: |
| 656 | builder_methods = [ m.name for m in api[builder].methods ] |
| 657 | |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 658 | for m in clazz.methods: |
| 659 | if m.typ == "boolean" and m.name.startswith("get") and m.name != "get" and len(m.args) == 0: |
| 660 | setter = "set" + m.name[3:] |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 661 | if setter in methods: |
| 662 | pass |
| 663 | elif builder is not None and setter in builder_methods: |
| 664 | pass |
| 665 | else: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 666 | warn(clazz, m, None, "Methods returning boolean should be named isFoo, hasFoo, areFoo") |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 667 | |
| 668 | |
| 669 | def verify_collections(clazz): |
| 670 | """Verifies that collection types are interfaces.""" |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 671 | if clazz.fullname == "android.os.Bundle": return |
| 672 | |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 673 | bad = ["java.util.Vector", "java.util.LinkedList", "java.util.ArrayList", "java.util.Stack", |
| 674 | "java.util.HashMap", "java.util.HashSet", "android.util.ArraySet", "android.util.ArrayMap"] |
| 675 | for m in clazz.methods: |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 676 | if m.typ in bad: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 677 | error(clazz, m, "CL2", "Return type is concrete collection; should be interface") |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 678 | for arg in m.args: |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 679 | if arg in bad: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 680 | error(clazz, m, "CL2", "Argument is concrete collection; should be interface") |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 681 | |
| 682 | |
| 683 | def verify_flags(clazz): |
| 684 | """Verifies that flags are non-overlapping.""" |
| 685 | known = collections.defaultdict(int) |
| 686 | for f in clazz.fields: |
| 687 | if "FLAG_" in f.name: |
| 688 | try: |
| 689 | val = int(f.value) |
| 690 | except: |
| 691 | continue |
| 692 | |
| 693 | scope = f.name[0:f.name.index("FLAG_")] |
| 694 | if val & known[scope]: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 695 | error(clazz, f, "C1", "Found overlapping flag constant value") |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 696 | known[scope] |= val |
| 697 | |
| 698 | |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 699 | def verify_exception(clazz): |
| 700 | """Verifies that methods don't throw generic exceptions.""" |
| 701 | for m in clazz.methods: |
| 702 | if "throws java.lang.Exception" in m.raw or "throws java.lang.Throwable" in m.raw or "throws java.lang.Error" in m.raw: |
| 703 | error(clazz, m, "S1", "Methods must not throw generic exceptions") |
| 704 | |
| 705 | |
| 706 | def verify_google(clazz): |
| 707 | """Verifies that APIs never reference Google.""" |
| 708 | |
| 709 | if re.search("google", clazz.raw, re.IGNORECASE): |
| 710 | error(clazz, None, None, "Must never reference Google") |
| 711 | |
| 712 | test = [] |
| 713 | test.extend(clazz.ctors) |
| 714 | test.extend(clazz.fields) |
| 715 | test.extend(clazz.methods) |
| 716 | |
| 717 | for t in test: |
| 718 | if re.search("google", t.raw, re.IGNORECASE): |
| 719 | error(clazz, t, None, "Must never reference Google") |
| 720 | |
| 721 | |
| 722 | def verify_bitset(clazz): |
| 723 | """Verifies that we avoid using heavy BitSet.""" |
| 724 | |
| 725 | for f in clazz.fields: |
| 726 | if f.typ == "java.util.BitSet": |
| 727 | error(clazz, f, None, "Field type must not be heavy BitSet") |
| 728 | |
| 729 | for m in clazz.methods: |
| 730 | if m.typ == "java.util.BitSet": |
| 731 | error(clazz, m, None, "Return type must not be heavy BitSet") |
| 732 | for arg in m.args: |
| 733 | if arg == "java.util.BitSet": |
| 734 | error(clazz, m, None, "Argument type must not be heavy BitSet") |
| 735 | |
| 736 | |
| 737 | def verify_manager(clazz): |
| 738 | """Verifies that FooManager is only obtained from Context.""" |
| 739 | |
| 740 | if not clazz.name.endswith("Manager"): return |
| 741 | |
| 742 | for c in clazz.ctors: |
| 743 | error(clazz, c, None, "Managers must always be obtained from Context") |
| 744 | |
| 745 | |
| 746 | def verify_boxed(clazz): |
| 747 | """Verifies that methods avoid boxed primitives.""" |
| 748 | |
| 749 | boxed = ["java.lang.Number", "java.lang.Byte","java.lang.Double","java.lang.Float","java.lang.Integer","java.lang.Long","java.lang.Short"] |
| 750 | |
| 751 | for c in clazz.ctors: |
| 752 | for arg in c.args: |
| 753 | if arg in boxed: |
| 754 | error(clazz, c, None, "Must avoid boxed primitives") |
| 755 | |
| 756 | for f in clazz.fields: |
| 757 | if f.typ in boxed: |
| 758 | error(clazz, f, None, "Must avoid boxed primitives") |
| 759 | |
| 760 | for m in clazz.methods: |
| 761 | if m.typ in boxed: |
| 762 | error(clazz, m, None, "Must avoid boxed primitives") |
| 763 | for arg in m.args: |
| 764 | if arg in boxed: |
| 765 | error(clazz, m, None, "Must avoid boxed primitives") |
| 766 | |
| 767 | |
| 768 | def verify_static_utils(clazz): |
| 769 | """Verifies that helper classes can't be constructed.""" |
| 770 | if clazz.fullname.startswith("android.opengl"): return |
| 771 | if re.match("android\.R\.[a-z]+", clazz.fullname): return |
| 772 | |
| 773 | if len(clazz.fields) > 0: return |
| 774 | if len(clazz.methods) == 0: return |
| 775 | |
| 776 | for m in clazz.methods: |
| 777 | if "static" not in m.split: |
| 778 | return |
| 779 | |
| 780 | # At this point, we have no fields, and all methods are static |
| 781 | if len(clazz.ctors) > 0: |
| 782 | error(clazz, None, None, "Fully-static utility classes must not have constructor") |
| 783 | |
| 784 | |
Jeff Sharkey | 037458a | 2014-09-04 15:46:20 -0700 | [diff] [blame] | 785 | def verify_style(api): |
| 786 | """Find all style issues in the given API level.""" |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 787 | global failures |
| 788 | |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 789 | failures = {} |
| 790 | for key in sorted(api.keys()): |
| 791 | clazz = api[key] |
| 792 | |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 793 | if clazz.pkg.name.startswith("java"): continue |
| 794 | if clazz.pkg.name.startswith("junit"): continue |
| 795 | if clazz.pkg.name.startswith("org.apache"): continue |
| 796 | if clazz.pkg.name.startswith("org.xml"): continue |
| 797 | if clazz.pkg.name.startswith("org.json"): continue |
| 798 | if clazz.pkg.name.startswith("org.w3c"): continue |
| 799 | |
| 800 | verify_constants(clazz) |
| 801 | verify_enums(clazz) |
| 802 | verify_class_names(clazz) |
| 803 | verify_method_names(clazz) |
| 804 | verify_callbacks(clazz) |
| 805 | verify_listeners(clazz) |
| 806 | verify_actions(clazz) |
| 807 | verify_extras(clazz) |
| 808 | verify_equals(clazz) |
| 809 | verify_parcelable(clazz) |
| 810 | verify_protected(clazz) |
| 811 | verify_fields(clazz) |
| 812 | verify_register(clazz) |
| 813 | verify_sync(clazz) |
| 814 | verify_intent_builder(clazz) |
| 815 | verify_helper_classes(clazz) |
| 816 | verify_builder(clazz) |
| 817 | verify_aidl(clazz) |
Jeff Sharkey | 932a07c | 2014-08-28 16:16:02 -0700 | [diff] [blame] | 818 | verify_internal(clazz) |
| 819 | verify_layering(clazz) |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 820 | verify_boolean(clazz, api) |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 821 | verify_collections(clazz) |
| 822 | verify_flags(clazz) |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 823 | verify_exception(clazz) |
| 824 | verify_google(clazz) |
| 825 | verify_bitset(clazz) |
| 826 | verify_manager(clazz) |
| 827 | verify_boxed(clazz) |
| 828 | verify_static_utils(clazz) |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 829 | |
| 830 | return failures |
| 831 | |
| 832 | |
Jeff Sharkey | 037458a | 2014-09-04 15:46:20 -0700 | [diff] [blame] | 833 | def verify_compat(cur, prev): |
| 834 | """Find any incompatible API changes between two levels.""" |
| 835 | global failures |
| 836 | |
| 837 | def class_exists(api, test): |
| 838 | return test.fullname in api |
| 839 | |
| 840 | def ctor_exists(api, clazz, test): |
| 841 | for m in clazz.ctors: |
| 842 | if m.ident == test.ident: return True |
| 843 | return False |
| 844 | |
| 845 | def all_methods(api, clazz): |
| 846 | methods = list(clazz.methods) |
| 847 | if clazz.extends is not None: |
| 848 | methods.extend(all_methods(api, api[clazz.extends])) |
| 849 | return methods |
| 850 | |
| 851 | def method_exists(api, clazz, test): |
| 852 | methods = all_methods(api, clazz) |
| 853 | for m in methods: |
| 854 | if m.ident == test.ident: return True |
| 855 | return False |
| 856 | |
| 857 | def field_exists(api, clazz, test): |
| 858 | for f in clazz.fields: |
| 859 | if f.ident == test.ident: return True |
| 860 | return False |
| 861 | |
| 862 | failures = {} |
| 863 | for key in sorted(prev.keys()): |
| 864 | prev_clazz = prev[key] |
| 865 | |
| 866 | if not class_exists(cur, prev_clazz): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 867 | error(prev_clazz, None, None, "Class removed or incompatible change") |
Jeff Sharkey | 037458a | 2014-09-04 15:46:20 -0700 | [diff] [blame] | 868 | continue |
| 869 | |
| 870 | cur_clazz = cur[key] |
| 871 | |
| 872 | for test in prev_clazz.ctors: |
| 873 | if not ctor_exists(cur, cur_clazz, test): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 874 | error(prev_clazz, prev_ctor, None, "Constructor removed or incompatible change") |
Jeff Sharkey | 037458a | 2014-09-04 15:46:20 -0700 | [diff] [blame] | 875 | |
| 876 | methods = all_methods(prev, prev_clazz) |
| 877 | for test in methods: |
| 878 | if not method_exists(cur, cur_clazz, test): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 879 | error(prev_clazz, test, None, "Method removed or incompatible change") |
Jeff Sharkey | 037458a | 2014-09-04 15:46:20 -0700 | [diff] [blame] | 880 | |
| 881 | for test in prev_clazz.fields: |
| 882 | if not field_exists(cur, cur_clazz, test): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame^] | 883 | error(prev_clazz, test, None, "Field removed or incompatible change") |
Jeff Sharkey | 037458a | 2014-09-04 15:46:20 -0700 | [diff] [blame] | 884 | |
| 885 | return failures |
| 886 | |
| 887 | |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 888 | if __name__ == "__main__": |
| 889 | cur = parse_api_file(sys.argv[1]) |
| 890 | cur_fail = verify_style(cur) |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 891 | |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 892 | if len(sys.argv) > 2: |
| 893 | prev = parse_api_file(sys.argv[2]) |
| 894 | prev_fail = verify_style(prev) |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 895 | |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 896 | # ignore errors from previous API level |
| 897 | for p in prev_fail: |
| 898 | if p in cur_fail: |
| 899 | del cur_fail[p] |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 900 | |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 901 | # look for compatibility issues |
| 902 | compat_fail = verify_compat(cur, prev) |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 903 | |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 904 | print "%s API compatibility issues %s\n" % ((format(fg=WHITE, bg=BLUE, bold=True), format(reset=True))) |
| 905 | for f in sorted(compat_fail): |
| 906 | print compat_fail[f] |
| 907 | print |
| 908 | |
| 909 | |
| 910 | print "%s API style issues %s\n" % ((format(fg=WHITE, bg=BLUE, bold=True), format(reset=True))) |
| 911 | for f in sorted(cur_fail): |
| 912 | print cur_fail[f] |
Jeff Sharkey | 037458a | 2014-09-04 15:46:20 -0700 | [diff] [blame] | 913 | print |