2

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 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.