net.sf.cglib.proxy
Class Enhancer

Generates dynamic subclasses to enable method interception. This
class started as a substitute for the standard Dynamic Proxy support
included with JDK 1.3, but one that allowed the proxies to extend a
concrete base class, in addition to implementing interfaces. The dynamically
generated subclasses override the non-final methods of the superclass and
have hooks which callback to user-defined interceptor
implementations.

The original and most general callback type is the MethodInterceptor, which
in AOP terms enables "around advice"--that is, you can invoke custom code both before
and after the invocation of the "super" method. In addition you can modify the
arguments before calling the super method, or not call it at all.

Although MethodInterceptor is generic enough to meet any
interception need, it is often overkill. For simplicity and performance, additional
specialized callback types, such as LazyLoader are also available.
Often a single callback will be used per enhanced class, but you can control
which callback is used on a per-method basis with a CallbackFilter.

The most common uses of this class are embodied in the static helper methods. For
advanced needs, such as customizing the ClassLoader to use, you should create
a new instance of Enhancer. Other classes within CGLIB follow a similar pattern.

All enhanced objects implement the Factory interface, unless setUseFactory(boolean) is
used to explicitly disable this feature. The Factory interface provides an API
to change the callbacks of an existing object, as well as a faster and easier way to create
new instances of the same type.

For an almost drop-in replacement for
java.lang.reflect.Proxy, see the Proxy class.

Enhancer

public Enhancer()

Create a new Enhancer. A new Enhancer
object should be used for each generated object, and should not
be shared across threads. To create additional instances of a
generated class, use the Factory interface.

setSuperclass

public void setSuperclass(java.lang.Class superclass)

Set the class which the generated class will extend. As a convenience,
if the supplied superclass is actually an interface, setInterfaces
will be called with the appropriate argument instead.
A non-interface argument must not be declared as final, and must have an
accessible constructor.

setUseFactory

public void setUseFactory(boolean useFactory)

Set whether the enhanced object instances should implement
the Factory interface.
This was added for tools that need for proxies to be more
indistinguishable from their targets. Also, in some cases it may
be necessary to disable the Factory interface to
prevent code from changing the underlying callbacks.

create

Generate a new class if necessary and uses the specified
callbacks (if any) to create a new object instance.
Uses the constructor of the superclass matching the argumentTypes
parameter, with the given arguments.

Parameters:

argumentTypes - constructor signature

arguments - compatible wrapped arguments to pass to constructor

Returns:

a new instance

createClass

public java.lang.Class createClass()

Generate a new class if necessary and return it without creating a new instance.
This ignores any callbacks that have been set.
To create a new instance you will have to use reflection, and methods
called during the constructor will not be intercepted. To avoid this problem,
use the multi-arg create method.