Extending Java Objects

348 views
Skip to first unread message

John Hubbard

unread,
Apr 18, 2017, 4:01:06 PM4/18/17
to Jep Project
I have some experience using Jython as a Python interpreter inside of a JVM.  I am now looking at using JEP to do the same (mostly to get Python 3 support).  The first hurdle that I've run into is that I have written Jython code that extends Java Objects.  When I try to run the same code in JEP I run into problems (see below).  Using JEP is it possible to write a Python class which extends a Java Object or implements a Java Interface?  

Jython code:
from java.lang import Object as JavaObject
class MyJavaObject(JavaObject): pass
jobj = MyJavaObject()
print(jobj.toString())

produces:
org.python.proxies.__main__$MyJavaObject$0@4b6e1c0

When trying to evaluate the 'class MyJavaObject(JavaObject) pass'  portion of the above code in JEP I get:

jep.JepException: <class 'TypeError'>: cannot create 'jep.PyJClass' instances
        at <string>.<module>(<string>:1)
        at <string>.<module>(<string>:3)


Thanks

Ben Steffensmeier

unread,
Apr 18, 2017, 4:49:28 PM4/18/17
to John Hubbard, Jep Project
You cannot extend a java class but you can implement one or more java interfaces using the jproxy method. There has been a few posts on this in the past here and here and this is not something that has changed. I threw together a quick example showing how to implement the Comparator interface in python that you may be able to use as a starting point:

>>> from jep import jproxy
>>> from java.util import Collections
>>> from java.util import ArrayList
>>>
>>> list = ArrayList(['a', 'b', 'c', 'A', 'B', 'C'])
>>>
>>> class InsensitiveCharComparator(object):
...     def compare(self, s1, s2):
...         return ord(s1.lower()) - ord(s2.lower())
...
>>> pyComparator = InsensitiveCharComparator()
>>> jComparator = jproxy(pyComparator, ['java.util.Comparator'])
>>> str(list)
'[a, b, c, A, B, C]'
>>> Collections.sort(list, jComparator)
>>> str(list)
'[a, A, b, B, c, C]'

Ben

--
You received this message because you are subscribed to the Google Groups "Jep Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jep-project+unsubscribe@googlegroups.com.
To post to this group, send email to jep-p...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jep-project/d11833e6-054f-4ab0-9b83-48ef97eebe6b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

John Hubbard

unread,
Apr 18, 2017, 7:04:14 PM4/18/17
to Jep Project, ende...@gmail.com
Thanks for the quick response.  Do you know if anyone is working on allowing Python objects to extend Java objects?  Also a quick test seems to indicate that implementing an interface won't work when calling a default method on the interface; is that expected?  

To unsubscribe from this group and stop receiving emails from it, send an email to jep-project...@googlegroups.com.

Ben Steffensmeier

unread,
Apr 19, 2017, 9:01:40 AM4/19/17
to Jep Project
I've considered trying to allow python types to extend java classes. I don't think it is possible without generating bytecode or pulling in a new dependency to handle the class generation which is a significant new direction to take the project. I have a few ideas how we could make the handling of python objects in java more robust which would make it easier to implement that kind of feature. Unfortunately for my job we are mostly concerned with java objects in python so that is where I am most familiar and I don't find much time to work on the python-in-java side.

It is a known issue that default methods don't work well. There is a cool pull request on github that includes some discussion about default methods. That is one of the important features I am hoping to get into jep 3.7, but I haven't had a chance to look at it in several months so there is no definitive roadmap.

Ben



To unsubscribe from this group and stop receiving emails from it, send an email to jep-project+unsubscribe@googlegroups.com.

To post to this group, send email to jep-p...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages