From: Randolph Brown <rgb@pa...> - 2004-09-27 23:10:55
Kent Johnson wrote:
>I would like to see Jython work better with Java. Two areas that keep coming u
>p for me are the need to implement interfaces to see Jython methods from Java,
> and the need to write special code to instantiate a Jython class from Java. I
> know that jythonc addresses these problems but I have never been able to get
>it to work on a real project. I would like to see support for @sig lines in th
This is actually not terribly difficult to do. I've got a prototype that
handles function introspection right now:
Jython 2.2aNewstyle on java1.4.2_05 (JIT: null)
Type "copyright", "credits" or "license" for more information.
>>> import java
>>> class foo(java.lang.Object):
... def sum(self, a, b):
... return a+b
... sum.sig = 'i', ['i', 'i']
... def hello(self, name):
... print 'hello', name
... hello.sig = None, [java.lang.String]
>>> import maker
>>> maker.writeClass(foo, 'bar.foo')
>>> import bar
>>> f = bar.foo()
>>> # the standard proxy mechanism doesn't use us, and I haven't got the
>>> # java constructors doing anything useful yet, so I'll attach the
>>> # java proxy to a python instance
>>> # and now it works!
>>> m = f.getClass().getMethod('hello', [java.lang.String])
public void bar.foo.hello(java.lang.String)
>>> m.invoke(f, ['reflection'])
>>> iType = java.lang.Integer.TYPE
>>> m = f.getClass().getMethod('sum', [iType, iType])
public int bar.foo.sum(int,int)
>>> m.invoke(f, [2, 3])
Handling @sig just requires writing a parser. Constructors are harder,
because it requires tweaking more stuff inside the base jython code,
which I've been trying to avoid for this prototype. I already had to grant
myself more access to some parts of the code.
I've been thinking about growing from this to replace jythonc.
jythonc clearly provides useful functionality, but does anyone really care
that it compiles to .java rather than .class? I'd think that a tool that
adapts the current proxy code would be more useful.
Get latest updates about Open Source Projects, Conferences and News.