将对象从 Java 传递到 Jython 时保留对象的 Java 类型

发布于 2024-07-15 20:05:23 字数 1735 浏览 3 评论 0原文

我想知道当您将 java 对象放入 Java ArrayList 中时,是否有可能让 jython 自动将 java 对象转换为 python 类型。

从 jython-console 复制的示例:

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

到目前为止,一切都很好,但是如果我将对象放入 ArrayList 中,

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

该对象将转换为类似 python 的布尔值(即 int)并且...

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

这意味着我无法再看到这曾经是一个 java.lang.Boolean。

澄清

我想真正想要实现的是在将对象从 Java 传递到 Python 时摆脱从 Java 类型到 Python 类型的隐式转换。 我将再举一个例子来澄清。

Python 模块:

import java

import IPythonModule

class PythonModule(IPythonModule):

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

以及使用该模块的 Java 类:

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 类 这里

I wonder if it possible to not have jython automagicaly transform java objects to python types when you put them in a Java ArrayList.

Example copied from a jython-console:

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

So far, everything is fine but if I put the object in an ArrayList

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

the object is transformed into a python-like boolean (i.e. an int) and...

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

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

Clarification

I guess what really want to achieve is to get rid of the implicit conversion from Java-types to Python-types when passing objects from Java to Python. I will give another example for clarification.

A Python module:

import java

import IPythonModule

class PythonModule(IPythonModule):

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

And a 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"));
    }   
}

Here I will see the output 'bool' because of the implicit conversion, but what I would really like is to see 'javainstance' or 'java.lang.Boolean'. If you want to run this code you will also need the JythonFactory-class that can be found here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素年丶 2024-07-22 20:05:23

您似乎使用的是旧版本的 Jython。 在当前的 Jython 版本中,Python bool 类型对应于 Java Boolean

Jython 不会在进入 ArrayList 的过程中将 Java 类型转换为 Python 类型 - 相反,当将原始 Python 类型传递给 Java 方法时,它会将原始 Python 类型转换为原始或包装 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

You appear to be using an old version of Jython. In current Jython versions, the Python bool type corresponds to a Java Boolean.

Jython is not transforming the Java type to a Python type on the way into the ArrayList - on the contrary, it will transform a primitive Python type to a primitive or wrapper Java type when passing it to a Java method, and a Java type to a Python type on the way out.

You can observe this by printing the contents of the array. Note that the Python bool is capitalized (True); the Java Boolean is not.

>>> 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]

If this still doesn't do what you want, consider writing a small Java helper function that examines the array for you without conversion. It's arguably a bug that Jython doesn't automatically convert the Boolean you constructed into a Python bool, and in this case it gives you no advantage over using Boolean.TRUE or the Python True.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文