cglib: The missing manual

The byte code instrumentation library cglib is a popular choice among many well-known Java frameworks such as Hibernate (not anymore) or Spring for doing their dirty work. Byte code instrumentation allows to manipulate or to create classes after the compilation phase of a Java application. Since Java classes are linked dynamically at run time, it is possible to add new classes to an already running Java program. Hibernate uses cglib for example for its generation of dynamic proxies. Instead of returning the full object that you stored in a database, Hibernate will return you an instrumented version of your stored class that lazily loads some values from the database only when they are requested. Spring used cglib for example when adding security constraints to your method calls. Instead of calling your method directly, Spring security will first check if a specified security check passes and only delegate to your actual method after this verification. Another popular use of cglib is within mocking frameworks such as mockito, where mocks are nothing more than instrumented class where the methods were replaced with empty implementations (plus some tracking logic).

Other than ASM – another very high-level byte code manipulation library on top of which cglib is built – cglib offers rather low-level byte code transformers that can be used without even knowing about the details of a compiled Java class. Unfortunately, the documentation of cglib is rather short, not to say that there is basically none. Besides a single blog article from 2005 that demonstrates the Enhancer class, there is not much to find. This blog article is an attempt to demonstrate cglib and its unfortunately often awkward API.

Enhancer

Let’s start with the Enhancer class, the probably most used class of the cglib library. An enhancer allows the creation of Java proxies for non-interface types. The Enhancer can be compared with the Java standard library’s Proxy class which was introduced in Java 1.3. The Enhancer dynamically creates a subclass of a given type but intercepts all method calls. Other than with the Proxy class, this works for both class and interface types. The following example and some of the examples after are based on this simple Java POJO:

1 public static class SampleClass {
2   public String test(String input) {
3     return "Hello world!";
4   }
5 }

Using cglib, the return value of test(String) method can easily be replaced by another value using an Enhancer and a FixedValue callback:

01 @Test
02 public void testFixedValue() throws Exception {
03   Enhancer enhancer = new Enhancer();
04   enhancer.setSuperclass(SampleClass.class);
05   enhancer.setCallback(new FixedValue() {
06     @Override
07     public Object loadObject() throws Exception {
08       return "Hello cglib!";
09     }
10   });
11   SampleClass proxy = (SampleClass) enhancer.create();
12   assertEquals("Hello cglib!", proxy.test(null));
13 }

In the above example, the enhancer will return an instance of an instrumented subclass of SampleClass where all method calls return a fixed value which is generated by the anonymous FixedValue implementation above. The object is created by Enhancer#create(Object...) where the method takes any number of arguments which are used to pick any constructor of the enhanced class. (Even though constructors are only methods on the Java byte code level, the Enhancer class cannot instrument constructors. Neither can it instrument static or final classes.) If you only want to create a class, but no instance, Enhancer#createClass will create a Class instance which can be used to create instances dynamically. All constructors of the enhanced class will be available as delegation constructors in this dynamically generated class.

Be aware that any method call will be delegated in the above example, also calls to the methods defined in java.lang.Object. As a result, a call to proxy.toString() will also return “Hello cglib!”. In contrast will a call to proxy.hashCode() result in a ClassCastException since the FixedValue interceptor always returns a String even though the Object#hashCode signature requires a primitive integer.

Another observation that can be made is that final methods are not intercepted. An example of such a method is Object#getClass which will return something like “SampleClass$$EnhancerByCGLIB$$e277c63c” when it is invoked. This class name is generated randomly by cglib in order to avoid naming conflicts. Be aware of the different class of the enhanced instance when you are making use of explicit types in your program code. The class generated by cglib will however be in the same package as the enhanced class (and therefore be able to override package-private methods). Similar to final methods, the subclassing approach makes for the inability of enhancing final classes. Therefore frameworks as Hibernate cannot persist final classes.

Next, let us look at a more powerful callback class, the InvocationHandler, that can also be used with an Enhancer:

