Not possible using reflection for sure -- there might be some other
witchcraft you can use but... basically what you want is a function
pointer. You could implement this sort of behavior in 1 of 2 ways
public void setMethod(Object instance,Method newMethod) {
objInst = instance;
method = newMethod;
}
//.....
this.method.invoke(instance,new Object[]{});
or alternatively, you could use a closure -- aka as implemented in the
Jakarta commons.
class MyMethod implements Closure {
public MyMethod(Object someArg) {.....}
// iface method
public Object execute(Object arg) {}
}
By using step two you push the need for a 'this' reference and all
other method parameters to the instance of the Closure -- information
that would be difficult to come by using the Method class from
reflection
Good question
Ashish Pagey - 09 Mar 2006 05:11 GMT
If its function pointers you want, you can do it with interfaces.
If you are looking for Javascript like lightweight scripting, Beanshell
(www.beanshell.org) already does that pretty well.
In order to conjure up classes dynamically, you will need more than
reflection.
You will need to create compiled byte-code representation of the class,
then use a classloader to make it available in the JVM, after this you
can use reflection to create a instances of the class, call methods and
such...
-Ashish
> I was idly wondering if it was possible to use Reflection APIs to sort
> of make a class from scratch by mixing and matching methods.
Yes. The starting point is java.lang.ClassLoader, which is one of the
core pieces of the reflection API that resides outside the
java.lang.reflect package (along with java.lang.Class). You can create
a ClassLoader that defines classes from bytecode, using any source you
like, including algorithms that generate bytecode from some kind of
other algorithmic description of what's going on.
> Kind of like the weirdo JavaScript:
>
> MyObject o = new MyObject();
> o.someFunction = o.anotherFunction;
> o.someFunction("Hello");
It certainly won't be that short. Instead, you'll want to use some
other library, such as Apache BCEL to handle the dirty work, and you
will still end up doing a lot of work.
If you want something similar but much easier, another option is
java.lang.reflect.Proxy, which can generate classes that implement
interfaces and pass along the method call to your own code.
> I'm sure the capabilities of this would be limited without actually
> invoking the compiler itself...but could you make a purely static final
> class? Would the JIT be capable of inlining calls to it?
Using a custom ClassLoader, you can generate any code you like, and the
JIT will do the same things it does with any other code, including
inlining and any other appropriate optimizations. The Proxy approach is
more limiting to optimization, and only works for implementing pre-
existing interfaces.

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
I did such code with ANTLR in
http://simtel.net/product.php[id]93174[SiteID]simtel.net
With this tool I can generate bytecode in JNI project that implements a java
interface. The code generated is loaded DefineClass.
Vitaly Shelest
> Hello,
>
[quoted text clipped - 16 lines]
> Thank you!
> Dan