Check for advisor-introduced interfaces specifically

See gh-31304
This commit is contained in:
Juergen Hoeller
2024-09-27 11:07:11 +02:00
parent d3dd01e227
commit 8680c43368
3 changed files with 53 additions and 27 deletions

View File

@@ -199,7 +199,7 @@ class ProxyFactoryTests {
Class<?>[] oldProxiedInterfaces = pf.getProxiedInterfaces();
long t = 555555L;
TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor(t);
pf.addAdvisor(0, new DefaultIntroductionAdvisor(ti, TimeStamped.class));
pf.addAdvisor(new DefaultIntroductionAdvisor(ti, TimeStamped.class));
Class<?>[] newProxiedInterfaces = pf.getProxiedInterfaces();
assertThat(newProxiedInterfaces).as("Advisor proxies one more interface after introduction").hasSize(oldProxiedInterfaces.length + 1);
@@ -328,11 +328,11 @@ class ProxyFactoryTests {
}
@Test
void proxyTargetClassWithIntroducedInterface() {
void proxyTargetClassInCaseOfIntroducedInterface() {
ProxyFactory pf = new ProxyFactory();
pf.setTargetClass(MyDate.class);
TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor(0L);
pf.addAdvisor(0, new DefaultIntroductionAdvisor(ti, TimeStamped.class));
pf.addAdvisor(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);
@@ -340,6 +340,20 @@ class ProxyFactoryTests {
assertThat(AopProxyUtils.ultimateTargetClass(proxy)).isEqualTo(MyDate.class);
}
@Test
void proxyInterfaceInCaseOfNonTargetInterface() {
ProxyFactory pf = new ProxyFactory();
pf.setTargetClass(MyDate.class);
pf.addInterface(TimeStamped.class);
pf.addAdvice((MethodInterceptor) invocation -> {
throw new UnsupportedOperationException();
});
Object proxy = pf.getProxy();
assertThat(AopUtils.isJdkDynamicProxy(proxy)).as("Proxy is a JDK proxy").isTrue();
assertThat(proxy).isInstanceOf(TimeStamped.class);
assertThat(AopProxyUtils.ultimateTargetClass(proxy)).isEqualTo(MyDate.class);
}
@Test
void interfaceProxiesCanBeOrderedThroughAnnotations() {
Object proxy1 = new ProxyFactory(new A()).getProxy();