Create CGLIB proxy in case of no target interfaces (just introductions)
Closes gh-31304
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2023 the original author or authors.
|
* Copyright 2002-2024 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -59,7 +59,7 @@ public class DefaultAopProxyFactory implements AopProxyFactory, Serializable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
|
public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
|
||||||
if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) {
|
if (config.isOptimize() || config.isProxyTargetClass() || !hasTargetInterfaces(config)) {
|
||||||
Class<?> targetClass = config.getTargetClass();
|
Class<?> targetClass = config.getTargetClass();
|
||||||
if (targetClass == null) {
|
if (targetClass == null) {
|
||||||
throw new AopConfigException("TargetSource cannot determine target class: " +
|
throw new AopConfigException("TargetSource cannot determine target class: " +
|
||||||
@@ -75,14 +75,14 @@ public class DefaultAopProxyFactory implements AopProxyFactory, Serializable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private boolean hasTargetInterfaces(AdvisedSupport config) {
|
||||||
* Determine whether the supplied {@link AdvisedSupport} has only the
|
Class<?> targetClass = config.getTargetClass();
|
||||||
* {@link org.springframework.aop.SpringProxy} interface specified
|
for (Class<?> ifc : config.getProxiedInterfaces()) {
|
||||||
* (or no proxy interfaces specified at all).
|
if (targetClass != null ? ifc.isAssignableFrom(targetClass) : !SpringProxy.class.isAssignableFrom(ifc)) {
|
||||||
*/
|
return true;
|
||||||
private boolean hasNoUserSuppliedProxyInterfaces(AdvisedSupport config) {
|
}
|
||||||
Class<?>[] ifcs = config.getProxiedInterfaces();
|
}
|
||||||
return (ifcs.length == 0 || (ifcs.length == 1 && SpringProxy.class.isAssignableFrom(ifcs[0])));
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -189,25 +189,22 @@ class ProxyFactoryTests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
TestBeanSubclass raw = new TestBeanSubclass();
|
TestBeanSubclass raw = new TestBeanSubclass();
|
||||||
ProxyFactory factory = new ProxyFactory(raw);
|
ProxyFactory pf = new ProxyFactory(raw);
|
||||||
//System.out.println("Proxied interfaces are " + StringUtils.arrayToDelimitedString(factory.getProxiedInterfaces(), ","));
|
assertThat(pf.getProxiedInterfaces()).as("Found correct number of interfaces").hasSize(5);
|
||||||
assertThat(factory.getProxiedInterfaces()).as("Found correct number of interfaces").hasSize(5);
|
ITestBean tb = (ITestBean) pf.getProxy();
|
||||||
ITestBean tb = (ITestBean) factory.getProxy();
|
|
||||||
assertThat(tb).as("Picked up secondary interface").isInstanceOf(IOther.class);
|
assertThat(tb).as("Picked up secondary interface").isInstanceOf(IOther.class);
|
||||||
raw.setAge(25);
|
raw.setAge(25);
|
||||||
assertThat(tb.getAge()).isEqualTo(raw.getAge());
|
assertThat(tb.getAge()).isEqualTo(raw.getAge());
|
||||||
|
|
||||||
|
Class<?>[] oldProxiedInterfaces = pf.getProxiedInterfaces();
|
||||||
long t = 555555L;
|
long t = 555555L;
|
||||||
TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor(t);
|
TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor(t);
|
||||||
|
pf.addAdvisor(0, new DefaultIntroductionAdvisor(ti, TimeStamped.class));
|
||||||
|
|
||||||
Class<?>[] oldProxiedInterfaces = factory.getProxiedInterfaces();
|
Class<?>[] newProxiedInterfaces = pf.getProxiedInterfaces();
|
||||||
|
|
||||||
factory.addAdvisor(0, new DefaultIntroductionAdvisor(ti, TimeStamped.class));
|
|
||||||
|
|
||||||
Class<?>[] newProxiedInterfaces = factory.getProxiedInterfaces();
|
|
||||||
assertThat(newProxiedInterfaces).as("Advisor proxies one more interface after introduction").hasSize(oldProxiedInterfaces.length + 1);
|
assertThat(newProxiedInterfaces).as("Advisor proxies one more interface after introduction").hasSize(oldProxiedInterfaces.length + 1);
|
||||||
|
|
||||||
TimeStamped ts = (TimeStamped) factory.getProxy();
|
TimeStamped ts = (TimeStamped) pf.getProxy();
|
||||||
assertThat(ts.getTimeStamp()).isEqualTo(t);
|
assertThat(ts.getTimeStamp()).isEqualTo(t);
|
||||||
// Shouldn't fail;
|
// Shouldn't fail;
|
||||||
((IOther) ts).absquatulate();
|
((IOther) ts).absquatulate();
|
||||||
@@ -224,26 +221,26 @@ class ProxyFactoryTests {
|
|||||||
|
|
||||||
NopInterceptor di = new NopInterceptor();
|
NopInterceptor di = new NopInterceptor();
|
||||||
NopInterceptor diUnused = new NopInterceptor();
|
NopInterceptor diUnused = new NopInterceptor();
|
||||||
ProxyFactory factory = new ProxyFactory(new TestBean());
|
ProxyFactory pf = new ProxyFactory(new TestBean());
|
||||||
factory.addAdvice(0, di);
|
pf.addAdvice(0, di);
|
||||||
assertThat(factory.getProxy()).isInstanceOf(ITestBean.class);
|
assertThat(pf.getProxy()).isInstanceOf(ITestBean.class);
|
||||||
assertThat(factory.adviceIncluded(di)).isTrue();
|
assertThat(pf.adviceIncluded(di)).isTrue();
|
||||||
assertThat(factory.adviceIncluded(diUnused)).isFalse();
|
assertThat(pf.adviceIncluded(diUnused)).isFalse();
|
||||||
assertThat(factory.countAdvicesOfType(NopInterceptor.class)).isEqualTo(1);
|
assertThat(pf.countAdvicesOfType(NopInterceptor.class)).isEqualTo(1);
|
||||||
assertThat(factory.countAdvicesOfType(MyInterceptor.class)).isEqualTo(0);
|
assertThat(pf.countAdvicesOfType(MyInterceptor.class)).isEqualTo(0);
|
||||||
|
|
||||||
factory.addAdvice(0, diUnused);
|
pf.addAdvice(0, diUnused);
|
||||||
assertThat(factory.adviceIncluded(diUnused)).isTrue();
|
assertThat(pf.adviceIncluded(diUnused)).isTrue();
|
||||||
assertThat(factory.countAdvicesOfType(NopInterceptor.class)).isEqualTo(2);
|
assertThat(pf.countAdvicesOfType(NopInterceptor.class)).isEqualTo(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void sealedInterfaceExclusion() {
|
void sealedInterfaceExclusion() {
|
||||||
// String implements ConstantDesc on JDK 12+, sealed as of JDK 17
|
// String implements ConstantDesc on JDK 12+, sealed as of JDK 17
|
||||||
ProxyFactory factory = new ProxyFactory("");
|
ProxyFactory pf = new ProxyFactory("");
|
||||||
NopInterceptor di = new NopInterceptor();
|
NopInterceptor di = new NopInterceptor();
|
||||||
factory.addAdvice(0, di);
|
pf.addAdvice(0, di);
|
||||||
Object proxy = factory.getProxy();
|
Object proxy = pf.getProxy();
|
||||||
assertThat(proxy).isInstanceOf(CharSequence.class);
|
assertThat(proxy).isInstanceOf(CharSequence.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -330,6 +327,19 @@ class ProxyFactoryTests {
|
|||||||
assertThat(AopProxyUtils.ultimateTargetClass(proxy2)).isEqualTo(TestBean.class);
|
assertThat(AopProxyUtils.ultimateTargetClass(proxy2)).isEqualTo(TestBean.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void proxyTargetClassWithIntroducedInterface() {
|
||||||
|
ProxyFactory pf = new ProxyFactory();
|
||||||
|
pf.setTargetClass(MyDate.class);
|
||||||
|
TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor(0L);
|
||||||
|
pf.addAdvisor(0, new DefaultIntroductionAdvisor(ti, TimeStamped.class));
|
||||||
|
Object proxy = pf.getProxy();
|
||||||
|
assertThat(AopUtils.isCglibProxy(proxy)).as("Proxy is a CGLIB proxy").isTrue();
|
||||||
|
assertThat(proxy).isInstanceOf(MyDate.class);
|
||||||
|
assertThat(proxy).isInstanceOf(TimeStamped.class);
|
||||||
|
assertThat(AopProxyUtils.ultimateTargetClass(proxy)).isEqualTo(MyDate.class);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void interfaceProxiesCanBeOrderedThroughAnnotations() {
|
void interfaceProxiesCanBeOrderedThroughAnnotations() {
|
||||||
Object proxy1 = new ProxyFactory(new A()).getProxy();
|
Object proxy1 = new ProxyFactory(new A()).getProxy();
|
||||||
|
|||||||
Reference in New Issue
Block a user