Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My Python-Java Interface, good design? And how to wrap JNI Functions?

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!
like image 882
Niklas R Avatar asked Nov 09 '11 20:11

Niklas R


People also ask

What is Jclass in JNI?

typedef jobject jclass; In C++, JNI introduces a set of dummy classes to enforce the subtyping relationship. For example: class _jobject {}; class _jclass : public _jobject {}; ...

What are JNI functions?

JNI is the Java Native Interface. It defines a way for the bytecode that Android compiles from managed code (written in the Java or Kotlin programming languages) to interact with native code (written in C/C++).

What is JNI Bridge?

Introduction to Java Native Interface: Establishing a bridge between Java and C/C++ JNI (Java Native Interface) is a foreign function interface that allows code running on JVM to call (or be called by) native applications. Using JNI, one can call methods written in C/C++ or even access assembly language.


1 Answers

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?

like image 194
Matt Fenwick Avatar answered Oct 17 '22 00:10

Matt Fenwick