Browse Source

Polishing.

Replace consecutive if-statements with loop.

Original pull request: #4545
See #4542
pull/4547/head
Mark Paluch 2 years ago
parent
commit
cd16375fea
No known key found for this signature in database
GPG Key ID: 4406B84C1661DCD1
  1. 24
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/CrudMethodMetadataPostProcessor.java
  2. 2
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/DefaultCrudMethodMetadataUnitTests.java

24
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; package org.springframework.data.mongodb.repository.support;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.HashSet; import java.util.HashSet;
import java.util.Optional; import java.util.Optional;
@ -179,28 +180,25 @@ class CrudMethodMetadataPostProcessor implements RepositoryProxyPostProcessor, B
*/ */
DefaultCrudMethodMetadata(Class<?> repositoryInterface, Method method) { DefaultCrudMethodMetadata(Class<?> repositoryInterface, Method method) {
Assert.notNull(repositoryInterface, "Repository interface must not be null");
Assert.notNull(method, "Method must not be null"); Assert.notNull(method, "Method must not be null");
this.readPreference = findReadPreference(repositoryInterface, method); this.readPreference = findReadPreference(method, repositoryInterface);
} }
private Optional<ReadPreference> findReadPreference(Class<?> repositoryInterface, Method method) { private static Optional<ReadPreference> findReadPreference(AnnotatedElement... annotatedElements) {
org.springframework.data.mongodb.repository.ReadPreference preference = AnnotatedElementUtils for (AnnotatedElement element : annotatedElements) {
.findMergedAnnotation(method, org.springframework.data.mongodb.repository.ReadPreference.class);
if (preference == null) { org.springframework.data.mongodb.repository.ReadPreference preference = AnnotatedElementUtils
.findMergedAnnotation(element, org.springframework.data.mongodb.repository.ReadPreference.class);
preference = AnnotatedElementUtils.findMergedAnnotation(repositoryInterface, if (preference != null) {
org.springframework.data.mongodb.repository.ReadPreference.class); return Optional.of(com.mongodb.ReadPreference.valueOf(preference.value()));
} }
if (preference == null) {
return Optional.empty();
} }
return Optional.of(com.mongodb.ReadPreference.valueOf(preference.value())); return Optional.empty();
} }
@Override @Override

2
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; import org.springframework.util.ReflectionUtils;
/** /**
* Unit tests for {@link DefaultCrudMethodMetadata}.
*
* @author Christoph Strobl * @author Christoph Strobl
*/ */
class DefaultCrudMethodMetadataUnitTests { class DefaultCrudMethodMetadataUnitTests {

Loading…
Cancel
Save