Merge 44c08b7918
into 30db2e4fb5
This commit is contained in:
commit
c07704f32d
|
@ -79,6 +79,7 @@ import org.springframework.util.ObjectUtils;
|
||||||
* @author Chris Beams
|
* @author Chris Beams
|
||||||
* @author Dave Syer
|
* @author Dave Syer
|
||||||
* @author Sebastien Deleuze
|
* @author Sebastien Deleuze
|
||||||
|
* @author Yongjun Hong
|
||||||
* @see org.springframework.cglib.proxy.Enhancer
|
* @see org.springframework.cglib.proxy.Enhancer
|
||||||
* @see AdvisedSupport#setProxyTargetClass
|
* @see AdvisedSupport#setProxyTargetClass
|
||||||
* @see DefaultAopProxyFactory
|
* @see DefaultAopProxyFactory
|
||||||
|
@ -233,9 +234,11 @@ class CglibAopProxy implements AopProxy, Serializable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (CodeGenerationException | IllegalArgumentException ex) {
|
catch (CodeGenerationException | IllegalArgumentException ex) {
|
||||||
throw new AopConfigException("Could not generate CGLIB subclass of " + this.advised.getTargetClass() +
|
if (logger.isWarnEnabled()) {
|
||||||
": Common causes of this problem include using a final class or a non-visible class",
|
logger.warn("CGLIB subclass failed: " + ex.getMessage() +
|
||||||
ex);
|
" - falling back to JDK dynamic proxy for " + this.advised.getTargetSource(), ex);
|
||||||
|
}
|
||||||
|
return handleCglibFailure(classLoader, ex);
|
||||||
}
|
}
|
||||||
catch (Throwable ex) {
|
catch (Throwable ex) {
|
||||||
// TargetSource.getTarget() failed
|
// TargetSource.getTarget() failed
|
||||||
|
@ -243,6 +246,23 @@ class CglibAopProxy implements AopProxy, Serializable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Object handleCglibFailure(@Nullable ClassLoader classLoader, Exception originalException) {
|
||||||
|
Class<?>[] interfaces = this.advised.getProxiedInterfaces();
|
||||||
|
|
||||||
|
if (interfaces.length == 0) {
|
||||||
|
throw new AopConfigException("Could not generate CGLIB subclass of " +
|
||||||
|
this.advised.getTargetClass() + " and JDK proxy fallback not possible", originalException);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
JdkDynamicAopProxy jdkProxy = new JdkDynamicAopProxy(this.advised);
|
||||||
|
return jdkProxy.getProxy(classLoader);
|
||||||
|
}
|
||||||
|
catch (Throwable ex) {
|
||||||
|
throw new AopConfigException("Could not generate CGLIB subclass of " +
|
||||||
|
this.advised.getTargetClass() + " and JDK proxy fallback failed for interfaces " + ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected Class<?> createProxyClass(Enhancer enhancer) {
|
protected Class<?> createProxyClass(Enhancer enhancer) {
|
||||||
enhancer.setInterceptDuringConstruction(false);
|
enhancer.setInterceptDuringConstruction(false);
|
||||||
return enhancer.createClass();
|
return enhancer.createClass();
|
||||||
|
|
|
@ -50,6 +50,7 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
|
||||||
* @author Rob Harrop
|
* @author Rob Harrop
|
||||||
* @author Ramnivas Laddad
|
* @author Ramnivas Laddad
|
||||||
* @author Chris Beams
|
* @author Chris Beams
|
||||||
|
* @author Yongjun Hong
|
||||||
*/
|
*/
|
||||||
class CglibProxyTests extends AbstractAopProxyTests {
|
class CglibProxyTests extends AbstractAopProxyTests {
|
||||||
|
|
||||||
|
@ -429,6 +430,48 @@ class CglibProxyTests extends AbstractAopProxyTests {
|
||||||
assertThat(proxy.doWithVarargs(MyEnum.A, MyOtherEnum.C)).isTrue();
|
assertThat(proxy.doWithVarargs(MyEnum.A, MyOtherEnum.C)).isTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testProxyCreationAndPrivateAccess() {
|
||||||
|
ProxyFactory proxyFactory = new ProxyFactory(new TestServiceImpl());
|
||||||
|
proxyFactory.setProxyTargetClass(true);
|
||||||
|
|
||||||
|
TestService proxy = (TestService) proxyFactory.getProxy();
|
||||||
|
|
||||||
|
String result = proxy.publicMethod();
|
||||||
|
assertThat(result).isEqualTo("Private Access Success");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testProxyCreationWithoutInterfaceShouldThrowException() {
|
||||||
|
ProxyFactory proxyFactory = new ProxyFactory(new TestServiceImplWithOutInterface());
|
||||||
|
proxyFactory.setProxyTargetClass(true);
|
||||||
|
|
||||||
|
assertThatExceptionOfType(AopConfigException.class)
|
||||||
|
.isThrownBy(proxyFactory::getProxy)
|
||||||
|
.withMessageContaining("Could not generate CGLIB subclass of");
|
||||||
|
}
|
||||||
|
|
||||||
|
interface TestService {
|
||||||
|
String publicMethod();
|
||||||
|
}
|
||||||
|
|
||||||
|
static class TestServiceImpl implements TestService {
|
||||||
|
private TestServiceImpl() { }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String publicMethod() {
|
||||||
|
return "Private Access Success";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static class TestServiceImplWithOutInterface {
|
||||||
|
|
||||||
|
private TestServiceImplWithOutInterface() { }
|
||||||
|
|
||||||
|
public String publicMethod() {
|
||||||
|
return "Private Access Success";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static class MyBean {
|
public static class MyBean {
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue