Lines Matching refs:clazz

51     def __init__(self, clazz, raw, blame):
52 self.clazz = clazz
76 def __init__(self, clazz, raw, blame):
77 self.clazz = clazz
157 clazz = None
175 clazz = Class(pkg, raw, blame)
176 api[clazz.fullname] = clazz
178 clazz.ctors.append(Method(clazz, raw, blame))
180 clazz.methods.append(Method(clazz, raw, blame))
182 clazz.fields.append(Field(clazz, raw, blame))
189 def _fail(clazz, detail, msg):
193 sig = "%s-%s-%s" % (clazz.fullname, repr(detail), msg)
197 blame = clazz.blame
201 res += "\n in " + repr(clazz)
202 res += "\n in " + repr(clazz.pkg)
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):
216 if re.match("android\.R\.[a-z]+", clazz.fullname): return
218 for f in clazz.fields:
221 error(clazz, f, "Constant field names should be FOO_NAME")
224 def verify_enums(clazz):
226 if "extends java.lang.Enum" in clazz.raw:
227 error(clazz, None, "Enums are not allowed")
230 def verify_class_names(clazz):
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):
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:
250 warn(clazz, m, "Method name style should be getMtu() instead of getMTU()")
252 error(clazz, m, "Method name must start with lowercase char")
255 def verify_callbacks(clazz):
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:
272 error(clazz, m, "Callback method names must be onFoo() style")
275 def verify_listeners(clazz):
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:
288 error(clazz, m, "Listener method names must be onFoo() style")
290 if len(clazz.methods) == 1 and clazz.name.startswith("On"):
291 m = clazz.methods[0]
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):
303 for f in clazz.fields:
311 error(clazz, f, "Intent action constant name must be ACTION_FOO")
313 if clazz.fullname == "android.content.Intent":
315 elif clazz.fullname == "android.provider.Settings":
317 elif clazz.fullname == "android.app.admin.DevicePolicyManager" or clazz.fullname == "android.app.admin.DeviceAdminReceiver":
320 prefix = clazz.pkg.name + ".action"
323 error(clazz, f, "Inconsistent action value; expected %s" % (expected))
326 def verify_extras(clazz):
333 if clazz.fullname == "android.app.Notification": return
334 if clazz.fullname == "android.appwidget.AppWidgetManager": return
336 for f in clazz.fields:
343 error(clazz, f, "Intent extra must be EXTRA_FOO")
345 if clazz.pkg.name == "android.content" and clazz.name == "Intent":
347 elif clazz.pkg.name == "android.app.admin":
350 prefix = clazz.pkg.name + ".extra"
353 error(clazz, f, "Inconsistent extra value; expected %s" % (expected))
356 def verify_equals(clazz):
358 methods = [ m.name for m in clazz.methods ]
362 error(clazz, None, "Must override both equals and hashCode; missing one")
365 def verify_parcelable(clazz):
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" ]
373 error(clazz, None, "Parcelable requires CREATOR, writeToParcel, and describeContents; missing one")
376 def verify_protected(clazz):
378 for m in clazz.methods:
380 error(clazz, m, "No protected methods; must be public")
381 for f in clazz.fields:
383 error(clazz, f, "No protected fields; must be public")
386 def verify_fields(clazz):
404 for f in clazz.fields:
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")
417 error(clazz, f, "Non-static fields must be named with myField style")
420 error(clazz, f, "Don't expose your internal objects")
424 error(clazz, f, "Constants must be marked static final")
427 def verify_register(clazz):
431 methods = [ m.name for m in clazz.methods ]
432 for m in clazz.methods:
437 error(clazz, m, "Missing unregister method")
441 error(clazz, m, "Missing register method")
444 error(clazz, m, "Callback methods should be named register/unregister")
450 error(clazz, m, "Missing remove method")
454 error(clazz, m, "Missing add method")
457 error(clazz, m, "Listener methods should be named add/remove")
460 def verify_sync(clazz):
462 for m in clazz.methods:
464 error(clazz, m, "Internal lock exposed")
467 def verify_intent_builder(clazz):
469 if clazz.name == "Intent": return
471 for m in clazz.methods:
476 error(clazz, m, "Methods creating an Intent should be named createFooIntent()")
479 def verify_helper_classes(clazz):
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:
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:
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:
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):
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:
558 error(clazz, m, "Builder methods names must follow setFoo() style")
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):
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):
576 if clazz.pkg.name.startswith("com.android"):
577 error(clazz, None, "Exposing internal class")
580 def verify_layering(clazz):
606 cr = rank(clazz.pkg.name)
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):
628 methods = [ m.name for m in clazz.methods ]
630 builder = clazz.fullname + ".Builder"
635 for m in clazz.methods:
643 warn(clazz, m, "Methods returning boolean should be named isFoo, hasFoo, areFoo")
646 def verify_collections(clazz):
648 if clazz.fullname == "android.os.Bundle": return
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):
663 for f in clazz.fields:
672 warn(clazz, f, "Found overlapping flag constant value")
682 clazz = api[key]
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)
692 verify_enums(clazz)
693 verify_class_names(clazz)
694 verify_method_names(clazz)
695 verify_callbacks(clazz)
696 verify_listeners(clazz)
697 verify_actions(clazz)
698 verify_extras(clazz)
699 verify_equals(clazz)
700 verify_parcelable(clazz)
701 verify_protected(clazz)
702 verify_fields(clazz)
703 verify_register(clazz)
704 verify_sync(clazz)
705 verify_intent_builder(clazz)
706 verify_helper_classes(clazz)
707 verify_builder(clazz)
708 verify_aidl(clazz)
709 verify_internal(clazz)
710 verify_layering(clazz)
711 verify_boolean(clazz, api)
712 verify_collections(clazz)
713 verify_flags(clazz)
725 def ctor_exists(api, clazz, test):
726 for m in clazz.ctors:
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)
742 def field_exists(api, clazz, test):
743 for f in clazz.fields: