software-mariodiana
6/24/2014 - 1:39 PM

A short example of how to build a Jython class subclassed from a Java class.

A short example of how to build a Jython class subclassed from a Java class.

# http://www.jython.org/archive/21/docs/subclassing.html


import java.lang
import java.util


class SafeStack(java.util.Stack):
    """
    http://docs.oracle.com/javase/7/docs/api/java/util/Stack.html
    """
    
    def __init__(self):
        # Note how you use a "Pythonized" constructor.
        java.util.Stack.__init__(self)
        
    def push(self, item):
        print "Pushing: %r" % (item)
        # Note how you must pass 'self' as an argument.
        java.util.Stack.push(self, item)
        
    def pop(self):
        try:
            # Note how you must pass 'self' as an argument.
            return java.util.Stack.pop(self)
        except java.lang.ArrayIndexOutOfBoundsException, e:
            return None
        
    def peek(self):
        try:
            # Note how you must pass 'self' as an argument.
            return java.util.Stack.peek(self)
        except java.util.EmptyStackException, e:
            return None