jpype-project / jpype

JPype is cross language bridge to allow Python programs full access to Java class libraries.
http://www.jpype.org
Apache License 2.0
1.12k stars 181 forks source link

Clarity in Implementation of interfaces with default methods #1181

Closed petrushy closed 2 weeks ago

petrushy commented 6 months ago

I can not find how jpype behaves for interfaces with default methods?

I have a class in EventHandler (https://www.orekit.org/site-orekit-12.0.1/jacoco/org.orekit.propagation.events.handlers/EventHandler.java.html) which has one required and two default methods, of which one is init(). It seems like in the JImplements of this class, the jpype requires the required + init (which is a method with default implementation). Why is init part of required methods? Is the other default method optional to implement in python - will the python code be used if it is implemented?

Many thanks for a very interesting tool.

Thrameos commented 6 months ago

I believe the problem is that type resolution function is unable to reach init due to its meta data. I replicated the problem locally, though I currently don’t know the source. Default methods are wired into Java in a rather odd way because they didn’t exist in the original specification, thus instead they appear more like methods in the derived class which in the case of Proxy doesn’t exist. It doesn’t seem to require them to consider the class complete as jpype isn’t producing a warning. Meaning if we can call the method, it would be fine. Investigating further the problem is not actually JPype, but instead Java itself. There is no easy way to access default methods of a proxy because any attempt to call the default method simply redirects back to the proxy at least last time I checked. I managed to patch something in using MethodHandles. Hopefully that addresses the issue.