Add support for Objenesis proxy creation.
Extended DefaultAopProxyFactory to create Objenesis based proxies if the library is on the classpath. This allows classes without a default constructor being CGLib proxied. We're now falling back to original CGLib based behavior in case the proxy creation using Objenesis fails. Objenesis 2.0 is now inlined into spring-core to avoid interfering with other Objenesis versions on the classpath. Issue: SPR-10594
This commit is contained in:
committed by
Phillip Webb
parent
41fffdcf7b
commit
1f9e8f68d4
@@ -79,7 +79,7 @@ import org.springframework.util.ObjectUtils;
|
||||
* @see DefaultAopProxyFactory
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
final class CglibAopProxy implements AopProxy, Serializable {
|
||||
class CglibAopProxy implements AopProxy, Serializable {
|
||||
|
||||
// Constants for CGLIB callback array indices
|
||||
private static final int AOP_PROXY = 0;
|
||||
@@ -185,29 +185,20 @@ final class CglibAopProxy implements AopProxy, Serializable {
|
||||
enhancer.setSuperclass(proxySuperClass);
|
||||
enhancer.setStrategy(new MemorySafeUndeclaredThrowableStrategy(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);
|
||||
enhancer.setCallbackFilter(new ProxyCallbackFilter(
|
||||
this.advised.getConfigurationOnlyCopy(), this.fixedInterceptorMap, this.fixedInterceptorOffset));
|
||||
|
||||
// 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;
|
||||
return createProxyClassAndInstance(enhancer, callbacks);
|
||||
}
|
||||
catch (CodeGenerationException ex) {
|
||||
throw new AopConfigException("Could not generate CGLIB subclass of class [" +
|
||||
@@ -227,6 +218,15 @@ final class CglibAopProxy implements AopProxy, Serializable {
|
||||
}
|
||||
}
|
||||
|
||||
protected Object createProxyClassAndInstance(Enhancer enhancer, Callback[] callbacks) {
|
||||
|
||||
enhancer.setInterceptDuringConstruction(false);
|
||||
enhancer.setCallbacks(callbacks);
|
||||
|
||||
return this.constructorArgs == null ? enhancer.create() : enhancer.create(
|
||||
this.constructorArgTypes, this.constructorArgs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the CGLIB {@link Enhancer}. Subclasses may wish to override this to return a custom
|
||||
* {@link Enhancer} implementation.
|
||||
|
||||
@@ -59,7 +59,7 @@ public class DefaultAopProxyFactory implements AopProxyFactory, Serializable {
|
||||
if (targetClass.isInterface()) {
|
||||
return new JdkDynamicAopProxy(config);
|
||||
}
|
||||
return CglibProxyFactory.createCglibProxy(config);
|
||||
return new ObjenesisCglibAopProxy(config);
|
||||
}
|
||||
else {
|
||||
return new JdkDynamicAopProxy(config);
|
||||
@@ -75,17 +75,4 @@ public class DefaultAopProxyFactory implements AopProxyFactory, Serializable {
|
||||
Class[] interfaces = config.getProxiedInterfaces();
|
||||
return (interfaces.length == 0 || (interfaces.length == 1 && SpringProxy.class.equals(interfaces[0])));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Inner factory class used to just introduce a CGLIB dependency
|
||||
* when actually creating a CGLIB proxy.
|
||||
*/
|
||||
private static class CglibProxyFactory {
|
||||
|
||||
public static AopProxy createCglibProxy(AdvisedSupport advisedSupport) {
|
||||
return new CglibAopProxy(advisedSupport);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.aop.framework;
|
||||
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.cglib.proxy.Callback;
|
||||
import org.springframework.cglib.proxy.Enhancer;
|
||||
import org.springframework.cglib.proxy.Factory;
|
||||
import org.springframework.objenesis.ObjenesisException;
|
||||
import org.springframework.objenesis.ObjenesisStd;
|
||||
|
||||
/**
|
||||
* Objenesis based extension of {@link CglibAopProxy} to create proxy instances without
|
||||
* invoking the constructor of the class.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @since 4.0
|
||||
*/
|
||||
class ObjenesisCglibAopProxy extends CglibAopProxy {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(ObjenesisCglibAopProxy.class);
|
||||
|
||||
private final ObjenesisStd objenesis;
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new {@link ObjenesisCglibAopProxy} using the given {@link AdvisedSupport}.
|
||||
* @param config must not be {@literal null}.
|
||||
*/
|
||||
public ObjenesisCglibAopProxy(AdvisedSupport config) {
|
||||
super(config);
|
||||
this.objenesis = new ObjenesisStd(true);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected Object createProxyClassAndInstance(Enhancer enhancer, Callback[] callbacks) {
|
||||
try {
|
||||
Factory factory = (Factory) objenesis.newInstance(enhancer.createClass());
|
||||
factory.setCallbacks(callbacks);
|
||||
return factory;
|
||||
}
|
||||
catch (ObjenesisException ex) {
|
||||
// Fallback to Cglib on unsupported JVMs
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Unable to instantiate proxy using Objenesis, falling back "
|
||||
+ "to regular proxy construction", ex);
|
||||
}
|
||||
return super.createProxyClassAndInstance(enhancer, callbacks);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user