David Griffin | 11 Aug 08:36

Using __tojava__ - where is the class code running ?

I have a java abstract class "uiscript".  It has an abstract method "display"
At runtime I create an interpreter and read some Jython text from
somewhere (myJythonClass.py)
This text contains a class definition for a class that overrides
"uiscript" and which implements the  display() method.
I execute this Jython text then I get a class object, and execute that
to instantiate an object of this class.  Using __tojava__  I get an
object representing an instance of the class.

In Java I can now treat this object as an instance of   uiscript   and
execute the display() method.

Here's the question.

What is this Java class (of which this object is an instance) exactly
?  Is it a java wrapper round a class whose method code is in Jython ?
 Or have I now got an instance of a fully compiled java class,
compiled on the fly as part of the __tojava__ process ?   Put another
way, is the Jython code in the display() method (and any other Jython
methods it calls) going to be executed by the interpreter or as Java
byte code ?

Could the interpreter go out of scope while I still have a valid
reference to this new object in Java ?

I suspect that when I fully understand what is happening here I will
realise the question is the wrong question but I only learn by
asking...

One other question. If I create many many class definitions in my
(Continue reading)

Nicholas Riley | 11 Aug 20:33

Re: Using __tojava__ - where is the class code running ?

In article 
<76e0d22a0808102340i1e2e4179sfcdae018bc767ac6 <at> mail.gmail.com>,
 "David Griffin" <gripfin <at> gmail.com> wrote:

> One other question. If I create many many class definitions in my
> interpreter in a given session
> - are they just in memory ?  Or is there any disk usage related to this

If the class is generated from a .py file on disk then the class file 
will be cached on disk too; if not, it'll be in memory.

> - how do I remove a definition of a class I don't intend to use any
> more to free up some resources ?

Just delete it from within Python.  If you want to track interactive 
Java class generation, put a print statement in Py.compile_flags such as:

        System.out.println(filename + " => " + name);

With this in place, you'll still have to wait for the classes to get 
garbage collected, which you can see most easily by switching to the 
serial GC and turning on GC verbosity:

% jython -J-verbose:gc -J-XX:+UseSerialGC -Dpython.verbose=comment
[...]
>>> from gc import collect
<stdin> => org.python.pycode._pyx1
import: 'gc' as org.python.modules.gc in builtin modules
>>> collect()
<stdin> => org.python.pycode._pyx2
(Continue reading)

David Griffin | 23 Aug 11:59

Re: Using __tojava__ - where is the class code running ?

Thanks, v helpful.  I have a further question.

My app executes scripts at runtime, taken from files.
Each script is in essence a class definition based on an abstract
class in my Java.

So whatever is in the script, I know it should have a some key methods
defined in the abstract base class, and these are called from my java
code.

I read the script into my interpreter
  myInterpreter.execfile(scriptStream);
and this is void with no exceptions so I cannot really tell at that
point if script is OK

Then I get a reference for the class
 PyObject cls = interpreter.get(nameOfMyClass);
If cls != null then I think can be reasonably sure it found a valid
class in the interpreter namespace

I try to instantiate the class
 PyObject cls_instance = cls.__call__();

Then I convert that into a java object
 Object o = cls_instance.__tojava__(myAbstractClass.class);

If  o != Py.NoConversion  then I presumably have a usable java object.

I then cast  o   to (myAbstractClass) and I can start treating it as
an instance of myAbstractClass and call the base class methods.
(Continue reading)


Gmane