Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

dynamic proxy creation  RSS feed

 
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
author
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]
 
Karthik Guru
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 995
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 995
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!