From 1733d0111d1fb0b253b0085be9347d269548bac4 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 4 Nov 2015 20:03:05 -0800 Subject: [PATCH] Add shortcuts for elements with no annotations Improve the performance of the `getMergedAnnotationAttributes` and `isAnnotated` methods in `AnnotatedElementUtils` by returning immediately when the element had no annotations. Issue: SPR-13621 --- .../core/annotation/AnnotatedElementUtils.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/spring-core/src/main/java/org/springframework/core/annotation/AnnotatedElementUtils.java b/spring-core/src/main/java/org/springframework/core/annotation/AnnotatedElementUtils.java index 10c3a14006..a1944f75ec 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/AnnotatedElementUtils.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/AnnotatedElementUtils.java @@ -201,6 +201,9 @@ public class AnnotatedElementUtils { Assert.notNull(element, "AnnotatedElement must not be null"); Assert.hasLength(annotationName, "annotationName must not be null or empty"); + if (element.getAnnotations().length == 0) { + return false; + } return Boolean.TRUE.equals(searchWithGetSemantics(element, annotationName, new SimpleAnnotationProcessor() { @Override public Boolean process(AnnotatedElement annotatedElement, Annotation annotation, int metaDepth) { @@ -331,6 +334,9 @@ public class AnnotatedElementUtils { public static AnnotationAttributes getMergedAnnotationAttributes(AnnotatedElement element, String annotationName, boolean classValuesAsString, boolean nestedAnnotationsAsMap) { + if (element.getAnnotations().length == 0) { + return null; + } AnnotationAttributes attributes = searchWithGetSemantics(element, annotationName, new MergedAnnotationAttributesProcessor(annotationName, classValuesAsString, nestedAnnotationsAsMap)); AnnotationUtils.postProcessAnnotationAttributes(element, attributes, classValuesAsString, nestedAnnotationsAsMap);