diff --git a/src/main/java/org/springframework/data/mapping/model/AnnotationBasedPersistentProperty.java b/src/main/java/org/springframework/data/mapping/model/AnnotationBasedPersistentProperty.java index ff04e3976..d37466141 100644 --- a/src/main/java/org/springframework/data/mapping/model/AnnotationBasedPersistentProperty.java +++ b/src/main/java/org/springframework/data/mapping/model/AnnotationBasedPersistentProperty.java @@ -39,6 +39,7 @@ import org.springframework.data.mapping.PersistentEntity; import org.springframework.data.mapping.PersistentProperty; import org.springframework.data.util.Lazy; import org.springframework.data.util.Optionals; +import org.springframework.data.util.StreamUtils; import org.springframework.lang.Nullable; import org.springframework.util.Assert; @@ -229,22 +230,13 @@ public abstract class AnnotationBasedPersistentProperty

Optional doFindAnnotation(Class annotationType) { - if (annotationCache != null && annotationCache.containsKey(annotationType)) { - return (Optional) annotationCache.get(annotationType); - } - - return cacheAndReturn(annotationType, getAccessors()// - .flatMap(it -> { - - A mergedAnnotation = AnnotatedElementUtils.findMergedAnnotation(it, annotationType); + return (Optional) annotationCache.computeIfAbsent(annotationType, type -> { - if (mergedAnnotation == null) { - return Stream.empty(); - } - - return Stream.of(mergedAnnotation); - })// - .findFirst()); + return getAccessors() // + .map(it -> AnnotatedElementUtils.findMergedAnnotation(it, type)) // + .flatMap(StreamUtils::fromNullable) // + .findFirst(); + }); } /* @@ -260,18 +252,6 @@ public abstract class AnnotationBasedPersistentProperty

Optional cacheAndReturn(Class type, Optional annotation) { - - annotationCache.put(type, annotation); - return annotation; - } - /** * Returns whether the property carries the an annotation of the given type. * diff --git a/src/main/java/org/springframework/data/util/StreamUtils.java b/src/main/java/org/springframework/data/util/StreamUtils.java index b1cfc8944..ad59b5816 100644 --- a/src/main/java/org/springframework/data/util/StreamUtils.java +++ b/src/main/java/org/springframework/data/util/StreamUtils.java @@ -28,6 +28,7 @@ import java.util.stream.Collector; import java.util.stream.Stream; import java.util.stream.StreamSupport; +import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.MultiValueMap; @@ -95,4 +96,15 @@ public interface StreamUtils { Function valueFunction) { return MultiValueMapCollector.of(keyFunction, valueFunction); } + + /** + * Creates a new {@link Stream} for the given value returning an empty {@link Stream} if the value is {@literal null}. + * + * @param source can be {@literal null}. + * @return a new {@link Stream} for the given value returning an empty {@link Stream} if the value is {@literal null}. + * @since 2.0.6 + */ + public static Stream fromNullable(@Nullable T source) { + return source == null ? Stream.empty() : Stream.of(source); + } }