我的Python-Java接口,设计得好吗?以及如何包装 JNI 函数?
我将编写自己的 Python-Java 接口。它被编译为 DLL 并 使用 ctypes 包装。
然而,找到 Java 类并分配 Java 对象是可能的。 但是如果不使用这些对象,另一种语言的接口会是什么? 方法?我的目标是让它尽可能自然。很遗憾, 仅通过名称来查找 Java 方法是不可能的。
我的模型如下:
JClass
- 此类的实例代表 Java 类。
JObject
- 此类的实例代表 Java 对象。它必须是 使用 JClass 实例初始化。 (当然,后来 也应该是构造函数的参数。)
JMethod
表示 Java 对象的方法。它包含所需方法的名称和签名。签名由初始化时给出的类动态评估。
示例:
mainMethod = JMethod('main', JStringArray)
请注意,
JStringArray
是表示字符串数组的 JClass 实例。可以将 JMethod 添加到 JClass 实例。但只能从实例化的 JObject 调用。
JStaticMethod
- 就像 JMethod 一样,但也可以从 JClass 调用 实例。
内置类型
我正在做
JInt
、JShort
、JLont
、JChar
等。成为 内置包装类型。喜欢:
JInt = JClass('java/lang/Integer') JShort = JClass('java/lang/Short') JString = JClass('java/lang/String')
问题:
- 您对此设计有何看法?
- 用于调用 Java 类/对象所有方法的 JNI-Functions 接受可变数量的参数。阅读了几个主题后 从执行此操作的函数中调用具有可变参数的函数, 还在这里问了一个问题,我知道这是不可能的。
现在,是否有不接受可变数量参数的函数 但是va_list
还是什么?我只需要找到一些方法在 Java 中调用 Python 的方法!
I'm going to write my own Python-Java interface. It is compiled as a DLL and
wrapped using ctypes.
Yet, it is possible to find Java-classes and allocate Java-objects.
But what would be an interface to another language without using those objects
methods? My aim is to make this as natural as possible. Unfortunately,
it isn't just possible to find Java-methods only by name.
My model is the following:
JClass
- An instance of this class represents a Java class.
JObject
- An instance of this class represents a Java-object. It has to be
initialized with a JClass-instance. (yet, of course, later there
should be arguments for the constructor also.)
JMethod
Represents a method of a Java-object. It contains the name and signature of the desired method. The signature is evaluated dynamically by the classes that are given on initialization.
Example:
mainMethod = JMethod('main', JStringArray)
Note that
JStringArray
is an instance of JClass that represents a string-array.A JMethod can be added to a JClass instance. But can then be called only from an instantiated JObject.
JStaticMethod
- Just like the JMethod, but it can also be called from a JClass
instance.
Built-In types
I'm doing
JInt
,JShort
,JLont
,JChar
, etc.. to be the
built-in wrapper types.Like:
JInt = JClass('java/lang/Integer') JShort = JClass('java/lang/Short') JString = JClass('java/lang/String')
Question(s):
- What do you think about this design?
- The JNI-Functions for calling methods of a Java-class / -object all
take a variable amount of arguments. After reading several topics on
calling a function with variable arguments from a function that does so,
and also asked a question here on SO, I'm aware that this is not possible.
Now, are there functions that don't take a variable number of arguments
but ava_list
or something? I just need to find some way to call a method from Python in Java!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1.我对此设计有何看法?
不清楚您要解决的实际问题是什么。
边缘情况怎么样;错误处理;向前/向后兼容性; Python/Java 中的错误?不好玩,但对于健壮的软件来说至关重要。
混合两种语言已经够困难的了,混合三种语言肯定会更糟糕。我预计会出现主要的可维护性和耦合问题。
这些问题已经有了解决方案。 RPC,用于让不同语言的程序相互通信。 Jython,用于 Java/Python 互操作性。我相信,Jython 甚至允许您直接在 Java 中创建 Python 对象,反之亦然。澄清这些现有系统的缺点以及如何解决这些缺点将会有所帮助。
这里有一些缺失的东西:
2。我只需要找到某种方法在 Java 中从 Python 调用方法! Jython、RPC 或者只是调用可执行文件怎么样?
1. What do I think of this design?
it's not clear what actual problem you're trying to solve.
what about edge cases; error-handling; forward-/backward-compatibility; bugs in Python/Java? Not fun, but essential for robust software.
mixing two languages is hard enough, mixing three is sure to be much much worse. I would expect major maintainability and coupling problems.
there are already solutions to these problems. RPC, for getting programs in different languages to talk to each other. Jython, for Java/Python interoperability. I believe, Jython even allows you to create Python objects in Java and vice versa directly. Clarifying any shortcomings of these existing systems, and how you would address these shortcomings, would be helpful.
Here are a few missing things:
2. I just need to find some way to call a method from Python in Java! What about Jython, RPC, or just calling an executable?