Top Description Inners Fields Constructors Methods
org.springframework.cglib.proxy

public Class Enhancer

extends AbstractClassGenerator
Class Inheritance
Annotations
@SuppressWarnings:rawtypes, unchecked
Imports
java.lang.ref.WeakReference, java.lang.reflect.Constructor, .Field, .InvocationTargetException, .Method, java.security.ProtectionDomain, java.util.ArrayList, .Arrays, .HashMap, .HashSet, .Iterator, .List, .Map, .Set, org.springframework.asm.ClassVisitor, .Label, .Type, org.springframework.cglib.core.AbstractClassGenerator, .ClassEmitter, .CodeEmitter, .CodeGenerationException, .CollectionUtils, .Constants, .DuplicatesPredicate, .EmitUtils, .Local, .MethodInfo, .MethodInfoTransformer, .MethodWrapper, .ObjectSwitchCallback, .ProcessSwitchCallback, .ReflectUtils, .RejectModifierPredicate, .Signature, .TypeUtils, .VisibilityPredicate, .WeakCacheKey

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 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 and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
Enhancer.EnhancerFactoryData

The idea of the class is to cache relevant java.lang.reflect instances so proxy-class can be instantiated faster that when using ReflectUtils#newInstance(Class, Class[], Object[]) and Enhancer#setThreadCallbacks(Class, Callback[])

private static record

Field Summary

Modifier and TypeField and Description
private static final CallbackFilter
private Object[]
private Class<T>[]
private static final Signature
private static final String
private static final Type
private static final Type
private static final String
CALLBACK_FILTER_FIELD

org.springframework.cglib.core.AbstractClassGenerator.ClassLoaderData#generatedClasses requires to keep cache key in a good shape (the keys should be up and running if the proxy class is alive), and one of the cache keys is CallbackFilter.

private Callback[]
private Type[]
private boolean
private static final String
private static final Signature
private Enhancer.EnhancerFactoryData
private Object
private static final Type
private static final String
private CallbackFilter
private static final Signature
private static final Signature
private static final Type
private static final Type
private boolean
private Class<T>[]
private static final Signature
private static final Signature
private static final Type
private Long
private static final Signature
private static final Signature
private static final Signature
private static final String
private static final Signature
private static final String
private static final Signature
private static final AbstractClassGenerator.Source
private static final String
private Class<T>
private static final String
private static final Type
private static final Signature
private static final Signature
private boolean
private boolean

Constructor Summary

AccessConstructor and Description
public
Enhancer()

Create a new Enhancer.

Method Summary

Modifier and TypeMethod and Description
public Object

Returns:

a new instance
create
()

Generate a new class if necessary and uses the specified callbacks (if any) to create a new object instance.

public Object

Returns:

a new instance
create
(Class<T>[]
constructor signature
argumentTypes
,
Object[]
compatible wrapped arguments to pass to constructor
arguments
)

Generate a new class if necessary and uses the specified callbacks (if any) to create a new object instance.

public static Object
create(Class<T>
class to extend or interface to implement
type
,
Callback
the callback to use for all methods
callback
)

Helper method to create an intercepted object.

public static Object
create(Class<T>
class to extend or interface to implement
superclass
,
Class<T>[]
array of interfaces to implement, or null
interfaces
,
Callback
the callback to use for all methods
callback
)

Helper method to create an intercepted object.

public static Object
create(Class<T>
class to extend or interface to implement
superclass
,
Class<T>[]
array of interfaces to implement, or null
interfaces
,
CallbackFilter
the callback filter to use when generating a new class
filter
,
Callback[]
callback implementations to use for the enhanced object
callbacks
)

Helper method to create an intercepted object.

public Class<T>
createClass()

Generate a new class if necessary and return it without creating a new instance.

private Object
private Object

Returns:

newly created instance
createUsingReflection
(Class<T>
class to instantiate
type
)

Instantiates a proxy instance and assigns callback values.

private void
private void
private void
emitConstructors(ClassEmitter ce, List<E> constructors)

private void
private void
private void
emitGetCallback(ClassEmitter ce, int[] keys)

