From 5ba363c8abcf4e282a2ac29644171e7b2c243533 Mon Sep 17 00:00:00 2001 From: Mahmoud Ben Hassine Date: Tue, 17 May 2022 23:09:37 +0200 Subject: [PATCH] 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 --- .../builder/RepositoryItemReaderBuilder.java | 109 +----------------- .../RepositoryItemReaderBuilderTests.java | 33 +----- 2 files changed, 3 insertions(+), 139 deletions(-) diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/builder/RepositoryItemReaderBuilder.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/builder/RepositoryItemReaderBuilder.java index c60d3cca8..50098723f 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/builder/RepositoryItemReaderBuilder.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/builder/RepositoryItemReaderBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright 2017-2018 the original author or authors. + * Copyright 2017-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -53,8 +53,6 @@ public class RepositoryItemReaderBuilder { private String methodName; - private RepositoryMethodReference repositoryMethodReference; - private boolean saveState = true; private String name; @@ -186,43 +184,11 @@ public class RepositoryItemReaderBuilder { return this; } - /** - * Specifies a repository and the type-safe method to call for the reader. The method - * configured via this mechanism must take - * {@link org.springframework.data.domain.Pageable} as the last argument. - * This method can be used in place of - * {@link #repository(PagingAndSortingRepository)}, {@link #methodName(String)}, and - * {@link #arguments(List)}. - * - * Note: The repository that is used by the repositoryMethodReference must be - * non-final. - * @param repositoryMethodReference of the used to get a repository and type-safe - * method for use by the reader. - * @return The current instance of the builder. - * @see RepositoryItemReader#setMethodName(String) - * @see RepositoryItemReader#setRepository(PagingAndSortingRepository) - * - */ - public RepositoryItemReaderBuilder repository(RepositoryMethodReference repositoryMethodReference) { - this.repositoryMethodReference = repositoryMethodReference; - - return this; - } - /** * Builds the {@link RepositoryItemReader}. * @return a {@link RepositoryItemReader} */ public RepositoryItemReader build() { - if (this.repositoryMethodReference != null) { - this.methodName = this.repositoryMethodReference.getMethodName(); - this.repository = this.repositoryMethodReference.getRepository(); - - if (CollectionUtils.isEmpty(this.arguments)) { - this.arguments = this.repositoryMethodReference.getArguments(); - } - } - Assert.notNull(this.sorts, "sorts map is required."); Assert.notNull(this.repository, "repository is required."); Assert.hasText(this.methodName, "methodName is required."); @@ -243,77 +209,4 @@ public class RepositoryItemReaderBuilder { return reader; } - /** - * Establishes a proxy that will capture a the Repository and the associated - * methodName that will be used by the reader. - * - * @param The type of repository that will be used by the reader. The class must - * not be final. - */ - public static class RepositoryMethodReference { - - private RepositoryMethodInterceptor repositoryInvocationHandler; - - private PagingAndSortingRepository repository; - - public RepositoryMethodReference(PagingAndSortingRepository repository) { - this.repository = repository; - this.repositoryInvocationHandler = new RepositoryMethodInterceptor(); - } - - /** - * The proxy returned prevents actual method execution and is only used to gather, - * information about the method. - * @return T is a proxy of the object passed in in the constructor - */ - @SuppressWarnings("unchecked") - public T methodIs() { - Enhancer enhancer = new Enhancer(); - enhancer.setSuperclass(this.repository.getClass()); - enhancer.setCallback(this.repositoryInvocationHandler); - return (T) enhancer.create(); - } - - PagingAndSortingRepository getRepository() { - return this.repository; - } - - String getMethodName() { - return this.repositoryInvocationHandler.getMethodName(); - } - - List getArguments() { - return this.repositoryInvocationHandler.getArguments(); - } - - } - - private static class RepositoryMethodInterceptor implements MethodInterceptor { - - private String methodName; - - private List arguments; - - @Override - public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { - this.methodName = method.getName(); - if (objects != null && objects.length > 1) { - arguments = new ArrayList<>(Arrays.asList(objects)); - // remove last entry because that will be provided by the - // RepositoryItemReader - arguments.remove(objects.length - 1); - } - return null; - } - - String getMethodName() { - return this.methodName; - } - - List getArguments() { - return arguments; - } - - } - } diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/builder/RepositoryItemReaderBuilderTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/builder/RepositoryItemReaderBuilderTests.java index 91ffec3cc..83101cc63 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/builder/RepositoryItemReaderBuilderTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/builder/RepositoryItemReaderBuilderTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2017-2021 the original author or authors. + * Copyright 2017-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -42,6 +42,7 @@ import static org.mockito.Mockito.when; /** * @author Glenn Renfro * @author Drummond Dawson + * @author Mahmoud Ben Hassine */ public class RepositoryItemReaderBuilderTests { @@ -88,36 +89,6 @@ public class RepositoryItemReaderBuilderTests { assertEquals("page size was not expected value.", 10, this.pageRequestContainer.getValue().getPageSize()); } - @Test - public void testRepositoryMethodReference() throws Exception { - RepositoryItemReaderBuilder.RepositoryMethodReference repositoryMethodReference = new RepositoryItemReaderBuilder.RepositoryMethodReference<>( - this.repository); - repositoryMethodReference.methodIs().foo(null); - RepositoryItemReader reader = new RepositoryItemReaderBuilder<>().repository(repositoryMethodReference) - .sorts(this.sorts).maxItemCount(5).name("bar").build(); - String result = (String) reader.read(); - assertEquals("Result returned from reader was not expected value.", TEST_CONTENT, result); - assertEquals("page size was not expected value.", 10, this.pageRequestContainer.getValue().getPageSize()); - } - - @Test - public void testRepositoryMethodReferenceWithArgs() throws Exception { - RepositoryItemReaderBuilder.RepositoryMethodReference repositoryMethodReference = new RepositoryItemReaderBuilder.RepositoryMethodReference<>( - this.repository); - repositoryMethodReference.methodIs().foo(ARG1, ARG2, ARG3, null); - RepositoryItemReader reader = new RepositoryItemReaderBuilder<>().repository(repositoryMethodReference) - .sorts(this.sorts).maxItemCount(5).name("bar").build(); - ArgumentCaptor arg1Captor = ArgumentCaptor.forClass(String.class); - ArgumentCaptor arg2Captor = ArgumentCaptor.forClass(String.class); - ArgumentCaptor arg3Captor = ArgumentCaptor.forClass(String.class); - when(this.repository.foo(arg1Captor.capture(), arg2Captor.capture(), arg3Captor.capture(), - this.pageRequestContainer.capture())).thenReturn(this.page); - - String result = (String) reader.read(); - assertEquals("Result returned from reader was not expected value.", TEST_CONTENT, result); - verifyMultiArgRead(arg1Captor, arg2Captor, arg3Captor, result); - } - @Test public void testCurrentItemCount() throws Exception { RepositoryItemReader reader = new RepositoryItemReaderBuilder<>().repository(this.repository)