Skip to content

Commit 5656eac

Browse files
committed
Avoid bridge resolution for method from unrelated class hierarchy
Closes gh-32087
1 parent 3b2f6e7 commit 5656eac

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

Diff for: spring-core/src/main/java/org/springframework/core/BridgeMethodResolver.java

+4
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ public static Method findBridgedMethod(Method bridgeMethod) {
8686
* @see org.springframework.util.ClassUtils#getMostSpecificMethod
8787
*/
8888
public static Method getMostSpecificMethod(Method bridgeMethod, @Nullable Class<?> targetClass) {
89+
if (targetClass != null && !bridgeMethod.getDeclaringClass().isAssignableFrom(targetClass)) {
90+
return bridgeMethod;
91+
}
92+
8993
Method specificMethod = ClassUtils.getMostSpecificMethod(bridgeMethod, targetClass);
9094
return resolveBridgeMethod(specificMethod,
9195
(targetClass != null ? targetClass : specificMethod.getDeclaringClass()));

Diff for: spring-core/src/test/java/org/springframework/core/BridgeMethodResolverTests.java

+15
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
* @author Rob Harrop
3939
* @author Juergen Hoeller
4040
* @author Chris Beams
41+
* @author Yanming Zhou
4142
*/
4243
@SuppressWarnings("rawtypes")
4344
class BridgeMethodResolverTests {
@@ -97,6 +98,13 @@ void findBridgedMethodFromOriginalMethodInHierarchy() throws Exception {
9798
assertThat(bridgedMethod.getParameterTypes()[0]).isEqualTo(Date.class);
9899
}
99100

101+
@Test
102+
void findBridgedMethodFromOriginalMethodNotInHierarchy() throws Exception {
103+
Method originalMethod = Adder.class.getMethod("add", Object.class);
104+
Method mostSpecificMethod = BridgeMethodResolver.getMostSpecificMethod(originalMethod, FakeAdder.class);
105+
assertThat(mostSpecificMethod).isSameAs(originalMethod);
106+
}
107+
100108
@Test
101109
void isBridgeMethodFor() throws Exception {
102110
Method bridged = MyBar.class.getDeclaredMethod("someMethod", String.class, Object.class);
@@ -406,6 +414,13 @@ public void add(Date date) {
406414
}
407415

408416

417+
public static class FakeAdder {
418+
419+
public void add(Date date) {
420+
}
421+
}
422+
423+
409424
public static class Enclosing<T> {
410425

411426
public class Enclosed<S> {

0 commit comments

Comments
 (0)