Exemplo n.º 1
0
def check(uast):
    findings = []

    bad_methods = (
        ('hashcode', 'int', 'hashCode'),
        ('tostring', 'String', 'toString'),
        ('equal', 'boolean', 'equals'),
    )

    for method in utils.get_methods(uast):
        if "public" not in method.modifiers:
            continue

        for tup in bad_methods:
            if method.name == tup[
                    0] and method.return_ and method.return_.type_name == tup[
                        1]:
                findings.append({
                    "msg":
                    "Probably misnamed method '{}' instead of '{}'".format(
                        method.name, tup[2]),
                    "pos":
                    method.node.start_position
                })

    return findings
Exemplo n.º 2
0
 def test_0060_getmethods(self):
     uast = self._parse_source("RSPEC-2447")
     methods = get_methods(uast)
     self.assertEqual(len(methods), 1)
     self.assertIsInstance(methods[0], Method)
     self.assertEqual(methods[0].name, "test")
     self.assertListEqual(methods[0].modifiers, [])
     self.assertListEqual(methods[0].modifiers, [])
     self.assertIsNotNone(methods[0].return_)
     self.assertIsInstance(methods[0].return_, Argument)
     self.assertIsNotNone(methods[0].body)
     self.assertIsNotNone(methods[0].node)
Exemplo n.º 3
0
def check(uast):
    findings = []

    if any(
            filter(
                lambda m: m.name == "clone" and "public" in m.modifiers and m.
                return_ and m.return_.type_name == "Object",
                utils.get_methods(uast))):

        findings.append({"msg": "Don't use clone"})

    return findings
Exemplo n.º 4
0
def check(uast):
    findings = []

    methods = utils.get_methods(uast)
    for m in methods:
        if m.return_ is None and m.name == "finalize" and 'public' in m.modifiers:
            findings.append({
                "msg": "Don't use a public finalize()",
                "pos": m.node.start_position
            })

    return findings
Exemplo n.º 5
0
def check(uast):
    findings = []

    methods = utils.get_methods(uast)

    for m in methods:
        if "public" not in m.modifiers:
            ann_trans = bblfsh.filter(
                m.node,
                "//*[@roleAnnotation]/Identifier[@Name='Transactional']")
            for ann in ann_trans:
                findings.append({
                    "msg": "@Transactional methods should be public",
                    "pos": ann.start_position
                })

    return findings
Exemplo n.º 6
0
def check(uast):
    findings = []

    methods = utils.get_methods(uast)

    for m in methods:
        # Should look at the roles to filter by Boolean but there is a bug in the
        # Java driver https://github.com/bblf../../java-driver/issues/83 so we check the token
        if m.return_ and m.return_.type_name == 'boolean':
            if any(
                    list(
                        bblfsh.filter(m.body,
                                      "//*[@roleReturn]//*[@roleNull]"))):
                findings.append(
                    {"msg": "Don't return Null on Boolean-return methods"})

    return findings
Exemplo n.º 7
0
def check(uast):
    findings = []

    methods = utils.get_methods(uast)

    for m in methods:
        if "public" not in m.modifiers:
            continue

        for arg in m.arguments:
            for a in bblfsh.filter(
                    m.node,
                    "//AssertStatement//Identifier[@Name='%s']" % arg.name):
                findings.append({
                    "msg": "Don't use asserts with public method parameters",
                    "pos": a.start_position
                })

    return findings
Exemplo n.º 8
0
def check(uast):
    findings = []

    fin_calls = bblfsh.filter(
        uast, "//MethodInvocation/BooleanLiteral[@internalRole='arguments'"
        " and @booleanValue='true']/parent::MethodInvocation/"
        "Identifier[@roleCall and @roleReceiver and @Name='System']/parent::MethodInvocation/"
        "Identifier[@roleCall and @roleCallee and @Name='runFinalizersOnExit']/parent::MethodInvocation"
    )

    if len(list(fin_calls)):
        findings.append({"msg": "Don't use System.runFinalizersOnExit"})

    methods = utils.get_methods(uast)
    for m in methods:
        if m.return_ is None and m.name == "finalize":
            findings.append({"msg": "Don't override finalize()"})

    return findings
Exemplo n.º 9
0
def check(uast):
    findings = []

    def returnsNull(node):
        return any(list(bblfsh.filter(node, "//*[@roleReturn]//*[@roleNull]")))

    methods = utils.get_methods(uast)

    for m in methods:
        if (m.name == "toString" and "public" in m.modifiers and m.return_ and m.return_.type_name == "String"
               and not m.arguments) or\
           (m.name == "clone" and "public" in m.modifiers and m.return_ and m.return_.type_name == "Object"
               and not m.arguments):
            if returnsNull(m.node):
                findings.append({
                    "msg": "Don't return Null on toString or clone methods",
                    "pos": m.node.start_position
                })

    return findings
Exemplo n.º 10
0
def check(uast):
    findings = []

    methods = utils.get_methods(uast)

    for m in methods:
        if m.name == "equals" and m.return_ and m.return_.type_name == "boolean" and \
                "public" in m.modifiers:
            args = m.arguments
            if len(args) != 1:
                findings.append({
                    "msg":
                    "equals method should have only one Object argument",
                    "pos": m.node.start_position
                })

            if args[0].type_name != "Object":
                findings.append({
                    "msg":
                    "equals should be declared with an argument of type Object",
                    "pos": m.node.start_position
                })

    return findings