01 @Test
02 public void testInvocationHandler() throws Exception {
03   Enhancer enhancer = new Enhancer();
04   enhancer.setSuperclass(SampleClass.class);
05   enhancer.setCallback(new InvocationHandler() {
06     @Override
07     public Object invoke(Object proxy, Method method, Object[] args)
08         throws Throwable {
09       if(method.getDeclaringClass() != Object.class && method.getReturnType() == String.class) {
10         return "Hello cglib!";
11       } else {
12         throw new RuntimeException("Do not know what to do.");
13       }
14     }
15   });
16   SampleClass proxy = (SampleClass) enhancer.create();
17   assertEquals("Hello cglib!", proxy.test(null));
18   assertNotEquals("Hello cglib!", proxy.toString());
19 }

This callback allows us to answer with regards to the invoked method. However, you should be careful when calling a method on the proxy object that comes with the InvocationHandler#invoke method. All calls on this method will be dispatched with the same InvocationHandler and might therefore result in an endless loop. In order to avoid this, we can use yet another callback dispatcher:

01 @Test
02 public void testMethodInterceptor() throws Exception {
03   Enhancer enhancer = new Enhancer();
04   enhancer.setSuperclass(SampleClass.class);
05   enhancer.setCallback(new MethodInterceptor() {
06     @Override
07     public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy)
08         throws Throwable {
09       if(method.getDeclaringClass() != Object.class && method.getReturnType() == String.class) {
10         return "Hello cglib!";
11       } else {
12         proxy.invokeSuper(obj, args);
13       }
14     }
15   });
16   SampleClass proxy = (SampleClass) enhancer.create();
17   assertEquals("Hello cglib!", proxy.test(null));
18   assertNotEquals("Hello cglib!", proxy.toString());
19   proxy.hashCode(); // Does not throw an exception or result in an endless loop.
20 }

The MethodInterceptor allows full control over the intercepted method and offers some utilities for calling the method of the enhanced class in their original state. But why would one want to use other methods anyways? Because the other methods are more efficient and cglib is often used in edge case frameworks where efficiency plays a significant role. The creation and linkage of the MethodInterceptor requires for example the generation of a different type of byte code and the creation of some runtime objects that are not required with the InvocationHandler. Because of that, there are other classes that can be used with the Enhancer:

  • LazyLoader: Even though the LazyLoader‘s only method has the same method signature as FixedValue, the LazyLoader is fundamentally different to the FixedValue interceptor. The LazyLoader is actually supposed to return an instance of a subclass of the enhanced class. This instance is requested only when a method is called on the enhanced object and then stored for future invocations of the generated proxy. This makes sense if your object is expensive in its creation without knowing if the object will ever be used. Be aware that some constructor of the enhanced class must be called both for the proxy object and for the lazily loaded object. Thus, make sure that there is another cheap (maybe protected) constructor available or use an interface type for the proxy. You can choose the invoked constructed by supplying arguments to Enhancer#create(Object...).
  • Dispatcher: The Dispatcher is like the LazyLoader but will be invoked on every method call without storing the loaded object. This allows to change the implementation of a class without changing the reference to it. Again, be aware that some constructor must be called for both the proxy and the generated objects.
  • ProxyRefDispatcher: This class carries a reference to the proxy object it is invoked from in its signature. This allows for example to delegate method calls to another method of this proxy. Be aware that this can easily cause an endless loop and will always cause an endless loop if the same method is called from within ProxyRefDispatcher#loadObject(Object).
  • NoOp: The NoOp class does not what its name suggests. Instead, it delegates each method call to the enhanced class’s method implementation.

At this point, the last two interceptors might not make sense to you. Why would you even want to enhance a class when you will always delegate method calls to the enhanced class anyways? And you are right. These interceptors should only be used together with a CallbackFilter as it is demonstrated in the following code snippet:

01 @Test
02 public void testCallbackFilter() throws Exception {
03   Enhancer enhancer = new Enhancer();
04   CallbackHelper callbackHelper = new CallbackHelper(SampleClass.class, new Class[0]) {
05     @Override
06     protected Object getCallback(Method method) {
07       if(method.getDeclaringClass() != Object.class && method.getReturnType() == String.class) {
08         return new FixedValue() {
09           @Override
10           public Object loadObject() throws Exception {
11             return "Hello cglib!";
12           };
13         }
14       } else {
15         return NoOp.INSTANCE; // A singleton provided by NoOp.
16       }
17     }
18   };
19   enhancer.setSuperclass(MyClass.class);
20   enhancer.setCallbackFilter(callbackHelper);
21   enhancer.setCallbacks(callbackHelper.getCallbacks());
22   SampleClass proxy = (SampleClass) enhancer.create();
23   assertEquals("Hello cglib!", proxy.test(null));
24   assertNotEquals("Hello cglib!", proxy.toString());
25   proxy.hashCode(); // Does not throw an exception or result in an endless loop.
26 }

