|
19 | 19 | import java.lang.annotation.Annotation;
|
20 | 20 | import java.lang.reflect.AnnotatedElement;
|
21 | 21 | import java.lang.reflect.Method;
|
| 22 | +import java.lang.reflect.Modifier; |
22 | 23 | import java.lang.reflect.Parameter;
|
23 | 24 | import java.util.ArrayList;
|
| 25 | +import java.util.Arrays; |
24 | 26 | import java.util.Collections;
|
25 | 27 | import java.util.HashSet;
|
26 | 28 | import java.util.List;
|
|
29 | 31 | import java.util.concurrent.ConcurrentHashMap;
|
30 | 32 |
|
31 | 33 | import org.springframework.core.MethodClassKey;
|
| 34 | +import org.springframework.core.ResolvableType; |
32 | 35 | import org.springframework.core.annotation.AnnotationConfigurationException;
|
33 | 36 | import org.springframework.core.annotation.MergedAnnotation;
|
34 | 37 | import org.springframework.core.annotation.MergedAnnotations;
|
@@ -169,18 +172,15 @@ private List<MergedAnnotation<A>> findClosestMethodAnnotations(Method method, Cl
|
169 | 172 | return Collections.emptyList();
|
170 | 173 | }
|
171 | 174 | classesToSkip.add(targetClass);
|
172 |
| - try { |
173 |
| - Method methodToUse = targetClass.getDeclaredMethod(method.getName(), method.getParameterTypes()); |
| 175 | + Method methodToUse = findMethod(method, targetClass); |
| 176 | + if (methodToUse != null) { |
174 | 177 | List<MergedAnnotation<A>> annotations = findDirectAnnotations(methodToUse);
|
175 | 178 | if (!annotations.isEmpty()) {
|
176 | 179 | return annotations;
|
177 | 180 | }
|
178 | 181 | }
|
179 |
| - catch (NoSuchMethodException ex) { |
180 |
| - // move on |
181 |
| - } |
182 |
| - List<MergedAnnotation<A>> annotations = new ArrayList<>(); |
183 |
| - annotations.addAll(findClosestMethodAnnotations(method, targetClass.getSuperclass(), classesToSkip)); |
| 182 | + List<MergedAnnotation<A>> annotations = new ArrayList<>( |
| 183 | + findClosestMethodAnnotations(method, targetClass.getSuperclass(), classesToSkip)); |
184 | 184 | for (Class<?> inter : targetClass.getInterfaces()) {
|
185 | 185 | annotations.addAll(findClosestMethodAnnotations(method, inter, classesToSkip));
|
186 | 186 | }
|
@@ -212,4 +212,52 @@ private List<MergedAnnotation<A>> findDirectAnnotations(AnnotatedElement element
|
212 | 212 | .toList();
|
213 | 213 | }
|
214 | 214 |
|
| 215 | + private static Method findMethod(Method method, Class<?> targetClass) { |
| 216 | + for (Method candidate : targetClass.getDeclaredMethods()) { |
| 217 | + if (candidate == method) { |
| 218 | + return candidate; |
| 219 | + } |
| 220 | + if (isOverride(method, candidate)) { |
| 221 | + return candidate; |
| 222 | + } |
| 223 | + } |
| 224 | + return null; |
| 225 | + } |
| 226 | + |
| 227 | + private static boolean isOverride(Method rootMethod, Method candidateMethod) { |
| 228 | + return (!Modifier.isPrivate(candidateMethod.getModifiers()) |
| 229 | + && candidateMethod.getName().equals(rootMethod.getName()) |
| 230 | + && hasSameParameterTypes(rootMethod, candidateMethod)); |
| 231 | + } |
| 232 | + |
| 233 | + private static boolean hasSameParameterTypes(Method rootMethod, Method candidateMethod) { |
| 234 | + if (candidateMethod.getParameterCount() != rootMethod.getParameterCount()) { |
| 235 | + return false; |
| 236 | + } |
| 237 | + Class<?>[] rootParameterTypes = rootMethod.getParameterTypes(); |
| 238 | + Class<?>[] candidateParameterTypes = candidateMethod.getParameterTypes(); |
| 239 | + if (Arrays.equals(candidateParameterTypes, rootParameterTypes)) { |
| 240 | + return true; |
| 241 | + } |
| 242 | + return hasSameGenericTypeParameters(rootMethod, candidateMethod, rootParameterTypes); |
| 243 | + } |
| 244 | + |
| 245 | + private static boolean hasSameGenericTypeParameters(Method rootMethod, Method candidateMethod, |
| 246 | + Class<?>[] rootParameterTypes) { |
| 247 | + |
| 248 | + Class<?> sourceDeclaringClass = rootMethod.getDeclaringClass(); |
| 249 | + Class<?> candidateDeclaringClass = candidateMethod.getDeclaringClass(); |
| 250 | + if (!candidateDeclaringClass.isAssignableFrom(sourceDeclaringClass)) { |
| 251 | + return false; |
| 252 | + } |
| 253 | + for (int i = 0; i < rootParameterTypes.length; i++) { |
| 254 | + Class<?> resolvedParameterType = ResolvableType.forMethodParameter(candidateMethod, i, sourceDeclaringClass) |
| 255 | + .resolve(); |
| 256 | + if (rootParameterTypes[i] != resolvedParameterType) { |
| 257 | + return false; |
| 258 | + } |
| 259 | + } |
| 260 | + return true; |
| 261 | + } |
| 262 | + |
215 | 263 | }
|
0 commit comments