Skip to content

Commit 5afb8ad

Browse files
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
1 parent 5d25645 commit 5afb8ad

File tree

7 files changed

+54
-39
lines changed

7 files changed

+54
-39
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/CrudMethodMetadata.java

+1-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package org.springframework.data.mongodb.repository.support;
1717

18-
import java.lang.reflect.Method;
1918
import java.util.Optional;
2019

2120
import com.mongodb.ReadPreference;
@@ -25,6 +24,7 @@
2524
* execution.
2625
*
2726
* @author Mark Paluch
27+
* @author Christoph Strobl
2828
* @since 4.2
2929
*/
3030
public interface CrudMethodMetadata {
@@ -36,10 +36,4 @@ public interface CrudMethodMetadata {
3636
*/
3737
Optional<ReadPreference> getReadPreference();
3838

39-
/**
40-
* Returns the {@link Method} that this metadata applies to.
41-
*
42-
* @return the {@link Method} that this metadata applies to.
43-
*/
44-
Method getMethod();
4539
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/CrudMethodMetadataPostProcessor.java

+10-13
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
* information or query hints on them.
4646
*
4747
* @author Mark Paluch
48+
* @author Christoph Strobl
49+
* @since 4.2
4850
*/
4951
class CrudMethodMetadataPostProcessor implements RepositoryProxyPostProcessor, BeanClassLoaderAware {
5052

@@ -103,13 +105,15 @@ static class CrudMethodMetadataPopulatingMethodInterceptor implements MethodInte
103105
*/
104106
static MethodInvocation currentInvocation() throws IllegalStateException {
105107

106-
MethodInvocation mi = currentInvocation.get();
108+
MethodInvocation invocation = currentInvocation.get();
107109

108-
if (mi == null)
109-
throw new IllegalStateException(
110-
"No MethodInvocation found: Check that an AOP invocation is in progress, and that the "
111-
+ "CrudMethodMetadataPopulatingMethodInterceptor is upfront in the interceptor chain.");
112-
return mi;
110+
if (invocation != null) {
111+
return invocation;
112+
}
113+
114+
throw new IllegalStateException(
115+
"No MethodInvocation found: Check that an AOP invocation is in progress, and that the "
116+
+ "CrudMethodMetadataPopulatingMethodInterceptor is upfront in the interceptor chain.");
113117
}
114118

115119
@Override
@@ -163,7 +167,6 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
163167
static class DefaultCrudMethodMetadata implements CrudMethodMetadata {
164168

165169
private final Optional<ReadPreference> readPreference;
166-
private final Method method;
167170

168171
/**
169172
* Creates a new {@link DefaultCrudMethodMetadata} for the given {@link Method}.
@@ -175,7 +178,6 @@ static class DefaultCrudMethodMetadata implements CrudMethodMetadata {
175178
Assert.notNull(method, "Method must not be null");
176179

177180
this.readPreference = findReadPreference(method);
178-
this.method = method;
179181
}
180182

181183
private Optional<ReadPreference> findReadPreference(Method method) {
@@ -201,11 +203,6 @@ private Optional<ReadPreference> findReadPreference(Method method) {
201203
public Optional<ReadPreference> getReadPreference() {
202204
return readPreference;
203205
}
204-
205-
@Override
206-
public Method getMethod() {
207-
return method;
208-
}
209206
}
210207

211208
private static class ThreadBoundTargetSource implements TargetSource {

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepository.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,9 @@ public <S extends T, R> R findBy(Example<S> example,
357357
* applied to queries.
358358
*
359359
* @param crudMethodMetadata
360+
* @since 4.2
360361
*/
361-
public void setRepositoryMethodMetadata(CrudMethodMetadata crudMethodMetadata) {
362+
void setRepositoryMethodMetadata(CrudMethodMetadata crudMethodMetadata) {
362363
this.crudMethodMetadata = crudMethodMetadata;
363364
}
364365

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepository.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -434,8 +434,9 @@ public <S extends T, R, P extends Publisher<R>> P findBy(Example<S> example,
434434
* applied to queries.
435435
*
436436
* @param crudMethodMetadata
437+
* @since 4.2
437438
*/
438-
public void setRepositoryMethodMetadata(CrudMethodMetadata crudMethodMetadata) {
439+
void setRepositoryMethodMetadata(CrudMethodMetadata crudMethodMetadata) {
439440
this.crudMethodMetadata = crudMethodMetadata;
440441
}
441442

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/MongoRepositoryFactoryUnitTests.java

+18-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import java.io.Serializable;
2222
import java.util.Optional;
23+
import java.util.Set;
2324

2425
import org.junit.jupiter.api.BeforeEach;
2526
import org.junit.jupiter.api.Test;
@@ -41,13 +42,15 @@
4142
import org.springframework.data.mongodb.repository.Person;
4243
import org.springframework.data.mongodb.repository.ReadPreference;
4344
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
45+
import org.springframework.data.repository.ListCrudRepository;
4446
import org.springframework.data.repository.Repository;
4547

4648
/**
4749
* Unit test for {@link MongoRepositoryFactory}.
4850
*
4951
* @author Oliver Gierke
5052
* @author Mark Paluch
53+
* @author Christoph Strobl
5154
*/
5255
@ExtendWith(MockitoExtension.class)
5356
@MockitoSettings(strictness = Strictness.LENIENT)
@@ -92,7 +95,21 @@ void considersCrudMethodMetadata() {
9295
assertThat(value.getReadPreference()).isEqualTo(com.mongodb.ReadPreference.secondary());
9396
}
9497

95-
interface MyPersonRepository extends Repository<Person, Long> {
98+
@Test // GH-2971
99+
void ignoresCrudMethodMetadataOnNonAnnotatedMethods() {
100+
101+
MongoRepositoryFactory factory = new MongoRepositoryFactory(template);
102+
MyPersonRepository repository = factory.getRepository(MyPersonRepository.class);
103+
repository.findAllById(Set.of(42L));
104+
105+
ArgumentCaptor<Query> captor = ArgumentCaptor.forClass(Query.class);
106+
verify(template).find(captor.capture(), eq(Person.class), eq("person"));
107+
108+
Query value = captor.getValue();
109+
assertThat(value.getReadPreference()).isNull();
110+
}
111+
112+
interface MyPersonRepository extends ListCrudRepository<Person, Long> {
96113

97114
@ReadPreference("secondary")
98115
Optional<Person> findById(Long id);

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/ReactiveMongoRepositoryFactoryUnitTests.java

+21-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
import static org.assertj.core.api.Assertions.*;
1919
import static org.mockito.Mockito.*;
2020

21+
import java.util.Set;
22+
23+
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
24+
import reactor.core.publisher.Flux;
2125
import reactor.core.publisher.Mono;
2226

2327
import org.junit.jupiter.api.BeforeEach;
@@ -72,7 +76,23 @@ void considersCrudMethodMetadata() {
7276
assertThat(value.getReadPreference()).isEqualTo(com.mongodb.ReadPreference.secondary());
7377
}
7478

75-
interface MyPersonRepository extends Repository<Person, Long> {
79+
@Test // GH-2971
80+
void ignoresCrudMethodMetadataOnNonAnnotatedMethods() {
81+
82+
when(template.find(any(), any(), anyString())).thenReturn(Flux.empty());
83+
84+
ReactiveMongoRepositoryFactory factory = new ReactiveMongoRepositoryFactory(template);
85+
MyPersonRepository repository = factory.getRepository(MyPersonRepository.class);
86+
repository.findAllById(Set.of(42L));
87+
88+
ArgumentCaptor<Query> captor = ArgumentCaptor.forClass(Query.class);
89+
verify(template).find(captor.capture(), eq(Person.class), eq("person"));
90+
91+
Query value = captor.getValue();
92+
assertThat(value.getReadPreference()).isNull();
93+
}
94+
95+
interface MyPersonRepository extends ReactiveCrudRepository<Person, Long> {
7696

7797
@ReadPreference("secondary")
7898
Mono<Person> findById(Long id);

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepositoryUnitTests.java

-15
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,6 @@ void shouldAddReadPreferenceToFindAllMethods(
161161
public Optional<com.mongodb.ReadPreference> getReadPreference() {
162162
return Optional.of(com.mongodb.ReadPreference.secondaryPreferred());
163163
}
164-
165-
@Override
166-
public Method getMethod() {
167-
return null;
168-
}
169164
});
170165
when(mongoOperations.find(any(), any(), any())).thenReturn(Flux.just("ok"));
171166

@@ -186,11 +181,6 @@ void shouldAddReadPreferenceToFindOne() {
186181
public Optional<com.mongodb.ReadPreference> getReadPreference() {
187182
return Optional.of(com.mongodb.ReadPreference.secondaryPreferred());
188183
}
189-
190-
@Override
191-
public Method getMethod() {
192-
return null;
193-
}
194184
});
195185
when(mongoOperations.find(any(), any(), any())).thenReturn(Flux.just("ok"));
196186

@@ -218,11 +208,6 @@ void shouldAddReadPreferenceToFluentFetchable() {
218208
public Optional<com.mongodb.ReadPreference> getReadPreference() {
219209
return Optional.of(com.mongodb.ReadPreference.secondaryPreferred());
220210
}
221-
222-
@Override
223-
public Method getMethod() {
224-
return null;
225-
}
226211
});
227212

228213
repository.findBy(Example.of(new TestDummy()), FluentQuery.ReactiveFluentQuery::all).subscribe();

0 commit comments

Comments
 (0)