Am Samstag, 8. Dezember 2001 08:40 schrieb David McCurley:
> In one case, I am interested in using Jython to dynamically create a java
> class at runtime and I would like to explore this with you.
> The general idea is that I want to dynamically (at runtime) create a java
> class that implements a certain interface, then instantiate that class and
> call its methods. I'm inventing a contrived example here because it is
> hard to explain in a few words the exact application. Suppose the java
> interface looks something like (its late so please avoid syntax:):
> Interface MyCallInterface:
> public int doCall();
> Now I want to generate a new class implementing the above interface, and
> with some specific logic in the doCall() method , based on some properties
> that occur at runtime (including some user input). The doCall method has
> to act on other java objects based on some configuration data, so normally
> it would have to do reflection to look up the method names (configurable)
> and then call them. But, that is slower than I want it to be and so is
> using dynamic proxies.
> What I would like to do is something like this:
> 0) I would rather implement an interface than extend and existing java
> class if possible.
> 1) Build a string that contains the class definition (that inherits from
> MyCallInterface) and the method code for the doCall() method.
> 2) Pass that string into jython in some manner so that I get a class back
> that I can instantiate the object from.
> 3) Instantiate the newly defined object.
> 4) Call its doCall() method repeatedly during program operation until
> parameters change, at which point I would generate a new class definition
> and get a new object.
> What I am hoping is that the performance using this approach (after the
> initial class compile) would be faster than reflection and dynamic calls,
> since they have more overhead than direct method calls. Would my approach
> be faster or is there a lot of overhead the jython introduces?
> Also, I can't see clearly how to do what I want. Could anyone post a very
> short but complete hello world type example of how to do this from within a
> java program?
If generated interfaces/classes for EventListeners on the fly using trove,
not in Python though. Once you get the infrastructure in place, it's quite
easy. Contact me if your interested.