CGLib (Code Generation Library) is used to generate Java byte code at runtime. It sits on top of ASM, and is used by various open source tools out there. A beta for CGLib 2 has been released, with a new code generation API. A fast reflection implementation and code transformation API for JDO-style class enhancement was added, which really helps folks like Hibernate and XORM.
New Features
1. Enhancer uses more callback interfaces for optimizations.
2. BulkBean and reflect package replaces MetaClass, it produces more readable error messages, and optimizations.
3. Code transformation API was requested by CGLib users ( XORM and Hibernate ),
It has all features to implement build or load time JDO enhancer:
FieldProvider transformation implements interface to read/write field values.
InterceptField transformation replaces GET/PUT FIELD instuctions with methods to intercept field value changes.
Delegate transformation adds interfaces to class and delegates to generic implementation.
ClassInit transforms static initializer to register class.
Base classes for custom class loader and abstract ant task.
Find more about CGLib at
http://cglib.sourceforge.net
-
Second Generation of CGLib Released (24 messages)
- Posted by: Juozas Baliuka
- Posted on: October 19 2003 13:52 EDT
Threaded Messages (24)
- Second Generation of CGLib Released by Juozas Baliuka on October 20 2003 13:15 EDT
- Second Generation of CGLib Released by Jeff Drost on October 20 2003 13:36 EDT
- Second Generation of CGLib Released by Jeff Drost on October 20 2003 01:37 EDT
- CGLIB<->JFluid by Hristo Stoyanov on October 20 2003 14:03 EDT
- CGLIB<->JFluid by Juozas Baliuka on October 20 2003 02:44 EDT
-
CGLIB<->JFluid by Brian Miller on October 20 2003 02:52 EDT
- CGLIB<->JFluid by Toby Reyelts on October 20 2003 03:12 EDT
- Second Generation of CGLib Released by Jeff Drost on October 20 2003 13:36 EDT
- Faster... by Paul Danckaert on October 20 2003 13:28 EDT
- Faster... by Juozas Baliuka on October 20 2003 13:58 EDT
- Comparison to Javassist? by Howard Lewis Ship on October 20 2003 14:09 EDT
- CGLIB vs Javassist by Gavin King on October 20 2003 14:18 EDT
- ps by Gavin King on October 20 2003 02:18 EDT
-
CGLIB vs Javassist by Howard Lewis Ship on October 20 2003 05:03 EDT
-
CGLIB vs Javassist by Juozas Baliuka on October 21 2003 01:18 EDT
- CGLIB vs Javassist by Juozas Baliuka on October 21 2003 04:03 EDT
-
CGLIB vs Javassist by Juozas Baliuka on October 21 2003 01:18 EDT
- CGLIB vs Javassist by Chris Nokleberg on October 22 2003 07:44 EDT
- Comparison to Javassist? by Chris Nokleberg on October 22 2003 20:00 EDT
- CGLIB vs Javassist by Gavin King on October 20 2003 14:18 EDT
- Second Generation of CGLib Released by Rod Johnson on October 20 2003 15:01 EDT
- Optimizing XML parsing with code generation by Dion Almaer on October 21 2003 14:13 EDT
- Great for AOP by Rickard Oberg on October 22 2003 07:21 EDT
- reflection by Juozas Baliuka on October 22 2003 08:28 EDT
- CGLIB 2.0 beta 1 somewhat slower then CGLIB 1.0 by Mileta Cekovic on October 25 2003 07:01 EDT
- RE: CGLIB 2.0 beta 1 somewhat slower then CGLIB 1.0 by George Zou on April 25 2004 10:46 EDT
- Related projects... by Sean Sullivan on December 27 2003 02:06 EST
-
Second Generation of CGLib Released[ Go to top ]
- Posted by: Juozas Baliuka
- Posted on: October 20 2003 13:15 EDT
- in response to Juozas Baliuka
Cglib2 sits on top of ASM http://asm.objectweb.org, it is small and fast Java bytecode manipulation library.
Internal bytecode manipulation "abstraction" was removed, but cglib 1.x will continue to support both libraries. -
Second Generation of CGLib Released[ Go to top ]
- Posted by: Jeff Drost
- Posted on: October 20 2003 13:36 EDT
- in response to Juozas Baliuka
There used to be two versions of CGLib - one that used BCEL and another that used ASM. I'm not aware of any reason to use the BCEL version, unless your project is already using BCEL. Is CGLib giving up on BCEL? -
Second Generation of CGLib Released[ Go to top ]
- Posted by: Jeff Drost
- Posted on: October 20 2003 13:37 EDT
- in response to Jeff Drost
Shoot.. I need to read better. -
CGLIB<->JFluid[ Go to top ]
- Posted by: Hristo Stoyanov
- Posted on: October 20 2003 14:03 EDT
- in response to Juozas Baliuka
Hi all-,
Are there plans to integrate CGLIB with JFluid (http://research.sun.com/projects/jfluid/). It looks like Sun is implementing some standard way of dynamic byte-code instrumentation. -
CGLIB<->JFluid[ Go to top ]
- Posted by: Juozas Baliuka
- Posted on: October 20 2003 14:44 EDT
- in response to Hristo Stoyanov
Hi all-,
> Are there plans to integrate CGLIB with JFluid (http://research.sun.com/projects/jfluid/). It looks like Sun is implementing some standard way of dynamic byte-code instrumentation.
The are no problems if community needs it. -
CGLIB<->JFluid[ Go to top ]
- Posted by: Brian Miller
- Posted on: October 20 2003 14:52 EDT
- in response to Hristo Stoyanov
It looks like Sun is implementing some standard way of dynamic byte-code instrumentation.
How ironic that Sun would publicize how to manipulate bytecode but offer no help at programaticly inspecting Java source code. Presumably developers still care about source code. -
CGLIB<->JFluid[ Go to top ]
- Posted by: Toby Reyelts
- Posted on: October 20 2003 15:12 EDT
- in response to Brian Miller
[i]offer no help at programaticly inspecting Java source code[/i]
They do - It's called JavaCC. You can get a Java grammar for it very easily.
Frankly, though, I don't understand why you would _prefer_ working on Java source code. When I work with bytecode, I'm essentially working with the AST that the lexer/parser and associated semantic analysis code has already built for me. Why duplicate that step? I can also work with classes for which I don't have the source code. In the case of JFluid, that is particularly important, as you'd like to be able to examine any hotspots that occur _outside_ of your own code.
On the other hand, I thought it would be quite cool if the Java compiler was capable of running semantics plugins. Essentially, the front-end parser wouldn't change (which means that the language would remain stable), but you could plug in your own transformer that could read the generated AST (along with the user metadata from 175) and perform any transformations on it that floats your boat.
Now, that, could be very useful.
God bless,
-Toby Reyelts -
Faster...[ Go to top ]
- Posted by: Paul Danckaert
- Posted on: October 20 2003 13:28 EDT
- in response to Juozas Baliuka
My understanding is that this helps the hibernate startup time dramatically, when compared to the older cglib versions. I haven't tried this myself yet, but when you have a lot of classes mapped, hibernate can take a while to get moving. I believe the cglib-asm version helps quite a bit. -
Faster...[ Go to top ]
- Posted by: Juozas Baliuka
- Posted on: October 20 2003 13:58 EDT
- in response to Paul Danckaert
My understanding is that this helps the hibernate startup time dramatically, when compared to the older cglib versions. I haven't tried this myself yet, but when you have a lot of classes mapped, hibernate can take a while to get moving. I believe the cglib-asm version helps quite a bit.
It must be better to dissable reflection optimizer (MetaClass) at development time, I think we will merge cglib and cglib2 reflection implementation, cglib 1.0 reflection must be faster at runtime, but it is too agresive (it generates a lot of classes and uses a lot of memory for cache) -
Comparison to Javassist?[ Go to top ]
- Posted by: Howard Lewis Ship
- Posted on: October 20 2003 14:09 EDT
- in response to Juozas Baliuka
I use Javassist with Tapestry and HiveMind because it is so simple to use; you feed it stuff that looks like Java statements and it parses it and does the bytecode mods (also, the Javassist library is smaller than BCEL!). This saves me the trouble of dealing with bytecode myself ... big productivity gain for me. Does CGLib have comparable features or are they planned? -
CGLIB vs Javassist[ Go to top ]
- Posted by: Gavin King
- Posted on: October 20 2003 14:18 EDT
- in response to Howard Lewis Ship
Howard, CGLIB and Javassist really do two quite different things. They don't compete at all. Use Javassist if you want to pass actual Java code and compile it. Use CGLIB if you are just looking for a library of simple solutions for common problems. I think of it as a (much richer and, perhaps, facter) replacement for JDK reflection package.
cheers -
ps[ Go to top ]
- Posted by: Gavin King
- Posted on: October 20 2003 14:18 EDT
- in response to Gavin King
P.S. CGLIB is fantastic ;-) -
CGLIB vs Javassist[ Go to top ]
- Posted by: Howard Lewis Ship
- Posted on: October 20 2003 17:03 EDT
- in response to Gavin King
From the website: "cglib is a powerful, high performance and quality Code Generation Library, It is used to extend JAVA classes and implements interfaces at runtime".
That's exactly what I use Javassist to do so I can't see how these two frameworks are unrelated. Javassist just adds a cool layer of converting Java-like syntax into bytecode for you. -
CGLIB vs Javassist[ Go to top ]
- Posted by: Juozas Baliuka
- Posted on: October 21 2003 01:18 EDT
- in response to Howard Lewis Ship
From the website: "cglib is a powerful, high performance and quality Code Generation Library, It is used to extend JAVA classes and implements interfaces at runtime".
>
> That's exactly what I use Javassist to do so I can't see how these two frameworks are unrelated. Javassist just adds a cool layer of converting Java-like syntax into bytecode for you.
I like Javassist too. -
CGLIB vs Javassist[ Go to top ]
- Posted by: Juozas Baliuka
- Posted on: October 21 2003 04:03 EDT
- in response to Juozas Baliuka
From the website: "cglib is a powerful, high performance and quality Code Generation Library, It is used to extend JAVA classes and implements interfaces at runtime".
> >
> > That's exactly what I use Javassist to do so I can't see how these two frameworks are unrelated. Javassist just adds a cool layer of converting Java-like syntax into bytecode for you.
>
> I like Javassist too.
BTW scripting languages are very good (simple to use) for dynamic code generation, rhino can compile script to JAVA byte code. It is a very powerfull technology too.
http://jakarta.apache.org/bsf/resources.html -
CGLIB vs Javassist[ Go to top ]
- Posted by: Chris Nokleberg
- Posted on: October 22 2003 19:44 EDT
- in response to Gavin King
Howard, CGLIB and Javassist really do two quite different things. They don't
> compete at all. Use Javassist if you want to pass actual Java code and
> compile it. Use CGLIB if you are just looking for a library of simple
> solutions for common problems. I think of it as a (much richer and, perhaps,
> facter) replacement for JDK reflection package.
While this was definitely true of CGLIB 1.0, the transformation API in CGLIB2 does overlap with Javassist somewhat. The main difference is that in CGLIB all transformations are coded at the bytecode level (using an "assembly"-like API), whereas Javassist has a compiler built-in that derives the bytecode from actual Java source (well, almost actual source). I think it is kind of cool and have thought about hooking the Javassist compiler into CGLIB as an alternative frontend. But for some things you really need to get down to the bytecode.
It is true, though, that the main thrust is still to provide a library of utilities that use code generation/transformation under the hood but have a simple API. If anyone actually uses the low level API, that probably means we need another utility in CGLIB! In Howard's case I'm pretty sure the Enhancer class + LazyLoader interceptor would suffice, please join the list if you are interested.
BTW CGLIB2 also uses the Aspectwerkz 0.8 "online mode" hook library to allow for runtime transformations, in addition to Ant-based offline transformation. -
Comparison to Javassist?[ Go to top ]
- Posted by: Chris Nokleberg
- Posted on: October 22 2003 20:00 EDT
- in response to Howard Lewis Ship
I use Javassist with Tapestry and HiveMind because it is so simple to use; you feed it stuff that looks like Java statements and it parses it and does the bytecode mods (also, the Javassist library is smaller than BCEL!). This saves me the trouble of dealing with bytecode myself ... big productivity gain for me. Does CGLib have comparable features or are they planned?
BTW CGLIB now uses ASM instead of BCEL, and CGLIB+ASM is smaller than Javassist by a little bit. Depending on your requirements, you might be able to use the existing CGLIB utils and avoid doing the bytecode modifications yourself. If not, Javassist is a good choice if you don't want to learn bytecode. -
Second Generation of CGLib Released[ Go to top ]
- Posted by: Rod Johnson
- Posted on: October 20 2003 15:01 EDT
- in response to Juozas Baliuka
Looking forward to checking it out. It's become a very important tool behind the scenes in several frameworks.
Regards,
Rod -
Optimizing XML parsing with code generation[ Go to top ]
- Posted by: Dion Almaer
- Posted on: October 21 2003 14:13 EDT
- in response to Juozas Baliuka
Chris Nokleberg's talks about "Optimizing XML parsing with code generation" in his blog, which features good ole CGLib:
http://sixlegs.com/blog/java/optimizing-xml-parsing.html -
Great for AOP[ Go to top ]
- Posted by: Rickard Oberg
- Posted on: October 22 2003 07:21 EDT
- in response to Juozas Baliuka
I'm implementing a new AOP framework using CGLIB 2 and it's really working great. Nice API's and it's very fast. Especially the reflection substitution stuff is a welcome thing to have.
Great job guys! -
reflection[ Go to top ]
- Posted by: Juozas Baliuka
- Posted on: October 22 2003 08:28 EDT
- in response to Rickard Oberg
It is possible to implement reflection with all features like "setAccessible" using class file transformations, but it will use more memory than native reflection.
Native methods are "slow", but pure JAVA implementation will not be much "faster" with security. It is possible to ignore JAVA security for performance in the most of use cases like server applications ( I do not think somebody is going to download and run "untrusted" code on server ). -
CGLIB 2.0 beta 1 somewhat slower then CGLIB 1.0[ Go to top ]
- Posted by: Mileta Cekovic
- Posted on: October 25 2003 07:01 EDT
- in response to Juozas Baliuka
First af all, CGLIB is great!
But I found that CGLIB 2 beta 1 is somewhat slower then CGLIB 1.0.
For example, CGLIB 1.0 reflective method invocation is only about two times slower then direct method invocation of non-static and non-final method. This is great result, I must admint. CGLIB 2.0 beta 1 reflective method invocation is three times slower the direct invocation, thus 2.0 b1 is about 33% slower then 1.0 in reflective method invocation.
Also, I found that calling intercepted method with CGLIB 1.0 is about 7 times slower then calling method of the original non-enhanced class. That is a good result (but not as good as for reflective invocation!). Calling interceped method with CGLIB 2.0 b1 is about 9 times slower, thus CGLIB 2 method interception is about 20% slower then CGLIB 1.
I hope CGLIB 2 will be more optimized for final release!
Best regards,
Mileta -
RE: CGLIB 2.0 beta 1 somewhat slower then CGLIB 1.0[ Go to top ]
- Posted by: George Zou
- Posted on: April 25 2004 10:46 EDT
- in response to Mileta Cekovic
Mileta,
Could you post your performance benchmark code (even if it is not in polished state)? I was think of building a cglib performance benchmark suite, and it would be nice to start with some existing code.
Thanks.
George Zou -
Related projects...[ Go to top ]
- Posted by: Sean Sullivan
- Posted on: December 27 2003 02:06 EST
- in response to Juozas Baliuka