bcdev / jpy

A bi-directional Python-Java bridge used to embed Java in CPython or the other way round.
Apache License 2.0
187 stars 37 forks source link

Subclassing/Inheriting Java classes? #164

Open csachs opened 5 years ago

csachs commented 5 years ago

I'd like to subclass / inherit Java types from within Python, however this seems not to work: Example:

import jpyutil
jpyutil.init_jvm()
import jpy

BigInteger = jpy.get_type("java.math.BigInteger")

print(BigInteger("1"))  # works

class MyBigInteger(BigInteger):
    def additionalMethod(self):
        print("...")

print(MyBigInteger("1")) # does not work

Yields

RuntimeError: no constructor found (missing JType attribute '__jinit__')

(Any relation to https://github.com/bcdev/jpy/issues/155 ?)

Am I doing something wrong? Is there any easy work-around for the situation? (Theoretically one proxy all calls etc., but I'd prefer if Python's OOP would remain intact) (Actually, I'd like to override certain Java-land methods and feed the subclassed object back to the Java-land, to be able to easily call them by the parent object's definition …)