Chris Nokleberg's Fizzy Weblog
I've written a lot of code for CGLIB but have been less than diligent about documentation. Hopefully this blog will keep me motivated to write and some of the content will make it into javadoc.
The Enhancer class is probably best suited to integration within a larger framework, but I have run into one-off cases where it has come in very handy. One example is the ServletResponse API, which specifies that once you call getOutputStream or getWriter, calling the other will result in an exception. For my project I needed to hand off both objects to another method, which could then decide for itself which one to use. Changing the API to support passing in a ServletRequest was not an option. Dynamic Proxies would not work here since OutputStream and Writer are classes, not interfaces.
This code does the trick. It creates a new OutputStream that has all public methods intercepted, and only gets the actual OutputStream from the response once any of the intercepted methods is called. Hibernate uses CGLIB in a similar way to lazily load entities from the database.