Class: net.sf.cglib.proxy.Enhancer
- public class Enhancer
- extends AbstractClassGenerator
The original and most general callback type is the net.sf.cglib.proxy.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 net.sf.cglib.proxy.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 net.sf.cglib.proxy.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 net.sf.cglib.proxy.Factory interface, unless net.sf.cglib.proxy.Enhancer.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 net.sf.cglib.proxy.Proxy class.
Inheritance
Superclass tree:- java.lang.Object
- net.sf.cglib.core.AbstractClassGenerator
- net.sf.cglib.proxy.Enhancer
Methods
-
Enhancertop
public Enhancer()Create a newEnhancer. A newEnhancerobject should be used for each generated object, and should not be shared across threads. To create additional instances of a generated class, use theFactoryinterface. -
createtop
public Object create()Generate a new class if necessary and uses the specified callbacks (if any) to create a new object instance. Uses the no-arg constructor of the superclass. -
createtop
Helper method to create an intercepted object. For finer control over the generated instance, use a new instance ofEnhancerinstead of this static method. -
createtop
Helper method to create an intercepted object. For finer control over the generated instance, use a new instance ofEnhancerinstead of this static method. -
createtop
public static Object create(Class superclass, Class[] interfaces, CallbackFilter filter, Callback[] callbacks)Helper method to create an intercepted object. For finer control over the generated instance, use a new instance ofEnhancerinstead of this static method. -
createtop
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 theargumentTypesparameter, with the given arguments. -
createClasstop
public 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-argcreatemethod. -
filterConstructorstop
Filter the list of constructors from the superclass. The constructors which remain will be included in the generated class. The default implementation is to filter out all private constructors, but subclasses may extend Enhancer to override this behavior. -
firstInstancetop
- Override hierarchy:
- firstInstance from AbstractClassGenerator
-
generateClasstop
public void generateClass(ClassVisitor v) throws Exception- Specified by:
- generateClass from ClassGenerator
-
getDefaultClassLoadertop
protected ClassLoader getDefaultClassLoader()- Override hierarchy:
- getDefaultClassLoader from AbstractClassGenerator
-
getMethodstop
Finds all of the methods that will be extended by an Enhancer-generated class using the specified superclass and interfaces. This can be useful in building a list of Callback objects. The methods are added to the end of the given list. Due to the subclassing nature of the classes generated by Enhancer, the methods are guaranteed to be non-static, non-final, and non-private. Each method signature will only occur once, even if it occurs in multiple classes. -
isEnhancedtop
public static boolean isEnhanced(Class type)Determine if a class was generated usingEnhancer. -
nextInstancetop
- Override hierarchy:
- nextInstance from AbstractClassGenerator
-
registerCallbackstop
Call this method to register the net.sf.cglib.proxy.Callback array to use before creating a new instance of the generated class via reflection. If you are using an instance ofEnhanceror the net.sf.cglib.proxy.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 net.sf.cglib.proxy.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 net.sf.cglib.proxy.Enhancer.registerStaticCallbacks(java.lang.Class, net.sf.cglib.proxy.Callback[]).
The registered callbacks are overwritten and subsequently cleared when calling any of the
createmethods (such as net.sf.cglib.proxy.Enhancer.create()), or any net.sf.cglib.proxy.FactorynewInstancemethod. Otherwise they are not cleared, and you should be careful to set them back tonullafter creating new instances via reflection if memory leakage is a concern. -
registerStaticCallbackstop
Similar to net.sf.cglib.proxy.Enhancer.registerCallbacks(java.lang.Class, net.sf.cglib.proxy.Callback[]), but suitable for use when multiple threads will be creating instances of the generated class. The thread-level callbacks will always override the static callbacks. Static callbacks are never cleared. -
setCallbacktop
public void setCallback(Callback callback)Set the single net.sf.cglib.proxy.Callback to use. Ignored if you use net.sf.cglib.proxy.Enhancer.createClass(). -
setCallbackFiltertop
public void setCallbackFilter(CallbackFilter filter)Set the net.sf.cglib.proxy.CallbackFilter used to map the generated class' methods to a particular callback index. New object instances will always use the same mapping, but may use different actual callback objects. -
setCallbackTypetop
public void setCallbackType(Class callbackType)Set the single type of net.sf.cglib.proxy.Callback to use. This may be used instead of net.sf.cglib.proxy.Enhancer.setCallback(net.sf.cglib.proxy.Callback) when calling net.sf.cglib.proxy.Enhancer.createClass(), since it may not be possible to have an array of actual callback instances. -
setCallbackTypestop
public void setCallbackTypes(Class[] callbackTypes)Set the array of callback types to use. This may be used instead of net.sf.cglib.proxy.Enhancer.setCallbacks(net.sf.cglib.proxy.Callback[]) when calling net.sf.cglib.proxy.Enhancer.createClass(), since it may not be possible to have an array of actual callback instances. You must use a net.sf.cglib.proxy.CallbackFilter to specify the index into this array for each method in the proxied class. -
setCallbackstop
public void setCallbacks(Callback[] callbacks)Set the array of callbacks to use. Ignored if you use net.sf.cglib.proxy.Enhancer.createClass(). You must use a net.sf.cglib.proxy.CallbackFilter to specify the index into this array for each method in the proxied class. -
setInterceptDuringConstructiontop
public void setInterceptDuringConstruction(boolean interceptDuringConstruction)Set whether methods called from within the proxy's constructer will be intercepted. The default value is true. Unintercepted methods will call the method of the proxy's base class, if it exists. -
setInterfacestop
public void setInterfaces(Class[] interfaces)Set the interfaces to implement. TheFactoryinterface will always be implemented regardless of what is specified here. -
setSerialVersionUIDtop
public void setSerialVersionUID(Long sUID)Insert a static serialVersionUID field into the generated class. -
setSuperclasstop
public void setSuperclass(Class superclass)Set the class which the generated class will extend. As a convenience, if the supplied superclass is actually an interface,setInterfaceswill be called with the appropriate argument instead. A non-interface argument must not be declared as final, and must have an accessible constructor. -
setUseFactorytop
public void setUseFactory(boolean useFactory)Set whether the enhanced object instances should implement the net.sf.cglib.proxy.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 theFactoryinterface to prevent code from changing the underlying callbacks.
