Java / CPython Language Bridge

I've implemented support for two-way compatibility between Java and Python/CPython, meaning that Java can import standard CPython modules and use code as if it were Java, and Python can import Java classes and use them as if they were Python classes.

Imports in both languages are done with standard import statements.

How It Works

Java code imported into Python is subjected to reflection and then Python wrapper classes are generated using the Python API that provide runtime translations between the two language runtimes. A native Python module is provided that provides the "magic import" functionality from Java to Python (more info).

Python code imported into Java is introspected using the Python C API and then bytecode is generated for Java wrapper classes that provide a similar runtime translation as the imports in the other direction. A custom compiler based on is provided (QoreJavaCompiler) as well as runtime dynamic code generation support for wrapper APIs in a custom class loader (QoreURLClassLoader) - more info.

This is all accomplished using a third language called Qore that provides the functionality of a "language bridge" and also manages the references to data in each separate runtime. Because Qore has a deterministic garbage collector, strong references to Python and Java data are managed in Qore and released when there are no more valid references to the data (in Qore - therefore various tricks are used to manage references transparently including sing thread-local data; there is also the possibility of using custom reference handlers to maintain the validity of objects using different language runtimes and garbage collection implementations). A high level description of the approach is here.

While support for importing Java into Python has been released, importing Python into Java will be released in April 2021 but is currently working and stable.

The idea is to facilitate the mixed use of enterprise technologies (Java) and AI / data science technologies (Python).

All of the source code required has been released under permissive open source licenses (MIT).

I hope it could be interesting and useful for someone - happy to provide more information if there's interest.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

compared to what?

i would guess that GraalVM Python is probably your main "competitor"?

Re: compared to what?

This integration is for native CPython; GraalVM is a great solution for native compilation of Python code, but is unfortunately not compatible with a lot of Python modules that have hard dependencies on CPython.

In an ideal world all Python modules would run on all standards-compliant Python implementations, but unfortunately that's not the case, so this implementation is meant to serve anyone needing Python Java integration with standard CPython.