The Enhancer instance accepts a CallbackFilter in its Enhancer#setCallbackFilter(CallbackFilter) method where it expects methods of the enhanced class to be mapped to array indices of an array of Callback instances. When a method is invoked on the created proxy, the Enhancer will then choose the according interceptor and dispatch the called method on the corresponding Callback (which is a marker interface for all the interceptors that were introduced so far). To make this API less awkward, cglib offers a CallbackHelper which will represent a CallbackFilter and which can create an array of Callbacks for you. The enhanced object above will be functionally equivalent to the one in the example for the MethodInterceptor but it allows you to write specialized interceptors whilst keeping the dispatching logic to these interceptors separate.

How does it work?

When the Enhancer creates a class, it will set create a private static field for each interceptor that was registered as a Callback for the enhanced class after its creation. This also means that class definitions that were created with cglib cannot be reused after their creation since the registration of callbacks does not become a part of the generated class’s initialization phase but are prepared manually by cglib after the class was already initialized by the JVM. This also means that classes created with cglib are not technically ready after their initialization and for example cannot be sent over the wire since the callbacks would not exist for the class loaded in the target machine.

Depending on the registered interceptors, cglib might register additional fields such as for example for the MethodInterceptor where two private static fields (one holding a reflective Method and a the other holding MethodProxy) are registered per method that is intercepted in the enhanced class or any of its subclasses. Be aware that the MethodProxy is making excessive use of the FastClass which triggers the creation of additional classes and is described in further detail below.

For all these reasons, be careful when using the Enhancer. And always register callback types defensively, since the MethodInterceptor will for example trigger the creation of additional classes and register additional static fields in the enhanced class. This is specifically dangerous since the callback variables are also stored as static variables in the enhanced class: This implies that the callback instances are never garbage collected (unless their ClassLoader is, what is unusual). This is in particular dangerous when using anonymous classes which silently carry a reference to their outer class. Recall the example above:

01 @Test
02 public void testFixedValue() throws Exception {
03   Enhancer enhancer = new Enhancer();
04   enhancer.setSuperclass(SampleClass.class);
05   enhancer.setCallback(new FixedValue() {
06     @Override
07     public Object loadObject() throws Exception {
08       return "Hello cglib!";
09     }
10   });
11   SampleClass proxy = (SampleClass) enhancer.create();
12   assertEquals("Hello cglib!", proxy.test(null));
13 }

The anonymous subclass of FixedValue would become hardly referenced from the enhanced SampleClass such that neither the anonymous FixedValue instance or the class holding the @Test method would ever be garbage collected. This can introduce nasty memory leaks in your applications. Therefore, do not use non-static inner classes with cglib. (I only use them in this blog entry for keeping the examples short.)

Finally, you should never intercept Object#finalize(). Due to the subclassing approach of cglib, intercepting finalize is implemented by overriding it what is in general a bad idea. Enhanced instances that intercept finalize will be treated differently by the garbage collector and will also cause these objects being queued in the JVM’s finalization queue. Also, if you (accidentally) create a hard reference to the enhanced class in your intercepted call to finalize, you have effectively created an noncollectable instance. This is in general nothing you want. Note that final methods are never intercepted by cglib. Thus, Object#wait, Object#notify and Object#notifyAll do not impose the same problems. Be however aware that Object#clone can be intercepted what is something you might not want to do.

Immutable bean

cglib’s ImmutableBean allows you to create an immutability wrapper similar to for example Collections#immutableSet. All changes of the underlying bean will be prevented by an IllegalStateException (however, not by an UnsupportedOperationException as recommended by the Java API). Looking at some bean

1 public class SampleBean {
2   private String value;
3   public String getValue() {
4     return value;
5   }
6   public void setValue(String value) {
7     this.value = value;
8   }
9 }

we can make this bean immutable:

