Skip to content

Commit 7d178c6

Browse files
committed
Detect bridge methods across ApplicationContexts in MethodIntrospector
Prior to this commit, MethodIntrospector failed to properly detect bridge methods for subsequent invocations of selectMethods() with the same targetType and MetadataLookup, if such subsequent invocations occurred after the ApplicationContext had been refreshed. The reason this occurs is due to the following. - Class#getDeclaredMethods() always returns "child copies" of the underlying Method instances -- which means that `equals()` should be used instead of `==` whenever the compared Method instances can come from different sources (such as the static caches mentioned below). - BridgeMethodResolver caches resolved bridge methods in a static cache -- which is never cleared. - ReflectionUtils caches declared methods in a static cache -- which gets cleared when an ApplicationContext is refreshed. Consequently, if you attempt to load an ApplicationContext twice in the same ClassLoader, the second attempt uses the existing, populated cache for bridged methods but a cleared, empty cache for declared methods. This results in new invocations of Class#getDeclaredMethods(), and identity checks with `==` then fail to detect equivalent bridge methods. This commit addresses this by additionally comparing bridge methods using `equals()` in MethodIntrospector.selectMethods(). Note that the `==` checks remain in place as an optimization for when `equals()` is unnecessary. Closes gh-32586 (cherry picked from commit e702733)
1 parent ba776d7 commit 7d178c6

File tree

2 files changed

+115
-1
lines changed

2 files changed

+115
-1
lines changed

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,6 +36,7 @@
3636
*
3737
* @author Juergen Hoeller
3838
* @author Rossen Stoyanchev
39+
* @author Sam Brannen
3940
* @since 4.2.3
4041
*/
4142
public final class MethodIntrospector {
@@ -75,6 +76,7 @@ public static <T> Map<Method, T> selectMethods(Class<?> targetType, final Metada
7576
if (result != null) {
7677
Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);
7778
if (bridgedMethod == specificMethod || bridgedMethod == method ||
79+
bridgedMethod.equals(specificMethod) || bridgedMethod.equals(method) ||
7880
metadataLookup.inspect(bridgedMethod) == null) {
7981
methodMap.put(specificMethod, result);
8082
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.core;
18+
19+
import java.lang.annotation.Retention;
20+
import java.lang.annotation.RetentionPolicy;
21+
import java.lang.reflect.Method;
22+
import java.util.Map;
23+
24+
import org.junit.jupiter.api.Test;
25+
26+
import org.springframework.core.MethodIntrospector.MetadataLookup;
27+
import org.springframework.core.annotation.MergedAnnotations;
28+
import org.springframework.util.ReflectionUtils;
29+
30+
import static org.assertj.core.api.Assertions.assertThat;
31+
import static org.springframework.core.annotation.MergedAnnotations.SearchStrategy.TYPE_HIERARCHY;
32+
33+
/**
34+
* Tests for {@link MethodIntrospector}.
35+
*
36+
* @author Sam Brannen
37+
* @since 5.3.34
38+
*/
39+
class MethodIntrospectorTests {
40+
41+
@Test // gh-32586
42+
void selectMethodsAndClearDeclaredMethodsCacheBetweenInvocations() {
43+
Class<?> targetType = ActualController.class;
44+
45+
// Preconditions for this use case.
46+
assertThat(targetType).isPublic();
47+
assertThat(targetType.getSuperclass()).isPackagePrivate();
48+
49+
MetadataLookup<String> metadataLookup = (MetadataLookup<String>) method -> {
50+
if (MergedAnnotations.from(method, TYPE_HIERARCHY).isPresent(Mapped.class)) {
51+
return method.getName();
52+
}
53+
return null;
54+
};
55+
56+
// Start with a clean slate.
57+
ReflectionUtils.clearCache();
58+
59+
// Round #1
60+
Map<Method, String> methods = MethodIntrospector.selectMethods(targetType, metadataLookup);
61+
assertThat(methods.values()).containsExactlyInAnyOrder("update", "delete");
62+
63+
// Simulate ConfigurableApplicationContext#refresh() which clears the
64+
// ReflectionUtils#declaredMethodsCache but NOT the BridgeMethodResolver#cache.
65+
// As a consequence, ReflectionUtils.getDeclaredMethods(...) will return a
66+
// new set of methods that are logically equivalent to but not identical
67+
// to (in terms of object identity) any bridged methods cached in the
68+
// BridgeMethodResolver cache.
69+
ReflectionUtils.clearCache();
70+
71+
// Round #2
72+
methods = MethodIntrospector.selectMethods(targetType, metadataLookup);
73+
assertThat(methods.values()).containsExactlyInAnyOrder("update", "delete");
74+
}
75+
76+
77+
@Retention(RetentionPolicy.RUNTIME)
78+
@interface Mapped {
79+
}
80+
81+
interface Controller {
82+
83+
void unmappedMethod();
84+
85+
@Mapped
86+
void update();
87+
88+
@Mapped
89+
void delete();
90+
}
91+
92+
// Must NOT be public.
93+
abstract static class AbstractController implements Controller {
94+
95+
@Override
96+
public void unmappedMethod() {
97+
}
98+
99+
@Override
100+
public void delete() {
101+
}
102+
}
103+
104+
// MUST be public.
105+
public static class ActualController extends AbstractController {
106+
107+
@Override
108+
public void update() {
109+
}
110+
}
111+
112+
}

0 commit comments

Comments
 (0)