Fixed fallback mode in ObjenesisCglibAopProxy, plus consistent support for bypassing Objenesis (e.g. on Google App Engine)

This 4.2 commit revises SpringObjenesis towards a smart delegate, including support for a "spring.objenesis.ignore" system property.

Issue: SPR-13131
This commit is contained in:
Juergen Hoeller
2015-06-16 22:01:37 +02:00
parent 581ab18b85
commit 92f1754b1e
5 changed files with 184 additions and 45 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 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.
@@ -102,9 +102,9 @@ class CglibAopProxy implements AopProxy, Serializable {
/** The configuration used to configure this proxy */
protected final AdvisedSupport advised;
private Object[] constructorArgs;
protected Object[] constructorArgs;
private Class<?>[] constructorArgTypes;
protected Class<?>[] constructorArgTypes;
/** Dispatcher used for methods on Advised */
private final transient AdvisedDispatcher advisedDispatcher;

View File

@@ -22,9 +22,7 @@ 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.Objenesis;
import org.springframework.objenesis.ObjenesisException;
import org.springframework.objenesis.ObjenesisStd;
import org.springframework.objenesis.SpringObjenesis;
/**
@@ -40,9 +38,7 @@ class ObjenesisCglibAopProxy extends CglibAopProxy {
private static final Log logger = LogFactory.getLog(ObjenesisCglibAopProxy.class);
private static final Objenesis cachedObjenesis = new SpringObjenesis();
private static final Objenesis nonCachedObjenesis = new ObjenesisStd(false);
private static final SpringObjenesis objenesis = new SpringObjenesis();
/**
@@ -57,17 +53,34 @@ class ObjenesisCglibAopProxy extends CglibAopProxy {
@Override
@SuppressWarnings("unchecked")
protected Object createProxyClassAndInstance(Enhancer enhancer, Callback[] callbacks) {
try {
Objenesis objenesis = (enhancer.getUseCache() ? cachedObjenesis : nonCachedObjenesis);
Factory factory = (Factory) objenesis.newInstance(enhancer.createClass());
factory.setCallbacks(callbacks);
return factory;
Class<?> proxyClass = enhancer.createClass();
Object proxyInstance = null;
if (objenesis.isWorthTrying()) {
try {
proxyInstance = objenesis.newInstance(proxyClass, enhancer.getUseCache());
}
catch (ObjenesisException ex) {
logger.debug("Unable to instantiate proxy using Objenesis, " +
"falling back to regular proxy construction", ex);
}
}
catch (ObjenesisException ex) {
// Fallback to regular proxy construction on unsupported JVMs
logger.debug("Unable to instantiate proxy using Objenesis, falling back to regular proxy construction", ex);
return super.createProxyClassAndInstance(enhancer, callbacks);
if (proxyInstance == null) {
// Regular instantiation via default constructor...
try {
proxyInstance = (this.constructorArgs != null ?
proxyClass.getConstructor(this.constructorArgTypes).newInstance(this.constructorArgs) :
proxyClass.newInstance());
}
catch (Exception ex) {
throw new AopConfigException("Unable to instantiate proxy using Objenesis, " +
"and regular proxy instantiation via default constructor fails as well", ex);
}
}
((Factory) proxyInstance).setCallbacks(callbacks);
return proxyInstance;
}
}