01 @Test(expected = IllegalStateException.class)
02 public void testImmutableBean() throws Exception {
03   SampleBean bean = new SampleBean();
04   bean.setValue("Hello world!");
05   SampleBean immutableBean = (SampleBean) ImmutableBean.create(bean);
06   assertEquals("Hello world!", immutableBean.getValue());
07   bean.setValue("Hello world, again!");
08   assertEquals("Hello world, again!", immutableBean.getValue());
09   immutableBean.setValue("Hello cglib!"); // Causes exception.
10 }

As obvious from the example, the immutable bean prevents all state changes by throwing an IllegalStateException. However, the state of the bean can be changed by changing the original object. All such changes will be reflected by the ImmutableBean.

Bean generator

The BeanGenerator is another bean utility of cglib. It will create a bean for you at run time:

01 @Test
02 public void testBeanGenerator() throws Exception {
03   BeanGenerator beanGenerator = new BeanGenerator();
04   beanGenerator.addProperty("value", String.class);
05   Object myBean = beanGenerator.create();
06   
07   Method setter = myBean.getClass().getMethod("setValue", String.class);
08   setter.invoke(myBean, "Hello cglib!");
09   Method getter = myBean.getClass().getMethod("getValue");
10   assertEquals("Hello cglib!", getter.invoke(myBean));
11 }

As obvious from the example, the BeanGenerator first takes some properties as name value pairs. On creation, the BeanGenerator creates the accessors

  • <type> get<name>()
  • void set<name>(<type>)

for you. This might be useful when another library expects beans which it resolved by reflection but you do not know these beans at run time. (An example would be Apache Wicket which works a lot with beans.)

Bean copier

The BeanCopier is another bean utility that copies beans by their property values. Consider another bean with similar properties as SampleBean:

1 public class OtherSampleBean {
2   private String value;
3   public String getValue() {
4     return value;
5   }
6   public void setValue(String value) {
7     this.value = value;
8   }
9 }

Now you can copy properties from one bean to another:

1 @Test
2 public void testBeanCopier() throws Exception {
3   BeanCopier copier = BeanCopier.create(SampleBean.class, OtherSampleBean.class, false);
4   SampleBean bean = new SampleBean();
5   myBean.setValue("Hello cglib!");
6   OtherSampleBean otherBean = new OtherSampleBean();
7   copier.copy(bean, otherBean, null);
8   assertEquals("Hello cglib!", otherBean.getValue());
9 }

without being restrained to a specific type. The BeanCopier#copy mehtod takles an (eventually) optional Converter which allows to do some further manipulations on each bean property. If the BeanCopier is created with false as the third constructor argument, the Converter is ignored and can therefore be null.

Bulk bean

A BulkBean allows to use a specified set of a bean’s accessors by arrays instead of method calls:

01 @Test
02 public void testBulkBean() throws Exception {
03   BulkBean bulkBean = BulkBean.create(SampleBean.class,
04       new String[]{"getValue"},
05       new String[]{"setValue"},
06       new Class[]{String.class});
07   SampleBean bean = new SampleBean();
08   bean.setValue("Hello world!");
09   assertEquals(1, bulkBean.getPropertyValues(bean).length);
10   assertEquals("Hello world!", bulkBean.getPropertyValues(bean)[0]);
11   bulkBean.setPropertyValues(bean, new Object[] {"Hello cglib!"});
12   assertEquals("Hello cglib!", bean.getValue());
13 }

The BulkBean takes an array of getter names, an array of setter names and an array of property types as its constructor arguments. The resulting instrumented class can then extracted as an array by BulkBean#getPropertyBalues(Object). Similarly, a bean’s properties can be set by BulkBean#setPropertyBalues(Object, Object[]).

Bean map

This is the last bean utility within the cglib library. The BeanMap converts all properties of a bean to a String-to-Object Java Map:

1 @Test
2 public void testBeanGenerator() throws Exception {
3   SampleBean bean = new SampleBean();
4   BeanMap map = BeanMap.create(bean);
5   bean.setValue("Hello cglib!");
6   assertEquals("Hello cglib", map.get("value"));
7 }

Additionally, the BeanMap#newInstance(Object) method allows to create maps for other beans by reusing the same Class.

Key factory

