From: <bc...@wo...> - 2001-03-21 10:46:38
|
[mike] >I am running into a problem trying to subclass a java class from >Jython2.0. The problem lies in the fact that the java class I extend from >uses reflection to implement the template method pattern. The methods it is >looking for is implemented in my jython class. Here is an example using the >JUnit framework which illustrates my problem: > >---- example.py ---- >import java >import junit > >class MyJavaClassTest(junit.framework.TestCase): > > def __init__(self, name): > junit.framework.TestCase.__init__(self, name) > > def setUp(self): > "@sig public void setUp()" > pass > > def testMyJavaClass(self): > "@sig public void testMyJavaClass()" > pass > >junit.textui.TestRunner.run(MyJavaClassTest("testMyJavaClass")) >---- end example.py ---- > >Running this from the command line I get: > >$ cmd /c c:/devel/jython-2.1/jython.bat example.py >.F >Time: 0 >There was 1 failure: >1) testMyJavaClass(org.python.proxies.__main__$MyJavaClassTest$1) "Method "testM >yJavaClass" not found" > >FAILURES!!! >Tests run: 1, Failures: 1, Errors: 0 > >As you can see the JUnit TestCase class is using reflection to look for the >testMyJavaClass method. Will this example only work if I run jythonc? Yes. >It >looks like the autogenerated proxy class is ignoring the "@sig" >information. Correct. This is unlikely to change. >Also, with the upcoming 2.1 release, will function attributes be used to >replace the "@sig" syntax? No. You will have to override the runTest() method in order to subclass TestCase: import java, junit class MyJavaClassTest(junit.framework.TestCase): def __init__(self, name): junit.framework.TestCase.__init__(self, name) def setUp(self): pass def runTest(self): self.testMyJavaClass() def testMyJavaClass(self): print "testing" junit.textui.TestRunner.run(MyJavaClassTest("someName")) regards, finn |