pyjnius
pyjnius copied to clipboard
JavaMultipleMethod not working at all
Here's a piece of code that is getting the definitions of a method called click
inside a java class called Region
:
def get_signature(cls_tp):
tp = cls_tp.getName()
if tp[0] == '[':
return tp.replace('.', '/')
signatures = {
'void': 'V', 'boolean': 'Z', 'byte': 'B',
'char': 'C', 'short': 'S', 'int': 'I',
'long': 'J', 'float': 'F', 'double': 'D'}
ret = signatures.get(tp)
if ret:
return ret
return 'L{0};'.format(tp.replace('.', '/'))
def get_signature_tuple(obj, objtype):
is_varargs = obj.isVarArgs()
if objtype == 'method':
is_static = Modifier.isStatic(obj.getModifiers())
sig = '({0}){1}'.format(
''.join([get_signature(x) for x in obj.getParameterTypes()]),
get_signature(obj.getReturnType()))
return (sig, is_static, is_varargs)
elif objtype == 'constructor':
sig = '({0})V'.format(
''.join([get_signature(x) for x in obj.getParameterTypes()]))
return (sig, is_varargs)
def run():
from pprint import pprint
klass = 'org/sikuli/script/Region'
c = find_javaclass(klass)
methods = c.getMethods()
method_signatures = [get_signature_tuple(method, 'method') for method in methods if method.getName() == 'click']
constructor_signatures = [get_signature_tuple(constructor, 'constructor') for constructor in constructors]
pprint(method_signatures)
The above code gives the following result, when the python function run
is run:
[
('(Ljava/lang/Object;)I', False, False),
('(Ljava/lang/Object;Ljava/lang/Integer;)I', False, False),
('()I', False, False)
]
So, we now have proof that the Region
class has a method called click
with three different possible definitions.
- The first definition says, it accepts an Object as argument1; returns an integer
- The second definition says, it accepts an Object as argument1, and Integer as argument2; returns an integer
- The third definition is irrelevant to us
Now, let's look at the following code:
class SRegion(JavaClass):
__javaclass__ = 'org/sikuli/script/Region'
__metaclass__ = MetaJavaClass
__javaconstructor__ = [
('(II)V', False),
('(IIII)V', False),
('(IIIII)V', False),
('(IIIILorg/sikuli/script/IScreen;)V', False),
('(Lorg/sikuli/script/Region;)V', False),
('(Ljava/awt/Rectangle;)V', False)]
click = JavaMultipleMethod([
('(Ljava/lang/Object;)I', False, False),
('(Ljava/lang/Object;Ljava/lang/Integer;)Ljava/lang/Object', False, False),
('()I', False, False)
])
def run():
target1 = '4-7aure5.png'
Screen = autoclass('org.sikuli.script.Screen')
reg = SRegion(Screen().getRect())
try:
match = reg.click(target1) # this works fine
except Exception, e:
logger.debug(e)
else:
logger.debug('Success for single argument')
try:
match = reg.click(target1, 1) # this fails
except Exception, e:
logger.debug('Failed for multiple arguments')
logger.debug('Error: {}'.format(e))
The above code spits the following result:
Success for single argument
Failed for multiple arguments
Error: No methods matching your arguments
In other words, pyjnius
is not able to find the overloaded method click
matching the second defintion above. Can anyone help with this please?