From 2b91ff36352d821c5e908be73744a62d6556fcfa Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Thu, 24 Nov 2016 17:21:11 +0100 Subject: [PATCH] DATACMNS-943 - We prefer direct matches on repository query method overloads. We now try to look up a target class method based on concrete name and parameter type before falling back on the more expensive type matches. This also eliminates the possibility of invalid method matches as described in the ticket. --- .../support/DefaultRepositoryInformation.java | 6 ++++ ...DefaultRepositoryInformationUnitTests.java | 31 ++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/springframework/data/repository/core/support/DefaultRepositoryInformation.java b/src/main/java/org/springframework/data/repository/core/support/DefaultRepositoryInformation.java index dd3158bcc..a9245c0ab 100644 --- a/src/main/java/org/springframework/data/repository/core/support/DefaultRepositoryInformation.java +++ b/src/main/java/org/springframework/data/repository/core/support/DefaultRepositoryInformation.java @@ -245,6 +245,12 @@ class DefaultRepositoryInformation implements RepositoryInformation { return method; } + Method result = findMethod(baseClass, method.getName(), method.getParameterTypes()); + + if (result != null) { + return result; + } + for (Method baseClassMethod : baseClass.getMethods()) { // Wrong name diff --git a/src/test/java/org/springframework/data/repository/core/support/DefaultRepositoryInformationUnitTests.java b/src/test/java/org/springframework/data/repository/core/support/DefaultRepositoryInformationUnitTests.java index 875d65475..909cfafc1 100644 --- a/src/test/java/org/springframework/data/repository/core/support/DefaultRepositoryInformationUnitTests.java +++ b/src/test/java/org/springframework/data/repository/core/support/DefaultRepositoryInformationUnitTests.java @@ -18,6 +18,8 @@ package org.springframework.data.repository.core.support; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; +import lombok.experimental.Delegate; + import java.io.Serializable; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; @@ -254,6 +256,22 @@ public class DefaultRepositoryInformationUnitTests { assertThat(information.isCustomMethod(customBaseRepositoryMethod), is(true)); } + /** + * @see DATACMNS-943 + * @throws Exception + */ + @Test + public void usesCorrectSaveOverload() throws Exception { + + RepositoryMetadata metadata = new DefaultRepositoryMetadata(DummyRepository.class); + RepositoryInformation information = new DefaultRepositoryInformation(metadata, DummyRepositoryImpl.class, null); + + Method method = DummyRepository.class.getMethod("save", Iterable.class); + + assertThat(information.getTargetClassMethod(method), + is(DummyRepositoryImpl.class.getMethod("save", Iterable.class))); + } + private static Method getMethodFrom(Class type, String name) { for (Method method : type.getMethods()) { @@ -303,7 +321,7 @@ public class DefaultRepositoryInformationUnitTests { @Override public Iterator iterator() { - return Collections.emptySet().iterator(); + return Collections. emptySet().iterator(); } } @@ -366,4 +384,15 @@ public class DefaultRepositoryInformationUnitTests { } static class Sample {} + + interface DummyRepository extends CrudRepository { + + @Override + List save(Iterable entites); + } + + static class DummyRepositoryImpl implements CrudRepository { + + private @Delegate CrudRepository delegate; + } }