net.sf.cglib.proxy
public class Enhancer extends AbstractClassGenerator
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 Enhancer 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.
Nested Class Summary | |
---|---|
interface | Enhancer.EnhancerKey Internal interface, only public due to ClassLoader issues. |
Constructor Summary | |
---|---|
Enhancer()
Create a new Enhancer . |
Method Summary | |
---|---|
Object | create()
Generate a new class if necessary and uses the specified
callbacks (if any) to create a new object instance.
|
Object | create(Class[] argumentTypes, Object[] arguments)
Generate a new class if necessary and uses the specified
callbacks (if any) to create a new object instance.
|
static Object | create(Class type, Callback callback)
Helper method to create an intercepted object.
|
static Object | create(Class superclass, Class[] interfaces, Callback callback)
Helper method to create an intercepted object.
|
static Object | create(Class superclass, Class[] interfaces, CallbackFilter filter, Callback[] callbacks)
Helper method to create an intercepted object.
|
Class | createClass()
Generate a new class if necessary and return it without creating a new instance.
|
void | generateClass(ClassVisitor v) |
static void | getMethods(Class superclass, Class[] interfaces, List methods)
Finds all of the methods that will be extended by an
Enhancer-generated class using the specified superclass and
interfaces. |
static boolean | isEnhanced(Class type)
Determine if a class was generated using Enhancer . |
static void | registerCallbacks(Class generatedClass, Callback[] callbacks)
Call this method to register the Callback array to use before
creating a new instance of the generated class via reflection. |
static void | registerStaticCallbacks(Class generatedClass, Callback[] callbacks)
Similar to Enhancer, but suitable for use
when multiple threads will be creating instances of the generated class.
|
void | setCallback(Callback callback)
Set the single Callback to use.
|
void | setCallbackFilter(CallbackFilter filter)
Set the CallbackFilter used to map the generated class' methods
to a particular callback index.
|
void | setCallbacks(Callback[] callbacks)
Set the array of callbacks to use.
|
void | setCallbackType(Class callbackType)
Set the single type of Callback to use.
|
void | setCallbackTypes(Class[] callbackTypes)
Set the array of callback types to use.
|
void | setInterceptDuringConstruction(boolean interceptDuringConstruction)
Set whether methods called from within the proxy's constructer
will be intercepted. |
void | setInterfaces(Class[] interfaces)
Set the interfaces to implement. |
void | setSerialVersionUID(Long sUID)
Insert a static serialVersionUID field into the generated class. |
void | setSuperclass(Class superclass)
Set the class which the generated class will extend. |
void | setUseFactory(boolean useFactory)
Set whether the enhanced object instances should implement
the Factory interface.
|
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.See Also: Factory
Returns: a new instance
argumentTypes
parameter, with the given arguments.Parameters: argumentTypes constructor signature arguments compatible wrapped arguments to pass to constructor
Returns: a new instance
Enhancer
instead of this static method.Parameters: type class to extend or interface to implement callback the callback to use for all methods
Enhancer
instead of this static method.Parameters: type class to extend or interface to implement interfaces array of interfaces to implement, or null callback the callback to use for all methods
Enhancer
instead of this static method.Parameters: type class to extend or interface to implement interfaces array of interfaces to implement, or null filter the callback filter to use when generating a new class callbacks callback implementations to use for the enhanced object
create
method.See Also: (Class[], Object[])
Parameters: superclass the class that will be extended, or null interfaces the list of interfaces that will be implemented, or null methods the list into which to copy the applicable methods
Enhancer
.Parameters: type any class
Returns: whether the class was generated using Enhancer
Enhancer
or the Factory interface to create
new instances, this method is unnecessary. Its primary use is for when you want to
cache and reuse a generated class yourself, and the generated class does
not implement the Factory interface.
Note that this method only registers the callbacks on the current thread. If you want to register callbacks for instances created by multiple threads, use Enhancer.
The registered callbacks are overwritten and subsequently cleared
when calling any of the create
methods (such as
Enhancer).
Parameters: generatedClass a class previously created by Enhancer callbacks the array of callbacks to use when instances of the generated class are created
See Also: Enhancer
Parameters: generatedClass a class previously created by Enhancer callbacks the array of callbacks to use when instances of the generated class are created
Parameters: callback the callback to use for all methods
See Also: Enhancer
Parameters: filter the callback filter to use when generating a new class
See Also: Enhancer
Parameters: callbacks the callback array
Parameters: callbackType the type of callback to use for all methods
See Also: Enhancer
Parameters: callbackTypes the array of callback types
Parameters: interceptDuringConstruction whether to intercept methods called from the constructor
Factory
interface will
always be implemented regardless of what is specified here.Parameters: interfaces array of interfaces to implement, or null
See Also: Factory
Parameters: sUID the field value, or null to avoid generating field.
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.Parameters: superclass class to extend or interface to implement
See Also: (Class[])
Factory
interface to
prevent code from changing the underlying callbacks.Parameters: useFactory whether to implement Factory
; default is true