The KeyFactory factory allows the dynamic creation of keys that are composed of multiple values that can be used in for example Map implementations. For doing so, the KeyFactory requires some interface that defines the values that should be used in such a key. This interface must contain a single method by the name newInstance that returns an Object. For example:

1 public interface SampleKeyFactory {
2   Object newInstance(String first, int second);
3 }

Now an instance of a a key can be created by:

1 @Test
2 public void testKeyFactory() throws Exception {
3   SampleKeyFactory keyFactory = (SampleKeyFactory) KeyFactory.create(Key.class);
4   Object key = keyFactory.newInstance("foo", 42);
5   Map<Object, String> map = new HashMap<Object, String>();
6   map.put(key, "Hello cglib!");
7   assertEquals("Hello cglib!", map.get(keyFactory.newInstance("foo", 42)));
8 }

The KeyFactory will assure the correct implementation of the Object#equals(Object) and Object#hashCode methods such that the resulting key objects can be used in a Map or a Set. The KeyFactory is also used quite a lot internally in the cglib library.

Mixin

Some might already know the concept of the Mixin class from other programing languages such as Ruby or Scala (where mixins are called traits). cglib Mixins allow the combination of several objects into a single object. However, in order to do so, those objects must be backed by interfaces:

01 public interface Interface1 {
02   String first();
03 }
04  
05 public interface Interface2 {
06   String second();
07 }
08  
09 public class Class1 implements Interface1 {
10   @Override
11   public String first() {
12     return "first";
13   }
14 }
15  
16 public class Class2 implements Interface2 {
17   @Override
18   public String second() {
19     return "second";
20   }
21 }

Now the classes Class1 and Class2 can be combined to a single class by an additional interface:

01 public interface MixinInterface extends Interface1, Interface2 { /* empty */ }
02  
03 @Test
04 public void testMixin() throws Exception {
05   Mixin mixin = Mixin.create(new Class[]{Interface1.class, Interface2.class,
06       MixinInterface.class}, new Object[]{new Class1(), new Class2()});
07   MixinInterface mixinDelegate = (MixinInterface) mixin;
08   assertEquals("first", mixinDelegate.first());
09   assertEquals("second", mixinDelegate.second());
10 }

Admittedly, the Mixin API is rather awkward since it requires the classes used for a mixin to implement some interface such that the problem could also be solved by non-instrumented Java.

String switcher

The StringSwitcher emulates a String to int Java Map:

1 @Test
2 public void testStringSwitcher() throws Exception {
3   String[] strings = new String[]{"one", "two"};
4   int[] values = new int[]{10, 20};
5   StringSwitcher stringSwitcher = StringSwitcher.create(strings, values, true);
6   assertEquals(10, stringSwitcher.intValue("one"));
7   assertEquals(20, stringSwitcher.intValue("two"));
8   assertEquals(-1, stringSwitcher.intValue("three"));
9 }

The StringSwitcher allows to emulate a switch command on Strings such as it is possible with the built-in Java switch statement since Java 7. If using the StringSwitcher in Java 6 or less really adds a benefit to your code remains however doubtful and I would personally not recommend its use.

Interface maker

The InterfaceMaker does what its name suggests: It dynamically creates a new interface.

01 @Test
02 public void testInterfaceMaker() throws Exception {
03   Signature signature = new Signature("foo", Type.DOUBLE_TYPE, new Type[]{Type.INT_TYPE});
04   InterfaceMaker interfaceMaker = new InterfaceMaker();
05   interfaceMaker.add(signature, new Type[0]);
06   Class iface = interfaceMaker.create();
07   assertEquals(1, iface.getMethods().length);
08   assertEquals("foo", iface.getMethods()[0].getName());
09   assertEquals(double.class, iface.getMethods()[0].getReturnType());
10 }

Other than any other class of cglib’s public API, the interface maker relies on ASM types. The creation of an interface in a running application will hardly make sense since an interface only represents a type which can be used by a compiler to check types. It can however make sense when you are generating code that is to be used in later development.

Method delegate

A MethodDelegate allows to emulate a C#-like delegate to a specific method by binding a method call to some interface. For example, the following code would bind the SampleBean#getValue method to a delegate:

