From 6dfa54b8ea6058835ebf0e618e6ffcdd8570704d Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 11 Feb 2026 16:41:55 +0100 Subject: [PATCH] Cache method annotations in MethodParameter Closes gh-36307 --- .../springframework/core/MethodParameter.java | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/MethodParameter.java b/spring-core/src/main/java/org/springframework/core/MethodParameter.java index 43ab00b0e61..b9e014efd50 100644 --- a/spring-core/src/main/java/org/springframework/core/MethodParameter.java +++ b/spring-core/src/main/java/org/springframework/core/MethodParameter.java @@ -87,6 +87,8 @@ public class MethodParameter { private volatile @Nullable Type genericParameterType; + private volatile Annotation @Nullable [] methodAnnotations; + private volatile Annotation @Nullable [] parameterAnnotations; private volatile @Nullable ParameterNameDiscoverer parameterNameDiscoverer = @@ -584,7 +586,12 @@ public class MethodParameter { * Return the annotations associated with the target method/constructor itself. */ public Annotation[] getMethodAnnotations() { - return adaptAnnotationArray(getAnnotatedElement().getAnnotations()); + Annotation[] methodAnns = this.methodAnnotations; + if (methodAnns == null) { + methodAnns = adaptAnnotationArray(getAnnotatedElement().getAnnotations()); + this.methodAnnotations = methodAnns; + } + return methodAnns; } /** @@ -592,9 +599,15 @@ public class MethodParameter { * @param annotationType the annotation type to look for * @return the annotation object, or {@code null} if not found */ + @SuppressWarnings("unchecked") public @Nullable A getMethodAnnotation(Class annotationType) { - A annotation = getAnnotatedElement().getAnnotation(annotationType); - return (annotation != null ? adaptAnnotation(annotation) : null); + Annotation[] anns = getMethodAnnotations(); + for (Annotation ann : anns) { + if (annotationType.isInstance(ann)) { + return (A) ann; + } + } + return null; } /** @@ -604,7 +617,7 @@ public class MethodParameter { * @see #getMethodAnnotation(Class) */ public boolean hasMethodAnnotation(Class annotationType) { - return getAnnotatedElement().isAnnotationPresent(annotationType); + return (getMethodAnnotation(annotationType) != null); } /**