Skip to content

Commit 6dfa54b

Browse files
committed
Cache method annotations in MethodParameter
Closes gh-36307
1 parent 2d3a2c5 commit 6dfa54b

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

spring-core/src/main/java/org/springframework/core/MethodParameter.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ public class MethodParameter {
8787

8888
private volatile @Nullable Type genericParameterType;
8989

90+
private volatile Annotation @Nullable [] methodAnnotations;
91+
9092
private volatile Annotation @Nullable [] parameterAnnotations;
9193

9294
private volatile @Nullable ParameterNameDiscoverer parameterNameDiscoverer =
@@ -584,17 +586,28 @@ public Type getNestedGenericParameterType() {
584586
* Return the annotations associated with the target method/constructor itself.
585587
*/
586588
public Annotation[] getMethodAnnotations() {
587-
return adaptAnnotationArray(getAnnotatedElement().getAnnotations());
589+
Annotation[] methodAnns = this.methodAnnotations;
590+
if (methodAnns == null) {
591+
methodAnns = adaptAnnotationArray(getAnnotatedElement().getAnnotations());
592+
this.methodAnnotations = methodAnns;
593+
}
594+
return methodAnns;
588595
}
589596

590597
/**
591598
* Return the method/constructor annotation of the given type, if available.
592599
* @param annotationType the annotation type to look for
593600
* @return the annotation object, or {@code null} if not found
594601
*/
602+
@SuppressWarnings("unchecked")
595603
public <A extends Annotation> @Nullable A getMethodAnnotation(Class<A> annotationType) {
596-
A annotation = getAnnotatedElement().getAnnotation(annotationType);
597-
return (annotation != null ? adaptAnnotation(annotation) : null);
604+
Annotation[] anns = getMethodAnnotations();
605+
for (Annotation ann : anns) {
606+
if (annotationType.isInstance(ann)) {
607+
return (A) ann;
608+
}
609+
}
610+
return null;
598611
}
599612

600613
/**
@@ -604,7 +617,7 @@ public Annotation[] getMethodAnnotations() {
604617
* @see #getMethodAnnotation(Class)
605618
*/
606619
public <A extends Annotation> boolean hasMethodAnnotation(Class<A> annotationType) {
607-
return getAnnotatedElement().isAnnotationPresent(annotationType);
620+
return (getMethodAnnotation(annotationType) != null);
608621
}
609622

610623
/**

0 commit comments

Comments
 (0)