01 public interface BeanDelegate {
02   String getValueFromDelegate();
03 }
04  
05 @Test
06 public void testMethodDelegate() throws Exception {
07   SampleBean bean = new SampleBean();
08   bean.setValue("Hello cglib!");
09   BeanDelegate delegate = (BeanDelegate) MethodDelegate.create(
10       bean, "getValue", BeanDelegate.class);
11   assertEquals("Hello world!", delegate.getValueFromDelegate());
12 }

There are however some things to note:

  • The factory method MethodDelegate#create takes exactly one method name as its second argument. This is the method the MethodDelegate will proxy for you.
  • There must be a method without arguments defined for the object which is given to the factory method as its first argument. Thus, the MethodDelegate is not as strong as it could be.
  • The third argument must be an interface with exactly one argument. The MethodDelegate implements this interface and can be cast to it. When the method is invoked, it will call the proxied method on the object that is the first argument.

Furthermore, consider these drawbacks:

  • cglib creates a new class for each proxy. Eventually, this will litter up your permanent generation heap space
  • You cannot proxy methods that take arguments.
  • If your interface takes arguments, the method delegation will simply not work without an exception thrown (the return value will always be null). If your interface requires another return type (even if that is more general), you will get a IllegalArgumentException.

Multicast delegate

The MulticastDelegate works a little different than the MethodDelegate even though it aims at similar functionality. For using the MulticastDelegate, we require an object that implements an interface:

01 public interface DelegatationProvider {
02   void setValue(String value);
03 }
04  
05 public class SimpleMulticastBean implements DelegatationProvider {
06   private String value;
07   public String getValue() {
08     return value;
09   }
10   public void setValue(String value) {
11     this.value = value;
12   }
13 }

Based on this interface-backed bean we can create a MulticastDelegate that dispatches all calls to setValue(String) to several classes that implement the DelegationProvider interface:

01 @Test
02 public void testMulticastDelegate() throws Exception {
03   MulticastDelegate multicastDelegate = MulticastDelegate.create(
04       DelegatationProvider.class);
05   SimpleMulticastBean first = new SimpleMulticastBean();
06   SimpleMulticastBean second = new SimpleMulticastBean();
07   multicastDelegate = multicastDelegate.add(first);
08   multicastDelegate = multicastDelegate.add(second);
09  
10   DelegatationProvider provider = (DelegatationProvider)multicastDelegate;
11   provider.setValue("Hello world!");
12  
13   assertEquals("Hello world!", first.getValue());
14   assertEquals("Hello world!", second.getValue());
15 }

Again, there are some drawbacks:

  • The objects need to implement a single-method interface. This sucks for third-party libraries and is awkward when you use CGlib to do some magic where this magic gets exposed to the normal code. Also, you could implement your own delegate easily (without byte code though but I doubt that you win so much over manual delegation).
  • When your delegates return a value, you will receive only that of the last delegate you added. All other return values are lost (but retrieved at some point by the multicast delegate).

Constructor delegate

A ConstructorDelegate allows to create a byte-instrumented factory method. For that, that we first require an interface with a single method newInstance which returns an Object and takes any amount of parameters to be used for a constructor call of the specified class. For example, in order to create a ConstructorDelegate for the SampleBean, we require the following to call SampleBean‘s default (no-argument) constructor:

01 public interface SampleBeanConstructorDelegate {
02   Object newInstance();
03 }
04  
05 @Test
06 public void testConstructorDelegate() throws Exception {
07   SampleBeanConstructorDelegate constructorDelegate = (SampleBeanConstructorDelegate) ConstructorDelegate.create(
08     SampleBean.class, SampleBeanConstructorDelegate.class);
09   SampleBean bean = (SampleBean) constructorDelegate.newInstance();
10   assertTrue(SampleBean.class.isAssignableFrom(bean.getClass()));
11 }

Parallel sorter

The ParallelSorter claims to be a faster alternative to the Java standard library’s array sorters when sorting arrays of arrays:

01 @Test
02 public void testParallelSorter() throws Exception {
03   Integer[][] value = {
04     {4, 3, 9, 0},
05     {2, 1, 6, 0}
06   };
07   ParallelSorter.create(value).mergeSort(0);
08   for(Integer[] row : value) {
09     int former = -1;
10     for(int val : row) {
11       assertTrue(former < val);
12       former = val;
13     }
14   }
15 }

