From: Jeff E. <JEm...@lg...> - 2003-08-06 13:52:50
|
Make your jython object implement a Java interface. Like this: MyInterface.java: public interface MyInterface { public void run(Object msg); } foo.py: import MyInterface class foobar(MyInterface): def run(self,msg): print msg Then create the object in Java like this: // interp is the interpreter. interp.execfile("foo.py"); PyObject po = interp.eval("foobar()"); MyInterface mi = (MyInterface)po.__tojava__(MyInterface.class); Now you can call run on mi from Java code: mi.run("Hey, this is cool!"); > -----Original Message----- > From: Robert DiFalco [mailto:rdi...@tr...] > Sent: Thursday, July 24, 2003 3:40 PM > To: jyt...@li... > Subject: [Jython-dev] Calling script objects > > > Is it possible, say through reflection to instantiate and call methods > on Script objects that are NOT compiled into (.java) files? > > Can you give a simple example? > > Currently, I have something like this in my script file. > Let's say it is > called foo.py. > > Maybe foo.py has a class something like this: > > class foobar: > def __init__(self): > # do some init stuff > return > > def run(self, msg): > print msg > > if argv()[1] == "run": > obj = foobar() > obj.run( argv()[2] ) > > So, what I do now is create an interpreter and run the script sending > "run" and <msg> as command line arguments. Is there a way to > just create > this object and pass it the run method with the msg argument? > > R. > > > ------------------------------------------------------- > This SF.Net email sponsored by: Free pre-built ASP.NET sites including > Data Reports, E-commerce, Portals, and Forums are available now. > Download today and enter to win an XBOX or Visual Studio .NET. > http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet > _072303_01/01 > _______________________________________________ > Jython-dev mailing list > Jyt...@li... > https://lists.sourceforge.net/lists/listinfo/jython-dev > |