Cache method annotations in AnnotatedMethod as well

See gh-36307
This commit is contained in:
Juergen Hoeller
2026-02-11 17:23:05 +01:00
parent 6dfa54b8ea
commit 1ef8734881
@@ -22,6 +22,8 @@ import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.jspecify.annotations.Nullable;
@@ -48,12 +50,16 @@ import org.springframework.util.StringUtils;
*/
public class AnnotatedMethod {
private static final Object NO_ANNOTATION = new Object();
private final Method method;
private final Method bridgedMethod;
private final MethodParameter[] parameters;
private final Map<Class<? extends Annotation>, Object> annotations = new ConcurrentHashMap<>(4);
private volatile @Nullable List<Annotation[][]> inheritedParameterAnnotations;
@@ -149,8 +155,13 @@ public class AnnotatedMethod {
* @return the annotation, or {@code null} if none found
* @see AnnotatedElementUtils#findMergedAnnotation
*/
@SuppressWarnings("unchecked")
public <A extends Annotation> @Nullable A getMethodAnnotation(Class<A> annotationType) {
return AnnotatedElementUtils.findMergedAnnotation(this.method, annotationType);
Object result = this.annotations.computeIfAbsent(annotationType, key -> {
Object value = AnnotatedElementUtils.findMergedAnnotation(this.method, annotationType);
return (value == null ? NO_ANNOTATION : value);
});
return (result == NO_ANNOTATION ? null : (A) result);
}
/**
@@ -160,7 +171,7 @@ public class AnnotatedMethod {
* @see AnnotatedElementUtils#hasAnnotation
*/
public <A extends Annotation> boolean hasMethodAnnotation(Class<A> annotationType) {
return AnnotatedElementUtils.hasAnnotation(this.method, annotationType);
return (getMethodAnnotation(annotationType) != null);
}
private List<Annotation[][]> getInheritedParameterAnnotations() {