[Erlend V Bøe]
>I'm having some problems with the following situation:
>
>I have a Java class that I extend in Jython.
>When I try to use this Jython class from Java, I get an exception similar to
>the following: "TypeError: instance already instantiated for
>problem.JavaClass"
>
>What am I doing wrong?
You need a python constructor in your PyClass, and when using jythonc,
the constructor must *not* call the superclass ctor. Something along the
lines of:
import problem
_jythonc = 1
class PyClass(problem.JavaClass):
def __init__(self, s=None):
if not _jythonc:
problem.JavaClass.__init__(self, s)
def myMethod(self):
"@sig void myMethod()"
print "python method myMethod called"
if __name__ == "__main__":
_jythonc = 0
x = PyClass("hi from python")
x.myMethod()
In the generated PyClass.java, we have code like:
public class PyClass extends problem.JavaClass
implements org.python.core.PyProxy
{
public PyClass(java.lang.String arg0) {
super(arg0);
Py.initProxy(this, ... new Object[] {arg0}, ...);
}
}
i.e, the JavaClass ctor is called before any python code gets to
execute. Without the __init__() method, jython will automaticly call the
JavaClass ctor (again) and the exception "instance already instantiated"
is thrown.
regards,
finn
|