def check_methods(tree): for method in tree.findall(".//method"): for modifier_list in method.findall(".//block//modifiers"): error_if(onein(["private", "public", "protected", "final"], modifiers(modifier_list)), "local variables cannot be private/public/protected/final.") for lvar in tree.findall(".//local_variable_declaration"): error_if(lvar.find(".//expression") is None and lvar.find( ".//array_initializer") is None, "Cannot have uninitialized local var declaration.") mods = modifiers(method) error_if("private" in mods, "private methods disallowed in joos.") error_if(allin(["static", "final"], mods), "#A static method cannot be final.") error_if("native" in mods and not "static" in mods, "#A native method must be static.") error_if("abstract" in mods and onein(["static", "final"], mods), "#An abstract method cannot be static or final.") if "abstract" in mods or "native" in mods: error_if(method.find(".//block") is not None, "#A method has a body if and only if it is neither " + "abstract nor native.") else: error_if(method.find(".//block") is None, "A method has a body if and only if it is neither " + "abstract nor native.") error_if(not onein(["public", "protected", "private"], mods), "A method cannot be package-private") error_if(tree.find(".//param//tok_final") is not None, "A formal parameter cannot be final.")
def check_interfaces(tree, filename): for interface in tree.findall(".//interface"): name = interface.get("name") error_if(name != filename, "interface must have same name as file.") for method in interface.findall(".//abstract_method"): mods = modifiers(method) error_if(onein(["static", "final", "native"], mods), "An interface method cannot be static, final, or native.")
def check_fields(tree): for field_declaration in tree.findall(".//field"): mods = modifiers(field_declaration) error_if("private" in mods, "Fields cannot be private.") error_if("final" in mods and not field_declaration.find( "field_initializer"), "Final fields need a initializer.") error_if(not onein(["public", "protected", "private"], mods), "A field cannot be package-private")
def check_classes(tree, filename): for clazz in tree.findall(".//class"): mods = modifiers(clazz) error_if(allin(["abstract", "final"], mods), "A class cannot be both abstract and final") error_if(not onein(["public", "protected", "private"], modifiers(clazz)), "A class cannot be package-private") name = clazz.get("name") error_if(name != filename, "Class must have same name as file.")