From cd16375fea1d8ff85d223da997b9e33c53038562 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Fri, 3 Nov 2023 09:57:20 +0100 Subject: [PATCH] Polishing. Replace consecutive if-statements with loop. Original pull request: #4545 See #4542 --- .../CrudMethodMetadataPostProcessor.java | 24 +++++++++---------- .../DefaultCrudMethodMetadataUnitTests.java | 2 ++ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/CrudMethodMetadataPostProcessor.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/CrudMethodMetadataPostProcessor.java index 183f427cd..c5c548e80 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/CrudMethodMetadataPostProcessor.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/CrudMethodMetadataPostProcessor.java @@ -15,6 +15,7 @@ */ package org.springframework.data.mongodb.repository.support; +import java.lang.reflect.AnnotatedElement; import java.lang.reflect.Method; import java.util.HashSet; import java.util.Optional; @@ -179,28 +180,25 @@ class CrudMethodMetadataPostProcessor implements RepositoryProxyPostProcessor, B */ DefaultCrudMethodMetadata(Class repositoryInterface, Method method) { + Assert.notNull(repositoryInterface, "Repository interface must not be null"); Assert.notNull(method, "Method must not be null"); - this.readPreference = findReadPreference(repositoryInterface, method); + this.readPreference = findReadPreference(method, repositoryInterface); } - private Optional findReadPreference(Class repositoryInterface, Method method) { + private static Optional findReadPreference(AnnotatedElement... annotatedElements) { - org.springframework.data.mongodb.repository.ReadPreference preference = AnnotatedElementUtils - .findMergedAnnotation(method, org.springframework.data.mongodb.repository.ReadPreference.class); + for (AnnotatedElement element : annotatedElements) { - if (preference == null) { + org.springframework.data.mongodb.repository.ReadPreference preference = AnnotatedElementUtils + .findMergedAnnotation(element, org.springframework.data.mongodb.repository.ReadPreference.class); - preference = AnnotatedElementUtils.findMergedAnnotation(repositoryInterface, - org.springframework.data.mongodb.repository.ReadPreference.class); - } - - if (preference == null) { - return Optional.empty(); + if (preference != null) { + return Optional.of(com.mongodb.ReadPreference.valueOf(preference.value())); + } } - return Optional.of(com.mongodb.ReadPreference.valueOf(preference.value())); - + return Optional.empty(); } @Override diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/DefaultCrudMethodMetadataUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/DefaultCrudMethodMetadataUnitTests.java index 68e23ca9e..00a48e743 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/DefaultCrudMethodMetadataUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/DefaultCrudMethodMetadataUnitTests.java @@ -27,6 +27,8 @@ import org.springframework.data.repository.CrudRepository; import org.springframework.util.ReflectionUtils; /** + * Unit tests for {@link DefaultCrudMethodMetadata}. + * * @author Christoph Strobl */ class DefaultCrudMethodMetadataUnitTests {