Calling right overload of the java method from jython

点点圈 提交于 2019-12-11 02:22:45

问题


I am using java library, that has overloaded methods in the class that I use.

JAVA:
void f(float[]);
void f(Object[]);

Now I call this class from jython, and I want to call the Object[] overload. The problem is that python sees my array as array of floats, and therefore calls the wrong overload methods.

JYTHON:
f([[1, 1.0]) 

How to force the Object[] method to be executed?


回答1:


It took me considerable amount of time to find out, so I decided to post a question together with the answer.

Forcing the right overload

Jython documentation tells that in order to to force a call of right overload you should cast arguments to java objects manually before the call:

from java.lang import Byte
foo(Byte(10))

However that doesn't work with java arrays.

Forcing the right overload for array

http://www.jython.org/archive/22/userguide.html#java-arrays

It's possible to create java arrays in jython. For example the following code will create in jython java array of type int[]

from jarray import array
array(python_array, 'i')

You can create Object[] like this, and force java to call the right overload.

from jarray import array
from java.lang import Object
oa = array(python_array, Object)
f(oa)


来源:https://stackoverflow.com/questions/21329491/calling-right-overload-of-the-java-method-from-jython

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!