diff --git a/jnius/jnius_export_class.pxi b/jnius/jnius_export_class.pxi index 53e8e15..9773122 100644 --- a/jnius/jnius_export_class.pxi +++ b/jnius/jnius_export_class.pxi @@ -253,7 +253,7 @@ cdef class JavaClass(object): self.j_cls = jcs.j_cls if 'noinstance' not in kwargs: - self.call_constructor(args) + self.call_constructor(args, kwargs) self.resolve_methods() self.resolve_fields() @@ -262,7 +262,7 @@ cdef class JavaClass(object): self.resolve_methods() self.resolve_fields() - cdef void call_constructor(self, args) except *: + cdef void call_constructor(self, args, kwargs) except *: # the goal is to find the class constructor, and call it with the # correct arguments. cdef jvalue *j_args = NULL @@ -297,9 +297,18 @@ cdef class JavaClass(object): ) else: scores = [] + requestedDefn = kwargs.get('param_types', None) for definition, is_varargs in definitions: found_definitions.append(definition) d_ret, d_args = parse_definition(definition) + #print(definition) + #print(d_args) + if requestedDefn == definition: + assert not is_varargs + score=1 + scores.append((score, definition, d_ret, d_args, args)) + break + if is_varargs: args_ = args[:len(d_args) - 1] + (args[len(d_args) - 1:],) else: diff --git a/tests/java-src/org/jnius/ConstructorTest.java b/tests/java-src/org/jnius/ConstructorTest.java index a09c021..2b6edd5 100644 --- a/tests/java-src/org/jnius/ConstructorTest.java +++ b/tests/java-src/org/jnius/ConstructorTest.java @@ -20,4 +20,12 @@ public class ConstructorTest { public ConstructorTest(int cret, char charet) { ret = cret + (int) charet; } + + public ConstructorTest(Object DO_NOT_CALL) { + throw new Error(); + } + + public ConstructorTest(java.io.OutputStream os) { + ret = 42; + } } diff --git a/tests/test_constructor.py b/tests/test_constructor.py index f619155..f37eca6 100644 --- a/tests/test_constructor.py +++ b/tests/test_constructor.py @@ -43,6 +43,18 @@ class TestConstructor(unittest.TestCase): self.assertEqual(inst.ret, ord('a')) self.assertTrue(ConstructorTest.getClass().isInstance(inst)) + + def test_constructor_multiobj(self): + ''' + Constructor expecting String. + ''' + + outputStream = autoclass('java.lang.System').out + ConstructorTest = autoclass('org.jnius.ConstructorTest') + inst = ConstructorTest(outputStream, param_types="(Ljava/io/OutputStream;)V") + self.assertEqual(inst.ret, 42) + self.assertTrue(ConstructorTest.getClass().isInstance(inst)) + def test_constructor_int_string(self): ''' Constructor expecting int and char, casting char to int and summing it