from java import awt class SpamListener(awt.event.ActionListener): def actionPerformed(self,event): if event.getActionCommand() == "Spam": print 'Spam and eggs!'
f = awt.Frame("Subclassing Example") b = awt.Button("Spam") b.addActionListener(SpamListener()) f.add(b, "Center") f.pack() f.setVisible(1)Note: This example can be accomplished much more elegantly by using JavaBeans properties (and event properties).
SuperClass.foo(self)If SuperClass is a Java class instead of a Python one, I must instead use the form:
self.super__foo()For every method in SuperClass, one of the magic "super__" methods will be created that can be used to call it. This difference is required by the different method lookup semantics in Python and Java and I think the current solution is the best that can be found.
from java.io import InputStream class InfiniteOnes(InputStream): def read(self, *args): if len(args) > 0: return apply(self.super__read, args) return 1 io = InfiniteOnes() for i in range(10): print io.read(), print
from java.io import DataInputStream dp = DataInputStream(io) dp.skipBytes(1000) print dp.readByte() print dp.readShort() print dp.readInt()
It's important to realize that your superclass is not initialized until you either explictly call it's "__init__" method, or your own "__init__" method terminates. You must do one of these two things before accessing any methods in your superclass.
from java.util import Random class rand(Random): def __init__(self, multiplier=1.0, seed=None): self.multiplier = multiplier if seed is None: Random.__init__(self) else: Random.__init__(self, seed) def nextDouble(self): return self.super__nextDouble()*self.multiplier r = rand(100, 23) for i in range(10): print r.nextDouble(), printThis example shows how the superclass's constructor can be effectively called in order to explictly choose a non-empty version.
You can set the "python.proxy.savedir" variable in your registry file. You must set this to point to a directory in your Java Classpath. I strongly reccommend setting it to <JPYTHON>/JavaCode where JPython is the directory into which the distribution was installed. This will cause JPython to cache copies of any new proxy classes that it creates.
You can also explicitly create proxy classes with the following command:
java org.python.compiler.ProxyMaker <directory> <classname>This will create the proxy class for a given Java class and install it in the given directory (which must be on your Java Classpath - see above).
The danger of these caching mechanisms is that JPython currently has
no way to invalidate this cache. So if you install an improved version
of JPython that produces different proxy files, or if you modify one of
the classes to which you've previously created a proxy, the change won't
be properly handled. So, only use proxy class caching if you feel
comfortable with JPython already, and if you start running into problems
with proxy classes, my first suggestion is to disable proxy class caching
and to delete your cache directory (<directory>/org/python/proxies).