net.sf.cglib
Class Enhancer

java.lang.Object
  extended bynet.sf.cglib.Enhancer

public class Enhancer
extends java.lang.Object

Provides methods to create dynamic proxies of any class, not just interfaces.

This code enhances a Vector object for tracing, by intercepting all methods:

   java.util.Vector vector = (java.util.Vector)Enhancer.enhance(
     java.util.Vector.class,                      // extend Vector
     new Class[]{ java.util.List.class },         // implement List
     new MethodInterceptor() {
         public Object intercept(Object obj, java.lang.reflect.Method method,
                                 Object[] args, MethodProxy proxy) throws Throwable {
             System.out.println(method);
             return proxy.invokeSuper(obj, args); // invoke original method
         }
     });
 

Enhancer is comparable to the standard Dynamic Proxy feature built into Java, but with some important differences, including:

All enhanced objects implement the Factory interface.

Version:
$Id: Enhancer.java,v 1.39 2003/05/28 03:56:30 herbyderby Exp $
Author:
Juozas Baliuka baliuka@mwm.lt
See Also:
MethodInterceptor, Factory

Nested Class Summary
static class Enhancer.InternalReplace
          Class containing the default implementation of the writeReplace method.
 
Method Summary
static java.lang.Object enhance(java.lang.Class cls, java.lang.Class[] interfaces, MethodInterceptor ih)
          Helper method, has same effect as
static java.lang.Object enhance(java.lang.Class cls, java.lang.Class[] interfaces, MethodInterceptor ih, java.lang.ClassLoader loader)
          Helper method, has same effect as
static java.lang.Object enhance(java.lang.Class cls, java.lang.Class[] interfaces, MethodInterceptor ih, java.lang.ClassLoader loader, java.lang.reflect.Method wreplace)
          Helper method, has same effect as
static java.lang.Object enhance(java.lang.Class cls, java.lang.Class[] interfaces, MethodInterceptor ih, java.lang.ClassLoader loader, java.lang.reflect.Method wreplace, MethodFilter filter)
          Enhances a public non-final class.
static Factory enhance(java.lang.Class cls, MethodInterceptor ih)
          Overrides non-abstract methods and implements all abstract methods.
static java.lang.Class enhanceClass(java.lang.Class cls, java.lang.Class[] interfaces, java.lang.ClassLoader loader, MethodFilter filter)
          This method can be used to enhance a class that does not have a no-args constructor.
static MethodInterceptor getMethodInterceptor(java.lang.Object enhanced)
          Helper method to get the current interceptor for an enhanced object.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getMethodInterceptor

public static MethodInterceptor getMethodInterceptor(java.lang.Object enhanced)
Helper method to get the current interceptor for an enhanced object.

Throws:
java.lang.ClassCastException - If the object is not an enhanced object (i.e. does not implement Factory).
See Also:
Factory.interceptor()

enhance

public static Factory enhance(java.lang.Class cls,
                              MethodInterceptor ih)
Overrides non-abstract methods and implements all abstract methods. The returned instance extends/implements the supplied class, and additionally implements the Factory interface.

Parameters:
cls - Class or interface to extend or implement
ih - interceptor used to handle implemented methods
Returns:
instance of supplied Class; new Class is defined in the same class loader

enhance

public static java.lang.Object enhance(java.lang.Class cls,
                                       java.lang.Class[] interfaces,
                                       MethodInterceptor ih)
Helper method, has same effect as
return enhance(cls, interfaces, ih, null, null, null);

See Also:
enhance(Class, Class[], MethodInterceptor, ClassLoader, Method, MethodFilter)

enhance

public static java.lang.Object enhance(java.lang.Class cls,
                                       java.lang.Class[] interfaces,
                                       MethodInterceptor ih,
                                       java.lang.ClassLoader loader)
Helper method, has same effect as
return enhance(cls, interfaces, ih, loader, null, null);

See Also:
enhance(Class, Class[], MethodInterceptor, ClassLoader, Method, MethodFilter)

enhance

public static java.lang.Object enhance(java.lang.Class cls,
                                       java.lang.Class[] interfaces,
                                       MethodInterceptor ih,
                                       java.lang.ClassLoader loader,
                                       java.lang.reflect.Method wreplace)
Helper method, has same effect as
return enhance(cls, interfaces, ih, loader, wreplace, null);

See Also:
enhance(Class, Class[], MethodInterceptor, ClassLoader, Method, MethodFilter)

enhance

public static java.lang.Object enhance(java.lang.Class cls,
                                       java.lang.Class[] interfaces,
                                       MethodInterceptor ih,
                                       java.lang.ClassLoader loader,
                                       java.lang.reflect.Method wreplace,
                                       MethodFilter filter)
Enhances a public non-final class. Source class must have a public or protected no-args constructor. Code is generated for protected and public non-final methods, and package methods if the source class is not in a the java.* hierarchy.

Parameters:
cls - class to extend, uses Object.class if null
interfaces - interfaces to implement, can be null or empty
ih - interceptor used to handle implemented methods
loader - ClassLoader for enhanced class, uses "current" if null
wreplace - static method to implement writeReplace, must have a single Object type parameter (to replace) and return type of Object. If null, a default implementation is used.
filter - a filter to prevent certain methods from being intercepted, may be null to intercept all possible methods
Returns:
an instance of the enhanced class. Will extend the source class and implement the given interfaces, plus the CGLIB Factory interface.
See Also:
Enhancer.InternalReplace.writeReplace(Object), Factory

enhanceClass

public static java.lang.Class enhanceClass(java.lang.Class cls,
                                           java.lang.Class[] interfaces,
                                           java.lang.ClassLoader loader,
                                           MethodFilter filter)
This method can be used to enhance a class that does not have a no-args constructor.

Parameters:
cls - class to extend, uses Object.class if null
interfaces - interfaces to implement, can be null or empty
loader - ClassLoader for enhanced class, uses "current" if null
filter - a filter to prevent certain methods from being intercepted, may be null to intercept all possible methods
Returns:
the generated class; you must use reflection to create new object instances.


Copyright (c) 2001 - Apache Software Foundation