Esempio n. 1
0
 def test_change_signature_remove_parameter_nodefault(self):
     self.first["foo"] = (Function(
         "foo", _generic,
         [Signature([Parameter("a", _generic),
                     Parameter("b", _generic)])]))
     self.second["foo"] = (Function(
         "foo", _generic, [Signature([Parameter("a", _generic)])]))
     self.expect(Bump.major)
Esempio n. 2
0
 def test_multiple_signatures_one_compat_one_removed(self):
     self.first["foo"] = (Function("foo", _generic, [
         Signature([Parameter("a", _a)]),
         Signature([Parameter("a", _a),
                    Parameter("b", _generic)])
     ]))
     self.second["foo"] = (Function(
         "foo", _generic, [Signature([Parameter("a", _compatWithA)])]))
     self.expect(Bump.major)
Esempio n. 3
0
 def test_multiple_signatures_new_one(self):
     self.first["foo"] = (Function("foo", _generic,
                                   [Signature([Parameter("a", _a)])]))
     self.second["foo"] = (Function("foo", _generic, [
         Signature([Parameter("a", _a)]),
         Signature([Parameter("a", _a),
                    Parameter("b", _generic)])
     ]))
     self.expect(Bump.minor)
Esempio n. 4
0
 def test_change_signature_rename_parameters(self):
     self.first["foo"] = (Function(
         "foo", _generic,
         [Signature([Parameter("a", _generic),
                     Parameter("b", _generic)])]))
     self.second["foo"] = (Function(
         "foo", _generic,
         [Signature([Parameter("b", _generic),
                     Parameter("a", _generic)])]))
     self.expect(Bump.patch)
Esempio n. 5
0
def _class_or_interface_to_unit(node, compilation, type_system):
    """Convert a class or interface declaration ('node') into a Unit.

    Requires the 'compilation' where the node so it can be used to look up type information."""
    assert isinstance(node, javalang.tree.ClassDeclaration) or \
           isinstance(node, javalang.tree.InterfaceDeclaration) or \
           isinstance(node, javalang.tree.AnnotationDeclaration) or \
           isinstance(node, javalang.tree.EnumDeclaration)

    # TODO: Handle annotation declarations
    if isinstance(node, javalang.tree.AnnotationDeclaration):
        return Unit("annotation", {}, {}, {})
    # TODO: Handle enum declarations
    if isinstance(node, javalang.tree.EnumDeclaration):
        return Unit("enum", {}, {}, {})

    fields = dict()
    functions = dict()
    units = dict()
    for n in [n for n in node.body if _is_public(n)]:

        # Convert fields.
        if isinstance(n, javalang.tree.FieldDeclaration):
            for declarator_name in _get_declarator_names(n):
                type_object = type_system.qualify_lookup(n.type, compilation)
                fields[declarator_name] = Field(declarator_name, type_object)

        # Convert methods.
        elif isinstance(n, javalang.tree.MethodDeclaration):
            parameters = _get_parameters(n, type_system, compilation)
            return_type = _get_return_type(n, type_system, compilation)
            # Java supports method overloading, where methods with the
            # same name can be differentiated by their parameters and return type.
            # To fit this into autobump's common representation, we set
            # the first parameter of the method to be the return type.
            # Then, we set a dummy type that is compatible with anything as the
            # actual return type of the function.
            parameters = [Parameter("$AUTOBUMP_RETURN$", return_type)
                          ] + parameters
            if n.name in functions:
                functions[n.name].signatures.append(Signature(parameters))
            else:
                functions[n.name] = Function(n.name, _dummyType,
                                             [Signature(parameters)])

        # Convert inner classes and interfaces.
        elif isinstance(n, javalang.tree.ClassDeclaration) or \
             isinstance(n, javalang.tree.InterfaceDeclaration):
            units[n.name] = _class_or_interface_to_unit(
                n, compilation, type_system)

    fqn = _qualify_type(node, compilation)
    return Unit(fqn, fields, functions, units)
Esempio n. 6
0
 def test_change_signature_add_parameter_default(self):
     self.first["foo"] = (Function(
         "foo", _generic,
         [Signature([Parameter("a", _generic),
                     Parameter("b", _generic)])]))
     self.second["foo"] = (Function("foo", _generic, [
         Signature([
             Parameter("a", _generic),
             Parameter("b", _generic),
             Parameter("c", _generic, default_value=True)
         ])
     ]))
     self.expect(Bump.minor)
Esempio n. 7
0
 def read_signature(lst):
     verify_tag("signature", lst)
     tag, positional, optional = lst
     parameters = [
         Parameter(name, _read_type(type)) for name, type in positional
     ]
     # TODO: Properly handle default values (:or idiom).
     parameters += [
         Parameter(name, _read_type(type), default_value=True)
         for name, type in optional
     ]
     return Signature(parameters)
Esempio n. 8
0
def _xml_get_signature_of_method(elt):
    """Convert all <signature>s of a <method> into a Signature."""
    assert elt.tag == "method"
    signature_elt = elt.find("signature")
    parameter_elts = signature_elt.findall("parameter")
    parameters = [
        Parameter(p.attrib["name"], _xml_element_to_type(p.find("type")))
        for p in parameter_elts
    ]
    return_type = Parameter("$AUTOBUMP_RETURN$",
                            _xml_element_to_type(elt.find("type")))
    parameters = [return_type] + parameters
    return Signature(parameters)
Esempio n. 9
0
def _get_signature(function):
    """Return the signature of a function AST node."""
    parameters = []
    args = function.args.args

    # Map all None parameters to a "TrueNone" object
    # because None indicates the absense of a default value.
    class TrueNone(object):
        pass

    defaults = [
        TrueNone if isinstance(a, ast.NameConstant) and a.value is None else a
        for a in function.args.defaults
    ]
    # Prepend no default values.
    defaults = [None] * (len(args) - len(defaults)) + defaults

    args_with_defaults = list(zip(args, defaults))
    for arg_with_default in args_with_defaults:
        arg, default = arg_with_default
        if isinstance(default, ast.Name):
            # TODO: This does not differentiate between
            # "abc" and abc.
            default = default.id
        elif isinstance(default, ast.NameConstant):
            default = default.value
        elif isinstance(default, ast.Num):
            default = default.n
        elif isinstance(default, ast.Str):
            default = default.s
        type = _get_type_of_parameter(function, arg.arg)
        parameters.append(Parameter(arg.arg, type, default))
    # Note: we need to return a list with the signature inside
    # because the common representation allows for overloading,
    # which Python doesn't.
    return [Signature(parameters)]
Esempio n. 10
0
 def test_change_signature_parameter_type_incompatible(self):
     self.first["foo"] = (Function("foo", _a,
                                   [Signature([Parameter("a", _generic)])]))
     self.second["foo"] = (Function(
         "foo", _incompatWithA, [Signature([Parameter("a", _generic)])]))
     self.expect(Bump.major)