From 5afb8ada859a8c39f80ee4dc239752ea2bcc3d00 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Thu, 12 Oct 2023 10:35:43 +0200 Subject: [PATCH] Reduce public API surface. Limit exposure of API methods to package visibility. Also remove fields and methods not directly required to support current feature scope. Finally add missing since tags and provide additional tests. See: #2971 --- .../support/CrudMethodMetadata.java | 8 +------ .../CrudMethodMetadataPostProcessor.java | 23 ++++++++----------- .../support/SimpleMongoRepository.java | 3 ++- .../SimpleReactiveMongoRepository.java | 3 ++- .../MongoRepositoryFactoryUnitTests.java | 19 ++++++++++++++- ...activeMongoRepositoryFactoryUnitTests.java | 22 +++++++++++++++++- ...impleReactiveMongoRepositoryUnitTests.java | 15 ------------ 7 files changed, 54 insertions(+), 39 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/CrudMethodMetadata.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/CrudMethodMetadata.java index 44c6c97ce..ee1d1fcd4 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/CrudMethodMetadata.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/CrudMethodMetadata.java @@ -15,7 +15,6 @@ */ package org.springframework.data.mongodb.repository.support; -import java.lang.reflect.Method; import java.util.Optional; import com.mongodb.ReadPreference; @@ -25,6 +24,7 @@ import com.mongodb.ReadPreference; * execution. * * @author Mark Paluch + * @author Christoph Strobl * @since 4.2 */ public interface CrudMethodMetadata { @@ -36,10 +36,4 @@ public interface CrudMethodMetadata { */ Optional getReadPreference(); - /** - * Returns the {@link Method} that this metadata applies to. - * - * @return the {@link Method} that this metadata applies to. - */ - Method getMethod(); } 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 1c4cb75bc..22992b733 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 @@ -45,6 +45,8 @@ import com.mongodb.ReadPreference; * information or query hints on them. * * @author Mark Paluch + * @author Christoph Strobl + * @since 4.2 */ class CrudMethodMetadataPostProcessor implements RepositoryProxyPostProcessor, BeanClassLoaderAware { @@ -103,13 +105,15 @@ class CrudMethodMetadataPostProcessor implements RepositoryProxyPostProcessor, B */ static MethodInvocation currentInvocation() throws IllegalStateException { - MethodInvocation mi = currentInvocation.get(); + MethodInvocation invocation = currentInvocation.get(); - if (mi == null) - throw new IllegalStateException( - "No MethodInvocation found: Check that an AOP invocation is in progress, and that the " - + "CrudMethodMetadataPopulatingMethodInterceptor is upfront in the interceptor chain."); - return mi; + if (invocation != null) { + return invocation; + } + + throw new IllegalStateException( + "No MethodInvocation found: Check that an AOP invocation is in progress, and that the " + + "CrudMethodMetadataPopulatingMethodInterceptor is upfront in the interceptor chain."); } @Override @@ -163,7 +167,6 @@ class CrudMethodMetadataPostProcessor implements RepositoryProxyPostProcessor, B static class DefaultCrudMethodMetadata implements CrudMethodMetadata { private final Optional readPreference; - private final Method method; /** * Creates a new {@link DefaultCrudMethodMetadata} for the given {@link Method}. @@ -175,7 +178,6 @@ class CrudMethodMetadataPostProcessor implements RepositoryProxyPostProcessor, B Assert.notNull(method, "Method must not be null"); this.readPreference = findReadPreference(method); - this.method = method; } private Optional findReadPreference(Method method) { @@ -201,11 +203,6 @@ class CrudMethodMetadataPostProcessor implements RepositoryProxyPostProcessor, B public Optional getReadPreference() { return readPreference; } - - @Override - public Method getMethod() { - return method; - } } private static class ThreadBoundTargetSource implements TargetSource { diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepository.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepository.java index 5671801d7..dd7a38fd1 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepository.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepository.java @@ -357,8 +357,9 @@ public class SimpleMongoRepository implements MongoRepository { * applied to queries. * * @param crudMethodMetadata + * @since 4.2 */ - public void setRepositoryMethodMetadata(CrudMethodMetadata crudMethodMetadata) { + void setRepositoryMethodMetadata(CrudMethodMetadata crudMethodMetadata) { this.crudMethodMetadata = crudMethodMetadata; } diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepository.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepository.java index e5361a34d..1c84e5fde 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepository.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepository.java @@ -434,8 +434,9 @@ public class SimpleReactiveMongoRepository implement * applied to queries. * * @param crudMethodMetadata + * @since 4.2 */ - public void setRepositoryMethodMetadata(CrudMethodMetadata crudMethodMetadata) { + void setRepositoryMethodMetadata(CrudMethodMetadata crudMethodMetadata) { this.crudMethodMetadata = crudMethodMetadata; } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/MongoRepositoryFactoryUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/MongoRepositoryFactoryUnitTests.java index 8929f5c4f..09ab6090b 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/MongoRepositoryFactoryUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/MongoRepositoryFactoryUnitTests.java @@ -20,6 +20,7 @@ import static org.mockito.Mockito.*; import java.io.Serializable; import java.util.Optional; +import java.util.Set; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -41,6 +42,7 @@ import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.repository.Person; import org.springframework.data.mongodb.repository.ReadPreference; import org.springframework.data.mongodb.repository.query.MongoEntityInformation; +import org.springframework.data.repository.ListCrudRepository; import org.springframework.data.repository.Repository; /** @@ -48,6 +50,7 @@ import org.springframework.data.repository.Repository; * * @author Oliver Gierke * @author Mark Paluch + * @author Christoph Strobl */ @ExtendWith(MockitoExtension.class) @MockitoSettings(strictness = Strictness.LENIENT) @@ -92,7 +95,21 @@ public class MongoRepositoryFactoryUnitTests { assertThat(value.getReadPreference()).isEqualTo(com.mongodb.ReadPreference.secondary()); } - interface MyPersonRepository extends Repository { + @Test // GH-2971 + void ignoresCrudMethodMetadataOnNonAnnotatedMethods() { + + MongoRepositoryFactory factory = new MongoRepositoryFactory(template); + MyPersonRepository repository = factory.getRepository(MyPersonRepository.class); + repository.findAllById(Set.of(42L)); + + ArgumentCaptor captor = ArgumentCaptor.forClass(Query.class); + verify(template).find(captor.capture(), eq(Person.class), eq("person")); + + Query value = captor.getValue(); + assertThat(value.getReadPreference()).isNull(); + } + + interface MyPersonRepository extends ListCrudRepository { @ReadPreference("secondary") Optional findById(Long id); diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/ReactiveMongoRepositoryFactoryUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/ReactiveMongoRepositoryFactoryUnitTests.java index 49a551324..ea54b00f6 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/ReactiveMongoRepositoryFactoryUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/ReactiveMongoRepositoryFactoryUnitTests.java @@ -18,6 +18,10 @@ package org.springframework.data.mongodb.repository.support; import static org.assertj.core.api.Assertions.*; import static org.mockito.Mockito.*; +import java.util.Set; + +import org.springframework.data.repository.reactive.ReactiveCrudRepository; +import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import org.junit.jupiter.api.BeforeEach; @@ -72,7 +76,23 @@ public class ReactiveMongoRepositoryFactoryUnitTests { assertThat(value.getReadPreference()).isEqualTo(com.mongodb.ReadPreference.secondary()); } - interface MyPersonRepository extends Repository { + @Test // GH-2971 + void ignoresCrudMethodMetadataOnNonAnnotatedMethods() { + + when(template.find(any(), any(), anyString())).thenReturn(Flux.empty()); + + ReactiveMongoRepositoryFactory factory = new ReactiveMongoRepositoryFactory(template); + MyPersonRepository repository = factory.getRepository(MyPersonRepository.class); + repository.findAllById(Set.of(42L)); + + ArgumentCaptor captor = ArgumentCaptor.forClass(Query.class); + verify(template).find(captor.capture(), eq(Person.class), eq("person")); + + Query value = captor.getValue(); + assertThat(value.getReadPreference()).isNull(); + } + + interface MyPersonRepository extends ReactiveCrudRepository { @ReadPreference("secondary") Mono findById(Long id); diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepositoryUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepositoryUnitTests.java index 32462a62c..7e3751dac 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepositoryUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepositoryUnitTests.java @@ -161,11 +161,6 @@ class SimpleReactiveMongoRepositoryUnitTests { public Optional getReadPreference() { return Optional.of(com.mongodb.ReadPreference.secondaryPreferred()); } - - @Override - public Method getMethod() { - return null; - } }); when(mongoOperations.find(any(), any(), any())).thenReturn(Flux.just("ok")); @@ -186,11 +181,6 @@ class SimpleReactiveMongoRepositoryUnitTests { public Optional getReadPreference() { return Optional.of(com.mongodb.ReadPreference.secondaryPreferred()); } - - @Override - public Method getMethod() { - return null; - } }); when(mongoOperations.find(any(), any(), any())).thenReturn(Flux.just("ok")); @@ -218,11 +208,6 @@ class SimpleReactiveMongoRepositoryUnitTests { public Optional getReadPreference() { return Optional.of(com.mongodb.ReadPreference.secondaryPreferred()); } - - @Override - public Method getMethod() { - return null; - } }); repository.findBy(Example.of(new TestDummy()), FluentQuery.ReactiveFluentQuery::all).subscribe();