Saving Java object type when passing it from Java to Jython

I wonder if jython automagicaly might not be able to convert java objects to python types when you put them in a Java ArrayList.

Example copied from jython console:

>>> b = java.lang.Boolean("True");
>>> type(b)
<type 'javainstance'>
>>> isinstance(b, java.lang.Boolean);
1

So far so good, but if I put an object in an ArrayList

>>> l = java.util.ArrayList();
>>> l.add(b)
1
>>> type(l.get(0))
<type 'int'>

object is converted to python-like boolean (i.e. int) and ...

>>> isinstance(l.get(0), java.lang.Boolean)
0

which means that I can no longer see that it was once java.lang.Boolean.

Explanation

I guess I really want to get rid of the implicit conversion from Java types to Python types when passing objects from Java to Python. I will give one more example for clarification.

Python module:

import java

import IPythonModule

class PythonModule(IPythonModule):

    def method(self, data):
        print type(data);

And the Java class that uses this module:

import java.util.ArrayList;

import org.python.core.PyList;
import org.testng.annotations.*;

import static org.testng.AssertJUnit.*;

public class Test1 {

    IPythonModule m;

    @BeforeClass
    public void setUp() {
        JythonFactory jf = JythonFactory.getInstance();
        m = (IPythonModule) jf.getJythonObject(
                "IPythonModule",
        "/Users/sg/workspace/JythonTests/src/PythonModule.py");
    }

    @Test
    public void testFirst() {
        m.method(new Boolean("true"));
    }   
}

"bool" - , "javainstance" "java.lang.Boolean". , JythonFactory, .

+5
1

, Jython. Jython Python bool Java Boolean.

Jython Java Python ArrayList - , Python Java Java Java Python .

, . , Python bool (True); Java Boolean - .

>>> from java.lang import Boolean
>>> b = Boolean('True')
>>> b      
true
>>> from java.util import ArrayList
>>> l = ArrayList()
>>> l.add(b)
True
>>> l
[true]
>>> l.add(True)
True
>>> l
[true, true]
>>> list(l) 
[True, True]

, , , Java, . , , Jython Boolean, Python bool, Boolean.TRUE Python True.

+1

All Articles