ninia / jep

Embed Python in Java
Other
1.31k stars 149 forks source link

does Jep enable Python classes to subclass Java classes? #486

Open mike-hunhoff opened 1 year ago

mike-hunhoff commented 1 year ago

Jep throws TypeError: cannot create 'jep.PyJClass' instances when attempting to subclass Java classes using Python classes. jep.jproxy appears to get us there for Java interfaces but throws ValueError: java.lang.IllegalArgumentException: <java_class_impl> is not an interface when attempting to proxy Java class implementations.

Any thoughts?

see https://github.com/mandiant/Ghidrathon/issues/55 for more context.

bsteffensmeier commented 1 year ago

That is not something that is currently possible. As far as I know there is no mechanism in Java to dynamically define classes at runtime so we cannot create a Java class corresponding to the python class. Without a Java class you can't create a new object of the subcla

With interfaces we are using the Proxy class built into the JVM to dynamically define classes that call back into the python but there is no similar mechanism for concrete classes.

Theoretically we could add the functionality by generating bytecode or using libraries like Javassist or CGLIB but I am hesitant to add dependencies to jep and it would be a significant undertaking. It's not something I have time to investigate further.

The only workaround I can think of is to provide interfaces for any functionality you want to be able to override from python so that it can work with jproxy. I know this is inconvenient and may not be possible in all cases but it is the only way to make python classes that the JVM understands.

I am hoping to make the jproxy functionality more automatic in the future, so that you can extend a java interface using a normal python class definition instead of requiring a call to jproxy, but I don't think that will be possible unless we replace PyJClass with pyjtype and there is still other work to do in that direction so it isn't likely to be coming soon.