-
Notifications
You must be signed in to change notification settings - Fork 38.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
2 changed files
with
115 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
spring-core/src/test/java/org/springframework/core/MethodIntrospectorTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* Copyright 2002-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.core; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.reflect.Method; | ||
import java.util.Map; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.core.MethodIntrospector.MetadataLookup; | ||
import org.springframework.core.annotation.MergedAnnotations; | ||
import org.springframework.util.ReflectionUtils; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.springframework.core.annotation.MergedAnnotations.SearchStrategy.TYPE_HIERARCHY; | ||
|
||
/** | ||
* Tests for {@link MethodIntrospector}. | ||
* | ||
* @author Sam Brannen | ||
* @since 5.3.34 | ||
*/ | ||
class MethodIntrospectorTests { | ||
|
||
@Test // gh-32586 | ||
void selectMethodsAndClearDeclaredMethodsCacheBetweenInvocations() { | ||
Class<?> targetType = ActualController.class; | ||
|
||
// Preconditions for this use case. | ||
assertThat(targetType).isPublic(); | ||
assertThat(targetType.getSuperclass()).isPackagePrivate(); | ||
|
||
MetadataLookup<String> metadataLookup = (MetadataLookup<String>) method -> { | ||
if (MergedAnnotations.from(method, TYPE_HIERARCHY).isPresent(Mapped.class)) { | ||
return method.getName(); | ||
} | ||
return null; | ||
}; | ||
|
||
// Start with a clean slate. | ||
ReflectionUtils.clearCache(); | ||
|
||
// Round #1 | ||
Map<Method, String> methods = MethodIntrospector.selectMethods(targetType, metadataLookup); | ||
assertThat(methods.values()).containsExactlyInAnyOrder("update", "delete"); | ||
|
||
// Simulate ConfigurableApplicationContext#refresh() which clears the | ||
// ReflectionUtils#declaredMethodsCache but NOT the BridgeMethodResolver#cache. | ||
// As a consequence, ReflectionUtils.getDeclaredMethods(...) will return a | ||
// new set of methods that are logically equivalent to but not identical | ||
// to (in terms of object identity) any bridged methods cached in the | ||
// BridgeMethodResolver cache. | ||
ReflectionUtils.clearCache(); | ||
|
||
// Round #2 | ||
methods = MethodIntrospector.selectMethods(targetType, metadataLookup); | ||
assertThat(methods.values()).containsExactlyInAnyOrder("update", "delete"); | ||
} | ||
|
||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@interface Mapped { | ||
} | ||
|
||
interface Controller { | ||
|
||
void unmappedMethod(); | ||
|
||
@Mapped | ||
void update(); | ||
|
||
@Mapped | ||
void delete(); | ||
} | ||
|
||
// Must NOT be public. | ||
abstract static class AbstractController implements Controller { | ||
|
||
@Override | ||
public void unmappedMethod() { | ||
} | ||
|
||
@Override | ||
public void delete() { | ||
} | ||
} | ||
|
||
// MUST be public. | ||
public static class ActualController extends AbstractController { | ||
|
||
@Override | ||
public void update() { | ||
} | ||
} | ||
|
||
} |