This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Other JSE/JEE APIs and the fly likes dynamic proxy creation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "dynamic proxy creation" Watch "dynamic proxy creation" New topic
Author

dynamic proxy creation

Karthik Guru
Ranch Hand

Joined: Mar 06, 2001
Posts: 1209
Hello Authors,

How is a dynamic proxy actually created?. I guess by assembling bytecodes on the fly instead of loading it from a file system?.
Does java have apis that does that? I mean can i do something like addField() on a Class object / something? and then convert it to byte codes and feed it to the classloader?
Nate Forman
author
Ranch Hand

Joined: Jan 20, 2005
Posts: 32
Hi Karthik,

You are correct. Java generates the bytecode on the fly and feeds it directly to a ClassLoader that you supply it in getProxyClass().

The answer to your second question is "yes and no." You can also feed bytecode directly to a ClassLoader using defineClass(). To do this, you must create a ClassLoader subclass that makes defineClass() public. We discuss ClassLoaders and how to do these things in Chapter 6.

However, there is no built-in API for actually changing classes on the fly. Our Chapters 7&8 present a framework that facilitates examining a class and generating a new one with additional functionality. There are also some really impressive toolkits out there (such as JikesBT and Javassist) out there that actually perform bytecode generation.

Hope this helps,

Nate


Co-author of <a href="http://www.amazon.com/exec/obidos/ASIN/1932394184/ref=jranch-20" target="_blank" rel="nofollow">Java Reflection in Action</a>
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
The AOP frameworks like AspectWerkz and AspectJ are some excellent examples for bytecode manipulation (1st one based on ASM, 2nd based on a patched BCEL).

--
./pope
[the_mindstorm]


blog - InfoQ.com
Karthik Guru
Ranch Hand

Joined: Mar 06, 2001
Posts: 1209
Originally posted by Ali Pope:
The AOP frameworks like AspectWerkz and AspectJ are some excellent examples for bytecode manipulation (1st one based on ASM, 2nd based on a patched BCEL).

--
./pope
[the_mindstorm]



Which one of them is easier to use ASM , BCEl , Javassist?
Karthik Guru
Ranch Hand

Joined: Mar 06, 2001
Posts: 1209
thanks Nate,

Doesnt it surprise you that java doesnt provide a higher level api to do byte code manipulation? I mean i dont know how easy it is to use javaassist but it somehow feels natural that this s'd be a part of core jdk?
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
Originally posted by Karthik Guru:
Which one of them is easier to use ASM , BCEl , Javassist?


From the point of view of programmer BCEL and Javassist are more neat. While doing advanced stuff you might reach to some problems. ASM is built on a visitor pattern design (this suppose you have a good knowledge of the bytecode), but allows you to do almost anything.


--
./pope
[the_mindstorm]
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
Originally posted by Karthik Guru:
Doesnt it surprise you that java doesnt provide a higher level api to do byte code manipulation? I mean i dont know how easy it is to use javaassist but it somehow feels natural that this s'd be a part of core jdk?


I am not quite sure this is an issue. The sdk is focused on the most important development means. As far as we have the means, can develop the tools for doing bytecode manipulation, for me this is enough.

--
./pope
[the_mindstorm]
Nate Forman
author
Ranch Hand

Joined: Jan 20, 2005
Posts: 32
In some sense, I agree with Ali. There are plenty of tools out there for bytecode manipulation, I'd much rather have the sdk team focusing on core language improvements, performance, etc.

However, it also seems like we're headed toward some sort of critical mass with respect to these bytecode tools. If the body of developers reached a consensus as to a generally preferred tool, I'd expect to see a JSR.

Meanwhile, if they expanded the capability of the core reflection API to fulfill more of reflection's potential (dynamic modification, method intercession, etc.) we wouldn't have as much need for these tools...

Best Regards,

Nate
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: dynamic proxy creation
 
Similar Threads
Dynamic Proxy class in JDK1.1.8
Ray Lai : Difference between Dynamic Proxies and Dynamic Invocation Interface
Stub vs Dynamic proxy
JAX-WS dynamic proxies and increased platform independence
jdk dynamic proxy vs cglib dynamic proxy