Skip to content

Commit 5ba363c

Browse files
committed
Remove RepositoryMethodReference from RepositoryItemReaderBuilder
This class and its usage have been reported to be confusing and causing context startup failures without real added value. This commit removes that class to simplify the creation of a RepositoryItemReader through its builder. Resolves #793
1 parent 565ddcd commit 5ba363c

File tree

2 files changed

+3
-139
lines changed

2 files changed

+3
-139
lines changed

Diff for: spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/builder/RepositoryItemReaderBuilder.java

+1-108
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2018 the original author or authors.
2+
* Copyright 2017-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -53,8 +53,6 @@ public class RepositoryItemReaderBuilder<T> {
5353

5454
private String methodName;
5555

56-
private RepositoryMethodReference<?> repositoryMethodReference;
57-
5856
private boolean saveState = true;
5957

6058
private String name;
@@ -186,43 +184,11 @@ public RepositoryItemReaderBuilder<T> methodName(String methodName) {
186184
return this;
187185
}
188186

189-
/**
190-
* Specifies a repository and the type-safe method to call for the reader. The method
191-
* configured via this mechanism must take
192-
* {@link org.springframework.data.domain.Pageable} as the <em>last</em> argument.
193-
* This method can be used in place of
194-
* {@link #repository(PagingAndSortingRepository)}, {@link #methodName(String)}, and
195-
* {@link #arguments(List)}.
196-
*
197-
* Note: The repository that is used by the repositoryMethodReference must be
198-
* non-final.
199-
* @param repositoryMethodReference of the used to get a repository and type-safe
200-
* method for use by the reader.
201-
* @return The current instance of the builder.
202-
* @see RepositoryItemReader#setMethodName(String)
203-
* @see RepositoryItemReader#setRepository(PagingAndSortingRepository)
204-
*
205-
*/
206-
public RepositoryItemReaderBuilder<T> repository(RepositoryMethodReference<?> repositoryMethodReference) {
207-
this.repositoryMethodReference = repositoryMethodReference;
208-
209-
return this;
210-
}
211-
212187
/**
213188
* Builds the {@link RepositoryItemReader}.
214189
* @return a {@link RepositoryItemReader}
215190
*/
216191
public RepositoryItemReader<T> build() {
217-
if (this.repositoryMethodReference != null) {
218-
this.methodName = this.repositoryMethodReference.getMethodName();
219-
this.repository = this.repositoryMethodReference.getRepository();
220-
221-
if (CollectionUtils.isEmpty(this.arguments)) {
222-
this.arguments = this.repositoryMethodReference.getArguments();
223-
}
224-
}
225-
226192
Assert.notNull(this.sorts, "sorts map is required.");
227193
Assert.notNull(this.repository, "repository is required.");
228194
Assert.hasText(this.methodName, "methodName is required.");
@@ -243,77 +209,4 @@ public RepositoryItemReader<T> build() {
243209
return reader;
244210
}
245211

246-
/**
247-
* Establishes a proxy that will capture a the Repository and the associated
248-
* methodName that will be used by the reader.
249-
*
250-
* @param <T> The type of repository that will be used by the reader. The class must
251-
* not be final.
252-
*/
253-
public static class RepositoryMethodReference<T> {
254-
255-
private RepositoryMethodInterceptor repositoryInvocationHandler;
256-
257-
private PagingAndSortingRepository<?, ?> repository;
258-
259-
public RepositoryMethodReference(PagingAndSortingRepository<?, ?> repository) {
260-
this.repository = repository;
261-
this.repositoryInvocationHandler = new RepositoryMethodInterceptor();
262-
}
263-
264-
/**
265-
* The proxy returned prevents actual method execution and is only used to gather,
266-
* information about the method.
267-
* @return T is a proxy of the object passed in in the constructor
268-
*/
269-
@SuppressWarnings("unchecked")
270-
public T methodIs() {
271-
Enhancer enhancer = new Enhancer();
272-
enhancer.setSuperclass(this.repository.getClass());
273-
enhancer.setCallback(this.repositoryInvocationHandler);
274-
return (T) enhancer.create();
275-
}
276-
277-
PagingAndSortingRepository<?, ?> getRepository() {
278-
return this.repository;
279-
}
280-
281-
String getMethodName() {
282-
return this.repositoryInvocationHandler.getMethodName();
283-
}
284-
285-
List<Object> getArguments() {
286-
return this.repositoryInvocationHandler.getArguments();
287-
}
288-
289-
}
290-
291-
private static class RepositoryMethodInterceptor implements MethodInterceptor {
292-
293-
private String methodName;
294-
295-
private List<Object> arguments;
296-
297-
@Override
298-
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
299-
this.methodName = method.getName();
300-
if (objects != null && objects.length > 1) {
301-
arguments = new ArrayList<>(Arrays.asList(objects));
302-
// remove last entry because that will be provided by the
303-
// RepositoryItemReader
304-
arguments.remove(objects.length - 1);
305-
}
306-
return null;
307-
}
308-
309-
String getMethodName() {
310-
return this.methodName;
311-
}
312-
313-
List<Object> getArguments() {
314-
return arguments;
315-
}
316-
317-
}
318-
319212
}

