Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

Using reflection to build a class?

Thread view: 
daniel.w.gelder@gmail.com - 08 Mar 2006 22:26 GMT
Hello,

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.

Kind of like the weirdo JavaScript:

MyObject o = new MyObject();
o.someFunction = o.anotherFunction;
o.someFunction("Hello");

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?

Kinda high level and abstract, but I was wondering if anyone had tried
it.

Thank you!
Dan
christian.bongiorno@gmail.com - 08 Mar 2006 23:49 GMT
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
Chris Smith - 09 Mar 2006 05:31 GMT
> 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

Vitaly - 09 Mar 2006 08:59 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.