|
| 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