org.springframework.aop.framework
final class: Cglib2AopProxy [javadoc |
source]
java.lang.Object
org.springframework.aop.framework.Cglib2AopProxy
All Implemented Interfaces:
AopProxy, Serializable
CGLIB2-based
AopProxy implementation for the Spring AOP framework.
Requires CGLIB 2.1+ on the classpath..
As of Spring 2.0, earlier CGLIB versions are not supported anymore.
Objects of this type should be obtained through proxy factories,
configured by an AdvisedSupport object. This class is internal
to Spring's AOP framework and need not be used directly by client code.
DefaultAopProxyFactory will automatically create CGLIB2-based
proxies if necessary, for example in case of proxying a target class
(see the attendant javadoc for details).
Proxies created using this class are thread-safe if the underlying
(target) class is thread-safe.
| Nested Class Summary: |
|---|
| public static class | Cglib2AopProxy.SerializableNoOp | Serializable replacement for CGLIB's NoOp interface.
Public to allow use elsewhere in the framework. |
| Field Summary |
|---|
| protected static final Log | logger | Logger available to subclasses; static to optimize serialization |
| protected final AdvisedSupport | advised | The configuration used to configure this proxy |
| Constructor: |
public Cglib2AopProxy(AdvisedSupport config) throws AopConfigException {
Assert.notNull(config, "AdvisedSupport must not be null");
if (config.getAdvisors().length == 0 && config.getTargetSource() == AdvisedSupport.EMPTY_TARGET_SOURCE) {
throw new AopConfigException("No advisors and no TargetSource specified");
}
this.advised = config;
this.advisedDispatcher = new AdvisedDispatcher(this.advised);
}
Create a new Cglib2AopProxy for the given AOP configuration. Parameters:
config - the AOP configuration as AdvisedSupport object
Throws:
AopConfigException - if the config is invalid. We try to throw an informative
exception in this case, rather than let a mysterious failure happen later.
|
| Method from org.springframework.aop.framework.Cglib2AopProxy Detail: |
protected Enhancer createEnhancer() {
return new Enhancer();
}
Creates the CGLIB Enhancer . Subclasses may wish to override this to return a custom
Enhancer implementation. |
public boolean equals(Object other) {
return (this == other || (other instanceof Cglib2AopProxy &&
AopProxyUtils.equalsInProxy(this.advised, ((Cglib2AopProxy) other).advised)));
}
|
public Object getProxy() {
return getProxy(null);
}
|
public Object getProxy(ClassLoader classLoader) {
if (logger.isDebugEnabled()) {
logger.debug("Creating CGLIB2 proxy: target source is " + this.advised.getTargetSource());
}
try {
Class rootClass = this.advised.getTargetClass();
Assert.state(rootClass != null, "Target class must be available for creating a CGLIB proxy");
Class proxySuperClass = rootClass;
if (AopUtils.isCglibProxyClass(rootClass)) {
proxySuperClass = rootClass.getSuperclass();
Class[] additionalInterfaces = rootClass.getInterfaces();
for (int i = 0; i < additionalInterfaces.length; i++) {
Class additionalInterface = additionalInterfaces[i];
this.advised.addInterface(additionalInterface);
}
}
// Validate the class, writing log messages as necessary.
validateClassIfNecessary(proxySuperClass);
// Configure CGLIB Enhancer...
Enhancer enhancer = createEnhancer();
if (classLoader != null) {
enhancer.setClassLoader(classLoader);
if (classLoader instanceof SmartClassLoader &&
((SmartClassLoader) classLoader).isClassReloadable(proxySuperClass)) {
enhancer.setUseCache(false);
}
}
enhancer.setSuperclass(proxySuperClass);
enhancer.setStrategy(new UndeclaredThrowableStrategy(UndeclaredThrowableException.class));
enhancer.setInterfaces(AopProxyUtils.completeProxiedInterfaces(this.advised));
enhancer.setInterceptDuringConstruction(false);
Callback[] callbacks = getCallbacks(rootClass);
enhancer.setCallbacks(callbacks);
enhancer.setCallbackFilter(new ProxyCallbackFilter(
this.advised.getConfigurationOnlyCopy(), this.fixedInterceptorMap, this.fixedInterceptorOffset));
Class[] types = new Class[callbacks.length];
for (int x = 0; x < types.length; x++) {
types[x] = callbacks[x].getClass();
}
enhancer.setCallbackTypes(types);
// Generate the proxy class and create a proxy instance.
Object proxy;
if (this.constructorArgs != null) {
proxy = enhancer.create(this.constructorArgTypes, this.constructorArgs);
}
else {
proxy = enhancer.create();
}
return proxy;
}
catch (CodeGenerationException ex) {
throw new AopConfigException("Could not generate CGLIB subclass of class [" +
this.advised.getTargetClass() + "]: " +
"Common causes of this problem include using a final class or a non-visible class",
ex);
}
catch (IllegalArgumentException ex) {
throw new AopConfigException("Could not generate CGLIB subclass of class [" +
this.advised.getTargetClass() + "]: " +
"Common causes of this problem include using a final class or a non-visible class",
ex);
}
catch (Exception ex) {
// TargetSource.getTarget() failed
throw new AopConfigException("Unexpected AOP exception", ex);
}
}
|
public int hashCode() {
return Cglib2AopProxy.class.hashCode() * 13 + this.advised.getTargetSource().hashCode();
}
|
public void setConstructorArguments(Object[] constructorArgs,
Class[] constructorArgTypes) {
if (constructorArgs == null || constructorArgTypes == null) {
throw new IllegalArgumentException("Both 'constructorArgs' and 'constructorArgTypes' need to be specified");
}
if (constructorArgs.length != constructorArgTypes.length) {
throw new IllegalArgumentException("Number of 'constructorArgs' (" + constructorArgs.length +
") must match number of 'constructorArgTypes' (" + constructorArgTypes.length + ")");
}
this.constructorArgs = constructorArgs;
this.constructorArgTypes = constructorArgTypes;
}
Set constructor arguments to use for creating the proxy. |