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 | |
Adrian Roos | 1f1b6a8 | 2019-01-05 20:09:38 +0100 | [diff] [blame] | 29 | import re, sys, collections, traceback, argparse, itertools |
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(): |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 54 | def __init__(self, clazz, line, raw, blame, sig_format = 1): |
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 | |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 60 | if sig_format == 2: |
| 61 | V2LineParser(raw).parse_into_field(self) |
| 62 | elif sig_format == 1: |
| 63 | # drop generics for now; may need multiple passes |
| 64 | raw = re.sub("<[^<]+?>", "", raw) |
| 65 | raw = re.sub("<[^<]+?>", "", raw) |
Jeff Sharkey | 40d67f4 | 2018-07-17 13:29:40 -0600 | [diff] [blame] | 66 | |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 67 | raw = raw.split() |
| 68 | self.split = list(raw) |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 69 | |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 70 | for r in ["field", "volatile", "transient", "public", "protected", "static", "final", "deprecated"]: |
| 71 | while r in raw: raw.remove(r) |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 72 | |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 73 | # ignore annotations for now |
| 74 | raw = [ r for r in raw if not r.startswith("@") ] |
Jeff Sharkey | 40d67f4 | 2018-07-17 13:29:40 -0600 | [diff] [blame] | 75 | |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 76 | self.typ = raw[0] |
| 77 | self.name = raw[1].strip(";") |
| 78 | if len(raw) >= 4 and raw[2] == "=": |
| 79 | self.value = raw[3].strip(';"') |
| 80 | else: |
| 81 | self.value = None |
| 82 | |
| 83 | self.ident = "-".join((self.typ, self.name, self.value or "")) |
Jeff Sharkey | 037458a | 2014-09-04 15:46:20 -0700 | [diff] [blame] | 84 | |
Jeff Sharkey | c6c6c34 | 2017-11-13 09:13:37 -0700 | [diff] [blame] | 85 | def __hash__(self): |
| 86 | return hash(self.raw) |
| 87 | |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 88 | def __repr__(self): |
| 89 | return self.raw |
| 90 | |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 91 | class Method(): |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 92 | def __init__(self, clazz, line, raw, blame, sig_format = 1): |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 93 | self.clazz = clazz |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 94 | self.line = line |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 95 | self.raw = raw.strip(" {;") |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 96 | self.blame = blame |
| 97 | |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 98 | if sig_format == 2: |
| 99 | V2LineParser(raw).parse_into_method(self) |
| 100 | elif sig_format == 1: |
| 101 | # drop generics for now; may need multiple passes |
| 102 | raw = re.sub("<[^<]+?>", "", raw) |
| 103 | raw = re.sub("<[^<]+?>", "", raw) |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 104 | |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 105 | # handle each clause differently |
| 106 | raw_prefix, raw_args, _, raw_throws = re.match(r"(.*?)\((.*?)\)( throws )?(.*?);$", raw).groups() |
Jeff Sharkey | 40d67f4 | 2018-07-17 13:29:40 -0600 | [diff] [blame] | 107 | |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 108 | # parse prefixes |
| 109 | raw = re.split("[\s]+", raw_prefix) |
| 110 | for r in ["", ";"]: |
| 111 | while r in raw: raw.remove(r) |
| 112 | self.split = list(raw) |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 113 | |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 114 | for r in ["method", "public", "protected", "static", "final", "deprecated", "abstract", "default", "operator", "synchronized"]: |
| 115 | while r in raw: raw.remove(r) |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 116 | |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 117 | self.typ = raw[0] |
| 118 | self.name = raw[1] |
Jeff Sharkey | 40d67f4 | 2018-07-17 13:29:40 -0600 | [diff] [blame] | 119 | |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 120 | # parse args |
| 121 | self.args = [] |
| 122 | for arg in re.split(",\s*", raw_args): |
| 123 | arg = re.split("\s", arg) |
| 124 | # ignore annotations for now |
| 125 | arg = [ a for a in arg if not a.startswith("@") ] |
| 126 | if len(arg[0]) > 0: |
| 127 | self.args.append(arg[0]) |
Jeff Sharkey | 40d67f4 | 2018-07-17 13:29:40 -0600 | [diff] [blame] | 128 | |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 129 | # parse throws |
| 130 | self.throws = [] |
| 131 | for throw in re.split(",\s*", raw_throws): |
| 132 | self.throws.append(throw) |
| 133 | else: |
| 134 | raise ValueError("Unknown signature format: " + sig_format) |
Jeff Sharkey | 40d67f4 | 2018-07-17 13:29:40 -0600 | [diff] [blame] | 135 | |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 136 | self.ident = "-".join((self.typ, self.name, "-".join(self.args))) |
| 137 | |
| 138 | def sig_matches(self, typ, name, args): |
| 139 | return typ == self.typ and name == self.name and args == self.args |
Jeff Sharkey | 037458a | 2014-09-04 15:46:20 -0700 | [diff] [blame] | 140 | |
Jeff Sharkey | c6c6c34 | 2017-11-13 09:13:37 -0700 | [diff] [blame] | 141 | def __hash__(self): |
| 142 | return hash(self.raw) |
| 143 | |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 144 | def __repr__(self): |
| 145 | return self.raw |
| 146 | |
| 147 | |
| 148 | class Class(): |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 149 | def __init__(self, pkg, line, raw, blame, sig_format = 1): |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 150 | self.pkg = pkg |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 151 | self.line = line |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 152 | self.raw = raw.strip(" {;") |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 153 | self.blame = blame |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 154 | self.ctors = [] |
| 155 | self.fields = [] |
| 156 | self.methods = [] |
| 157 | |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 158 | if sig_format == 2: |
| 159 | V2LineParser(raw).parse_into_class(self) |
| 160 | elif sig_format == 1: |
| 161 | # drop generics for now; may need multiple passes |
| 162 | raw = re.sub("<[^<]+?>", "", raw) |
| 163 | raw = re.sub("<[^<]+?>", "", raw) |
Jeff Sharkey | 40d67f4 | 2018-07-17 13:29:40 -0600 | [diff] [blame] | 164 | |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 165 | raw = raw.split() |
| 166 | self.split = list(raw) |
| 167 | if "class" in raw: |
| 168 | self.fullname = raw[raw.index("class")+1] |
| 169 | elif "interface" in raw: |
| 170 | self.fullname = raw[raw.index("interface")+1] |
| 171 | elif "@interface" in raw: |
| 172 | self.fullname = raw[raw.index("@interface")+1] |
| 173 | else: |
| 174 | raise ValueError("Funky class type %s" % (self.raw)) |
Jeff Sharkey | 037458a | 2014-09-04 15:46:20 -0700 | [diff] [blame] | 175 | |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 176 | if "extends" in raw: |
| 177 | self.extends = raw[raw.index("extends")+1] |
| 178 | else: |
| 179 | self.extends = None |
| 180 | |
| 181 | if "implements" in raw: |
| 182 | self.implements = raw[raw.index("implements")+1] |
| 183 | else: |
| 184 | self.implements = None |
Jeff Sharkey | 037458a | 2014-09-04 15:46:20 -0700 | [diff] [blame] | 185 | else: |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 186 | raise ValueError("Unknown signature format: " + sig_format) |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 187 | |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 188 | self.fullname = self.pkg.name + "." + self.fullname |
Jeff Sharkey | 047d7f0 | 2015-02-25 11:27:55 -0800 | [diff] [blame] | 189 | self.fullname_path = self.fullname.split(".") |
| 190 | |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 191 | if self.extends is not None: |
| 192 | self.extends_path = self.extends.split(".") |
| 193 | else: |
| 194 | self.extends_path = [] |
| 195 | |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 196 | self.name = self.fullname[self.fullname.rindex(".")+1:] |
| 197 | |
Adrian Roos | 6eb57b0 | 2018-12-13 22:08:29 +0100 | [diff] [blame] | 198 | def merge_from(self, other): |
| 199 | self.ctors.extend(other.ctors) |
| 200 | self.fields.extend(other.fields) |
| 201 | self.methods.extend(other.methods) |
| 202 | |
Jeff Sharkey | c6c6c34 | 2017-11-13 09:13:37 -0700 | [diff] [blame] | 203 | def __hash__(self): |
| 204 | return hash((self.raw, tuple(self.ctors), tuple(self.fields), tuple(self.methods))) |
| 205 | |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 206 | def __repr__(self): |
| 207 | return self.raw |
| 208 | |
| 209 | |
| 210 | class Package(): |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 211 | def __init__(self, line, raw, blame): |
| 212 | self.line = line |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 213 | self.raw = raw.strip(" {;") |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 214 | self.blame = blame |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 215 | |
| 216 | raw = raw.split() |
| 217 | self.name = raw[raw.index("package")+1] |
Jeff Sharkey | 047d7f0 | 2015-02-25 11:27:55 -0800 | [diff] [blame] | 218 | self.name_path = self.name.split(".") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 219 | |
| 220 | def __repr__(self): |
| 221 | return self.raw |
| 222 | |
Adrian Roos | e5eeae7 | 2019-01-04 20:10:06 +0100 | [diff] [blame] | 223 | class V2Tokenizer(object): |
| 224 | __slots__ = ["raw"] |
| 225 | |
Adrian Roos | 5cdfb69 | 2019-01-05 22:04:55 +0100 | [diff] [blame] | 226 | DELIMITER = re.compile(r'\s+|[()@<>;,={}/"!?]|\[\]|\.\.\.') |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 227 | STRING_SPECIAL = re.compile(r'["\\]') |
| 228 | |
| 229 | def __init__(self, raw): |
| 230 | self.raw = raw |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 231 | |
Adrian Roos | e5eeae7 | 2019-01-04 20:10:06 +0100 | [diff] [blame] | 232 | def tokenize(self): |
| 233 | tokens = [] |
| 234 | current = 0 |
| 235 | raw = self.raw |
| 236 | length = len(raw) |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 237 | |
Adrian Roos | e5eeae7 | 2019-01-04 20:10:06 +0100 | [diff] [blame] | 238 | while current < length: |
| 239 | while current < length: |
| 240 | start = current |
| 241 | match = V2Tokenizer.DELIMITER.search(raw, start) |
| 242 | if match is not None: |
| 243 | match_start = match.start() |
| 244 | if match_start == current: |
| 245 | end = match.end() |
| 246 | else: |
| 247 | end = match_start |
| 248 | else: |
| 249 | end = length |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 250 | |
Adrian Roos | e5eeae7 | 2019-01-04 20:10:06 +0100 | [diff] [blame] | 251 | token = raw[start:end] |
| 252 | current = end |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 253 | |
Adrian Roos | e5eeae7 | 2019-01-04 20:10:06 +0100 | [diff] [blame] | 254 | if token == "" or token[0] == " ": |
| 255 | continue |
| 256 | else: |
| 257 | break |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 258 | |
Adrian Roos | e5eeae7 | 2019-01-04 20:10:06 +0100 | [diff] [blame] | 259 | if token == "@": |
| 260 | if raw[start:start+11] == "@interface ": |
| 261 | current = start + 11 |
| 262 | tokens.append("@interface") |
| 263 | continue |
| 264 | elif token == '/': |
| 265 | if raw[start:start+2] == "//": |
| 266 | current = length |
| 267 | continue |
| 268 | elif token == '"': |
| 269 | current, string_token = self.tokenize_string(raw, length, current) |
| 270 | tokens.append(token + string_token) |
| 271 | continue |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 272 | |
Adrian Roos | e5eeae7 | 2019-01-04 20:10:06 +0100 | [diff] [blame] | 273 | tokens.append(token) |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 274 | |
Adrian Roos | e5eeae7 | 2019-01-04 20:10:06 +0100 | [diff] [blame] | 275 | return tokens |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 276 | |
Adrian Roos | e5eeae7 | 2019-01-04 20:10:06 +0100 | [diff] [blame] | 277 | def tokenize_string(self, raw, length, current): |
| 278 | start = current |
| 279 | end = length |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 280 | while start < end: |
Adrian Roos | e5eeae7 | 2019-01-04 20:10:06 +0100 | [diff] [blame] | 281 | match = V2Tokenizer.STRING_SPECIAL.search(raw, start) |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 282 | if match: |
| 283 | if match.group() == '"': |
| 284 | end = match.end() |
| 285 | break |
| 286 | elif match.group() == '\\': |
| 287 | # ignore whatever is after the slash |
| 288 | start += 2 |
| 289 | else: |
| 290 | raise ValueError("Unexpected match: `%s`" % (match.group())) |
| 291 | else: |
Adrian Roos | e5eeae7 | 2019-01-04 20:10:06 +0100 | [diff] [blame] | 292 | raise ValueError("Unexpected EOF tokenizing string: `%s`" % (raw[current - 1:],)) |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 293 | |
Adrian Roos | e5eeae7 | 2019-01-04 20:10:06 +0100 | [diff] [blame] | 294 | token = raw[current:end] |
| 295 | return end, token |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 296 | |
Adrian Roos | e5eeae7 | 2019-01-04 20:10:06 +0100 | [diff] [blame] | 297 | class V2LineParser(object): |
| 298 | __slots__ = ["tokenized", "current", "len"] |
| 299 | |
Adrian Roos | 258c572 | 2019-01-21 15:43:15 +0100 | [diff] [blame] | 300 | FIELD_KINDS = ("field", "property", "enum_constant") |
Adrian Roos | d1e3892 | 2019-01-14 15:44:15 +0100 | [diff] [blame] | 301 | MODIFIERS = set("public protected internal private abstract default static final transient volatile synchronized native operator sealed strictfp infix inline suspend vararg".split()) |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 302 | JAVA_LANG_TYPES = set("AbstractMethodError AbstractStringBuilder Appendable ArithmeticException ArrayIndexOutOfBoundsException ArrayStoreException AssertionError AutoCloseable Boolean BootstrapMethodError Byte Character CharSequence Class ClassCastException ClassCircularityError ClassFormatError ClassLoader ClassNotFoundException Cloneable CloneNotSupportedException Comparable Compiler Deprecated Double Enum EnumConstantNotPresentException Error Exception ExceptionInInitializerError Float FunctionalInterface IllegalAccessError IllegalAccessException IllegalArgumentException IllegalMonitorStateException IllegalStateException IllegalThreadStateException IncompatibleClassChangeError IndexOutOfBoundsException InheritableThreadLocal InstantiationError InstantiationException Integer InternalError InterruptedException Iterable LinkageError Long Math NegativeArraySizeException NoClassDefFoundError NoSuchFieldError NoSuchFieldException NoSuchMethodError NoSuchMethodException NullPointerException Number NumberFormatException Object OutOfMemoryError Override Package package-info.java Process ProcessBuilder ProcessEnvironment ProcessImpl Readable ReflectiveOperationException Runnable Runtime RuntimeException RuntimePermission SafeVarargs SecurityException SecurityManager Short StackOverflowError StackTraceElement StrictMath String StringBuffer StringBuilder StringIndexOutOfBoundsException SuppressWarnings System Thread ThreadDeath ThreadGroup ThreadLocal Throwable TypeNotPresentException UNIXProcess UnknownError UnsatisfiedLinkError UnsupportedClassVersionError UnsupportedOperationException VerifyError VirtualMachineError Void".split()) |
| 303 | |
| 304 | def __init__(self, raw): |
Adrian Roos | e5eeae7 | 2019-01-04 20:10:06 +0100 | [diff] [blame] | 305 | self.tokenized = V2Tokenizer(raw).tokenize() |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 306 | self.current = 0 |
Adrian Roos | e5eeae7 | 2019-01-04 20:10:06 +0100 | [diff] [blame] | 307 | self.len = len(self.tokenized) |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 308 | |
| 309 | def parse_into_method(self, method): |
| 310 | method.split = [] |
| 311 | kind = self.parse_one_of("ctor", "method") |
| 312 | method.split.append(kind) |
| 313 | annotations = self.parse_annotations() |
| 314 | method.split.extend(self.parse_modifiers()) |
| 315 | self.parse_matching_paren("<", ">") |
| 316 | if "@Deprecated" in annotations: |
| 317 | method.split.append("deprecated") |
| 318 | if kind == "ctor": |
| 319 | method.typ = "ctor" |
| 320 | else: |
| 321 | method.typ = self.parse_type() |
| 322 | method.split.append(method.typ) |
| 323 | method.name = self.parse_name() |
| 324 | method.split.append(method.name) |
| 325 | self.parse_token("(") |
| 326 | method.args = self.parse_args() |
| 327 | self.parse_token(")") |
| 328 | method.throws = self.parse_throws() |
| 329 | if "@interface" in method.clazz.split: |
| 330 | self.parse_annotation_default() |
| 331 | self.parse_token(";") |
| 332 | self.parse_eof() |
| 333 | |
| 334 | def parse_into_class(self, clazz): |
| 335 | clazz.split = [] |
| 336 | annotations = self.parse_annotations() |
| 337 | if "@Deprecated" in annotations: |
| 338 | clazz.split.append("deprecated") |
| 339 | clazz.split.extend(self.parse_modifiers()) |
| 340 | kind = self.parse_one_of("class", "interface", "@interface", "enum") |
| 341 | if kind == "enum": |
| 342 | # enums are implicitly final |
| 343 | clazz.split.append("final") |
| 344 | clazz.split.append(kind) |
| 345 | clazz.fullname = self.parse_name() |
| 346 | self.parse_matching_paren("<", ">") |
| 347 | extends = self.parse_extends() |
| 348 | clazz.extends = extends[0] if extends else None |
| 349 | implements = self.parse_implements() |
| 350 | clazz.implements = implements[0] if implements else None |
| 351 | # The checks assume that interfaces are always found in implements, which isn't true for |
| 352 | # subinterfaces. |
| 353 | if not implements and "interface" in clazz.split: |
| 354 | clazz.implements = clazz.extends |
| 355 | self.parse_token("{") |
| 356 | self.parse_eof() |
| 357 | |
| 358 | def parse_into_field(self, field): |
Adrian Roos | 258c572 | 2019-01-21 15:43:15 +0100 | [diff] [blame] | 359 | kind = self.parse_one_of(*V2LineParser.FIELD_KINDS) |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 360 | field.split = [kind] |
| 361 | annotations = self.parse_annotations() |
| 362 | if "@Deprecated" in annotations: |
| 363 | field.split.append("deprecated") |
| 364 | field.split.extend(self.parse_modifiers()) |
| 365 | field.typ = self.parse_type() |
| 366 | field.split.append(field.typ) |
| 367 | field.name = self.parse_name() |
| 368 | field.split.append(field.name) |
| 369 | if self.parse_if("="): |
| 370 | field.value = self.parse_value_stripped() |
| 371 | else: |
| 372 | field.value = None |
| 373 | |
| 374 | self.parse_token(";") |
| 375 | self.parse_eof() |
| 376 | |
| 377 | def lookahead(self): |
| 378 | return self.tokenized[self.current] |
| 379 | |
| 380 | def parse_one_of(self, *options): |
| 381 | found = self.lookahead() |
| 382 | if found not in options: |
| 383 | raise ValueError("Parsing failed, expected one of `%s` but found `%s` in %s" % (options, found, repr(self.tokenized))) |
| 384 | return self.parse_token() |
| 385 | |
| 386 | def parse_token(self, tok = None): |
| 387 | found = self.lookahead() |
| 388 | if tok is not None and found != tok: |
| 389 | raise ValueError("Parsing failed, expected `%s` but found `%s` in %s" % (tok, found, repr(self.tokenized))) |
| 390 | self.current += 1 |
| 391 | return found |
| 392 | |
| 393 | def eof(self): |
Adrian Roos | e5eeae7 | 2019-01-04 20:10:06 +0100 | [diff] [blame] | 394 | return self.current == self.len |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 395 | |
| 396 | def parse_eof(self): |
| 397 | if not self.eof(): |
| 398 | raise ValueError("Parsing failed, expected EOF, but %s has not been parsed in %s" % (self.tokenized[self.current:], self.tokenized)) |
| 399 | |
| 400 | def parse_if(self, tok): |
| 401 | if not self.eof() and self.lookahead() == tok: |
| 402 | self.parse_token() |
| 403 | return True |
| 404 | return False |
| 405 | |
| 406 | def parse_annotations(self): |
| 407 | ret = [] |
| 408 | while self.lookahead() == "@": |
| 409 | ret.append(self.parse_annotation()) |
| 410 | return ret |
| 411 | |
| 412 | def parse_annotation(self): |
| 413 | ret = self.parse_token("@") + self.parse_token() |
| 414 | self.parse_matching_paren("(", ")") |
| 415 | return ret |
| 416 | |
| 417 | def parse_matching_paren(self, open, close): |
| 418 | start = self.current |
| 419 | if not self.parse_if(open): |
| 420 | return |
| 421 | length = len(self.tokenized) |
| 422 | count = 1 |
| 423 | while count > 0: |
| 424 | if self.current == length: |
| 425 | raise ValueError("Unexpected EOF looking for closing paren: `%s`" % (self.tokenized[start:],)) |
| 426 | t = self.parse_token() |
| 427 | if t == open: |
| 428 | count += 1 |
| 429 | elif t == close: |
| 430 | count -= 1 |
| 431 | return self.tokenized[start:self.current] |
| 432 | |
| 433 | def parse_modifiers(self): |
| 434 | ret = [] |
| 435 | while self.lookahead() in V2LineParser.MODIFIERS: |
| 436 | ret.append(self.parse_token()) |
| 437 | return ret |
| 438 | |
Adrian Roos | 5cdfb69 | 2019-01-05 22:04:55 +0100 | [diff] [blame] | 439 | def parse_kotlin_nullability(self): |
| 440 | t = self.lookahead() |
| 441 | if t == "?" or t == "!": |
| 442 | return self.parse_token() |
| 443 | return None |
| 444 | |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 445 | def parse_type(self): |
Adrian Roos | d1e3892 | 2019-01-14 15:44:15 +0100 | [diff] [blame] | 446 | self.parse_annotations() |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 447 | type = self.parse_token() |
Adrian Roos | d1e3892 | 2019-01-14 15:44:15 +0100 | [diff] [blame] | 448 | if type[-1] == '.': |
| 449 | self.parse_annotations() |
| 450 | type += self.parse_token() |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 451 | if type in V2LineParser.JAVA_LANG_TYPES: |
| 452 | type = "java.lang." + type |
| 453 | self.parse_matching_paren("<", ">") |
Adrian Roos | 5cdfb69 | 2019-01-05 22:04:55 +0100 | [diff] [blame] | 454 | while True: |
| 455 | t = self.lookahead() |
Adrian Roos | d1e3892 | 2019-01-14 15:44:15 +0100 | [diff] [blame] | 456 | if t == "@": |
| 457 | self.parse_annotation() |
| 458 | elif t == "[]": |
Adrian Roos | 5cdfb69 | 2019-01-05 22:04:55 +0100 | [diff] [blame] | 459 | type += self.parse_token() |
| 460 | elif self.parse_kotlin_nullability() is not None: |
| 461 | pass # discard nullability for now |
| 462 | else: |
| 463 | break |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 464 | return type |
| 465 | |
| 466 | def parse_arg_type(self): |
| 467 | type = self.parse_type() |
| 468 | if self.parse_if("..."): |
| 469 | type += "..." |
Adrian Roos | 5cdfb69 | 2019-01-05 22:04:55 +0100 | [diff] [blame] | 470 | self.parse_kotlin_nullability() # discard nullability for now |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 471 | return type |
| 472 | |
| 473 | def parse_name(self): |
| 474 | return self.parse_token() |
| 475 | |
| 476 | def parse_args(self): |
| 477 | args = [] |
| 478 | if self.lookahead() == ")": |
| 479 | return args |
| 480 | |
| 481 | while True: |
| 482 | args.append(self.parse_arg()) |
| 483 | if self.lookahead() == ")": |
| 484 | return args |
| 485 | self.parse_token(",") |
| 486 | |
| 487 | def parse_arg(self): |
Adrian Roos | d1e3892 | 2019-01-14 15:44:15 +0100 | [diff] [blame] | 488 | self.parse_if("vararg") # kotlin vararg |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 489 | self.parse_annotations() |
Adrian Roos | 5cdfb69 | 2019-01-05 22:04:55 +0100 | [diff] [blame] | 490 | type = self.parse_arg_type() |
| 491 | l = self.lookahead() |
| 492 | if l != "," and l != ")": |
Adrian Roos | d1e3892 | 2019-01-14 15:44:15 +0100 | [diff] [blame] | 493 | if self.lookahead() != '=': |
| 494 | self.parse_token() # kotlin argument name |
Adrian Roos | 5cdfb69 | 2019-01-05 22:04:55 +0100 | [diff] [blame] | 495 | if self.parse_if('='): # kotlin default value |
Adrian Roos | d1e3892 | 2019-01-14 15:44:15 +0100 | [diff] [blame] | 496 | self.parse_expression() |
Adrian Roos | 5cdfb69 | 2019-01-05 22:04:55 +0100 | [diff] [blame] | 497 | return type |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 498 | |
Adrian Roos | d1e3892 | 2019-01-14 15:44:15 +0100 | [diff] [blame] | 499 | def parse_expression(self): |
| 500 | while not self.lookahead() in [')', ',', ';']: |
| 501 | (self.parse_matching_paren('(', ')') or |
| 502 | self.parse_matching_paren('{', '}') or |
| 503 | self.parse_token()) |
| 504 | |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 505 | def parse_throws(self): |
| 506 | ret = [] |
| 507 | if self.parse_if("throws"): |
| 508 | ret.append(self.parse_type()) |
| 509 | while self.parse_if(","): |
| 510 | ret.append(self.parse_type()) |
| 511 | return ret |
| 512 | |
| 513 | def parse_extends(self): |
| 514 | if self.parse_if("extends"): |
| 515 | return self.parse_space_delimited_type_list() |
| 516 | return [] |
| 517 | |
| 518 | def parse_implements(self): |
| 519 | if self.parse_if("implements"): |
| 520 | return self.parse_space_delimited_type_list() |
| 521 | return [] |
| 522 | |
| 523 | def parse_space_delimited_type_list(self, terminals = ["implements", "{"]): |
| 524 | types = [] |
| 525 | while True: |
| 526 | types.append(self.parse_type()) |
| 527 | if self.lookahead() in terminals: |
| 528 | return types |
| 529 | |
| 530 | def parse_annotation_default(self): |
| 531 | if self.parse_if("default"): |
Adrian Roos | d1e3892 | 2019-01-14 15:44:15 +0100 | [diff] [blame] | 532 | self.parse_expression() |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 533 | |
| 534 | def parse_value(self): |
| 535 | if self.lookahead() == "{": |
| 536 | return " ".join(self.parse_matching_paren("{", "}")) |
| 537 | elif self.lookahead() == "(": |
| 538 | return " ".join(self.parse_matching_paren("(", ")")) |
| 539 | else: |
| 540 | return self.parse_token() |
| 541 | |
| 542 | def parse_value_stripped(self): |
| 543 | value = self.parse_value() |
| 544 | if value[0] == '"': |
| 545 | return value[1:-1] |
| 546 | return value |
| 547 | |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 548 | |
Adrian Roos | 038a029 | 2018-12-19 17:11:21 +0100 | [diff] [blame] | 549 | def _parse_stream(f, clazz_cb=None, base_f=None, out_classes_with_base=None, |
| 550 | in_classes_with_base=[]): |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 551 | api = {} |
Adrian Roos | 038a029 | 2018-12-19 17:11:21 +0100 | [diff] [blame] | 552 | in_classes_with_base = _retry_iterator(in_classes_with_base) |
Adrian Roos | 6eb57b0 | 2018-12-13 22:08:29 +0100 | [diff] [blame] | 553 | |
| 554 | if base_f: |
Adrian Roos | 038a029 | 2018-12-19 17:11:21 +0100 | [diff] [blame] | 555 | base_classes = _retry_iterator(_parse_stream_to_generator(base_f)) |
Adrian Roos | 6eb57b0 | 2018-12-13 22:08:29 +0100 | [diff] [blame] | 556 | else: |
| 557 | base_classes = [] |
| 558 | |
Adrian Roos | 038a029 | 2018-12-19 17:11:21 +0100 | [diff] [blame] | 559 | def handle_class(clazz): |
Adrian Roos | 6eb57b0 | 2018-12-13 22:08:29 +0100 | [diff] [blame] | 560 | if clazz_cb: |
| 561 | clazz_cb(clazz) |
| 562 | else: # In callback mode, don't keep track of the full API |
| 563 | api[clazz.fullname] = clazz |
| 564 | |
Adrian Roos | 038a029 | 2018-12-19 17:11:21 +0100 | [diff] [blame] | 565 | def handle_missed_classes_with_base(clazz): |
| 566 | for c in _yield_until_matching_class(in_classes_with_base, clazz): |
| 567 | base_class = _skip_to_matching_class(base_classes, c) |
| 568 | if base_class: |
| 569 | handle_class(base_class) |
| 570 | |
| 571 | for clazz in _parse_stream_to_generator(f): |
| 572 | # Before looking at clazz, let's see if there's some classes that were not present, but |
| 573 | # may have an entry in the base stream. |
| 574 | handle_missed_classes_with_base(clazz) |
| 575 | |
| 576 | base_class = _skip_to_matching_class(base_classes, clazz) |
| 577 | if base_class: |
| 578 | clazz.merge_from(base_class) |
| 579 | if out_classes_with_base is not None: |
| 580 | out_classes_with_base.append(clazz) |
| 581 | handle_class(clazz) |
| 582 | |
| 583 | handle_missed_classes_with_base(None) |
| 584 | |
Adrian Roos | 6eb57b0 | 2018-12-13 22:08:29 +0100 | [diff] [blame] | 585 | return api |
| 586 | |
| 587 | def _parse_stream_to_generator(f): |
| 588 | line = 0 |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 589 | pkg = None |
| 590 | clazz = None |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 591 | blame = None |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 592 | sig_format = 1 |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 593 | |
| 594 | re_blame = re.compile("^([a-z0-9]{7,}) \(<([^>]+)>.+?\) (.+?)$") |
Adrian Roos | 258c572 | 2019-01-21 15:43:15 +0100 | [diff] [blame] | 595 | |
| 596 | field_prefixes = map(lambda kind: " %s" % (kind,), V2LineParser.FIELD_KINDS) |
| 597 | def startsWithFieldPrefix(raw): |
| 598 | for prefix in field_prefixes: |
| 599 | if raw.startswith(prefix): |
| 600 | return True |
| 601 | return False |
| 602 | |
Jeff Sharkey | a18a2e3 | 2015-02-22 15:54:32 -0800 | [diff] [blame] | 603 | for raw in f: |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 604 | line += 1 |
| 605 | raw = raw.rstrip() |
| 606 | match = re_blame.match(raw) |
| 607 | if match is not None: |
| 608 | blame = match.groups()[0:2] |
| 609 | raw = match.groups()[2] |
| 610 | else: |
| 611 | blame = None |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 612 | |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 613 | if line == 1 and raw == "// Signature format: 2.0": |
| 614 | sig_format = 2 |
| 615 | elif raw.startswith("package"): |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 616 | pkg = Package(line, raw, blame) |
| 617 | elif raw.startswith(" ") and raw.endswith("{"): |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 618 | clazz = Class(pkg, line, raw, blame, sig_format=sig_format) |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 619 | elif raw.startswith(" ctor"): |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 620 | clazz.ctors.append(Method(clazz, line, raw, blame, sig_format=sig_format)) |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 621 | elif raw.startswith(" method"): |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 622 | clazz.methods.append(Method(clazz, line, raw, blame, sig_format=sig_format)) |
Adrian Roos | 258c572 | 2019-01-21 15:43:15 +0100 | [diff] [blame] | 623 | elif startsWithFieldPrefix(raw): |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 624 | clazz.fields.append(Field(clazz, line, raw, blame, sig_format=sig_format)) |
Adrian Roos | 6eb57b0 | 2018-12-13 22:08:29 +0100 | [diff] [blame] | 625 | elif raw.startswith(" }") and clazz: |
Adrian Roos | 038a029 | 2018-12-19 17:11:21 +0100 | [diff] [blame] | 626 | yield clazz |
Jeff Sharkey | a18a2e3 | 2015-02-22 15:54:32 -0800 | [diff] [blame] | 627 | |
Adrian Roos | 5ed42b6 | 2018-12-19 17:10:22 +0100 | [diff] [blame] | 628 | def _retry_iterator(it): |
| 629 | """Wraps an iterator, such that calling send(True) on it will redeliver the same element""" |
| 630 | for e in it: |
| 631 | while True: |
| 632 | retry = yield e |
| 633 | if not retry: |
| 634 | break |
| 635 | # send() was called, asking us to redeliver clazz on next(). Still need to yield |
| 636 | # a dummy value to the send() first though. |
| 637 | if (yield "Returning clazz on next()"): |
| 638 | raise TypeError("send() must be followed by next(), not send()") |
| 639 | |
Adrian Roos | 038a029 | 2018-12-19 17:11:21 +0100 | [diff] [blame] | 640 | def _skip_to_matching_class(classes, needle): |
| 641 | """Takes a classes iterator and consumes entries until it returns the class we're looking for |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 642 | |
Adrian Roos | 6eb57b0 | 2018-12-13 22:08:29 +0100 | [diff] [blame] | 643 | This relies on classes being sorted by package and class name.""" |
| 644 | |
| 645 | for clazz in classes: |
| 646 | if clazz.pkg.name < needle.pkg.name: |
| 647 | # We haven't reached the right package yet |
| 648 | continue |
Adrian Roos | 5ed42b6 | 2018-12-19 17:10:22 +0100 | [diff] [blame] | 649 | if clazz.pkg.name == needle.pkg.name and clazz.fullname < needle.fullname: |
| 650 | # We're in the right package, but not the right class yet |
Adrian Roos | 6eb57b0 | 2018-12-13 22:08:29 +0100 | [diff] [blame] | 651 | continue |
| 652 | if clazz.fullname == needle.fullname: |
| 653 | return clazz |
| 654 | # We ran past the right class. Send it back into the generator, then report failure. |
| 655 | classes.send(clazz) |
| 656 | return None |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 657 | |
Adrian Roos | 038a029 | 2018-12-19 17:11:21 +0100 | [diff] [blame] | 658 | def _yield_until_matching_class(classes, needle): |
| 659 | """Takes a class iterator and yields entries it until it reaches the class we're looking for. |
| 660 | |
| 661 | This relies on classes being sorted by package and class name.""" |
| 662 | |
| 663 | for clazz in classes: |
| 664 | if needle is None: |
| 665 | yield clazz |
| 666 | elif clazz.pkg.name < needle.pkg.name: |
| 667 | # We haven't reached the right package yet |
| 668 | yield clazz |
| 669 | elif clazz.pkg.name == needle.pkg.name and clazz.fullname < needle.fullname: |
| 670 | # We're in the right package, but not the right class yet |
| 671 | yield clazz |
| 672 | elif clazz.fullname == needle.fullname: |
| 673 | # Class found, abort. |
| 674 | return |
| 675 | else: |
| 676 | # We ran past the right class. Send it back into the iterator, then abort. |
| 677 | classes.send(clazz) |
| 678 | return |
| 679 | |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 680 | class Failure(): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 681 | def __init__(self, sig, clazz, detail, error, rule, msg): |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 682 | self.sig = sig |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 683 | self.error = error |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 684 | self.rule = rule |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 685 | self.msg = msg |
| 686 | |
| 687 | if error: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 688 | self.head = "Error %s" % (rule) if rule else "Error" |
| 689 | 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] | 690 | else: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 691 | self.head = "Warning %s" % (rule) if rule else "Warning" |
| 692 | 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] | 693 | |
| 694 | self.line = clazz.line |
| 695 | blame = clazz.blame |
| 696 | if detail is not None: |
| 697 | dump += "\n in " + repr(detail) |
| 698 | self.line = detail.line |
| 699 | blame = detail.blame |
| 700 | dump += "\n in " + repr(clazz) |
| 701 | dump += "\n in " + repr(clazz.pkg) |
| 702 | dump += "\n at line " + repr(self.line) |
| 703 | if blame is not None: |
| 704 | dump += "\n last modified by %s in %s" % (blame[1], blame[0]) |
| 705 | |
| 706 | self.dump = dump |
| 707 | |
| 708 | def __repr__(self): |
| 709 | return self.dump |
| 710 | |
| 711 | |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 712 | failures = {} |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 713 | |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 714 | def _fail(clazz, detail, error, rule, msg): |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 715 | """Records an API failure to be processed later.""" |
| 716 | global failures |
| 717 | |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 718 | sig = "%s-%s-%s" % (clazz.fullname, detail.ident if detail else None, msg) |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 719 | sig = sig.replace(" deprecated ", " ") |
| 720 | |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 721 | failures[sig] = Failure(sig, clazz, detail, error, rule, msg) |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 722 | |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 723 | |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 724 | def warn(clazz, detail, rule, msg): |
| 725 | _fail(clazz, detail, False, rule, msg) |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 726 | |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 727 | def error(clazz, detail, rule, msg): |
| 728 | _fail(clazz, detail, True, rule, msg) |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 729 | |
| 730 | |
Jeff Sharkey | c6c6c34 | 2017-11-13 09:13:37 -0700 | [diff] [blame] | 731 | noticed = {} |
| 732 | |
| 733 | def notice(clazz): |
| 734 | global noticed |
| 735 | |
| 736 | noticed[clazz.fullname] = hash(clazz) |
| 737 | |
| 738 | |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 739 | def verify_constants(clazz): |
| 740 | """All static final constants must be FOO_NAME style.""" |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 741 | if re.match("android\.R\.[a-z]+", clazz.fullname): return |
Jeff Sharkey | 24bda1d | 2017-04-19 16:04:44 -0600 | [diff] [blame] | 742 | if clazz.fullname.startswith("android.os.Build"): return |
| 743 | if clazz.fullname == "android.system.OsConstants": return |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 744 | |
Jeff Sharkey | 24bda1d | 2017-04-19 16:04:44 -0600 | [diff] [blame] | 745 | req = ["java.lang.String","byte","short","int","long","float","double","boolean","char"] |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 746 | for f in clazz.fields: |
| 747 | if "static" in f.split and "final" in f.split: |
| 748 | if re.match("[A-Z0-9_]+", f.name) is None: |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 749 | error(clazz, f, "C2", "Constant field names must be FOO_NAME") |
Jeff Sharkey | 24bda1d | 2017-04-19 16:04:44 -0600 | [diff] [blame] | 750 | if f.typ != "java.lang.String": |
Jeff Sharkey | 331279b | 2016-02-29 16:02:02 -0700 | [diff] [blame] | 751 | if f.name.startswith("MIN_") or f.name.startswith("MAX_"): |
| 752 | warn(clazz, f, "C8", "If min/max could change in future, make them dynamic methods") |
Jeff Sharkey | 24bda1d | 2017-04-19 16:04:44 -0600 | [diff] [blame] | 753 | if f.typ in req and f.value is None: |
| 754 | error(clazz, f, None, "All constants must be defined at compile time") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 755 | |
| 756 | |
| 757 | def verify_enums(clazz): |
| 758 | """Enums are bad, mmkay?""" |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 759 | if clazz.extends == "java.lang.Enum" or "enum" in clazz.split: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 760 | error(clazz, None, "F5", "Enums are not allowed") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 761 | |
| 762 | |
| 763 | def verify_class_names(clazz): |
| 764 | """Try catching malformed class names like myMtp or MTPUser.""" |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 765 | if clazz.fullname.startswith("android.opengl"): return |
| 766 | if clazz.fullname.startswith("android.renderscript"): return |
| 767 | if re.match("android\.R\.[a-z]+", clazz.fullname): return |
| 768 | |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 769 | if re.search("[A-Z]{2,}", clazz.name) is not None: |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 770 | 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] | 771 | if re.match("[^A-Z]", clazz.name): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 772 | error(clazz, None, "S1", "Class must start with uppercase char") |
Jeff Sharkey | ee21f69 | 2018-02-16 13:29:29 -0700 | [diff] [blame] | 773 | if clazz.name.endswith("Impl"): |
| 774 | error(clazz, None, None, "Don't expose your implementation details") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 775 | |
| 776 | |
| 777 | def verify_method_names(clazz): |
| 778 | """Try catching malformed method names, like Foo() or getMTU().""" |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 779 | if clazz.fullname.startswith("android.opengl"): return |
| 780 | if clazz.fullname.startswith("android.renderscript"): return |
| 781 | if clazz.fullname == "android.system.OsConstants": return |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 782 | |
| 783 | for m in clazz.methods: |
| 784 | if re.search("[A-Z]{2,}", m.name) is not None: |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 785 | 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] | 786 | if re.match("[^a-z]", m.name): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 787 | error(clazz, m, "S1", "Method name must start with lowercase char") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 788 | |
| 789 | |
| 790 | def verify_callbacks(clazz): |
| 791 | """Verify Callback classes. |
| 792 | All callback classes must be abstract. |
| 793 | All methods must follow onFoo() naming style.""" |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 794 | if clazz.fullname == "android.speech.tts.SynthesisCallback": return |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 795 | |
| 796 | if clazz.name.endswith("Callbacks"): |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 797 | error(clazz, None, "L1", "Callback class names should be singular") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 798 | if clazz.name.endswith("Observer"): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 799 | warn(clazz, None, "L1", "Class should be named FooCallback") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 800 | |
| 801 | if clazz.name.endswith("Callback"): |
| 802 | if "interface" in clazz.split: |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 803 | 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] | 804 | |
| 805 | for m in clazz.methods: |
| 806 | if not re.match("on[A-Z][a-z]*", m.name): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 807 | error(clazz, m, "L1", "Callback method names must be onFoo() style") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 808 | |
| 809 | |
| 810 | def verify_listeners(clazz): |
| 811 | """Verify Listener classes. |
| 812 | All Listener classes must be interface. |
| 813 | All methods must follow onFoo() naming style. |
| 814 | If only a single method, it must match class name: |
| 815 | interface OnFooListener { void onFoo() }""" |
| 816 | |
| 817 | if clazz.name.endswith("Listener"): |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 818 | if "abstract" in clazz.split and "class" in clazz.split: |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 819 | 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] | 820 | |
| 821 | for m in clazz.methods: |
| 822 | if not re.match("on[A-Z][a-z]*", m.name): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 823 | error(clazz, m, "L1", "Listener method names must be onFoo() style") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 824 | |
| 825 | if len(clazz.methods) == 1 and clazz.name.startswith("On"): |
| 826 | m = clazz.methods[0] |
| 827 | if (m.name + "Listener").lower() != clazz.name.lower(): |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 828 | error(clazz, m, "L1", "Single listener method name must match class name") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 829 | |
| 830 | |
| 831 | def verify_actions(clazz): |
| 832 | """Verify intent actions. |
| 833 | All action names must be named ACTION_FOO. |
| 834 | All action values must be scoped by package and match name: |
| 835 | package android.foo { |
| 836 | String ACTION_BAR = "android.foo.action.BAR"; |
| 837 | }""" |
| 838 | for f in clazz.fields: |
| 839 | if f.value is None: continue |
| 840 | if f.name.startswith("EXTRA_"): continue |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 841 | if f.name == "SERVICE_INTERFACE" or f.name == "PROVIDER_INTERFACE": continue |
Jeff Sharkey | 24bda1d | 2017-04-19 16:04:44 -0600 | [diff] [blame] | 842 | if "INTERACTION" in f.name: continue |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 843 | |
| 844 | if "static" in f.split and "final" in f.split and f.typ == "java.lang.String": |
| 845 | if "_ACTION" in f.name or "ACTION_" in f.name or ".action." in f.value.lower(): |
| 846 | if not f.name.startswith("ACTION_"): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 847 | error(clazz, f, "C3", "Intent action constant name must be ACTION_FOO") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 848 | else: |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 849 | if clazz.fullname == "android.content.Intent": |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 850 | prefix = "android.intent.action" |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 851 | elif clazz.fullname == "android.provider.Settings": |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 852 | prefix = "android.settings" |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 853 | elif clazz.fullname == "android.app.admin.DevicePolicyManager" or clazz.fullname == "android.app.admin.DeviceAdminReceiver": |
| 854 | prefix = "android.app.action" |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 855 | else: |
| 856 | prefix = clazz.pkg.name + ".action" |
| 857 | expected = prefix + "." + f.name[7:] |
| 858 | if f.value != expected: |
Jeff Sharkey | 336dd0b | 2018-01-12 12:12:27 -0700 | [diff] [blame] | 859 | error(clazz, f, "C4", "Inconsistent action value; expected '%s'" % (expected)) |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 860 | |
| 861 | |
| 862 | def verify_extras(clazz): |
| 863 | """Verify intent extras. |
| 864 | All extra names must be named EXTRA_FOO. |
| 865 | All extra values must be scoped by package and match name: |
| 866 | package android.foo { |
| 867 | String EXTRA_BAR = "android.foo.extra.BAR"; |
| 868 | }""" |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 869 | if clazz.fullname == "android.app.Notification": return |
| 870 | if clazz.fullname == "android.appwidget.AppWidgetManager": return |
| 871 | |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 872 | for f in clazz.fields: |
| 873 | if f.value is None: continue |
| 874 | if f.name.startswith("ACTION_"): continue |
| 875 | |
| 876 | if "static" in f.split and "final" in f.split and f.typ == "java.lang.String": |
| 877 | if "_EXTRA" in f.name or "EXTRA_" in f.name or ".extra" in f.value.lower(): |
| 878 | if not f.name.startswith("EXTRA_"): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 879 | error(clazz, f, "C3", "Intent extra must be EXTRA_FOO") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 880 | else: |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 881 | if clazz.pkg.name == "android.content" and clazz.name == "Intent": |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 882 | prefix = "android.intent.extra" |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 883 | elif clazz.pkg.name == "android.app.admin": |
| 884 | prefix = "android.app.extra" |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 885 | else: |
| 886 | prefix = clazz.pkg.name + ".extra" |
| 887 | expected = prefix + "." + f.name[6:] |
| 888 | if f.value != expected: |
Jeff Sharkey | 336dd0b | 2018-01-12 12:12:27 -0700 | [diff] [blame] | 889 | error(clazz, f, "C4", "Inconsistent extra value; expected '%s'" % (expected)) |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 890 | |
| 891 | |
| 892 | def verify_equals(clazz): |
| 893 | """Verify that equals() and hashCode() must be overridden together.""" |
Jeff Sharkey | 40d623e | 2016-12-21 13:46:33 -0700 | [diff] [blame] | 894 | eq = False |
| 895 | hc = False |
| 896 | for m in clazz.methods: |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 897 | if "static" in m.split: continue |
| 898 | if m.sig_matches("boolean", "equals", ["java.lang.Object"]): eq = True |
| 899 | if m.sig_matches("int", "hashCode", []): hc = True |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 900 | if eq != hc: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 901 | error(clazz, None, "M8", "Must override both equals and hashCode; missing one") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 902 | |
| 903 | |
| 904 | def verify_parcelable(clazz): |
| 905 | """Verify that Parcelable objects aren't hiding required bits.""" |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 906 | if clazz.implements == "android.os.Parcelable": |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 907 | creator = [ i for i in clazz.fields if i.name == "CREATOR" ] |
| 908 | write = [ i for i in clazz.methods if i.name == "writeToParcel" ] |
| 909 | describe = [ i for i in clazz.methods if i.name == "describeContents" ] |
| 910 | |
| 911 | if len(creator) == 0 or len(write) == 0 or len(describe) == 0: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 912 | error(clazz, None, "FW3", "Parcelable requires CREATOR, writeToParcel, and describeContents; missing one") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 913 | |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 914 | if "final" not in clazz.split: |
Jeff Sharkey | 331279b | 2016-02-29 16:02:02 -0700 | [diff] [blame] | 915 | error(clazz, None, "FW8", "Parcelable classes must be final") |
| 916 | |
Jeff Sharkey | 336dd0b | 2018-01-12 12:12:27 -0700 | [diff] [blame] | 917 | for c in clazz.ctors: |
| 918 | if c.args == ["android.os.Parcel"]: |
| 919 | error(clazz, c, "FW3", "Parcelable inflation is exposed through CREATOR, not raw constructors") |
| 920 | |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 921 | |
| 922 | def verify_protected(clazz): |
Jeff Sharkey | b46a969 | 2015-02-17 17:19:41 -0800 | [diff] [blame] | 923 | """Verify that no protected methods or fields are allowed.""" |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 924 | for m in clazz.methods: |
Jeff Sharkey | eff9e22 | 2018-09-27 16:29:25 -0600 | [diff] [blame] | 925 | if m.name == "finalize": continue |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 926 | if "protected" in m.split: |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 927 | error(clazz, m, "M7", "Protected methods not allowed; must be public") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 928 | for f in clazz.fields: |
| 929 | if "protected" in f.split: |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 930 | error(clazz, f, "M7", "Protected fields not allowed; must be public") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 931 | |
| 932 | |
| 933 | def verify_fields(clazz): |
| 934 | """Verify that all exposed fields are final. |
| 935 | Exposed fields must follow myName style. |
| 936 | Catch internal mFoo objects being exposed.""" |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 937 | |
| 938 | IGNORE_BARE_FIELDS = [ |
| 939 | "android.app.ActivityManager.RecentTaskInfo", |
| 940 | "android.app.Notification", |
| 941 | "android.content.pm.ActivityInfo", |
| 942 | "android.content.pm.ApplicationInfo", |
Jeff Sharkey | 24bda1d | 2017-04-19 16:04:44 -0600 | [diff] [blame] | 943 | "android.content.pm.ComponentInfo", |
| 944 | "android.content.pm.ResolveInfo", |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 945 | "android.content.pm.FeatureGroupInfo", |
| 946 | "android.content.pm.InstrumentationInfo", |
| 947 | "android.content.pm.PackageInfo", |
| 948 | "android.content.pm.PackageItemInfo", |
Jeff Sharkey | 24bda1d | 2017-04-19 16:04:44 -0600 | [diff] [blame] | 949 | "android.content.res.Configuration", |
| 950 | "android.graphics.BitmapFactory.Options", |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 951 | "android.os.Message", |
| 952 | "android.system.StructPollfd", |
| 953 | ] |
| 954 | |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 955 | for f in clazz.fields: |
| 956 | if not "final" in f.split: |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 957 | if clazz.fullname in IGNORE_BARE_FIELDS: |
| 958 | pass |
| 959 | elif clazz.fullname.endswith("LayoutParams"): |
| 960 | pass |
| 961 | elif clazz.fullname.startswith("android.util.Mutable"): |
| 962 | pass |
| 963 | else: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 964 | 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] | 965 | |
Adrian Roos | d1e3892 | 2019-01-14 15:44:15 +0100 | [diff] [blame] | 966 | if "static" not in f.split and "property" not in f.split: |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 967 | if not re.match("[a-z]([a-zA-Z]+)?", f.name): |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 968 | 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] | 969 | |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 970 | if re.match("[ms][A-Z]", f.name): |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 971 | error(clazz, f, "F1", "Internal objects must not be exposed") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 972 | |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 973 | if re.match("[A-Z_]+", f.name): |
| 974 | if "static" not in f.split or "final" not in f.split: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 975 | error(clazz, f, "C2", "Constants must be marked static final") |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 976 | |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 977 | |
| 978 | def verify_register(clazz): |
| 979 | """Verify parity of registration methods. |
| 980 | Callback objects use register/unregister methods. |
| 981 | Listener objects use add/remove methods.""" |
| 982 | methods = [ m.name for m in clazz.methods ] |
| 983 | for m in clazz.methods: |
| 984 | if "Callback" in m.raw: |
| 985 | if m.name.startswith("register"): |
| 986 | other = "unregister" + m.name[8:] |
| 987 | if other not in methods: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 988 | error(clazz, m, "L2", "Missing unregister method") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 989 | if m.name.startswith("unregister"): |
| 990 | other = "register" + m.name[10:] |
| 991 | if other not in methods: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 992 | error(clazz, m, "L2", "Missing register method") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 993 | |
| 994 | if m.name.startswith("add") or m.name.startswith("remove"): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 995 | error(clazz, m, "L3", "Callback methods should be named register/unregister") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 996 | |
| 997 | if "Listener" in m.raw: |
| 998 | if m.name.startswith("add"): |
| 999 | other = "remove" + m.name[3:] |
| 1000 | if other not in methods: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 1001 | error(clazz, m, "L2", "Missing remove method") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 1002 | if m.name.startswith("remove") and not m.name.startswith("removeAll"): |
| 1003 | other = "add" + m.name[6:] |
| 1004 | if other not in methods: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 1005 | error(clazz, m, "L2", "Missing add method") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 1006 | |
| 1007 | if m.name.startswith("register") or m.name.startswith("unregister"): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 1008 | error(clazz, m, "L3", "Listener methods should be named add/remove") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 1009 | |
| 1010 | |
| 1011 | def verify_sync(clazz): |
| 1012 | """Verify synchronized methods aren't exposed.""" |
| 1013 | for m in clazz.methods: |
| 1014 | if "synchronized" in m.split: |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 1015 | error(clazz, m, "M5", "Internal locks must not be exposed") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 1016 | |
| 1017 | |
| 1018 | def verify_intent_builder(clazz): |
| 1019 | """Verify that Intent builders are createFooIntent() style.""" |
| 1020 | if clazz.name == "Intent": return |
| 1021 | |
| 1022 | for m in clazz.methods: |
| 1023 | if m.typ == "android.content.Intent": |
| 1024 | if m.name.startswith("create") and m.name.endswith("Intent"): |
| 1025 | pass |
| 1026 | else: |
Adam Powell | 539ea12 | 2015-04-10 13:01:37 -0700 | [diff] [blame] | 1027 | warn(clazz, m, "FW1", "Methods creating an Intent should be named createFooIntent()") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 1028 | |
| 1029 | |
| 1030 | def verify_helper_classes(clazz): |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 1031 | """Verify that helper classes are named consistently with what they extend. |
| 1032 | All developer extendable methods should be named onFoo().""" |
| 1033 | test_methods = False |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 1034 | if clazz.extends == "android.app.Service": |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 1035 | test_methods = True |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 1036 | if not clazz.name.endswith("Service"): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 1037 | error(clazz, None, "CL4", "Inconsistent class name; should be FooService") |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 1038 | |
| 1039 | found = False |
| 1040 | for f in clazz.fields: |
| 1041 | if f.name == "SERVICE_INTERFACE": |
| 1042 | found = True |
| 1043 | if f.value != clazz.fullname: |
Jeff Sharkey | 336dd0b | 2018-01-12 12:12:27 -0700 | [diff] [blame] | 1044 | error(clazz, f, "C4", "Inconsistent interface constant; expected '%s'" % (clazz.fullname)) |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 1045 | |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 1046 | if clazz.extends == "android.content.ContentProvider": |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 1047 | test_methods = True |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 1048 | if not clazz.name.endswith("Provider"): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 1049 | error(clazz, None, "CL4", "Inconsistent class name; should be FooProvider") |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 1050 | |
| 1051 | found = False |
| 1052 | for f in clazz.fields: |
| 1053 | if f.name == "PROVIDER_INTERFACE": |
| 1054 | found = True |
| 1055 | if f.value != clazz.fullname: |
Jeff Sharkey | 336dd0b | 2018-01-12 12:12:27 -0700 | [diff] [blame] | 1056 | error(clazz, f, "C4", "Inconsistent interface constant; expected '%s'" % (clazz.fullname)) |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 1057 | |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 1058 | if clazz.extends == "android.content.BroadcastReceiver": |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 1059 | test_methods = True |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 1060 | if not clazz.name.endswith("Receiver"): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 1061 | error(clazz, None, "CL4", "Inconsistent class name; should be FooReceiver") |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 1062 | |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 1063 | if clazz.extends == "android.app.Activity": |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 1064 | test_methods = True |
| 1065 | if not clazz.name.endswith("Activity"): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 1066 | error(clazz, None, "CL4", "Inconsistent class name; should be FooActivity") |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 1067 | |
| 1068 | if test_methods: |
| 1069 | for m in clazz.methods: |
| 1070 | if "final" in m.split: continue |
| 1071 | if not re.match("on[A-Z]", m.name): |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 1072 | if "abstract" in m.split: |
Jeff Sharkey | b46a969 | 2015-02-17 17:19:41 -0800 | [diff] [blame] | 1073 | warn(clazz, m, None, "Methods implemented by developers should be named onFoo()") |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 1074 | else: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 1075 | 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] | 1076 | |
| 1077 | |
| 1078 | def verify_builder(clazz): |
| 1079 | """Verify builder classes. |
| 1080 | Methods should return the builder to enable chaining.""" |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 1081 | if clazz.extends: return |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 1082 | if not clazz.name.endswith("Builder"): return |
| 1083 | |
| 1084 | if clazz.name != "Builder": |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 1085 | warn(clazz, None, None, "Builder should be defined as inner class") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 1086 | |
| 1087 | has_build = False |
| 1088 | for m in clazz.methods: |
| 1089 | if m.name == "build": |
| 1090 | has_build = True |
| 1091 | continue |
| 1092 | |
| 1093 | if m.name.startswith("get"): continue |
| 1094 | if m.name.startswith("clear"): continue |
| 1095 | |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 1096 | if m.name.startswith("with"): |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 1097 | warn(clazz, m, None, "Builder methods names should use setFoo() style") |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 1098 | |
| 1099 | if m.name.startswith("set"): |
| 1100 | if not m.typ.endswith(clazz.fullname): |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 1101 | warn(clazz, m, "M4", "Methods must return the builder object") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 1102 | |
| 1103 | if not has_build: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 1104 | warn(clazz, None, None, "Missing build() method") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 1105 | |
| 1106 | |
| 1107 | def verify_aidl(clazz): |
| 1108 | """Catch people exposing raw AIDL.""" |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 1109 | if clazz.extends == "android.os.Binder" or clazz.implements == "android.os.IInterface": |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 1110 | error(clazz, None, None, "Raw AIDL interfaces must not be exposed") |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 1111 | |
| 1112 | |
Jeff Sharkey | 932a07c | 2014-08-28 16:16:02 -0700 | [diff] [blame] | 1113 | def verify_internal(clazz): |
| 1114 | """Catch people exposing internal classes.""" |
| 1115 | if clazz.pkg.name.startswith("com.android"): |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 1116 | error(clazz, None, None, "Internal classes must not be exposed") |
Jeff Sharkey | 932a07c | 2014-08-28 16:16:02 -0700 | [diff] [blame] | 1117 | |
Adrian Roos | 1f1b6a8 | 2019-01-05 20:09:38 +0100 | [diff] [blame] | 1118 | def layering_build_ranking(ranking_list): |
| 1119 | r = {} |
| 1120 | for rank, ps in enumerate(ranking_list): |
| 1121 | if not isinstance(ps, list): |
| 1122 | ps = [ps] |
| 1123 | for p in ps: |
| 1124 | rs = r |
| 1125 | for n in p.split('.'): |
| 1126 | if n not in rs: |
| 1127 | rs[n] = {} |
| 1128 | rs = rs[n] |
| 1129 | rs['-rank'] = rank |
| 1130 | return r |
| 1131 | |
| 1132 | LAYERING_PACKAGE_RANKING = layering_build_ranking([ |
| 1133 | ["android.service","android.accessibilityservice","android.inputmethodservice","android.printservice","android.appwidget","android.webkit","android.preference","android.gesture","android.print"], |
| 1134 | "android.app", |
| 1135 | "android.widget", |
| 1136 | "android.view", |
| 1137 | "android.animation", |
| 1138 | "android.provider", |
| 1139 | ["android.content","android.graphics.drawable"], |
| 1140 | "android.database", |
| 1141 | "android.text", |
| 1142 | "android.graphics", |
| 1143 | "android.os", |
| 1144 | "android.util" |
| 1145 | ]) |
Jeff Sharkey | 932a07c | 2014-08-28 16:16:02 -0700 | [diff] [blame] | 1146 | |
| 1147 | def verify_layering(clazz): |
| 1148 | """Catch package layering violations. |
| 1149 | For example, something in android.os depending on android.app.""" |
Jeff Sharkey | 932a07c | 2014-08-28 16:16:02 -0700 | [diff] [blame] | 1150 | |
| 1151 | def rank(p): |
Adrian Roos | 1f1b6a8 | 2019-01-05 20:09:38 +0100 | [diff] [blame] | 1152 | r = None |
| 1153 | l = LAYERING_PACKAGE_RANKING |
| 1154 | for n in p.split('.'): |
| 1155 | if n in l: |
| 1156 | l = l[n] |
| 1157 | if '-rank' in l: |
| 1158 | r = l['-rank'] |
Jeff Sharkey | 932a07c | 2014-08-28 16:16:02 -0700 | [diff] [blame] | 1159 | else: |
Adrian Roos | 1f1b6a8 | 2019-01-05 20:09:38 +0100 | [diff] [blame] | 1160 | break |
| 1161 | return r |
Jeff Sharkey | 932a07c | 2014-08-28 16:16:02 -0700 | [diff] [blame] | 1162 | |
| 1163 | cr = rank(clazz.pkg.name) |
| 1164 | if cr is None: return |
| 1165 | |
| 1166 | for f in clazz.fields: |
| 1167 | ir = rank(f.typ) |
Adrian Roos | 1f1b6a8 | 2019-01-05 20:09:38 +0100 | [diff] [blame] | 1168 | if ir is not None and ir < cr: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 1169 | warn(clazz, f, "FW6", "Field type violates package layering") |
Jeff Sharkey | 932a07c | 2014-08-28 16:16:02 -0700 | [diff] [blame] | 1170 | |
Adrian Roos | 1f1b6a8 | 2019-01-05 20:09:38 +0100 | [diff] [blame] | 1171 | for m in itertools.chain(clazz.methods, clazz.ctors): |
Jeff Sharkey | 932a07c | 2014-08-28 16:16:02 -0700 | [diff] [blame] | 1172 | ir = rank(m.typ) |
Adrian Roos | 1f1b6a8 | 2019-01-05 20:09:38 +0100 | [diff] [blame] | 1173 | if ir is not None and ir < cr: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 1174 | warn(clazz, m, "FW6", "Method return type violates package layering") |
Jeff Sharkey | 932a07c | 2014-08-28 16:16:02 -0700 | [diff] [blame] | 1175 | for arg in m.args: |
| 1176 | ir = rank(arg) |
Adrian Roos | 1f1b6a8 | 2019-01-05 20:09:38 +0100 | [diff] [blame] | 1177 | if ir is not None and ir < cr: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 1178 | warn(clazz, m, "FW6", "Method argument type violates package layering") |
Jeff Sharkey | 932a07c | 2014-08-28 16:16:02 -0700 | [diff] [blame] | 1179 | |
| 1180 | |
Jeff Sharkey | a18a2e3 | 2015-02-22 15:54:32 -0800 | [diff] [blame] | 1181 | def verify_boolean(clazz): |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 1182 | """Verifies that boolean accessors are named correctly. |
| 1183 | For example, hasFoo() and setHasFoo().""" |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 1184 | |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 1185 | def is_get(m): return len(m.args) == 0 and m.typ == "boolean" |
| 1186 | 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] | 1187 | |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 1188 | gets = [ m for m in clazz.methods if is_get(m) ] |
| 1189 | sets = [ m for m in clazz.methods if is_set(m) ] |
| 1190 | |
| 1191 | def error_if_exists(methods, trigger, expected, actual): |
| 1192 | for m in methods: |
| 1193 | if m.name == actual: |
| 1194 | 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] | 1195 | |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 1196 | for m in clazz.methods: |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 1197 | if is_get(m): |
| 1198 | if re.match("is[A-Z]", m.name): |
| 1199 | target = m.name[2:] |
| 1200 | expected = "setIs" + target |
| 1201 | error_if_exists(sets, m.name, expected, "setHas" + target) |
| 1202 | elif re.match("has[A-Z]", m.name): |
| 1203 | target = m.name[3:] |
| 1204 | expected = "setHas" + target |
| 1205 | error_if_exists(sets, m.name, expected, "setIs" + target) |
| 1206 | error_if_exists(sets, m.name, expected, "set" + target) |
| 1207 | elif re.match("get[A-Z]", m.name): |
| 1208 | target = m.name[3:] |
| 1209 | expected = "set" + target |
| 1210 | error_if_exists(sets, m.name, expected, "setIs" + target) |
| 1211 | error_if_exists(sets, m.name, expected, "setHas" + target) |
| 1212 | |
| 1213 | if is_set(m): |
| 1214 | if re.match("set[A-Z]", m.name): |
| 1215 | target = m.name[3:] |
| 1216 | expected = "get" + target |
| 1217 | error_if_exists(sets, m.name, expected, "is" + target) |
| 1218 | error_if_exists(sets, m.name, expected, "has" + target) |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 1219 | |
| 1220 | |
| 1221 | def verify_collections(clazz): |
| 1222 | """Verifies that collection types are interfaces.""" |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 1223 | if clazz.fullname == "android.os.Bundle": return |
| 1224 | |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 1225 | bad = ["java.util.Vector", "java.util.LinkedList", "java.util.ArrayList", "java.util.Stack", |
| 1226 | "java.util.HashMap", "java.util.HashSet", "android.util.ArraySet", "android.util.ArrayMap"] |
| 1227 | for m in clazz.methods: |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 1228 | if m.typ in bad: |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 1229 | 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] | 1230 | for arg in m.args: |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 1231 | if arg in bad: |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 1232 | 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] | 1233 | |
| 1234 | |
| 1235 | def verify_flags(clazz): |
| 1236 | """Verifies that flags are non-overlapping.""" |
| 1237 | known = collections.defaultdict(int) |
| 1238 | for f in clazz.fields: |
| 1239 | if "FLAG_" in f.name: |
| 1240 | try: |
| 1241 | val = int(f.value) |
| 1242 | except: |
| 1243 | continue |
| 1244 | |
| 1245 | scope = f.name[0:f.name.index("FLAG_")] |
| 1246 | if val & known[scope]: |
Jeff Sharkey | b46a969 | 2015-02-17 17:19:41 -0800 | [diff] [blame] | 1247 | warn(clazz, f, "C1", "Found overlapping flag constant value") |
Jeff Sharkey | 294f0de | 2014-08-29 17:41:43 -0700 | [diff] [blame] | 1248 | known[scope] |= val |
| 1249 | |
| 1250 | |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 1251 | def verify_exception(clazz): |
| 1252 | """Verifies that methods don't throw generic exceptions.""" |
| 1253 | for m in clazz.methods: |
Jeff Sharkey | 336dd0b | 2018-01-12 12:12:27 -0700 | [diff] [blame] | 1254 | for t in m.throws: |
| 1255 | if t in ["java.lang.Exception", "java.lang.Throwable", "java.lang.Error"]: |
| 1256 | error(clazz, m, "S1", "Methods must not throw generic exceptions") |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 1257 | |
Jeff Sharkey | 336dd0b | 2018-01-12 12:12:27 -0700 | [diff] [blame] | 1258 | if t in ["android.os.RemoteException"]: |
| 1259 | if clazz.name == "android.content.ContentProviderClient": continue |
| 1260 | if clazz.name == "android.os.Binder": continue |
| 1261 | if clazz.name == "android.os.IBinder": continue |
Jeff Sharkey | 331279b | 2016-02-29 16:02:02 -0700 | [diff] [blame] | 1262 | |
Jeff Sharkey | 336dd0b | 2018-01-12 12:12:27 -0700 | [diff] [blame] | 1263 | error(clazz, m, "FW9", "Methods calling into system server should rethrow RemoteException as RuntimeException") |
| 1264 | |
| 1265 | if len(m.args) == 0 and t in ["java.lang.IllegalArgumentException", "java.lang.NullPointerException"]: |
| 1266 | warn(clazz, m, "S1", "Methods taking no arguments should throw IllegalStateException") |
Jeff Sharkey | 331279b | 2016-02-29 16:02:02 -0700 | [diff] [blame] | 1267 | |
Adrian Roos | 1f1b6a8 | 2019-01-05 20:09:38 +0100 | [diff] [blame] | 1268 | GOOGLE_IGNORECASE = re.compile("google", re.IGNORECASE) |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 1269 | |
| 1270 | def verify_google(clazz): |
| 1271 | """Verifies that APIs never reference Google.""" |
| 1272 | |
Adrian Roos | 1f1b6a8 | 2019-01-05 20:09:38 +0100 | [diff] [blame] | 1273 | if GOOGLE_IGNORECASE.search(clazz.raw) is not None: |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 1274 | error(clazz, None, None, "Must never reference Google") |
| 1275 | |
Adrian Roos | 1f1b6a8 | 2019-01-05 20:09:38 +0100 | [diff] [blame] | 1276 | for test in clazz.ctors, clazz.fields, clazz.methods: |
| 1277 | for t in test: |
| 1278 | if GOOGLE_IGNORECASE.search(t.raw) is not None: |
| 1279 | error(clazz, t, None, "Must never reference Google") |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 1280 | |
| 1281 | |
| 1282 | def verify_bitset(clazz): |
| 1283 | """Verifies that we avoid using heavy BitSet.""" |
| 1284 | |
| 1285 | for f in clazz.fields: |
| 1286 | if f.typ == "java.util.BitSet": |
| 1287 | error(clazz, f, None, "Field type must not be heavy BitSet") |
| 1288 | |
| 1289 | for m in clazz.methods: |
| 1290 | if m.typ == "java.util.BitSet": |
| 1291 | error(clazz, m, None, "Return type must not be heavy BitSet") |
| 1292 | for arg in m.args: |
| 1293 | if arg == "java.util.BitSet": |
| 1294 | error(clazz, m, None, "Argument type must not be heavy BitSet") |
| 1295 | |
| 1296 | |
| 1297 | def verify_manager(clazz): |
| 1298 | """Verifies that FooManager is only obtained from Context.""" |
| 1299 | |
| 1300 | if not clazz.name.endswith("Manager"): return |
| 1301 | |
| 1302 | for c in clazz.ctors: |
Jeff Sharkey | 047d7f0 | 2015-02-25 11:27:55 -0800 | [diff] [blame] | 1303 | 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] | 1304 | |
Jeff Sharkey | 24bda1d | 2017-04-19 16:04:44 -0600 | [diff] [blame] | 1305 | for m in clazz.methods: |
| 1306 | if m.typ == clazz.fullname: |
| 1307 | error(clazz, m, None, "Managers must always be obtained from Context") |
| 1308 | |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 1309 | |
| 1310 | def verify_boxed(clazz): |
| 1311 | """Verifies that methods avoid boxed primitives.""" |
| 1312 | |
Jeff Sharkey | b46a969 | 2015-02-17 17:19:41 -0800 | [diff] [blame] | 1313 | 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] | 1314 | |
| 1315 | for c in clazz.ctors: |
| 1316 | for arg in c.args: |
| 1317 | if arg in boxed: |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 1318 | error(clazz, c, "M11", "Must avoid boxed primitives") |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 1319 | |
| 1320 | for f in clazz.fields: |
| 1321 | if f.typ in boxed: |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 1322 | error(clazz, f, "M11", "Must avoid boxed primitives") |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 1323 | |
| 1324 | for m in clazz.methods: |
| 1325 | if m.typ in boxed: |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 1326 | error(clazz, m, "M11", "Must avoid boxed primitives") |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 1327 | for arg in m.args: |
| 1328 | if arg in boxed: |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 1329 | error(clazz, m, "M11", "Must avoid boxed primitives") |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 1330 | |
| 1331 | |
| 1332 | def verify_static_utils(clazz): |
| 1333 | """Verifies that helper classes can't be constructed.""" |
| 1334 | if clazz.fullname.startswith("android.opengl"): return |
Jeff Sharkey | 24bda1d | 2017-04-19 16:04:44 -0600 | [diff] [blame] | 1335 | if clazz.fullname.startswith("android.R"): return |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 1336 | |
Jeff Sharkey | 24bda1d | 2017-04-19 16:04:44 -0600 | [diff] [blame] | 1337 | # Only care about classes with default constructors |
| 1338 | if len(clazz.ctors) == 1 and len(clazz.ctors[0].args) == 0: |
| 1339 | test = [] |
| 1340 | test.extend(clazz.fields) |
| 1341 | test.extend(clazz.methods) |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 1342 | |
Jeff Sharkey | 24bda1d | 2017-04-19 16:04:44 -0600 | [diff] [blame] | 1343 | if len(test) == 0: return |
| 1344 | for t in test: |
| 1345 | if "static" not in t.split: |
| 1346 | return |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 1347 | |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 1348 | error(clazz, None, None, "Fully-static utility classes must not have constructor") |
| 1349 | |
| 1350 | |
Jeff Sharkey | b46a969 | 2015-02-17 17:19:41 -0800 | [diff] [blame] | 1351 | def verify_overload_args(clazz): |
| 1352 | """Verifies that method overloads add new arguments at the end.""" |
| 1353 | if clazz.fullname.startswith("android.opengl"): return |
| 1354 | |
| 1355 | overloads = collections.defaultdict(list) |
| 1356 | for m in clazz.methods: |
| 1357 | if "deprecated" in m.split: continue |
| 1358 | overloads[m.name].append(m) |
| 1359 | |
Jeff Sharkey | a18a2e3 | 2015-02-22 15:54:32 -0800 | [diff] [blame] | 1360 | for name, methods in overloads.items(): |
Jeff Sharkey | b46a969 | 2015-02-17 17:19:41 -0800 | [diff] [blame] | 1361 | if len(methods) <= 1: continue |
| 1362 | |
| 1363 | # Look for arguments common across all overloads |
| 1364 | def cluster(args): |
| 1365 | count = collections.defaultdict(int) |
| 1366 | res = set() |
| 1367 | for i in range(len(args)): |
| 1368 | a = args[i] |
| 1369 | res.add("%s#%d" % (a, count[a])) |
| 1370 | count[a] += 1 |
| 1371 | return res |
| 1372 | |
| 1373 | common_args = cluster(methods[0].args) |
| 1374 | for m in methods: |
| 1375 | common_args = common_args & cluster(m.args) |
| 1376 | |
| 1377 | if len(common_args) == 0: continue |
| 1378 | |
| 1379 | # Require that all common arguments are present at start of signature |
| 1380 | locked_sig = None |
| 1381 | for m in methods: |
| 1382 | sig = m.args[0:len(common_args)] |
| 1383 | if not common_args.issubset(cluster(sig)): |
| 1384 | warn(clazz, m, "M2", "Expected common arguments [%s] at beginning of overloaded method" % (", ".join(common_args))) |
| 1385 | elif not locked_sig: |
| 1386 | locked_sig = sig |
| 1387 | elif locked_sig != sig: |
| 1388 | error(clazz, m, "M2", "Expected consistent argument ordering between overloads: %s..." % (", ".join(locked_sig))) |
| 1389 | |
| 1390 | |
| 1391 | def verify_callback_handlers(clazz): |
| 1392 | """Verifies that methods adding listener/callback have overload |
| 1393 | for specifying delivery thread.""" |
| 1394 | |
Jeff Sharkey | 047d7f0 | 2015-02-25 11:27:55 -0800 | [diff] [blame] | 1395 | # Ignore UI packages which assume main thread |
Jeff Sharkey | b46a969 | 2015-02-17 17:19:41 -0800 | [diff] [blame] | 1396 | skip = [ |
Jeff Sharkey | 047d7f0 | 2015-02-25 11:27:55 -0800 | [diff] [blame] | 1397 | "animation", |
| 1398 | "view", |
| 1399 | "graphics", |
| 1400 | "transition", |
| 1401 | "widget", |
| 1402 | "webkit", |
Jeff Sharkey | b46a969 | 2015-02-17 17:19:41 -0800 | [diff] [blame] | 1403 | ] |
| 1404 | for s in skip: |
Jeff Sharkey | 047d7f0 | 2015-02-25 11:27:55 -0800 | [diff] [blame] | 1405 | if s in clazz.pkg.name_path: return |
| 1406 | if s in clazz.extends_path: return |
Jeff Sharkey | b46a969 | 2015-02-17 17:19:41 -0800 | [diff] [blame] | 1407 | |
Jeff Sharkey | 047d7f0 | 2015-02-25 11:27:55 -0800 | [diff] [blame] | 1408 | # Ignore UI classes which assume main thread |
| 1409 | if "app" in clazz.pkg.name_path or "app" in clazz.extends_path: |
| 1410 | for s in ["ActionBar","Dialog","Application","Activity","Fragment","Loader"]: |
| 1411 | if s in clazz.fullname: return |
| 1412 | if "content" in clazz.pkg.name_path or "content" in clazz.extends_path: |
| 1413 | for s in ["Loader"]: |
| 1414 | if s in clazz.fullname: return |
Jeff Sharkey | b46a969 | 2015-02-17 17:19:41 -0800 | [diff] [blame] | 1415 | |
| 1416 | found = {} |
| 1417 | by_name = collections.defaultdict(list) |
Jeff Sharkey | 336dd0b | 2018-01-12 12:12:27 -0700 | [diff] [blame] | 1418 | examine = clazz.ctors + clazz.methods |
| 1419 | for m in examine: |
Jeff Sharkey | b46a969 | 2015-02-17 17:19:41 -0800 | [diff] [blame] | 1420 | if m.name.startswith("unregister"): continue |
| 1421 | if m.name.startswith("remove"): continue |
| 1422 | if re.match("on[A-Z]+", m.name): continue |
| 1423 | |
| 1424 | by_name[m.name].append(m) |
| 1425 | |
| 1426 | for a in m.args: |
| 1427 | if a.endswith("Listener") or a.endswith("Callback") or a.endswith("Callbacks"): |
| 1428 | found[m.name] = m |
| 1429 | |
| 1430 | for f in found.values(): |
| 1431 | takes_handler = False |
Jeff Sharkey | bedb3fc | 2017-12-05 09:42:28 -0700 | [diff] [blame] | 1432 | takes_exec = False |
Jeff Sharkey | b46a969 | 2015-02-17 17:19:41 -0800 | [diff] [blame] | 1433 | for m in by_name[f.name]: |
| 1434 | if "android.os.Handler" in m.args: |
| 1435 | takes_handler = True |
Jeff Sharkey | bedb3fc | 2017-12-05 09:42:28 -0700 | [diff] [blame] | 1436 | if "java.util.concurrent.Executor" in m.args: |
| 1437 | takes_exec = True |
| 1438 | if not takes_exec: |
| 1439 | warn(clazz, f, "L1", "Registration methods should have overload that accepts delivery Executor") |
Jeff Sharkey | b46a969 | 2015-02-17 17:19:41 -0800 | [diff] [blame] | 1440 | |
| 1441 | |
| 1442 | def verify_context_first(clazz): |
| 1443 | """Verifies that methods accepting a Context keep it the first argument.""" |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 1444 | examine = clazz.ctors + clazz.methods |
| 1445 | for m in examine: |
| 1446 | if len(m.args) > 1 and m.args[0] != "android.content.Context": |
Jeff Sharkey | b46a969 | 2015-02-17 17:19:41 -0800 | [diff] [blame] | 1447 | if "android.content.Context" in m.args[1:]: |
| 1448 | error(clazz, m, "M3", "Context is distinct, so it must be the first argument") |
Jeff Sharkey | 24bda1d | 2017-04-19 16:04:44 -0600 | [diff] [blame] | 1449 | if len(m.args) > 1 and m.args[0] != "android.content.ContentResolver": |
| 1450 | if "android.content.ContentResolver" in m.args[1:]: |
| 1451 | error(clazz, m, "M3", "ContentResolver is distinct, so it must be the first argument") |
Jeff Sharkey | b46a969 | 2015-02-17 17:19:41 -0800 | [diff] [blame] | 1452 | |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 1453 | |
| 1454 | def verify_listener_last(clazz): |
| 1455 | """Verifies that methods accepting a Listener or Callback keep them as last arguments.""" |
| 1456 | examine = clazz.ctors + clazz.methods |
| 1457 | for m in examine: |
| 1458 | if "Listener" in m.name or "Callback" in m.name: continue |
| 1459 | found = False |
| 1460 | for a in m.args: |
| 1461 | if a.endswith("Callback") or a.endswith("Callbacks") or a.endswith("Listener"): |
| 1462 | found = True |
Jeff Sharkey | 336dd0b | 2018-01-12 12:12:27 -0700 | [diff] [blame] | 1463 | elif found: |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 1464 | warn(clazz, m, "M3", "Listeners should always be at end of argument list") |
| 1465 | |
| 1466 | |
| 1467 | def verify_resource_names(clazz): |
| 1468 | """Verifies that resource names have consistent case.""" |
| 1469 | if not re.match("android\.R\.[a-z]+", clazz.fullname): return |
| 1470 | |
| 1471 | # Resources defined by files are foo_bar_baz |
| 1472 | if clazz.name in ["anim","animator","color","dimen","drawable","interpolator","layout","transition","menu","mipmap","string","plurals","raw","xml"]: |
| 1473 | for f in clazz.fields: |
Jeff Sharkey | eff9e22 | 2018-09-27 16:29:25 -0600 | [diff] [blame] | 1474 | if re.match("config_[a-z][a-zA-Z1-9]*$", f.name): continue |
| 1475 | if f.name.startswith("config_"): |
| 1476 | error(clazz, f, None, "Expected config name to be config_fooBarBaz style") |
| 1477 | |
Jeff Sharkey | 90b547b | 2015-02-18 16:45:54 -0800 | [diff] [blame] | 1478 | if re.match("[a-z1-9_]+$", f.name): continue |
| 1479 | error(clazz, f, None, "Expected resource name in this class to be foo_bar_baz style") |
| 1480 | |
| 1481 | # Resources defined inside files are fooBarBaz |
| 1482 | if clazz.name in ["array","attr","id","bool","fraction","integer"]: |
| 1483 | for f in clazz.fields: |
| 1484 | if re.match("config_[a-z][a-zA-Z1-9]*$", f.name): continue |
| 1485 | if re.match("layout_[a-z][a-zA-Z1-9]*$", f.name): continue |
| 1486 | if re.match("state_[a-z_]*$", f.name): continue |
| 1487 | |
| 1488 | if re.match("[a-z][a-zA-Z1-9]*$", f.name): continue |
| 1489 | error(clazz, f, "C7", "Expected resource name in this class to be fooBarBaz style") |
| 1490 | |
| 1491 | # Styles are FooBar_Baz |
| 1492 | if clazz.name in ["style"]: |
| 1493 | for f in clazz.fields: |
| 1494 | if re.match("[A-Z][A-Za-z1-9]+(_[A-Z][A-Za-z1-9]+?)*$", f.name): continue |
| 1495 | 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] | 1496 | |
| 1497 | |
Jeff Sharkey | 331279b | 2016-02-29 16:02:02 -0700 | [diff] [blame] | 1498 | def verify_files(clazz): |
| 1499 | """Verifies that methods accepting File also accept streams.""" |
| 1500 | |
| 1501 | has_file = set() |
| 1502 | has_stream = set() |
| 1503 | |
| 1504 | test = [] |
| 1505 | test.extend(clazz.ctors) |
| 1506 | test.extend(clazz.methods) |
| 1507 | |
| 1508 | for m in test: |
| 1509 | if "java.io.File" in m.args: |
| 1510 | has_file.add(m) |
| 1511 | 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: |
| 1512 | has_stream.add(m.name) |
| 1513 | |
| 1514 | for m in has_file: |
| 1515 | if m.name not in has_stream: |
| 1516 | warn(clazz, m, "M10", "Methods accepting File should also accept FileDescriptor or streams") |
| 1517 | |
| 1518 | |
Jeff Sharkey | 70168dd | 2016-03-30 21:47:16 -0600 | [diff] [blame] | 1519 | def verify_manager_list(clazz): |
| 1520 | """Verifies that managers return List<? extends Parcelable> instead of arrays.""" |
| 1521 | |
| 1522 | if not clazz.name.endswith("Manager"): return |
| 1523 | |
| 1524 | for m in clazz.methods: |
| 1525 | if m.typ.startswith("android.") and m.typ.endswith("[]"): |
| 1526 | warn(clazz, m, None, "Methods should return List<? extends Parcelable> instead of Parcelable[] to support ParceledListSlice under the hood") |
| 1527 | |
| 1528 | |
Jeff Sharkey | 26c8090 | 2016-12-21 13:41:17 -0700 | [diff] [blame] | 1529 | def verify_abstract_inner(clazz): |
| 1530 | """Verifies that abstract inner classes are static.""" |
| 1531 | |
| 1532 | if re.match(".+?\.[A-Z][^\.]+\.[A-Z]", clazz.fullname): |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 1533 | if "abstract" in clazz.split and "static" not in clazz.split: |
Jeff Sharkey | 26c8090 | 2016-12-21 13:41:17 -0700 | [diff] [blame] | 1534 | warn(clazz, None, None, "Abstract inner classes should be static to improve testability") |
| 1535 | |
| 1536 | |
Jeff Sharkey | 24bda1d | 2017-04-19 16:04:44 -0600 | [diff] [blame] | 1537 | def verify_runtime_exceptions(clazz): |
| 1538 | """Verifies that runtime exceptions aren't listed in throws.""" |
| 1539 | |
| 1540 | banned = [ |
| 1541 | "java.lang.NullPointerException", |
| 1542 | "java.lang.ClassCastException", |
| 1543 | "java.lang.IndexOutOfBoundsException", |
| 1544 | "java.lang.reflect.UndeclaredThrowableException", |
| 1545 | "java.lang.reflect.MalformedParametersException", |
| 1546 | "java.lang.reflect.MalformedParameterizedTypeException", |
| 1547 | "java.lang.invoke.WrongMethodTypeException", |
| 1548 | "java.lang.EnumConstantNotPresentException", |
| 1549 | "java.lang.IllegalMonitorStateException", |
| 1550 | "java.lang.SecurityException", |
| 1551 | "java.lang.UnsupportedOperationException", |
| 1552 | "java.lang.annotation.AnnotationTypeMismatchException", |
| 1553 | "java.lang.annotation.IncompleteAnnotationException", |
| 1554 | "java.lang.TypeNotPresentException", |
| 1555 | "java.lang.IllegalStateException", |
| 1556 | "java.lang.ArithmeticException", |
| 1557 | "java.lang.IllegalArgumentException", |
| 1558 | "java.lang.ArrayStoreException", |
| 1559 | "java.lang.NegativeArraySizeException", |
| 1560 | "java.util.MissingResourceException", |
| 1561 | "java.util.EmptyStackException", |
| 1562 | "java.util.concurrent.CompletionException", |
| 1563 | "java.util.concurrent.RejectedExecutionException", |
| 1564 | "java.util.IllformedLocaleException", |
| 1565 | "java.util.ConcurrentModificationException", |
| 1566 | "java.util.NoSuchElementException", |
| 1567 | "java.io.UncheckedIOException", |
| 1568 | "java.time.DateTimeException", |
| 1569 | "java.security.ProviderException", |
| 1570 | "java.nio.BufferUnderflowException", |
| 1571 | "java.nio.BufferOverflowException", |
| 1572 | ] |
| 1573 | |
Jeff Sharkey | 336dd0b | 2018-01-12 12:12:27 -0700 | [diff] [blame] | 1574 | examine = clazz.ctors + clazz.methods |
| 1575 | for m in examine: |
| 1576 | for t in m.throws: |
| 1577 | if t in banned: |
| 1578 | error(clazz, m, None, "Methods must not mention RuntimeException subclasses in throws clauses") |
Jeff Sharkey | 24bda1d | 2017-04-19 16:04:44 -0600 | [diff] [blame] | 1579 | |
| 1580 | |
| 1581 | def verify_error(clazz): |
| 1582 | """Verifies that we always use Exception instead of Error.""" |
| 1583 | if not clazz.extends: return |
| 1584 | if clazz.extends.endswith("Error"): |
| 1585 | error(clazz, None, None, "Trouble must be reported through an Exception, not Error") |
| 1586 | if clazz.extends.endswith("Exception") and not clazz.name.endswith("Exception"): |
| 1587 | error(clazz, None, None, "Exceptions must be named FooException") |
| 1588 | |
| 1589 | |
| 1590 | def verify_units(clazz): |
| 1591 | """Verifies that we use consistent naming for units.""" |
| 1592 | |
| 1593 | # If we find K, recommend replacing with V |
| 1594 | bad = { |
| 1595 | "Ns": "Nanos", |
| 1596 | "Ms": "Millis or Micros", |
| 1597 | "Sec": "Seconds", "Secs": "Seconds", |
| 1598 | "Hr": "Hours", "Hrs": "Hours", |
| 1599 | "Mo": "Months", "Mos": "Months", |
| 1600 | "Yr": "Years", "Yrs": "Years", |
| 1601 | "Byte": "Bytes", "Space": "Bytes", |
| 1602 | } |
| 1603 | |
| 1604 | for m in clazz.methods: |
| 1605 | if m.typ not in ["short","int","long"]: continue |
| 1606 | for k, v in bad.iteritems(): |
| 1607 | if m.name.endswith(k): |
| 1608 | error(clazz, m, None, "Expected method name units to be " + v) |
| 1609 | if m.name.endswith("Nanos") or m.name.endswith("Micros"): |
| 1610 | warn(clazz, m, None, "Returned time values are strongly encouraged to be in milliseconds unless you need the extra precision") |
| 1611 | if m.name.endswith("Seconds"): |
| 1612 | error(clazz, m, None, "Returned time values must be in milliseconds") |
| 1613 | |
| 1614 | for m in clazz.methods: |
| 1615 | typ = m.typ |
| 1616 | if typ == "void": |
| 1617 | if len(m.args) != 1: continue |
| 1618 | typ = m.args[0] |
| 1619 | |
| 1620 | if m.name.endswith("Fraction") and typ != "float": |
| 1621 | error(clazz, m, None, "Fractions must use floats") |
| 1622 | if m.name.endswith("Percentage") and typ != "int": |
| 1623 | error(clazz, m, None, "Percentage must use ints") |
| 1624 | |
| 1625 | |
| 1626 | def verify_closable(clazz): |
| 1627 | """Verifies that classes are AutoClosable.""" |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 1628 | if clazz.implements == "java.lang.AutoCloseable": return |
| 1629 | if clazz.implements == "java.io.Closeable": return |
Jeff Sharkey | 24bda1d | 2017-04-19 16:04:44 -0600 | [diff] [blame] | 1630 | |
| 1631 | for m in clazz.methods: |
| 1632 | if len(m.args) > 0: continue |
| 1633 | if m.name in ["close","release","destroy","finish","finalize","disconnect","shutdown","stop","free","quit"]: |
| 1634 | warn(clazz, m, None, "Classes that release resources should implement AutoClosable and CloseGuard") |
| 1635 | return |
| 1636 | |
| 1637 | |
Jake Wharton | 9e6738f | 2017-08-23 11:59:55 -0400 | [diff] [blame] | 1638 | def verify_member_name_not_kotlin_keyword(clazz): |
| 1639 | """Prevent method names which are keywords in Kotlin.""" |
| 1640 | |
| 1641 | # https://kotlinlang.org/docs/reference/keyword-reference.html#hard-keywords |
| 1642 | # This list does not include Java keywords as those are already impossible to use. |
| 1643 | keywords = [ |
| 1644 | 'as', |
| 1645 | 'fun', |
| 1646 | 'in', |
| 1647 | 'is', |
| 1648 | 'object', |
| 1649 | 'typealias', |
| 1650 | 'val', |
| 1651 | 'var', |
| 1652 | 'when', |
| 1653 | ] |
| 1654 | |
| 1655 | for m in clazz.methods: |
| 1656 | if m.name in keywords: |
| 1657 | error(clazz, m, None, "Method name must not be a Kotlin keyword") |
| 1658 | for f in clazz.fields: |
| 1659 | if f.name in keywords: |
| 1660 | error(clazz, f, None, "Field name must not be a Kotlin keyword") |
| 1661 | |
| 1662 | |
| 1663 | def verify_method_name_not_kotlin_operator(clazz): |
| 1664 | """Warn about method names which become operators in Kotlin.""" |
| 1665 | |
| 1666 | binary = set() |
| 1667 | |
| 1668 | def unique_binary_op(m, op): |
| 1669 | if op in binary: |
| 1670 | error(clazz, m, None, "Only one of '{0}' and '{0}Assign' methods should be present for Kotlin".format(op)) |
| 1671 | binary.add(op) |
| 1672 | |
| 1673 | for m in clazz.methods: |
Adrian Roos | d1e3892 | 2019-01-14 15:44:15 +0100 | [diff] [blame] | 1674 | if 'static' in m.split or 'operator' in m.split: |
Jake Wharton | 9e6738f | 2017-08-23 11:59:55 -0400 | [diff] [blame] | 1675 | continue |
| 1676 | |
| 1677 | # https://kotlinlang.org/docs/reference/operator-overloading.html#unary-prefix-operators |
| 1678 | if m.name in ['unaryPlus', 'unaryMinus', 'not'] and len(m.args) == 0: |
| 1679 | warn(clazz, m, None, "Method can be invoked as a unary operator from Kotlin") |
| 1680 | |
| 1681 | # https://kotlinlang.org/docs/reference/operator-overloading.html#increments-and-decrements |
| 1682 | if m.name in ['inc', 'dec'] and len(m.args) == 0 and m.typ != 'void': |
| 1683 | # This only applies if the return type is the same or a subtype of the enclosing class, but we have no |
| 1684 | # practical way of checking that relationship here. |
| 1685 | warn(clazz, m, None, "Method can be invoked as a pre/postfix inc/decrement operator from Kotlin") |
| 1686 | |
| 1687 | # https://kotlinlang.org/docs/reference/operator-overloading.html#arithmetic |
| 1688 | if m.name in ['plus', 'minus', 'times', 'div', 'rem', 'mod', 'rangeTo'] and len(m.args) == 1: |
| 1689 | warn(clazz, m, None, "Method can be invoked as a binary operator from Kotlin") |
| 1690 | unique_binary_op(m, m.name) |
| 1691 | |
| 1692 | # https://kotlinlang.org/docs/reference/operator-overloading.html#in |
| 1693 | if m.name == 'contains' and len(m.args) == 1 and m.typ == 'boolean': |
| 1694 | warn(clazz, m, None, "Method can be invoked as a 'in' operator from Kotlin") |
| 1695 | |
| 1696 | # https://kotlinlang.org/docs/reference/operator-overloading.html#indexed |
| 1697 | if (m.name == 'get' and len(m.args) > 0) or (m.name == 'set' and len(m.args) > 1): |
| 1698 | warn(clazz, m, None, "Method can be invoked with an indexing operator from Kotlin") |
| 1699 | |
| 1700 | # https://kotlinlang.org/docs/reference/operator-overloading.html#invoke |
| 1701 | if m.name == 'invoke': |
| 1702 | warn(clazz, m, None, "Method can be invoked with function call syntax from Kotlin") |
| 1703 | |
| 1704 | # https://kotlinlang.org/docs/reference/operator-overloading.html#assignments |
| 1705 | if m.name in ['plusAssign', 'minusAssign', 'timesAssign', 'divAssign', 'remAssign', 'modAssign'] \ |
| 1706 | and len(m.args) == 1 \ |
| 1707 | and m.typ == 'void': |
| 1708 | warn(clazz, m, None, "Method can be invoked as a compound assignment operator from Kotlin") |
| 1709 | unique_binary_op(m, m.name[:-6]) # Remove 'Assign' suffix |
| 1710 | |
| 1711 | |
Jeff Sharkey | bedb3fc | 2017-12-05 09:42:28 -0700 | [diff] [blame] | 1712 | def verify_collections_over_arrays(clazz): |
| 1713 | """Warn that [] should be Collections.""" |
| 1714 | |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 1715 | if "@interface" in clazz.split: |
| 1716 | return |
| 1717 | |
Jeff Sharkey | bedb3fc | 2017-12-05 09:42:28 -0700 | [diff] [blame] | 1718 | safe = ["java.lang.String[]","byte[]","short[]","int[]","long[]","float[]","double[]","boolean[]","char[]"] |
| 1719 | for m in clazz.methods: |
| 1720 | if m.typ.endswith("[]") and m.typ not in safe: |
| 1721 | warn(clazz, m, None, "Method should return Collection<> (or subclass) instead of raw array") |
| 1722 | for arg in m.args: |
| 1723 | if arg.endswith("[]") and arg not in safe: |
| 1724 | warn(clazz, m, None, "Method argument should be Collection<> (or subclass) instead of raw array") |
| 1725 | |
| 1726 | |
Jeff Sharkey | 336dd0b | 2018-01-12 12:12:27 -0700 | [diff] [blame] | 1727 | def verify_user_handle(clazz): |
| 1728 | """Methods taking UserHandle should be ForUser or AsUser.""" |
| 1729 | if clazz.name.endswith("Listener") or clazz.name.endswith("Callback") or clazz.name.endswith("Callbacks"): return |
| 1730 | if clazz.fullname == "android.app.admin.DeviceAdminReceiver": return |
| 1731 | if clazz.fullname == "android.content.pm.LauncherApps": return |
| 1732 | if clazz.fullname == "android.os.UserHandle": return |
| 1733 | if clazz.fullname == "android.os.UserManager": return |
| 1734 | |
| 1735 | for m in clazz.methods: |
Jeff Sharkey | 336dd0b | 2018-01-12 12:12:27 -0700 | [diff] [blame] | 1736 | if re.match("on[A-Z]+", m.name): continue |
Jeff Sharkey | a8e5df0 | 2018-11-27 17:33:42 -0700 | [diff] [blame] | 1737 | |
| 1738 | has_arg = "android.os.UserHandle" in m.args |
| 1739 | has_name = m.name.endswith("AsUser") or m.name.endswith("ForUser") |
| 1740 | |
| 1741 | if clazz.fullname.endswith("Manager") and has_arg: |
| 1742 | warn(clazz, m, None, "When a method overload is needed to target a specific " |
| 1743 | "UserHandle, callers should be directed to use " |
| 1744 | "Context.createPackageContextAsUser() and re-obtain the relevant " |
| 1745 | "Manager, and no new API should be added") |
| 1746 | elif has_arg and not has_name: |
| 1747 | warn(clazz, m, None, "Method taking UserHandle should be named 'doFooAsUser' " |
| 1748 | "or 'queryFooForUser'") |
Jeff Sharkey | 336dd0b | 2018-01-12 12:12:27 -0700 | [diff] [blame] | 1749 | |
| 1750 | |
| 1751 | def verify_params(clazz): |
| 1752 | """Parameter classes should be 'Params'.""" |
| 1753 | if clazz.name.endswith("Params"): return |
| 1754 | if clazz.fullname == "android.app.ActivityOptions": return |
| 1755 | if clazz.fullname == "android.app.BroadcastOptions": return |
| 1756 | if clazz.fullname == "android.os.Bundle": return |
| 1757 | if clazz.fullname == "android.os.BaseBundle": return |
| 1758 | if clazz.fullname == "android.os.PersistableBundle": return |
| 1759 | |
| 1760 | bad = ["Param","Parameter","Parameters","Args","Arg","Argument","Arguments","Options","Bundle"] |
| 1761 | for b in bad: |
| 1762 | if clazz.name.endswith(b): |
| 1763 | error(clazz, None, None, "Classes holding a set of parameters should be called 'FooParams'") |
| 1764 | |
| 1765 | |
| 1766 | def verify_services(clazz): |
| 1767 | """Service name should be FOO_BAR_SERVICE = 'foo_bar'.""" |
| 1768 | if clazz.fullname != "android.content.Context": return |
| 1769 | |
| 1770 | for f in clazz.fields: |
| 1771 | if f.typ != "java.lang.String": continue |
| 1772 | found = re.match(r"([A-Z_]+)_SERVICE", f.name) |
| 1773 | if found: |
| 1774 | expected = found.group(1).lower() |
| 1775 | if f.value != expected: |
| 1776 | error(clazz, f, "C4", "Inconsistent service value; expected '%s'" % (expected)) |
| 1777 | |
| 1778 | |
| 1779 | def verify_tense(clazz): |
| 1780 | """Verify tenses of method names.""" |
| 1781 | if clazz.fullname.startswith("android.opengl"): return |
| 1782 | |
| 1783 | for m in clazz.methods: |
| 1784 | if m.name.endswith("Enable"): |
| 1785 | warn(clazz, m, None, "Unexpected tense; probably meant 'enabled'") |
| 1786 | |
| 1787 | |
Jeff Sharkey | ee21f69 | 2018-02-16 13:29:29 -0700 | [diff] [blame] | 1788 | def verify_icu(clazz): |
| 1789 | """Verifies that richer ICU replacements are used.""" |
| 1790 | better = { |
| 1791 | "java.util.TimeZone": "android.icu.util.TimeZone", |
| 1792 | "java.util.Calendar": "android.icu.util.Calendar", |
| 1793 | "java.util.Locale": "android.icu.util.ULocale", |
| 1794 | "java.util.ResourceBundle": "android.icu.util.UResourceBundle", |
| 1795 | "java.util.SimpleTimeZone": "android.icu.util.SimpleTimeZone", |
| 1796 | "java.util.StringTokenizer": "android.icu.util.StringTokenizer", |
| 1797 | "java.util.GregorianCalendar": "android.icu.util.GregorianCalendar", |
| 1798 | "java.lang.Character": "android.icu.lang.UCharacter", |
| 1799 | "java.text.BreakIterator": "android.icu.text.BreakIterator", |
| 1800 | "java.text.Collator": "android.icu.text.Collator", |
| 1801 | "java.text.DecimalFormatSymbols": "android.icu.text.DecimalFormatSymbols", |
| 1802 | "java.text.NumberFormat": "android.icu.text.NumberFormat", |
| 1803 | "java.text.DateFormatSymbols": "android.icu.text.DateFormatSymbols", |
| 1804 | "java.text.DateFormat": "android.icu.text.DateFormat", |
| 1805 | "java.text.SimpleDateFormat": "android.icu.text.SimpleDateFormat", |
| 1806 | "java.text.MessageFormat": "android.icu.text.MessageFormat", |
| 1807 | "java.text.DecimalFormat": "android.icu.text.DecimalFormat", |
| 1808 | } |
| 1809 | |
| 1810 | for m in clazz.ctors + clazz.methods: |
| 1811 | types = [] |
| 1812 | types.extend(m.typ) |
| 1813 | types.extend(m.args) |
| 1814 | for arg in types: |
| 1815 | if arg in better: |
| 1816 | warn(clazz, m, None, "Type %s should be replaced with richer ICU type %s" % (arg, better[arg])) |
| 1817 | |
| 1818 | |
| 1819 | def verify_clone(clazz): |
| 1820 | """Verify that clone() isn't implemented; see EJ page 61.""" |
| 1821 | for m in clazz.methods: |
| 1822 | if m.name == "clone": |
| 1823 | error(clazz, m, None, "Provide an explicit copy constructor instead of implementing clone()") |
| 1824 | |
| 1825 | |
Jeff Sharkey | eff9e22 | 2018-09-27 16:29:25 -0600 | [diff] [blame] | 1826 | def verify_pfd(clazz): |
| 1827 | """Verify that android APIs use PFD over FD.""" |
| 1828 | examine = clazz.ctors + clazz.methods |
| 1829 | for m in examine: |
| 1830 | if m.typ == "java.io.FileDescriptor": |
| 1831 | error(clazz, m, "FW11", "Must use ParcelFileDescriptor") |
| 1832 | if m.typ == "int": |
| 1833 | if "Fd" in m.name or "FD" in m.name or "FileDescriptor" in m.name: |
| 1834 | error(clazz, m, "FW11", "Must use ParcelFileDescriptor") |
| 1835 | for arg in m.args: |
| 1836 | if arg == "java.io.FileDescriptor": |
| 1837 | error(clazz, m, "FW11", "Must use ParcelFileDescriptor") |
| 1838 | |
| 1839 | for f in clazz.fields: |
| 1840 | if f.typ == "java.io.FileDescriptor": |
| 1841 | error(clazz, f, "FW11", "Must use ParcelFileDescriptor") |
| 1842 | |
| 1843 | |
| 1844 | def verify_numbers(clazz): |
| 1845 | """Discourage small numbers types like short and byte.""" |
| 1846 | |
| 1847 | discouraged = ["short","byte"] |
| 1848 | |
| 1849 | for c in clazz.ctors: |
| 1850 | for arg in c.args: |
| 1851 | if arg in discouraged: |
| 1852 | warn(clazz, c, "FW12", "Should avoid odd sized primitives; use int instead") |
| 1853 | |
| 1854 | for f in clazz.fields: |
| 1855 | if f.typ in discouraged: |
| 1856 | warn(clazz, f, "FW12", "Should avoid odd sized primitives; use int instead") |
| 1857 | |
| 1858 | for m in clazz.methods: |
| 1859 | if m.typ in discouraged: |
| 1860 | warn(clazz, m, "FW12", "Should avoid odd sized primitives; use int instead") |
| 1861 | for arg in m.args: |
| 1862 | if arg in discouraged: |
| 1863 | warn(clazz, m, "FW12", "Should avoid odd sized primitives; use int instead") |
| 1864 | |
| 1865 | |
| 1866 | def verify_singleton(clazz): |
| 1867 | """Catch singleton objects with constructors.""" |
| 1868 | |
| 1869 | singleton = False |
| 1870 | for m in clazz.methods: |
| 1871 | if m.name.startswith("get") and m.name.endswith("Instance") and " static " in m.raw: |
| 1872 | singleton = True |
| 1873 | |
| 1874 | if singleton: |
| 1875 | for c in clazz.ctors: |
| 1876 | error(clazz, c, None, "Singleton classes should use getInstance() methods") |
| 1877 | |
| 1878 | |
| 1879 | |
Jeff Sharkey | fe5ee6e | 2018-04-20 11:26:16 -0600 | [diff] [blame] | 1880 | def is_interesting(clazz): |
| 1881 | """Test if given class is interesting from an Android PoV.""" |
| 1882 | |
| 1883 | if clazz.pkg.name.startswith("java"): return False |
| 1884 | if clazz.pkg.name.startswith("junit"): return False |
| 1885 | if clazz.pkg.name.startswith("org.apache"): return False |
| 1886 | if clazz.pkg.name.startswith("org.xml"): return False |
| 1887 | if clazz.pkg.name.startswith("org.json"): return False |
| 1888 | if clazz.pkg.name.startswith("org.w3c"): return False |
| 1889 | if clazz.pkg.name.startswith("android.icu."): return False |
| 1890 | return True |
| 1891 | |
| 1892 | |
Jeff Sharkey | a18a2e3 | 2015-02-22 15:54:32 -0800 | [diff] [blame] | 1893 | def examine_clazz(clazz): |
| 1894 | """Find all style issues in the given class.""" |
Jeff Sharkey | c6c6c34 | 2017-11-13 09:13:37 -0700 | [diff] [blame] | 1895 | |
| 1896 | notice(clazz) |
| 1897 | |
Jeff Sharkey | fe5ee6e | 2018-04-20 11:26:16 -0600 | [diff] [blame] | 1898 | if not is_interesting(clazz): return |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 1899 | |
Jeff Sharkey | a18a2e3 | 2015-02-22 15:54:32 -0800 | [diff] [blame] | 1900 | verify_constants(clazz) |
| 1901 | verify_enums(clazz) |
| 1902 | verify_class_names(clazz) |
| 1903 | verify_method_names(clazz) |
| 1904 | verify_callbacks(clazz) |
| 1905 | verify_listeners(clazz) |
| 1906 | verify_actions(clazz) |
| 1907 | verify_extras(clazz) |
| 1908 | verify_equals(clazz) |
| 1909 | verify_parcelable(clazz) |
| 1910 | verify_protected(clazz) |
| 1911 | verify_fields(clazz) |
| 1912 | verify_register(clazz) |
| 1913 | verify_sync(clazz) |
| 1914 | verify_intent_builder(clazz) |
| 1915 | verify_helper_classes(clazz) |
| 1916 | verify_builder(clazz) |
| 1917 | verify_aidl(clazz) |
| 1918 | verify_internal(clazz) |
| 1919 | verify_layering(clazz) |
| 1920 | verify_boolean(clazz) |
| 1921 | verify_collections(clazz) |
| 1922 | verify_flags(clazz) |
| 1923 | verify_exception(clazz) |
Adam Metcalf | 1c7e70a | 2015-03-27 16:11:23 -0700 | [diff] [blame] | 1924 | if not ALLOW_GOOGLE: verify_google(clazz) |
Jeff Sharkey | a18a2e3 | 2015-02-22 15:54:32 -0800 | [diff] [blame] | 1925 | verify_bitset(clazz) |
| 1926 | verify_manager(clazz) |
| 1927 | verify_boxed(clazz) |
| 1928 | verify_static_utils(clazz) |
Jeff Sharkey | bedb3fc | 2017-12-05 09:42:28 -0700 | [diff] [blame] | 1929 | # verify_overload_args(clazz) |
Jeff Sharkey | a18a2e3 | 2015-02-22 15:54:32 -0800 | [diff] [blame] | 1930 | verify_callback_handlers(clazz) |
| 1931 | verify_context_first(clazz) |
| 1932 | verify_listener_last(clazz) |
| 1933 | verify_resource_names(clazz) |
Jeff Sharkey | 331279b | 2016-02-29 16:02:02 -0700 | [diff] [blame] | 1934 | verify_files(clazz) |
Jeff Sharkey | 70168dd | 2016-03-30 21:47:16 -0600 | [diff] [blame] | 1935 | verify_manager_list(clazz) |
Jeff Sharkey | 26c8090 | 2016-12-21 13:41:17 -0700 | [diff] [blame] | 1936 | verify_abstract_inner(clazz) |
Jeff Sharkey | 24bda1d | 2017-04-19 16:04:44 -0600 | [diff] [blame] | 1937 | verify_runtime_exceptions(clazz) |
| 1938 | verify_error(clazz) |
| 1939 | verify_units(clazz) |
| 1940 | verify_closable(clazz) |
Jake Wharton | 9e6738f | 2017-08-23 11:59:55 -0400 | [diff] [blame] | 1941 | verify_member_name_not_kotlin_keyword(clazz) |
| 1942 | verify_method_name_not_kotlin_operator(clazz) |
Jeff Sharkey | bedb3fc | 2017-12-05 09:42:28 -0700 | [diff] [blame] | 1943 | verify_collections_over_arrays(clazz) |
Jeff Sharkey | 336dd0b | 2018-01-12 12:12:27 -0700 | [diff] [blame] | 1944 | verify_user_handle(clazz) |
| 1945 | verify_params(clazz) |
| 1946 | verify_services(clazz) |
| 1947 | verify_tense(clazz) |
Jeff Sharkey | ee21f69 | 2018-02-16 13:29:29 -0700 | [diff] [blame] | 1948 | verify_icu(clazz) |
| 1949 | verify_clone(clazz) |
Jeff Sharkey | eff9e22 | 2018-09-27 16:29:25 -0600 | [diff] [blame] | 1950 | verify_pfd(clazz) |
| 1951 | verify_numbers(clazz) |
| 1952 | verify_singleton(clazz) |
Jeff Sharkey | a18a2e3 | 2015-02-22 15:54:32 -0800 | [diff] [blame] | 1953 | |
| 1954 | |
Adrian Roos | 038a029 | 2018-12-19 17:11:21 +0100 | [diff] [blame] | 1955 | def examine_stream(stream, base_stream=None, in_classes_with_base=[], out_classes_with_base=None): |
Jeff Sharkey | a18a2e3 | 2015-02-22 15:54:32 -0800 | [diff] [blame] | 1956 | """Find all style issues in the given API stream.""" |
Jeff Sharkey | c6c6c34 | 2017-11-13 09:13:37 -0700 | [diff] [blame] | 1957 | global failures, noticed |
Jeff Sharkey | a18a2e3 | 2015-02-22 15:54:32 -0800 | [diff] [blame] | 1958 | failures = {} |
Jeff Sharkey | c6c6c34 | 2017-11-13 09:13:37 -0700 | [diff] [blame] | 1959 | noticed = {} |
Adrian Roos | 038a029 | 2018-12-19 17:11:21 +0100 | [diff] [blame] | 1960 | _parse_stream(stream, examine_clazz, base_f=base_stream, |
| 1961 | in_classes_with_base=in_classes_with_base, |
| 1962 | out_classes_with_base=out_classes_with_base) |
Jeff Sharkey | c6c6c34 | 2017-11-13 09:13:37 -0700 | [diff] [blame] | 1963 | return (failures, noticed) |
Jeff Sharkey | a18a2e3 | 2015-02-22 15:54:32 -0800 | [diff] [blame] | 1964 | |
| 1965 | |
| 1966 | def examine_api(api): |
| 1967 | """Find all style issues in the given parsed API.""" |
| 1968 | global failures |
Jeff Sharkey | 1498f9c | 2014-09-04 12:45:33 -0700 | [diff] [blame] | 1969 | failures = {} |
| 1970 | for key in sorted(api.keys()): |
Jeff Sharkey | a18a2e3 | 2015-02-22 15:54:32 -0800 | [diff] [blame] | 1971 | examine_clazz(api[key]) |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 1972 | return failures |
| 1973 | |
| 1974 | |
Jeff Sharkey | 037458a | 2014-09-04 15:46:20 -0700 | [diff] [blame] | 1975 | def verify_compat(cur, prev): |
| 1976 | """Find any incompatible API changes between two levels.""" |
| 1977 | global failures |
| 1978 | |
| 1979 | def class_exists(api, test): |
| 1980 | return test.fullname in api |
| 1981 | |
| 1982 | def ctor_exists(api, clazz, test): |
| 1983 | for m in clazz.ctors: |
| 1984 | if m.ident == test.ident: return True |
| 1985 | return False |
| 1986 | |
| 1987 | def all_methods(api, clazz): |
| 1988 | methods = list(clazz.methods) |
| 1989 | if clazz.extends is not None: |
| 1990 | methods.extend(all_methods(api, api[clazz.extends])) |
| 1991 | return methods |
| 1992 | |
| 1993 | def method_exists(api, clazz, test): |
| 1994 | methods = all_methods(api, clazz) |
| 1995 | for m in methods: |
| 1996 | if m.ident == test.ident: return True |
| 1997 | return False |
| 1998 | |
| 1999 | def field_exists(api, clazz, test): |
| 2000 | for f in clazz.fields: |
| 2001 | if f.ident == test.ident: return True |
| 2002 | return False |
| 2003 | |
| 2004 | failures = {} |
| 2005 | for key in sorted(prev.keys()): |
| 2006 | prev_clazz = prev[key] |
| 2007 | |
| 2008 | if not class_exists(cur, prev_clazz): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 2009 | error(prev_clazz, None, None, "Class removed or incompatible change") |
Jeff Sharkey | 037458a | 2014-09-04 15:46:20 -0700 | [diff] [blame] | 2010 | continue |
| 2011 | |
| 2012 | cur_clazz = cur[key] |
| 2013 | |
| 2014 | for test in prev_clazz.ctors: |
| 2015 | if not ctor_exists(cur, cur_clazz, test): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 2016 | error(prev_clazz, prev_ctor, None, "Constructor removed or incompatible change") |
Jeff Sharkey | 037458a | 2014-09-04 15:46:20 -0700 | [diff] [blame] | 2017 | |
| 2018 | methods = all_methods(prev, prev_clazz) |
| 2019 | for test in methods: |
| 2020 | if not method_exists(cur, cur_clazz, test): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 2021 | error(prev_clazz, test, None, "Method removed or incompatible change") |
Jeff Sharkey | 037458a | 2014-09-04 15:46:20 -0700 | [diff] [blame] | 2022 | |
| 2023 | for test in prev_clazz.fields: |
| 2024 | if not field_exists(cur, cur_clazz, test): |
Jeff Sharkey | 9f64d5c6 | 2015-02-14 17:03:47 -0800 | [diff] [blame] | 2025 | error(prev_clazz, test, None, "Field removed or incompatible change") |
Jeff Sharkey | 037458a | 2014-09-04 15:46:20 -0700 | [diff] [blame] | 2026 | |
| 2027 | return failures |
| 2028 | |
| 2029 | |
Jeff Sharkey | 8b141b9 | 2018-04-11 10:05:44 -0600 | [diff] [blame] | 2030 | def show_deprecations_at_birth(cur, prev): |
| 2031 | """Show API deprecations at birth.""" |
| 2032 | global failures |
| 2033 | |
| 2034 | # Remove all existing things so we're left with new |
| 2035 | for prev_clazz in prev.values(): |
| 2036 | cur_clazz = cur[prev_clazz.fullname] |
Jeff Sharkey | fe5ee6e | 2018-04-20 11:26:16 -0600 | [diff] [blame] | 2037 | if not is_interesting(cur_clazz): continue |
Jeff Sharkey | 8b141b9 | 2018-04-11 10:05:44 -0600 | [diff] [blame] | 2038 | |
| 2039 | sigs = { i.ident: i for i in prev_clazz.ctors } |
| 2040 | cur_clazz.ctors = [ i for i in cur_clazz.ctors if i.ident not in sigs ] |
| 2041 | sigs = { i.ident: i for i in prev_clazz.methods } |
| 2042 | cur_clazz.methods = [ i for i in cur_clazz.methods if i.ident not in sigs ] |
| 2043 | sigs = { i.ident: i for i in prev_clazz.fields } |
| 2044 | cur_clazz.fields = [ i for i in cur_clazz.fields if i.ident not in sigs ] |
| 2045 | |
| 2046 | # Forget about class entirely when nothing new |
| 2047 | if len(cur_clazz.ctors) == 0 and len(cur_clazz.methods) == 0 and len(cur_clazz.fields) == 0: |
| 2048 | del cur[prev_clazz.fullname] |
| 2049 | |
| 2050 | for clazz in cur.values(): |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 2051 | if "deprecated" in clazz.split and not clazz.fullname in prev: |
Jeff Sharkey | 8b141b9 | 2018-04-11 10:05:44 -0600 | [diff] [blame] | 2052 | error(clazz, None, None, "Found API deprecation at birth") |
| 2053 | |
| 2054 | for i in clazz.ctors + clazz.methods + clazz.fields: |
Adrian Roos | b787c18 | 2019-01-03 18:54:33 +0100 | [diff] [blame] | 2055 | if "deprecated" in i.split: |
Jeff Sharkey | 8b141b9 | 2018-04-11 10:05:44 -0600 | [diff] [blame] | 2056 | error(clazz, i, None, "Found API deprecation at birth") |
| 2057 | |
| 2058 | print "%s Deprecated at birth %s\n" % ((format(fg=WHITE, bg=BLUE, bold=True), |
| 2059 | format(reset=True))) |
| 2060 | for f in sorted(failures): |
| 2061 | print failures[f] |
| 2062 | print |
| 2063 | |
| 2064 | |
Jeff Sharkey | fe5ee6e | 2018-04-20 11:26:16 -0600 | [diff] [blame] | 2065 | def show_stats(cur, prev): |
| 2066 | """Show API stats.""" |
| 2067 | |
| 2068 | stats = collections.defaultdict(int) |
| 2069 | for cur_clazz in cur.values(): |
| 2070 | if not is_interesting(cur_clazz): continue |
| 2071 | |
| 2072 | if cur_clazz.fullname not in prev: |
| 2073 | stats['new_classes'] += 1 |
| 2074 | stats['new_ctors'] += len(cur_clazz.ctors) |
| 2075 | stats['new_methods'] += len(cur_clazz.methods) |
| 2076 | stats['new_fields'] += len(cur_clazz.fields) |
| 2077 | else: |
| 2078 | prev_clazz = prev[cur_clazz.fullname] |
| 2079 | |
| 2080 | sigs = { i.ident: i for i in prev_clazz.ctors } |
| 2081 | ctors = len([ i for i in cur_clazz.ctors if i.ident not in sigs ]) |
| 2082 | sigs = { i.ident: i for i in prev_clazz.methods } |
| 2083 | methods = len([ i for i in cur_clazz.methods if i.ident not in sigs ]) |
| 2084 | sigs = { i.ident: i for i in prev_clazz.fields } |
| 2085 | fields = len([ i for i in cur_clazz.fields if i.ident not in sigs ]) |
| 2086 | |
| 2087 | if ctors + methods + fields > 0: |
| 2088 | stats['extend_classes'] += 1 |
| 2089 | stats['extend_ctors'] += ctors |
| 2090 | stats['extend_methods'] += methods |
| 2091 | stats['extend_fields'] += fields |
| 2092 | |
| 2093 | print "#", "".join([ k.ljust(20) for k in sorted(stats.keys()) ]) |
| 2094 | print " ", "".join([ str(stats[k]).ljust(20) for k in sorted(stats.keys()) ]) |
| 2095 | |
| 2096 | |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 2097 | if __name__ == "__main__": |
Adam Metcalf | 1c7e70a | 2015-03-27 16:11:23 -0700 | [diff] [blame] | 2098 | parser = argparse.ArgumentParser(description="Enforces common Android public API design \ |
| 2099 | patterns. It ignores lint messages from a previous API level, if provided.") |
| 2100 | parser.add_argument("current.txt", type=argparse.FileType('r'), help="current.txt") |
| 2101 | parser.add_argument("previous.txt", nargs='?', type=argparse.FileType('r'), default=None, |
| 2102 | help="previous.txt") |
Adrian Roos | 6eb57b0 | 2018-12-13 22:08:29 +0100 | [diff] [blame] | 2103 | parser.add_argument("--base-current", nargs='?', type=argparse.FileType('r'), default=None, |
| 2104 | help="The base current.txt to use when examining system-current.txt or" |
| 2105 | " test-current.txt") |
| 2106 | parser.add_argument("--base-previous", nargs='?', type=argparse.FileType('r'), default=None, |
| 2107 | help="The base previous.txt to use when examining system-previous.txt or" |
| 2108 | " test-previous.txt") |
Adam Metcalf | 1c7e70a | 2015-03-27 16:11:23 -0700 | [diff] [blame] | 2109 | parser.add_argument("--no-color", action='store_const', const=True, |
| 2110 | help="Disable terminal colors") |
| 2111 | parser.add_argument("--allow-google", action='store_const', const=True, |
| 2112 | help="Allow references to Google") |
Jeff Sharkey | c6c6c34 | 2017-11-13 09:13:37 -0700 | [diff] [blame] | 2113 | parser.add_argument("--show-noticed", action='store_const', const=True, |
| 2114 | help="Show API changes noticed") |
Jeff Sharkey | 8b141b9 | 2018-04-11 10:05:44 -0600 | [diff] [blame] | 2115 | parser.add_argument("--show-deprecations-at-birth", action='store_const', const=True, |
| 2116 | help="Show API deprecations at birth") |
Jeff Sharkey | fe5ee6e | 2018-04-20 11:26:16 -0600 | [diff] [blame] | 2117 | parser.add_argument("--show-stats", action='store_const', const=True, |
| 2118 | help="Show API stats") |
Adam Metcalf | 1c7e70a | 2015-03-27 16:11:23 -0700 | [diff] [blame] | 2119 | args = vars(parser.parse_args()) |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 2120 | |
Adam Metcalf | 1c7e70a | 2015-03-27 16:11:23 -0700 | [diff] [blame] | 2121 | if args['no_color']: |
| 2122 | USE_COLOR = False |
| 2123 | |
| 2124 | if args['allow_google']: |
| 2125 | ALLOW_GOOGLE = True |
| 2126 | |
| 2127 | current_file = args['current.txt'] |
Adrian Roos | 6eb57b0 | 2018-12-13 22:08:29 +0100 | [diff] [blame] | 2128 | base_current_file = args['base_current'] |
Adam Metcalf | 1c7e70a | 2015-03-27 16:11:23 -0700 | [diff] [blame] | 2129 | previous_file = args['previous.txt'] |
Adrian Roos | 6eb57b0 | 2018-12-13 22:08:29 +0100 | [diff] [blame] | 2130 | base_previous_file = args['base_previous'] |
Adam Metcalf | 1c7e70a | 2015-03-27 16:11:23 -0700 | [diff] [blame] | 2131 | |
Jeff Sharkey | 8b141b9 | 2018-04-11 10:05:44 -0600 | [diff] [blame] | 2132 | if args['show_deprecations_at_birth']: |
| 2133 | with current_file as f: |
| 2134 | cur = _parse_stream(f) |
| 2135 | with previous_file as f: |
| 2136 | prev = _parse_stream(f) |
| 2137 | show_deprecations_at_birth(cur, prev) |
| 2138 | sys.exit() |
| 2139 | |
Jeff Sharkey | fe5ee6e | 2018-04-20 11:26:16 -0600 | [diff] [blame] | 2140 | if args['show_stats']: |
| 2141 | with current_file as f: |
| 2142 | cur = _parse_stream(f) |
| 2143 | with previous_file as f: |
| 2144 | prev = _parse_stream(f) |
| 2145 | show_stats(cur, prev) |
| 2146 | sys.exit() |
| 2147 | |
Adrian Roos | 038a029 | 2018-12-19 17:11:21 +0100 | [diff] [blame] | 2148 | classes_with_base = [] |
| 2149 | |
Adam Metcalf | 1c7e70a | 2015-03-27 16:11:23 -0700 | [diff] [blame] | 2150 | with current_file as f: |
Adrian Roos | 6eb57b0 | 2018-12-13 22:08:29 +0100 | [diff] [blame] | 2151 | if base_current_file: |
| 2152 | with base_current_file as base_f: |
Adrian Roos | 038a029 | 2018-12-19 17:11:21 +0100 | [diff] [blame] | 2153 | cur_fail, cur_noticed = examine_stream(f, base_f, |
| 2154 | out_classes_with_base=classes_with_base) |
Adrian Roos | 6eb57b0 | 2018-12-13 22:08:29 +0100 | [diff] [blame] | 2155 | else: |
Adrian Roos | 038a029 | 2018-12-19 17:11:21 +0100 | [diff] [blame] | 2156 | cur_fail, cur_noticed = examine_stream(f, out_classes_with_base=classes_with_base) |
| 2157 | |
Adam Metcalf | 1c7e70a | 2015-03-27 16:11:23 -0700 | [diff] [blame] | 2158 | if not previous_file is None: |
| 2159 | with previous_file as f: |
Adrian Roos | 6eb57b0 | 2018-12-13 22:08:29 +0100 | [diff] [blame] | 2160 | if base_previous_file: |
| 2161 | with base_previous_file as base_f: |
Adrian Roos | 038a029 | 2018-12-19 17:11:21 +0100 | [diff] [blame] | 2162 | prev_fail, prev_noticed = examine_stream(f, base_f, |
| 2163 | in_classes_with_base=classes_with_base) |
Adrian Roos | 6eb57b0 | 2018-12-13 22:08:29 +0100 | [diff] [blame] | 2164 | else: |
Adrian Roos | 038a029 | 2018-12-19 17:11:21 +0100 | [diff] [blame] | 2165 | prev_fail, prev_noticed = examine_stream(f, in_classes_with_base=classes_with_base) |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 2166 | |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 2167 | # ignore errors from previous API level |
| 2168 | for p in prev_fail: |
| 2169 | if p in cur_fail: |
| 2170 | del cur_fail[p] |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 2171 | |
Jeff Sharkey | c6c6c34 | 2017-11-13 09:13:37 -0700 | [diff] [blame] | 2172 | # ignore classes unchanged from previous API level |
| 2173 | for k, v in prev_noticed.iteritems(): |
| 2174 | if k in cur_noticed and v == cur_noticed[k]: |
| 2175 | del cur_noticed[k] |
| 2176 | |
Jeff Sharkey | a18a2e3 | 2015-02-22 15:54:32 -0800 | [diff] [blame] | 2177 | """ |
| 2178 | # NOTE: disabled because of memory pressure |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 2179 | # look for compatibility issues |
| 2180 | compat_fail = verify_compat(cur, prev) |
Jeff Sharkey | 8190f488 | 2014-08-28 12:24:07 -0700 | [diff] [blame] | 2181 | |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 2182 | print "%s API compatibility issues %s\n" % ((format(fg=WHITE, bg=BLUE, bold=True), format(reset=True))) |
| 2183 | for f in sorted(compat_fail): |
| 2184 | print compat_fail[f] |
| 2185 | print |
Jeff Sharkey | a18a2e3 | 2015-02-22 15:54:32 -0800 | [diff] [blame] | 2186 | """ |
Jeff Sharkey | ed6aaf0 | 2015-01-30 13:31:45 -0700 | [diff] [blame] | 2187 | |
Jeff Sharkey | c6c6c34 | 2017-11-13 09:13:37 -0700 | [diff] [blame] | 2188 | if args['show_noticed'] and len(cur_noticed) != 0: |
| 2189 | print "%s API changes noticed %s\n" % ((format(fg=WHITE, bg=BLUE, bold=True), format(reset=True))) |
| 2190 | for f in sorted(cur_noticed.keys()): |
| 2191 | print f |
| 2192 | print |
| 2193 | |
Jason Monk | 53b2a73 | 2017-11-10 15:43:17 -0500 | [diff] [blame] | 2194 | if len(cur_fail) != 0: |
| 2195 | print "%s API style issues %s\n" % ((format(fg=WHITE, bg=BLUE, bold=True), format(reset=True))) |
| 2196 | for f in sorted(cur_fail): |
| 2197 | print cur_fail[f] |
| 2198 | print |
| 2199 | sys.exit(77) |