Diff for: spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/builder/RepositoryItemReaderBuilderTests.java

+2-31
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2021 the original author or authors.
2+
* Copyright 2017-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -42,6 +42,7 @@
4242
/**
4343
* @author Glenn Renfro
4444
* @author Drummond Dawson
45+
* @author Mahmoud Ben Hassine
4546
*/
4647
public class RepositoryItemReaderBuilderTests {
4748

@@ -88,36 +89,6 @@ public void testBasicRead() throws Exception {
8889
assertEquals("page size was not expected value.", 10, this.pageRequestContainer.getValue().getPageSize());
8990
}
9091

91-
@Test
92-
public void testRepositoryMethodReference() throws Exception {
93-
RepositoryItemReaderBuilder.RepositoryMethodReference<TestRepository> repositoryMethodReference = new RepositoryItemReaderBuilder.RepositoryMethodReference<>(
94-
this.repository);
95-
repositoryMethodReference.methodIs().foo(null);
96-
RepositoryItemReader<Object> reader = new RepositoryItemReaderBuilder<>().repository(repositoryMethodReference)
97-
.sorts(this.sorts).maxItemCount(5).name("bar").build();
98-
String result = (String) reader.read();
99-
assertEquals("Result returned from reader was not expected value.", TEST_CONTENT, result);
100-
assertEquals("page size was not expected value.", 10, this.pageRequestContainer.getValue().getPageSize());
101-
}
102-
103-
@Test
104-
public void testRepositoryMethodReferenceWithArgs() throws Exception {
105-
RepositoryItemReaderBuilder.RepositoryMethodReference<TestRepository> repositoryMethodReference = new RepositoryItemReaderBuilder.RepositoryMethodReference<>(
106-
this.repository);
107-
repositoryMethodReference.methodIs().foo(ARG1, ARG2, ARG3, null);
108-
RepositoryItemReader<Object> reader = new RepositoryItemReaderBuilder<>().repository(repositoryMethodReference)
109-
.sorts(this.sorts).maxItemCount(5).name("bar").build();
110-
ArgumentCaptor<String> arg1Captor = ArgumentCaptor.forClass(String.class);
111-
ArgumentCaptor<String> arg2Captor = ArgumentCaptor.forClass(String.class);
112-
ArgumentCaptor<String> arg3Captor = ArgumentCaptor.forClass(String.class);
113-
when(this.repository.foo(arg1Captor.capture(), arg2Captor.capture(), arg3Captor.capture(),
114-
this.pageRequestContainer.capture())).thenReturn(this.page);
115-
116-
String result = (String) reader.read();
117-
assertEquals("Result returned from reader was not expected value.", TEST_CONTENT, result);
118-
verifyMultiArgRead(arg1Captor, arg2Captor, arg3Captor, result);
119-
}
120-
12192
@Test
12293
public void testCurrentItemCount() throws Exception {
12394
RepositoryItemReader<Object> reader = new RepositoryItemReaderBuilder<>().repository(this.repository)

0 commit comments

Comments
 (0)