The ParallelSorter takes an array of arrays and allows to either apply a merge sort or a quick sort on every row of the array. Be however careful when you use it:

  • When using arrays of primitives, you have to call merge sort with explicit sorting ranges (e.g. ParallelSorter.create(value).mergeSort(0, 0, 3) in the example. Otherwise, the ParallelSorter has a pretty obvious bug where it tries to cast the primitive array to an array Object[] what will cause a ClassCastException.
  • If the array rows are uneven, the first argument will determine the length of what row to consider. Uneven rows will either lead to the extra values not being considered for sorting or a ArrayIndexOutOfBoundException.

Personally, I doubt that the ParallelSorter really offers a time advantage. Admittedly, I did however not yet try to benchmark it. If you tried it, I’d be happy to hear about it in the comments.

Fast class and fast members

The FastClass promises a faster invocation of methods than the Java reflection API by wrapping a Java class and offering similar methods to the reflection API:

1 @Test
2 public void testFastClass() throws Exception {
3   FastClass fastClass = FastClass.create(SampleBean.class);
4   FastMethod fastMethod = fastClass.getMethod(SampleBean.class.getMethod("getValue"));
5   MyBean myBean = new MyBean();
6   myBean.setValue("Hello cglib!");
7   assertTrue("Hello cglib!", fastMethod.invoke(myBean, new Object[0]));
8 }

Besides the demonstrated FastMethod, the FastClass can also create FastConstructors but no fast fields. But how can the FastClass be faster than normal reflection? Java reflection is executed by JNI where method invocations are executed by some C-code. The FastClass on the other side creates some byte code that calls the method directly from within the JVM. However, the newer versions of the HotSpot JVM (and probably many other modern JVMs) know a concept called inflation where the JVM will translate reflective method calls into native version‘s of FastClass when a reflective method is executed often enough. You can even control this behavior (at least on a HotSpot JVM) with setting the sun.reflect.inflationThreshold property to a lower value. (The default is 15.) This property determines after how many reflective invocations a JNI call should be substituted by a byte code instrumented version. I would therefore recommend to not use FastClass on modern JVMs, it can however fine-tune performance on older Java virtual machines.

cglib proxy

The cglib Proxy is a reimplementation of the Java Proxy class mentioned in the beginning of this article. It is intended to allow using the Java library’s proxy in Java versions before Java 1.3 and differs only in minor details. The better documentation of the cglib Proxy can however be found in the Java standard library’s Proxy javadoc where an example of its use is provided. For this reason, I will skip a more detailed discussion of the cglib’s Proxy at this place.

A final word of warning

After this overview of cglib’s functionality, I want to speak a final word of warning. All cglib classes generate byte code which results in additional classes being stored in a special section of the JVM’s memory: The so called perm space. This permanent space is, as the name suggests, used for permanent objects that do not usually get garbage collected. This is however not completely true: Once a Class is loaded, it cannot be unloaded until the loading ClassLoader becomes available for garbage collection. This is only the case the Class was loaded with a custom ClassLoader which is not a native JVM system ClassLoader. This ClassLoader can be garbage collected if itself, all Classes it ever loaded and all instances of all Classes it ever loaded become available for garbage collection. This means: If you create more and more classes throughout the life of a Java application and if you do not take care of the removal of these classes, you will sooner or later run of of perm space what will result in your application’s death by the hands of an OutOfMemoryError. Therefore, use cglib sparingly. However, if you use cglib wisely and carefully, you can really do amazing things with it that go beyond what you can do with non-instrumented Java applications.

Lastly, when creating projects that depend on cglib, you should be aware of the fact that the cglib project is not as well maintained and active as it should be, considering its popularity. The missing documentation is a first hint. The often messy public API a second. But then there are also broken deploys of cglib to Maven central. The mailing list reads like an archive of spam messages. And the release cycles are rather unstable. You might therefore want to have a look at javassist, the only real low-level alternative to cglib. Javassist comes bundled with a pseudo-java compiler what allows to create quite amazing byte code instrumentations without even understanding Java byte code. If you like to get your hands dirty, you might also like ASM on top of which cglib is built. ASM comes with a great documentation of both the library and Java class files and their byte code.

你可能感兴趣的:(cglib)