My Python-Java interface, good design? And how to wrap JNI functions?

I am going to write my own Python-Java interface. It is compiled as a DLL and wrapped using ctypes.

However, you can find Java classes and highlight Java objects. But what will be the interface to another language without using these objects methods? My goal is to make it as natural as possible. Unfortunately, it is not possible to find Java methods by name alone.

My model is this:

Jclass

  • An instance of this class represents a Java class.

Jobject

  • An instance of this class is a Java object. Must be initialized with an instance of JClass. (but, of course, later should also be arguments for the constructor.)

Jmethod

  • Represents a method of a Java object. It contains the name and signature of the desired method. The signature is dynamically evaluated by the classes specified during initialization.

    Example:

    mainMethod = JMethod('main', JStringArray) 

    Note that JStringArray is an instance of JClass that represents a string array.

    JMethod can be added to a JClass instance. But then it can only be called from a JObject instance.

JStaticMethod

  • Like JMethod, but it can also be called from a JClass instance.

Built-in types

  • I do JInt , JShort , JLont , JChar , etc. to be built-in JChar types.

    how

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

Question (s)

  • What do you think of this project?
  • JNI-Functions to call the methods of the Java class / object all take a variable number of arguments. After reading several topics, calling a function with variable arguments from a function that does this, and also asked a question about SO here, I know this is not possible.
    Now there are functions that do not accept a variable number of arguments but a va_list or something else? I just need to find a way to call a method from Python in Java!
+6
source share
1 answer

1. What do I think of this design?

  • It’s not clear what actual problem you are trying to solve.

  • in extreme cases; error processing; Forward / backward compatibility; errors in Python / Java? Not fun, but important for reliable software.

  • Mixing two languages ​​is difficult enough, mixing three will probably be much worse. I would expect serious maintainability and communication problems.

  • there are already solutions to these problems. RPC, so that programs in different languages ​​speak to each other. Jython for Java / Python interoperability. I believe that Jython even allows you to create Python objects in Java and vice versa. It would be useful to clarify any shortcomings of these existing systems and how to address these shortcomings.

Here are some missing things:

  • packages
  • confidentiality
  • interfaces / abstract classes
  • method resolution: overloads and overrides (especially when matching multiple methods)
  • exceptions
  • type checking or recovering from type errors

2. I just need to find a way to call a method from Python in Java! What about Jython, RPC, or just calling an executable?

+11
source

Source: https://habr.com/ru/post/901153/


All Articles