3 # Copyright (C) 2014 The Android Open Source Project
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 Enforces common Android public API design patterns. It ignores lint messages from
19 a previous API level, if provided.
21 Usage: apilint.py current.txt
22 Usage: apilint.py current.txt previous.txt
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
29 import re, sys, collections, traceback
32 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
34 def format(fg=None, bg=None, bright=False, bold=False, dim=False, reset=False):
35 # manually derived from http://en.wikipedia.org/wiki/ANSI_escape_code#Codes
36 if "--no-color" in sys.argv: return ""
38 if reset: codes.append("0")
40 if not fg is None: codes.append("3%d" % (fg))
42 if not bright: codes.append("4%d" % (bg))
43 else: codes.append("10%d" % (bg))
44 if bold: codes.append("1")
45 elif dim: codes.append("2")
46 else: codes.append("22")
47 return "\033[%sm" % (";".join(codes))
51 def __init__(self, clazz, raw, blame):
53 self.raw = raw.strip(" {;")
57 self.split = list(raw)
59 for r in ["field", "volatile", "transient", "public", "protected", "static", "final", "deprecated"]:
60 while r in raw: raw.remove(r)
63 self.name = raw[1].strip(";")
64 if len(raw) >= 4 and raw[2] == "=":
65 self.value = raw[3].strip(';"')
69 self.ident = self.raw.replace(" deprecated ", " ")
76 def __init__(self, clazz, raw, blame):
78 self.raw = raw.strip(" {;")
81 # drop generics for now
82 raw = re.sub("<.+?>", "", raw)
84 raw = re.split("[\s(),;]+", raw)
86 while r in raw: raw.remove(r)
87 self.split = list(raw)
89 for r in ["method", "public", "protected", "static", "final", "deprecated", "abstract"]:
90 while r in raw: raw.remove(r)
96 if r == "throws": break
99 # identity for compat purposes
101 ident = ident.replace(" deprecated ", " ")
102 ident = ident.replace(" synchronized ", " ")
103 ident = re.sub("<.+?>", "", ident)
104 if " throws " in ident:
105 ident = ident[:ident.index(" throws ")]
113 def __init__(self, pkg, raw, blame):
115 self.raw = raw.strip(" {;")
122 self.split = list(raw)
124 self.fullname = raw[raw.index("class")+1]
125 elif "interface" in raw:
126 self.fullname = raw[raw.index("interface")+1]
128 raise ValueError("Funky class type %s" % (self.raw))
131 self.extends = raw[raw.index("extends")+1]
135 self.fullname = self.pkg.name + "." + self.fullname
136 self.name = self.fullname[self.fullname.rindex(".")+1:]
143 def __init__(self, raw, blame):
144 self.raw = raw.strip(" {;")
148 self.name = raw[raw.index("package")+1]
160 re_blame = re.compile("^([a-z0-9]{7,}) \(<([^>]+)>.+?\) (.+?)$")
163 for raw in f.readlines():
165 match = re_blame.match(raw)
166 if match is not None:
167 blame = match.groups()[0:2]
168 raw = match.groups()[2]
172 if raw.startswith("package"):
173 pkg = Package(raw, blame)
174 elif raw.startswith(" ") and raw.endswith("{"):
175 clazz = Class(pkg, raw, blame)
176 api[clazz.fullname] = clazz
177 elif raw.startswith(" ctor"):
178 clazz.ctors.append(Method(clazz, raw, blame))
179 elif raw.startswith(" method"):
180 clazz.methods.append(Method(clazz, raw, blame))
181 elif raw.startswith(" field"):
182 clazz.fields.append(Field(clazz, raw, blame))
189 def _fail(clazz, detail, msg):
190 """Records an API failure to be processed later."""
193 sig = "%s-%s-%s" % (clazz.fullname, repr(detail), msg)
194 sig = sig.replace(" deprecated ", " ")
198 if detail is not None:
199 res += "\n in " + repr(detail)
201 res += "\n in " + repr(clazz)
202 res += "\n in " + repr(clazz.pkg)
203 if blame is not None:
204 res += "\n last modified by %s in %s" % (blame[1], blame[0])
207 def warn(clazz, detail, msg):
208 _fail(clazz, detail, "%sWarning:%s %s" % (format(fg=YELLOW, bg=BLACK, bold=True), format(reset=True), msg))
210 def error(clazz, detail, msg):
211 _fail(clazz, detail, "%sError:%s %s" % (format(fg=RED, bg=BLACK, bold=True), format(reset=True), msg))
214 def verify_constants(clazz):
215 """All static final constants must be FOO_NAME style."""
216 if re.match("android\.R\.[a-z]+", clazz.fullname): return
218 for f in clazz.fields:
219 if "static" in f.split and "final" in f.split:
220 if re.match("[A-Z0-9_]+", f.name) is None:
221 error(clazz, f, "Constant field names should be FOO_NAME")
224 def verify_enums(clazz):
225 """Enums are bad, mmkay?"""
226 if "extends java.lang.Enum" in clazz.raw:
227 error(clazz, None, "Enums are not allowed")
230 def verify_class_names(clazz):
231 """Try catching malformed class names like myMtp or MTPUser."""
232 if clazz.fullname.startswith("android.opengl"): return
233 if clazz.fullname.startswith("android.renderscript"): return
234 if re.match("android\.R\.[a-z]+", clazz.fullname): return
236 if re.search("[A-Z]{2,}", clazz.name) is not None:
237 warn(clazz, None, "Class name style should be Mtp not MTP")
238 if re.match("[^A-Z]", clazz.name):
239 error(clazz, None, "Class must start with uppercase char")
242 def verify_method_names(clazz):
243 """Try catching malformed method names, like Foo() or getMTU()."""
244 if clazz.fullname.startswith("android.opengl"): return
245 if clazz.fullname.startswith("android.renderscript"): return
246 if clazz.fullname == "android.system.OsConstants": return
248 for m in clazz.methods:
249 if re.search("[A-Z]{2,}", m.name) is not None:
250 warn(clazz, m, "Method name style should be getMtu() instead of getMTU()")
251 if re.match("[^a-z]", m.name):
252 error(clazz, m, "Method name must start with lowercase char")
255 def verify_callbacks(clazz):
256 """Verify Callback classes.
257 All callback classes must be abstract.
258 All methods must follow onFoo() naming style."""
259 if clazz.fullname == "android.speech.tts.SynthesisCallback": return
261 if clazz.name.endswith("Callbacks"):
262 error(clazz, None, "Class name must not be plural")
263 if clazz.name.endswith("Observer"):
264 warn(clazz, None, "Class should be named FooCallback")
266 if clazz.name.endswith("Callback"):
267 if "interface" in clazz.split:
268 error(clazz, None, "Callback must be abstract class to enable extension in future API levels")
270 for m in clazz.methods:
271 if not re.match("on[A-Z][a-z]*", m.name):
272 error(clazz, m, "Callback method names must be onFoo() style")
275 def verify_listeners(clazz):
276 """Verify Listener classes.
277 All Listener classes must be interface.
278 All methods must follow onFoo() naming style.
279 If only a single method, it must match class name:
280 interface OnFooListener { void onFoo() }"""
282 if clazz.name.endswith("Listener"):
283 if " abstract class " in clazz.raw:
284 error(clazz, None, "Listener should be an interface, otherwise renamed Callback")
286 for m in clazz.methods:
287 if not re.match("on[A-Z][a-z]*", m.name):
288 error(clazz, m, "Listener method names must be onFoo() style")
290 if len(clazz.methods) == 1 and clazz.name.startswith("On"):
292 if (m.name + "Listener").lower() != clazz.name.lower():
293 error(clazz, m, "Single listener method name should match class name")
296 def verify_actions(clazz):
297 """Verify intent actions.
298 All action names must be named ACTION_FOO.
299 All action values must be scoped by package and match name:
300 package android.foo {
301 String ACTION_BAR = "android.foo.action.BAR";
303 for f in clazz.fields:
304 if f.value is None: continue
305 if f.name.startswith("EXTRA_"): continue
306 if f.name == "SERVICE_INTERFACE" or f.name == "PROVIDER_INTERFACE": continue
308 if "static" in f.split and "final" in f.split and f.typ == "java.lang.String":
309 if "_ACTION" in f.name or "ACTION_" in f.name or ".action." in f.value.lower():
310 if not f.name.startswith("ACTION_"):
311 error(clazz, f, "Intent action constant name must be ACTION_FOO")
313 if clazz.fullname == "android.content.Intent":
314 prefix = "android.intent.action"
315 elif clazz.fullname == "android.provider.Settings":
316 prefix = "android.settings"
317 elif clazz.fullname == "android.app.admin.DevicePolicyManager" or clazz.fullname == "android.app.admin.DeviceAdminReceiver":
318 prefix = "android.app.action"
320 prefix = clazz.pkg.name + ".action"
321 expected = prefix + "." + f.name[7:]
322 if f.value != expected:
323 error(clazz, f, "Inconsistent action value; expected %s" % (expected))
326 def verify_extras(clazz):
327 """Verify intent extras.
328 All extra names must be named EXTRA_FOO.
329 All extra values must be scoped by package and match name:
330 package android.foo {
331 String EXTRA_BAR = "android.foo.extra.BAR";
333 if clazz.fullname == "android.app.Notification": return
334 if clazz.fullname == "android.appwidget.AppWidgetManager": return
336 for f in clazz.fields:
337 if f.value is None: continue
338 if f.name.startswith("ACTION_"): continue
340 if "static" in f.split and "final" in f.split and f.typ == "java.lang.String":
341 if "_EXTRA" in f.name or "EXTRA_" in f.name or ".extra" in f.value.lower():
342 if not f.name.startswith("EXTRA_"):
343 error(clazz, f, "Intent extra must be EXTRA_FOO")
345 if clazz.pkg.name == "android.content" and clazz.name == "Intent":
346 prefix = "android.intent.extra"
347 elif clazz.pkg.name == "android.app.admin":
348 prefix = "android.app.extra"
350 prefix = clazz.pkg.name + ".extra"
351 expected = prefix + "." + f.name[6:]
352 if f.value != expected:
353 error(clazz, f, "Inconsistent extra value; expected %s" % (expected))
356 def verify_equals(clazz):
357 """Verify that equals() and hashCode() must be overridden together."""
358 methods = [ m.name for m in clazz.methods ]
359 eq = "equals" in methods
360 hc = "hashCode" in methods
362 error(clazz, None, "Must override both equals and hashCode; missing one")
365 def verify_parcelable(clazz):
366 """Verify that Parcelable objects aren't hiding required bits."""
367 if "implements android.os.Parcelable" in clazz.raw:
368 creator = [ i for i in clazz.fields if i.name == "CREATOR" ]
369 write = [ i for i in clazz.methods if i.name == "writeToParcel" ]
370 describe = [ i for i in clazz.methods if i.name == "describeContents" ]
372 if len(creator) == 0 or len(write) == 0 or len(describe) == 0:
373 error(clazz, None, "Parcelable requires CREATOR, writeToParcel, and describeContents; missing one")
376 def verify_protected(clazz):
377 """Verify that no protected methods are allowed."""
378 for m in clazz.methods:
379 if "protected" in m.split:
380 error(clazz, m, "No protected methods; must be public")
381 for f in clazz.fields:
382 if "protected" in f.split:
383 error(clazz, f, "No protected fields; must be public")
386 def verify_fields(clazz):
387 """Verify that all exposed fields are final.
388 Exposed fields must follow myName style.
389 Catch internal mFoo objects being exposed."""
391 IGNORE_BARE_FIELDS = [
392 "android.app.ActivityManager.RecentTaskInfo",
393 "android.app.Notification",
394 "android.content.pm.ActivityInfo",
395 "android.content.pm.ApplicationInfo",
396 "android.content.pm.FeatureGroupInfo",
397 "android.content.pm.InstrumentationInfo",
398 "android.content.pm.PackageInfo",
399 "android.content.pm.PackageItemInfo",
400 "android.os.Message",
401 "android.system.StructPollfd",
404 for f in clazz.fields:
405 if not "final" in f.split:
406 if clazz.fullname in IGNORE_BARE_FIELDS:
408 elif clazz.fullname.endswith("LayoutParams"):
410 elif clazz.fullname.startswith("android.util.Mutable"):
413 error(clazz, f, "Bare fields must be marked final; consider adding accessors")
415 if not "static" in f.split:
416 if not re.match("[a-z]([a-zA-Z]+)?", f.name):
417 error(clazz, f, "Non-static fields must be named with myField style")
419 if re.match("[ms][A-Z]", f.name):
420 error(clazz, f, "Don't expose your internal objects")
422 if re.match("[A-Z_]+", f.name):
423 if "static" not in f.split or "final" not in f.split:
424 error(clazz, f, "Constants must be marked static final")
427 def verify_register(clazz):
428 """Verify parity of registration methods.
429 Callback objects use register/unregister methods.
430 Listener objects use add/remove methods."""
431 methods = [ m.name for m in clazz.methods ]
432 for m in clazz.methods:
433 if "Callback" in m.raw:
434 if m.name.startswith("register"):
435 other = "unregister" + m.name[8:]
436 if other not in methods:
437 error(clazz, m, "Missing unregister method")
438 if m.name.startswith("unregister"):
439 other = "register" + m.name[10:]
440 if other not in methods:
441 error(clazz, m, "Missing register method")
443 if m.name.startswith("add") or m.name.startswith("remove"):
444 error(clazz, m, "Callback methods should be named register/unregister")
446 if "Listener" in m.raw:
447 if m.name.startswith("add"):
448 other = "remove" + m.name[3:]
449 if other not in methods:
450 error(clazz, m, "Missing remove method")
451 if m.name.startswith("remove") and not m.name.startswith("removeAll"):
452 other = "add" + m.name[6:]
453 if other not in methods:
454 error(clazz, m, "Missing add method")
456 if m.name.startswith("register") or m.name.startswith("unregister"):
457 error(clazz, m, "Listener methods should be named add/remove")
460 def verify_sync(clazz):
461 """Verify synchronized methods aren't exposed."""
462 for m in clazz.methods:
463 if "synchronized" in m.split:
464 error(clazz, m, "Internal lock exposed")
467 def verify_intent_builder(clazz):
468 """Verify that Intent builders are createFooIntent() style."""
469 if clazz.name == "Intent": return
471 for m in clazz.methods:
472 if m.typ == "android.content.Intent":
473 if m.name.startswith("create") and m.name.endswith("Intent"):
476 error(clazz, m, "Methods creating an Intent should be named createFooIntent()")
479 def verify_helper_classes(clazz):
480 """Verify that helper classes are named consistently with what they extend.
481 All developer extendable methods should be named onFoo()."""
483 if "extends android.app.Service" in clazz.raw:
485 if not clazz.name.endswith("Service"):
486 error(clazz, None, "Inconsistent class name; should be FooService")
489 for f in clazz.fields:
490 if f.name == "SERVICE_INTERFACE":
492 if f.value != clazz.fullname:
493 error(clazz, f, "Inconsistent interface constant; expected %s" % (clazz.fullname))
496 warn(clazz, None, "Missing SERVICE_INTERFACE constant")
498 if "abstract" in clazz.split and not clazz.fullname.startswith("android.service."):
499 warn(clazz, None, "Services extended by developers should be under android.service")
501 if "extends android.content.ContentProvider" in clazz.raw:
503 if not clazz.name.endswith("Provider"):
504 error(clazz, None, "Inconsistent class name; should be FooProvider")
507 for f in clazz.fields:
508 if f.name == "PROVIDER_INTERFACE":
510 if f.value != clazz.fullname:
511 error(clazz, f, "Inconsistent interface name; expected %s" % (clazz.fullname))
514 warn(clazz, None, "Missing PROVIDER_INTERFACE constant")
516 if "abstract" in clazz.split and not clazz.fullname.startswith("android.provider."):
517 warn(clazz, None, "Providers extended by developers should be under android.provider")
519 if "extends android.content.BroadcastReceiver" in clazz.raw:
521 if not clazz.name.endswith("Receiver"):
522 error(clazz, None, "Inconsistent class name; should be FooReceiver")
524 if "extends android.app.Activity" in clazz.raw:
526 if not clazz.name.endswith("Activity"):
527 error(clazz, None, "Inconsistent class name; should be FooActivity")
530 for m in clazz.methods:
531 if "final" in m.split: continue
532 if not re.match("on[A-Z]", m.name):
533 if "abstract" in m.split:
534 error(clazz, m, "Methods implemented by developers must be named onFoo()")
536 warn(clazz, m, "If implemented by developer, should be named onFoo(); otherwise consider marking final")
539 def verify_builder(clazz):
540 """Verify builder classes.
541 Methods should return the builder to enable chaining."""
542 if " extends " in clazz.raw: return
543 if not clazz.name.endswith("Builder"): return
545 if clazz.name != "Builder":
546 warn(clazz, None, "Builder should be defined as inner class")
549 for m in clazz.methods:
550 if m.name == "build":
554 if m.name.startswith("get"): continue
555 if m.name.startswith("clear"): continue
557 if m.name.startswith("with"):
558 error(clazz, m, "Builder methods names must follow setFoo() style")
560 if m.name.startswith("set"):
561 if not m.typ.endswith(clazz.fullname):
562 warn(clazz, m, "Methods should return the builder")
565 warn(clazz, None, "Missing build() method")
568 def verify_aidl(clazz):
569 """Catch people exposing raw AIDL."""
570 if "extends android.os.Binder" in clazz.raw or "implements android.os.IInterface" in clazz.raw:
571 error(clazz, None, "Exposing raw AIDL interface")
574 def verify_internal(clazz):
575 """Catch people exposing internal classes."""
576 if clazz.pkg.name.startswith("com.android"):
577 error(clazz, None, "Exposing internal class")
580 def verify_layering(clazz):
581 """Catch package layering violations.
582 For example, something in android.os depending on android.app."""
584 ["android.service","android.accessibilityservice","android.inputmethodservice","android.printservice","android.appwidget","android.webkit","android.preference","android.gesture","android.print"],
590 ["android.content","android.graphics.drawable"],
599 for i in range(len(ranking)):
600 if isinstance(ranking[i], list):
602 if p.startswith(j): return i
604 if p.startswith(ranking[i]): return i
606 cr = rank(clazz.pkg.name)
607 if cr is None: return
609 for f in clazz.fields:
612 warn(clazz, f, "Field type violates package layering")
614 for m in clazz.methods:
617 warn(clazz, m, "Method return type violates package layering")
621 warn(clazz, m, "Method argument type violates package layering")
624 def verify_boolean(clazz, api):
625 """Catches people returning boolean from getFoo() style methods.
626 Ignores when matching setFoo() is present."""
628 methods = [ m.name for m in clazz.methods ]
630 builder = clazz.fullname + ".Builder"
633 builder_methods = [ m.name for m in api[builder].methods ]
635 for m in clazz.methods:
636 if m.typ == "boolean" and m.name.startswith("get") and m.name != "get" and len(m.args) == 0:
637 setter = "set" + m.name[3:]
638 if setter in methods:
640 elif builder is not None and setter in builder_methods:
643 warn(clazz, m, "Methods returning boolean should be named isFoo, hasFoo, areFoo")
646 def verify_collections(clazz):
647 """Verifies that collection types are interfaces."""
648 if clazz.fullname == "android.os.Bundle": return
650 bad = ["java.util.Vector", "java.util.LinkedList", "java.util.ArrayList", "java.util.Stack",
651 "java.util.HashMap", "java.util.HashSet", "android.util.ArraySet", "android.util.ArrayMap"]
652 for m in clazz.methods:
654 error(clazz, m, "Return type is concrete collection; should be interface")
657 error(clazz, m, "Argument is concrete collection; should be interface")
660 def verify_flags(clazz):
661 """Verifies that flags are non-overlapping."""
662 known = collections.defaultdict(int)
663 for f in clazz.fields:
664 if "FLAG_" in f.name:
670 scope = f.name[0:f.name.index("FLAG_")]
671 if val & known[scope]:
672 warn(clazz, f, "Found overlapping flag constant value")
676 def verify_style(api):
677 """Find all style issues in the given API level."""
681 for key in sorted(api.keys()):
684 if clazz.pkg.name.startswith("java"): continue
685 if clazz.pkg.name.startswith("junit"): continue
686 if clazz.pkg.name.startswith("org.apache"): continue
687 if clazz.pkg.name.startswith("org.xml"): continue
688 if clazz.pkg.name.startswith("org.json"): continue
689 if clazz.pkg.name.startswith("org.w3c"): continue
691 verify_constants(clazz)
693 verify_class_names(clazz)
694 verify_method_names(clazz)
695 verify_callbacks(clazz)
696 verify_listeners(clazz)
697 verify_actions(clazz)
700 verify_parcelable(clazz)
701 verify_protected(clazz)
703 verify_register(clazz)
705 verify_intent_builder(clazz)
706 verify_helper_classes(clazz)
707 verify_builder(clazz)
709 verify_internal(clazz)
710 verify_layering(clazz)
711 verify_boolean(clazz, api)
712 verify_collections(clazz)
718 def verify_compat(cur, prev):
719 """Find any incompatible API changes between two levels."""
722 def class_exists(api, test):
723 return test.fullname in api
725 def ctor_exists(api, clazz, test):
726 for m in clazz.ctors:
727 if m.ident == test.ident: return True
730 def all_methods(api, clazz):
731 methods = list(clazz.methods)
732 if clazz.extends is not None:
733 methods.extend(all_methods(api, api[clazz.extends]))
736 def method_exists(api, clazz, test):
737 methods = all_methods(api, clazz)
739 if m.ident == test.ident: return True
742 def field_exists(api, clazz, test):
743 for f in clazz.fields:
744 if f.ident == test.ident: return True
748 for key in sorted(prev.keys()):
749 prev_clazz = prev[key]
751 if not class_exists(cur, prev_clazz):
752 error(prev_clazz, None, "Class removed or incompatible change")
757 for test in prev_clazz.ctors:
758 if not ctor_exists(cur, cur_clazz, test):
759 error(prev_clazz, prev_ctor, "Constructor removed or incompatible change")
761 methods = all_methods(prev, prev_clazz)
763 if not method_exists(cur, cur_clazz, test):
764 error(prev_clazz, test, "Method removed or incompatible change")
766 for test in prev_clazz.fields:
767 if not field_exists(cur, cur_clazz, test):
768 error(prev_clazz, test, "Field removed or incompatible change")
773 cur = parse_api(sys.argv[1])
774 cur_fail = verify_style(cur)
776 if len(sys.argv) > 2:
777 prev = parse_api(sys.argv[2])
778 prev_fail = verify_style(prev)
780 # ignore errors from previous API level
785 # look for compatibility issues
786 compat_fail = verify_compat(cur, prev)
788 print "%s API compatibility issues %s\n" % ((format(fg=WHITE, bg=BLUE, bold=True), format(reset=True)))
789 for f in sorted(compat_fail):
794 print "%s API style issues %s\n" % ((format(fg=WHITE, bg=BLUE, bold=True), format(reset=True)))
795 for f in sorted(cur_fail):