private void
private void
emitMethods(final ClassEmitter ce, List<E> methods, List<E> actualMethods)

private void
private void
private void
private void
emitSetCallback(ClassEmitter ce, int[] keys)

private void
private void
private void
protected void
filterConstructors(Class<T>
the superclass
sc
,
List<E>
the list of all declared constructors from the superclass
constructors
)

Filter the list of constructors from the superclass.

protected Object

Returns:

newly created proxy instance
firstInstance
(Class<T>
class to instantiate
type
)

Implements abstract org.springframework.cglib.core.AbstractClassGenerator.firstInstance.

This method should not be called in regular flow.

protected Class<T>
public void
private static String
getCallbackField(int index)

private int[]
private static Method
getCallbacksSetter(Class<T> type, String methodName)

protected ClassLoader
public static void
getMethods(Class<T>
the class that will be extended, or null
superclass
,
Class<T>[]
the list of interfaces that will be implemented, or null
interfaces
,
List<E>
the list into which to copy the applicable methods
methods
)

Finds all of the methods that will be extended by an Enhancer-generated class using the specified superclass and interfaces.

private static void
getMethods(Class<T> superclass, Class<T>[] interfaces, List<E> methods, List<E> interfaceMethods, Set<E> forcePublic)

protected ProtectionDomain
getProtectionDomain()

Overrides org.springframework.cglib.core.AbstractClassGenerator.getProtectionDomain.

Returns the protection domain to use when defining the class.

private Type
public static boolean

Returns:

whether the class was generated using Enhancer
isEnhanced
(Class<T>
any class
type
)

Determine if a class was generated using Enhancer.

protected Object
private void
public static void
registerCallbacks(Class<T>
a class previously created by Enhancer
generatedClass
,
Callback[]
the array of callbacks to use when instances of the generated class are created
callbacks
)

Call this method to register the Callback array to use before creating a new instance of the generated class via reflection.

public static void
registerStaticCallbacks(Class<T>
a class previously created by Enhancer
generatedClass
,
Callback[]
the array of callbacks to use when instances of the generated class are created
callbacks
)

Similar to registerCallbacks, but suitable for use when multiple threads will be creating instances of the generated class.

private Signature
rename(Signature sig, int index)

public void
setCallback(final Callback
the callback to use for all methods
callback
)

Set the single Callback to use.

public void
setCallbackFilter(CallbackFilter
the callback filter to use when generating a new class
filter
)

Set the CallbackFilter used to map the generated class' methods to a particular callback index.

public void
setCallbacks(Callback[]
the callback array
callbacks
)

Set the array of callbacks to use.

private static void
setCallbacksHelper(Class<T> type, Callback[] callbacks, String methodName)

public void
setCallbackType(Class<T>
the type of callback to use for all methods
callbackType
)

Set the single type of Callback to use.

public void
setCallbackTypes(Class<T>[]
the array of callback types
callbackTypes
)

Set the array of callback types to use.

public void
setInterceptDuringConstruction(boolean
whether to intercept methods called from the constructor
interceptDuringConstruction
)

Set whether methods called from within the proxy's constructer will be intercepted.

public void
setInterfaces(Class<T>[]
array of interfaces to implement, or null
interfaces
)

Set the interfaces to implement.

public void
setSerialVersionUID(Long
the field value, or null to avoid generating field.
sUID
)

Insert a static serialVersionUID field into the generated class.

public void
setSuperclass(Class<T>
class to extend or interface to implement
superclass
)

Set the class which the generated class will extend.

private static void
setThreadCallbacks(Class<T> type, Callback[] callbacks)

public void
setUseFactory(boolean
whether to implement Factory; default is true
useFactory
)

Set whether the enhanced object instances should implement the Factory interface.

protected Object
private void
protected Object
Inherited from org.springframework.cglib.core.AbstractClassGenerator:
creategetAttemptLoadgetClassLoadergetClassNamegetCurrentgetNamingPolicygetStrategygetUseCachesetAttemptLoadsetClassLoadersetContextClasssetNamePrefixsetNamingPolicysetStrategysetUseCache