work on varargs support

This commit is contained in:
gabriel.pettier 2012-08-26 02:51:22 +02:00
parent 3651d1ac20
commit 17258f100d
2 changed files with 36 additions and 12 deletions

View File

@ -134,13 +134,19 @@ cdef class JavaClass(object):
elif len(definitions) == 1:
definition = definitions[0]
d_ret, d_args = parse_definition(definition)
print args, d_args
if len(args) != len(d_args):
raise JavaException('Invalid call, number of argument'
' mismatch for constructor')
else:
scores = []
for definition in definitions:
for definition, is_varargs in definitions:
d_ret, d_args = parse_definition(definition)
if is_varargs:
args_ = args[:len(d_args) - 1] + (args[len(d_args) - 1:],)
else:
args_ = args
score = calculate_score(d_args, args)
if score == -1:
continue
@ -417,6 +423,7 @@ cdef class JavaMethod(object):
cdef bytes classname
cdef bytes definition
cdef object is_static
cdef bint is_varargs
cdef object definition_return
cdef object definition_args
@ -432,6 +439,8 @@ cdef class JavaMethod(object):
self.definition_return, self.definition_args = \
parse_definition(definition)
self.is_static = kwargs.get('static', False)
self.is_varargs = kwargs.get('varargs', False)
print self, self.is_varargs
cdef void ensure_method(self) except *:
if self.j_method != NULL:
@ -456,6 +465,7 @@ cdef class JavaMethod(object):
self.j_env = j_env
self.j_cls = j_cls
self.j_self = j_self
print "resolve", self, self.is_varargs
def __get__(self, obj, objtype):
if obj is None:
@ -470,6 +480,9 @@ cdef class JavaMethod(object):
# argument array to pass to the method
cdef jvalue *j_args = NULL
cdef list d_args = self.definition_args
if self.is_varargs:
args = args[:len(d_args) - 1] + (args[len(d_args) - 1:],)
if len(args) != len(d_args):
raise JavaException('Invalid call, number of argument mismatch')
@ -685,19 +698,19 @@ cdef class JavaMultipleMethod(object):
self.name = name
self.classname = classname
for signature, static in self.definitions:
for signature, static, is_varargs in self.definitions:
jm = None
if j_self is None and static:
if signature in self.static_methods:
continue
jm = JavaStaticMethod(signature)
jm = JavaStaticMethod(signature, varargs=is_varargs)
jm.set_resolve_info(j_env, j_cls, j_self, name, classname)
self.static_methods[signature] = jm
elif j_self is not None and not static:
if signature in self.instance_methods:
continue
jm = JavaMethod(signature)
jm = JavaMethod(signature, varargs=is_varargs)
jm.set_resolve_info(j_env, j_cls, None, name, classname)
self.instance_methods[signature] = jm
@ -714,19 +727,26 @@ cdef class JavaMultipleMethod(object):
for signature in methods:
sign_ret, sign_args = parse_definition(signature)
score = calculate_score(sign_args, args)
print methods[signature]
if methods[signature].is_varargs:
args_ = args[:len(sign_args) - 1] + (args[len(sign_args) - 1:],)
else:
args_ = args
score = calculate_score(sign_args, args_)
if score <= 0:
continue
scores.append((score, signature))
scores.append((score, signature, args_))
if not scores:
raise JavaException('No methods matching your arguments')
scores.sort()
score, signature = scores[-1]
score, signature, args_ = scores[-1]
jm = methods[signature]
jm.j_self = self.j_self
return jm.__call__(*args)
return jm.__call__(*args_)
class JavaStaticMethod(JavaMethod):

View File

@ -47,6 +47,7 @@ class Method(JavaClass):
getParameterTypes = JavaMethod('()[Ljava/lang/Class;')
getReturnType = JavaMethod('()Ljava/lang/Class;')
getModifiers = JavaMethod('()I')
isVarArgs = JavaMethod('()Z')
class Field(JavaClass):
@ -65,6 +66,7 @@ class Constructor(JavaClass):
toString = JavaMethod('()Ljava/lang/String;')
getParameterTypes = JavaMethod('()[Ljava/lang/Class;')
getModifiers = JavaMethod('()I')
isVarArgs = JavaMethod('()Z')
def get_signature(cls_tp):
tp = cls_tp.getName()
@ -98,7 +100,8 @@ def ensureclass(clsname):
def autoclass(clsname):
jniname = clsname.replace('.', '/')
cls = MetaJavaClass.get_javaclass(jniname)
if cls: return cls
if cls:
return cls
classDict = {}
@ -112,7 +115,7 @@ def autoclass(clsname):
for constructor in c.getConstructors():
sig = '({0})V'.format(
''.join([get_signature(x) for x in constructor.getParameterTypes()]))
constructors.append(sig)
constructors.append((sig, constructor.isVarArgs()))
classDict['__javaconstructor__'] = constructors
methods = c.getMethods()
@ -126,11 +129,12 @@ def autoclass(clsname):
# only one method available
if count == 1:
static = Modifier.isStatic(method.getModifiers())
varargs = method.isVarArgs()
sig = '({0}){1}'.format(
''.join([get_signature(x) for x in method.getParameterTypes()]),
get_signature(method.getReturnType()))
cls = JavaStaticMethod if static else JavaMethod
classDict[name] = cls(sig)
classDict[name] = cls(sig, varargs=varargs)
continue
# multpile signatures
@ -158,7 +162,7 @@ def autoclass(clsname):
print 'Abstract', Modifier.isAbstract(m)
print 'Strict', Modifier.isStrict(m)
'''
signatures.append((sig, Modifier.isStatic(method.getModifiers())))
signatures.append((sig, Modifier.isStatic(method.getModifiers()), method.isVarArgs()))
classDict[name] = JavaMultipleMethod(signatures)