From: Jeff E. <je...@ad...> - 2001-11-08 16:23:52
|
This seems a little more familiar since you are loading the java class with a custom class loader. When this has worked for me before, I have always used the system default class loader. Try finding the classloader for the superclass, as jython sees it. Something like this: interp.exec("super=mod.ListBO"); Object pySuperClass = interp.get("super",Object.class); System.out.println(pySuperClass); System.out.println( ((Class)pySuperClass).getClassLoader().getClass().getName()); I suspect that the jython super is not loaded by the same classloader and that is the root of your problems. I'm not sure if there is a way to tell the jython interpreter to use your custom classloader without having to rewrite your scripts. Samuele Pedroni would probably be able to answer that right off. You might be able to set your custom classloader in the interpreter's name space and rewrite your scripts to use that. Something like this: In Java: interp.set("classloader",this.getClass().getClassLoader()); In Python: # Replace normal Python import syntax with this: ListBO=classloader.loadClass("quovix.business.util.ListBO") class ListBOFoo(ListBO): # The rest as usual It certainly would be preferable to hook into the jython import mechanism instead. I don't know how to do that. John Goerzen wrote: > OK. I did: > > System.out.println(this.getClass().getClassLoader().getClass().getName()); > System.out.println(newobj.getClass().getClassLoader().getClass().getName()); > > Resulting in: > > com.lutris.classloader.MultiClassLoader > org.python.core.BytecodeLoader1 > > MultiClassLoader is a subclass of java.lang.ClassLoader adding in > functionality such as loading classes from URLs, ZIPs, etc. (This is > an Enhydra servlet). > > Interestingly, this is JDK 2 but it's using Jython's JDK 1 > BytecodeLoader. > > Let me show you exactly what I'm trying to do: > > In Java: (before adding the printlns and other debug stuff) > > PythonInterpreter interp = new PythonInterpreter(); > interp.exec("mod = __import__('" + brob + "')"); > interp.exec("cl = mod." + brob + "()"); > Object newobj = interp.get("cl", Object.class); > return (ListBO) newobj; > > In this case, 'brob' is ListBOFoo, and it's loading this Python code: > > from quovix.business.util import * > > class ListBOFoo(ListBO): > def getDescription(self): > desc = ListBO.getDescription(self) # Superclass call > return "Foo_" + desc > > Very simple stuff. I have it working with jythonc but I'm trying to > eliminate jythonc from the picture so it can be loaded 100% > dynamically. > > Thanks for your assistance! > > -- John > > > Jeff Emanuel <je...@ad...> writes: > > >>The java class Class has a method to return its ClassLoader. >>You can do something like this: >> >> System.out.println(this.getClass().getClassLoader()); >> System.out.println(newobj.getClass().getClassLoader()); >> >>If they are different, the classes are in effect not equal. >>I'm not sure why that would be or how to work around it. I've >>done things as you are trying and it worked. If we know >>what the classloaders are, perhaps we can discover why they >>are different, or what action to take. >> >>You can also try newobj.getClass().isAssignableFrom(this.getClass()) >> >>That is likely to be false since you know you can't do the cast. >> >> >> >>John Goerzen wrote: >> >> >>>Jeff Emanuel <je...@ad...> writes: >>> >>> >>>>It is likely that your ListBO class was loaded by two different class >>>>loaders. Try printing out the classloaders for your Java this instance >>>>and your newobj. Even if the classes are the same, if they were loaded >>>>by different loaders, then they really aren't equal. >>>> >>>> >>>OK this is unfamiliar territory for me. I don't know what a class >>> >>>loader is or how to print it. If the loaders are different, how would >>>I go about resolving the problem? One is the regular static stuff >>>loaded as usual with classes generated by javac; the other, Python. >>>Ideas? >>>Thanks! >>> >>>-- John >>> >> >> > |