Skip to content

Commit 24759a7

Browse files
committed
Restore ability to return original method for proxy-derived method
Closes gh-32365
1 parent 7493ce8 commit 24759a7

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

spring-aop/src/test/java/org/springframework/aop/support/AopUtilsTests.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,19 @@
1717
package org.springframework.aop.support;
1818

1919
import java.lang.reflect.Method;
20+
import java.util.List;
2021

2122
import org.junit.jupiter.api.Test;
2223

2324
import org.springframework.aop.ClassFilter;
2425
import org.springframework.aop.MethodMatcher;
2526
import org.springframework.aop.Pointcut;
27+
import org.springframework.aop.framework.ProxyFactory;
2628
import org.springframework.aop.interceptor.ExposeInvocationInterceptor;
2729
import org.springframework.aop.target.EmptyTargetSource;
2830
import org.springframework.aop.testfixture.interceptor.NopInterceptor;
2931
import org.springframework.beans.testfixture.beans.TestBean;
32+
import org.springframework.core.ResolvableType;
3033
import org.springframework.core.testfixture.io.SerializationTestUtils;
3134
import org.springframework.lang.Nullable;
3235
import org.springframework.util.ReflectionUtils;
@@ -37,6 +40,7 @@
3740
* @author Rod Johnson
3841
* @author Chris Beams
3942
* @author Sebastien Deleuze
43+
* @author Juergen Hoeller
4044
*/
4145
class AopUtilsTests {
4246

@@ -99,4 +103,36 @@ void testInvokeJoinpointUsingReflection() throws Throwable {
99103
assertThat(result).isEqualTo(name);
100104
}
101105

106+
@Test // gh-32365
107+
void mostSpecificMethodBetweenJdkProxyAndTarget() throws Exception {
108+
Class<?> proxyClass = new ProxyFactory(new WithInterface()).getProxyClass(getClass().getClassLoader());
109+
Method specificMethod = AopUtils.getMostSpecificMethod(proxyClass.getMethod("handle", List.class), WithInterface.class);
110+
assertThat(ResolvableType.forMethodParameter(specificMethod, 0).getGeneric().toClass()).isEqualTo(String.class);
111+
}
112+
113+
@Test // gh-32365
114+
void mostSpecificMethodBetweenCglibProxyAndTarget() throws Exception {
115+
Class<?> proxyClass = new ProxyFactory(new WithoutInterface()).getProxyClass(getClass().getClassLoader());
116+
Method specificMethod = AopUtils.getMostSpecificMethod(proxyClass.getMethod("handle", List.class), WithoutInterface.class);
117+
assertThat(ResolvableType.forMethodParameter(specificMethod, 0).getGeneric().toClass()).isEqualTo(String.class);
118+
}
119+
120+
121+
interface ProxyInterface {
122+
123+
void handle(List<String> list);
124+
}
125+
126+
static class WithInterface implements ProxyInterface {
127+
128+
public void handle(List<String> list) {
129+
}
130+
}
131+
132+
static class WithoutInterface {
133+
134+
public void handle(List<String> list) {
135+
}
136+
}
137+
102138
}

spring-core/src/main/java/org/springframework/core/BridgeMethodResolver.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.core;
1818

1919
import java.lang.reflect.Method;
20+
import java.lang.reflect.Proxy;
2021
import java.lang.reflect.Type;
2122
import java.util.ArrayList;
2223
import java.util.Arrays;
@@ -86,7 +87,10 @@ public static Method findBridgedMethod(Method bridgeMethod) {
8687
* @see org.springframework.util.ClassUtils#getMostSpecificMethod
8788
*/
8889
public static Method getMostSpecificMethod(Method bridgeMethod, @Nullable Class<?> targetClass) {
89-
if (targetClass != null && !bridgeMethod.getDeclaringClass().isAssignableFrom(targetClass)) {
90+
if (targetClass != null &&
91+
!ClassUtils.getUserClass(bridgeMethod.getDeclaringClass()).isAssignableFrom(targetClass) &&
92+
!Proxy.isProxyClass(bridgeMethod.getDeclaringClass())) {
93+
// From a different class hierarchy, and not a JDK or CGLIB proxy either -> return as-is.
9094
return bridgeMethod;
9195
}
9296

0 commit comments

Comments
 (0)