Пример #1
0
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.")
Пример #2
0
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.")