我的Python-Java接口,设计得好吗?以及如何包装 JNI 函数?

发布于 2024-12-14 10:47:09 字数 1265 浏览 1 评论 0原文

我将编写自己的 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 调用 实例。

内置类型

  • 我正在做JIntJShortJLontJChar等。成为 内置包装类型。

    喜欢:

    JInt = JClass('java/lang/Integer')
    JShort = JClass('java/lang/Short')
    JString = JClass('java/lang/String')
    

问题

  1. 您对此设计有何看法?
  2. 用于调用 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):

  1. What do you think about this design?
  2. 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 a va_list or something? I just need to find some way to call a method from Python in Java!

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

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

发布评论

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

评论(1

烟若柳尘 2024-12-21 10:47:09

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:

  • packages
  • privacy
  • interfaces/abstract classes
  • method resolution: overloads and overrides(especially when more than one method would match)
  • exceptions
  • type-checking, or recovering from type errors

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?

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