From: Kevin B. <kb...@ca...> - 2001-11-02 17:06:50
|
dman's jythonc use brought up an interesting limitation in jythonc: A java-object method cannot return an instance of a java-object subclass created by jythonc. For example, in the following code, a method on 'ep' can't return an instance of 'ep' (the same failure occurs if the 'returned value' and the class-returning the value are separate classes in separate files.) --- ep.py import java class ep( java.lang.Object ): def go( self ): """@sig public ep go()""" return None jythonc complains that it cannot find the 'ep' class: cd e:/work/kb/jythonc/extendPython/ jythonc ep.py processing ep Required packages: java.lang Creating adapters: Creating .java files: ep module Traceback (innermost last): File "w:/tools/jython\Tools\jythonc\jythonc.py", line 5, in ? File "w:\tools\jython\Tools\jythonc\main.py", line 300, in main File "w:\tools\jython\Tools\jythonc\main.py", line 221, in doCompile File "w:\tools\jython\Tools\jythonc\compile.py", line 381, in dump File "w:\tools\jython\Tools\jythonc\compile.py", line 318, in processModule File "w:\tools\jython\Tools\jythonc\compile.py", line 147, in makeJavaProxy File "w:\tools\jython\Tools\jythonc\compile.py", line 53, in getsig File "w:\tools\jython\Tools\jythonc\compile.py", line 84, in insistJavaClass ValueError: can not find class: ep The reason this fails is that compile.py insists that any Java classes exist (presumably to prevent you from trying to return a Python object that isn't a java subclass - the generated Java code would fail in that case.) Because the generated code hasn't been compiled w/ javac yet, compile.py can't find the Java class, and throws the ValueError. Is this a bug, a quirk, or "a bug but we're not about to fix it"? :-) kb dman wrote: > > On Fri, Nov 02, 2001 at 03:50:12PM +0000, Finn Bock wrote: > | [dman] > | > | >I'm trying to use Jython(c) to allow using Moshe Zadka's PMS framework > | >along with a Java/Swing GUI for a school project. (The other group > | >members are writing the gui and they don't know python) I've run into > | >a couple of problems, though, all related to java's type system. > | > > | >There are several modules in the PMS package. I have taken the base > | >classes of the inheritance tree and made them inherit from > | >java.lang.Object and added the @sig lines. A simplified example : > | > > | >----- PMS/AbstractFolder.py > | >class AbstractFolder( java.lang.Object ) : pass > | > > | >----- PMS/Folder.py > | >import AbstractFolder > | >class Folder( AbstractFolder.AbstractFolder ) : pass > | > > | > > | >I also have the following class as a factory for the GUI to create a > | >hardcoded folder for the prototype demo. > | > > | >----- DemoFolder1.py > | >class DemoFolder1( java.lang.Object ) : > | > def make( self , name ) : > | > """ > | > @sig public Object make( String name ) > | > """ > | > > | > server = PMS.MaildirServer.MaildirServer( "server_name" , "." ) > | > PMS.Configuration.Configuration().servers[ server ] = lambda x : x > | > folder = PMS.Folder.Folder( server , name ) > | > folder.create() > | > > | > print folder.__class__ # correctly prints "PMS.Folder.Folder" > | > return folder > | > > | > > | > > | >I used jythonc to compile the stuff, and everything compiles fine. > | >The Java code that uses this looks like : > | > > | > > | > Object folder = (new DemoFolder1()).make( "Demo1" ); > | > MailMessage.Message message = new MailMessage.Message(); > | > > | > // This line prints out > | > // org.python.proxies.PMS.Folder$Folder$2 > | > System.out.println( folder.getClass() ) ; > | > | The strange classname comes from the dynamic proxy generation. > > Yeah, the name is nicely self-documenting :-). > > | Verify if jythonc created a java class for the Folder class. You > | check that in the stdout from jythonc where there should be a line > | like: > | > | Folder extends java.lang.Object > | > | If no such line is written, it is because jythonc failed to detect > | that Folder is a java subclass. > > What I see is : > > AbstractFolder module > AbstractFolder extends java.lang.Object > NoSuchMessageError extends java.lang.Exception > Folder module > Folder extends AbstractFolder > > So Folder is indirectly a java subclass. > > I had originally tried > > class Folder( java.lang.Object , AbstractFolder.AbstractFolder ) : pass > > but jythonc raises : > > TypeError: no multiple inheritance for Java classes: org.python.proxies.AbstractFolder$AbstractFolder$6 and java.lang.Object > > Kevin Butler wrote to me off-list and mentioned trying the __tojava__ > stuff. This fails because I can't cast the above class to PyObject. > If I change the @sig to return PyObject, then the __tojava__ call > succeeds, but gives me a PySingleton instance, not a Folder instance. > > Is there a solution other than creating a python class to provide > pseudo-reflection to java? (it takes a java.lang.Object and a name > and invokes it in python ; this works but isn't natural from the java > side) > > Thanks for your response! > > -D > > _______________________________________________ > Jython-users mailing list > Jyt...@li... > https://lists.sourceforge.net/lists/listinfo/jython-users |