mirror of https://github.com/kivy/pyjnius.git
Python workarounds for wrong constructor selection. #474
This commit is contained in:
parent
019d0a2b7c
commit
cdd653edf5
|
@ -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:
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue