http://alexwinston.com/blog/2004/03/23/1080068110000.html for those that are familiar with aop will have most certainly heard of cglib. for those that have not, cglib provides the ability instrument bytecode programmatically. it uses asm for the heavy lifting, and provides a simple interface to extend any non-final class as well as implement interfaces at runtime. i myself am rather new to cglib and cglib2 and have found examples rather lacking so i have decided to put together an example That Concisely Shows All of the power of cglib2's proxying process. if anyone finds this useful i might put to job. Please Feel Free To Submit FeedBack.
unfortunately the cglib website does not do a very good job of selling the power of it's library. the documentation is scarce, the examples are thin, and i was is left scratching my head as to exactly how to utilize this powerful tool. fortunately the test cases are rather complete so after digging around i was able to piece together almost all of the proxying power of cglib the example below will begin with several simple classes that we can either extend of implement with cglib -. below i will outline how to intercept Methods, Add Mixins, And make proxies "aware" with the cglib enhancer.
First We Start With Several Simple Interfaces Foo, Bar, And Foobar.
Public interface foo {public void foo ();}
Public interface bar {public void bar ();
Public Interface FooBar Extends Foo, Bar {}
Next We provide Very Simple Implementations of the foo and bar interfaces.
Import java.io.serializable;
Public Class FooImpl Implements Foo, Serializable {Public Void Foo () {System.out.Println ("foo");}}
Import java.io.serializable; public class barimpl IMPLEMENTS BAR, Serializable {public void bar () {system.out.println ("bar");}}
Next We provide a Simple Interceptor Implementation To Log Method Calls.
public class LoggingInterceptor implements MethodInterceptor {public Object intercept (Object obj, Method method, Object [] args, MethodProxy proxy) throws Throwable {try {System.out.println ( "before" method); return proxy.invokeSuper (obj, args ); Finally {system.out.println ("after" method);}}}
once this is done we can instruct cglib to enhance our classes accordingly. the first example shows how we can extend the superclass FooImpl by implementing the Bar interface and dispatch to the implementation and intercepting method calls. you will also notice that we are instructing cglib to Implement the foobar interface - The purpose is to consolidate Our Interfaces INTO One "Aware" Interface So We do Not Have to Cast to Each Interface CGLIB IMPLEMENTED for US.
public void testExtendingSuperclass () throws Exception {Enhancer enhancer = new Enhancer (); enhancer.setSuperclass (FooImpl.class); enhancer.setInterfaces (new Class [] {Bar.class, FooBar.class}); enhancer.setCallbackTypes (new Class [] {Dispatcher.class, MethodInterceptor.class}); enhancer.setCallbackFilter (new CallbackFilter () {public int accept (Method method) {if (Bar.class.isAssignableFrom (method.getDeclaringClass ())) return 0; else return 1; } });
Class clazz = enhancer.createClass ();
enhancer.registerCallbacks (clazz, new Callback [] {new Dispatcher () {public Object loadObject () {return Enhancer.create (BarImpl.class, new LoggingInterceptor ());}}, new LoggingInterceptor ()}); FooBar foobar = FOOBAR) CLAZZ.NEWINSTANCE (); FOOBAR.FOO (); foobar.bar ();
The Next Example Shows How We CAN Have CGLIB CREATE A NEW CLASS for US WITHOUTETING A SUPERCLASS. IN This Example We Are Dispatching to All The Implementation That CGLIB HAS IMPLEMENTED For US.
public void testNoSuperclass () throws Exception {Enhancer enhancer = new Enhancer (); enhancer.setInterfaces (new Class [] {Foo.class, Bar.class, FooBar.class}); enhancer.setCallbackTypes (new Class [] {Dispatcher. class, Dispatcher.class}); enhancer.setCallbackFilter (new CallbackFilter () {public int accept (Method method) {return Foo.class.isAssignableFrom (method.getDeclaringClass ()) 0:? 1;}}); / * enhancer .setStrategy (new DefaultGeneratorStrategy () {protected byte [] transform (byte [] b) {try {FileOutputStream fos = new FileOutputStream ( "build / Cglib2Generated.class"); fos.write (b);} catch (Exception e) {E.PrintStackTrace ();
Return B;}}); * /
Class clazz = enhancer.createClass ();
enhancer.registerCallbacks (clazz, new Callback [] {new Dispatcher () {public Object loadObject () {return Enhancer.create (FooImpl.class, new LoggingInterceptor ());}}, new Dispatcher () {public Object loadObject () {Return Enhancer.create (barImpl.class, new logginginterceptor ());}}}); foobar foobar = (foobar) Clazz.newinstance (); foobar.foo (); foobar.bar ();
with these simple examples we are on our way to fully utilizing the power of cglib proxies. you may also notice that i have commented out setting the strategy in the second example. this is a very powerful undocumented feature that allows us to directly access the byte [] of our proxy. I Hope this provides Some Valuable Insight Into How you can use cglib to it's fullest.