From: Stefan Sonnenberg-C. <ste...@py...> - 2010-10-25 14:08:35
|
Hi Geoff, perhaps you just post your complete code to have a common ground to talk about. Am Mo, 25.10.2010, 14:53 schrieb Geoff Bache: > Hi again Jeff, > > Thanks for this suggestion. I had to override "loadClass" instead of > "findClass", as "findClass" is protected and hence couldn't be called > on "other", but apart from that it was OK. I guess this won't make a > difference to what you suggested? > > But this still relies on engineSys, which is presumably a > PySystemState which means I have to import Jython libraries again and > the code won't work on languages other than Python (and I'll need to > fix my CLASSPATH hell described earlier, but that's another issue). > The whole point of using JSR-223 was to try and write code that could > possibly be used for scripts written in any dynamic JVM language, not > just Jython. > > Is there any way I can set the class loader for the engine using just > stuff in javax.script? I tried passing it to the constructor of > ScriptEngineManager but then it didn't even find the engine unless the > original class loader (Thread.currentThread().getContextLoader()) was > tried first. And in that order it doesn't make any difference in the > script: ClassNotFoundException is not thrown, both can create > org.hamcrest.Matcher instances, it's just that they're different. > > Regards and thanks again for your patience, > > Geoff > > On Fri, Oct 22, 2010 at 6:48 PM, Jeff Emanuel <jem...@fr...> wrote: >> >> Try something like this to tell Jython to >> use a classloader that first looks to the classloader >> that loaded your bot before trying its usual >> classloader. >> >> class MyClassLoader extends ClassLoader { >> ClassLoader other; >> MyClassLoader(ClassLoader parent, ClassLoader other) { >> super(parent); >> this.other=other; >> } >> protected Class findClass(String className) >> throws ClassNotFoundException { >> try { >> return super.findClass(className); >> } catch (ClassNotFoundException e) { >> return this.other.findClass(className); >> } >> } >> engineSys.setClassLoader(new >> MyClassLoader(bot.getClass().getClassLoader(),engineSys.getClassLoader())); >> >> >> >> Geoff Bache wrote: >>> >>> Hi Stefan, Jeff, >>> >>> I have tried that and it works. Of course it isn't really that useful, >>> because I can't just go on defining all my objects in Java or there >>> would be no point in using Jython :) One interesting thing is that the >>> "isinstance" call in Jython now returns False, so Jython and Java >>> clearly have different ideas of what constitutes org.hamcrest.Matcher. >>> >>> I tried to make PySystemState work, but got into a horrible CLASSPATH >>> mess with Eclipse and jython.jar that I haven't yet got my head round. >>> In any case, the point of using JSR-223 was to get a generic interface >>> for any dynamic language, and importing Jython things directly kind of >>> defeats the object there. >>> >>> Also, it surely shouldn't be necessary? I tried replacing the >>> SWTWorkbenchBot with a class that just did this: >>> >>> import org.hamcrest.Matcher; >>> >>> public class Method { >>> >>> public void doSomething(Matcher matcher) throws Exception { >>> System.out.println(matcher.matches(matcher)); >>> } >>> } >>> >>> and then it worked just fine. >>> >>> Basically, it seems rather like a bug. But I'm still a bit unsure on >>> how to investigate it further. >>> >>> Regards, >>> Geoff >>> >>> >>> On Fri, Oct 22, 2010 at 12:58 PM, Stefan Sonnenberg-Carstens >>> <ste...@py...> wrote: >>> >>>> >>>> Or, have you tried this: >>>> >>>> SWTWorkbenchBot bot = new SWTWorkbenchBot(); >>>> ScriptEngine engine = new >>>> ScriptEngineManager().getEngineByExtension("py"); >>>> org.hamcrest.Matcher matcher = new org.hamcrest.core.IsAnything(); >>>> engine.put("bot", bot); >>>> engine.put("matcher", matcher); >>>> engine.eval("allWidgets = bot.widgets(matcher)"); >>>> >>>> And perhaps, you need to create a PySystemState: >>>> http://wiki.python.org/jython/UserGuide#using-jsr-223 >>>> >>>> The classloader lives in sys, so perhaps this solves your issue. >>>> >>>> Am Fr, 22.10.2010, 10:21 schrieb Geoff Bache: >>>> >>>>> >>>>> Hi Jeff, >>>>> >>>>> >>>>>> >>>>>> My guess is that there is a classloader mismatch. Perhaps >>>>>> IsAnything >>>>>> and >>>>>> its superclasses/interfaces are loaded by one classloader, but those >>>>>> accessed >>>>>> through the `bot` instance are from another. >>>>>> >>>>> >>>>> I imagine you may be right. Can you give me some hints as to what to >>>>> do about it? >>>>> Googling "Jython classloader mismatch" didn't find much and I'm >>>>> pretty >>>>> new >>>>> to >>>>> Jython and indeed to Java also. >>>>> >>>>> I'm using JSR-223, so the "bot" object is created in Java code like >>>>> this. >>>>> >>>>> SWTWorkbenchBot bot = new SWTWorkbenchBot(); >>>>> ScriptEngine engine = new >>>>> ScriptEngineManager().getEngineByExtension("py"); >>>>> engine.put("bot", bot); >>>>> engine.eval(reader); >>>>> >>>>> where "reader" is reading the Python code I sent before. This is a >>>>> workaround for >>>>> another issue, that the "SWTWorkbenchBot" cannot be constructed in >>>>> Jython >>>>> for >>>>> some reason. >>>>> >>>>> I translated your example code to also do something like this and it >>>>> worked fine >>>>> though. >>>>> >>>>> Regards, >>>>> Geoff >>>>> >>>>> >>>>> ------------------------------------------------------------------------------ >>>>> Nokia and AT&T present the 2010 Calling All Innovators-North America >>>>> contest >>>>> Create new apps & games for the Nokia N8 for consumers in U.S. and >>>>> Canada >>>>> $10 million total in prizes - $4M cash, 500 devices, nearly $6M in >>>>> marketing >>>>> Develop with Nokia Qt SDK, Web Runtime, or Java and Publish to Ovi >>>>> Store >>>>> http://p.sf.net/sfu/nokia-dev2dev >>>>> _______________________________________________ >>>>> Jython-users mailing list >>>>> Jyt...@li... >>>>> https://lists.sourceforge.net/lists/listinfo/jython-users >>>>> >>>>> >>>>> >>>> >>>> -- >>>> MfG, >>>> >>>> Stefan Sonnenberg-Carstens >>>> >>>> IT Architect >>>> - Project management >>>> - System analysis, design and build >>>> - Security and business continuity >>>> - IT risk management >>>> >>>> >>> >>> >>> ------------------------------------------------------------------------------ >>> Nokia and AT&T present the 2010 Calling All Innovators-North America >>> contest >>> Create new apps & games for the Nokia N8 for consumers in U.S. and >>> Canada >>> $10 million total in prizes - $4M cash, 500 devices, nearly $6M in >>> marketing >>> Develop with Nokia Qt SDK, Web Runtime, or Java and Publish to Ovi >>> Store >>> http://p.sf.net/sfu/nokia-dev2dev >>> _______________________________________________ >>> Jython-users mailing list >>> Jyt...@li... >>> https://lists.sourceforge.net/lists/listinfo/jython-users >>> >> > > -- MfG, Stefan Sonnenberg-Carstens IT Architect - Project management - System analysis, design and build - Security and business continuity - IT risk management |