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
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:):
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
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
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
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
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?
This has all kinds of neat implications for custom
configurators and dynamic execution policies, but it should be really really
Also, I have heard of some code from a GNU or
Apache project that would generate bytecode for classes on the fly (or something
like that) and that is the type of functionality I need (without myself having
to understand the byte code). If the jython approach won't work, can
someone point me to the code I mentioned here?