From e0fc7deb053fb94d87bba1a8df88359f09820aeb Mon Sep 17 00:00:00 2001 From: Terje Strand Date: Mon, 16 May 2016 21:43:01 -0700 Subject: [PATCH] DATACMNS-854 - Allow custom implementation of repository methods with generics. Add additional check for detecting customized method where method contains generics. Overriding methods without generics already works, this patch simply makes the behavior consistent. Original pull request: #162. --- .../support/DefaultRepositoryInformation.java | 5 ++++- ...DefaultRepositoryInformationUnitTests.java | 21 ++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) 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 48bcdac42..c21d93510 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 @@ -356,7 +356,10 @@ class DefaultRepositoryInformation implements RepositoryInformation { return false; } } else { - if (!types[i].equals(parameterType)) { + // We must either have an exact match, or, + if (!types[i].equals(parameterType) && + // the tpes must be assignable _and_ signature definition types must be identical. + !(types[i].isAssignableFrom(parameterType) && types[i].equals(method.getParameterTypes()[i]))) { return false; } } 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 78ab79074..d175b6d8f 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 @@ -94,6 +94,22 @@ public class DefaultRepositoryInformationUnitTests { assertThat(information.getTargetClassMethod(source), is(expected)); } + /** + * @see DATACMNS-854 + */ + @Test + public void discoversCustomlyImplementedCrudMethodWithGenerics() throws SecurityException, NoSuchMethodException { + + RepositoryMetadata metadata = new DefaultRepositoryMetadata(FooRepository.class); + RepositoryInformation information = new DefaultRepositoryInformation(metadata, CrudRepository.class, + customImplementation.getClass()); + + Method source = FooRepositoryCustom.class.getMethod("exists", Object.class); + Method expected = customImplementation.getClass().getMethod("exists", Object.class); + + assertThat(information.getTargetClassMethod(source), is(expected)); + } + @Test public void considersIntermediateMethodsAsFinderMethods() { @@ -248,8 +264,11 @@ public class DefaultRepositoryInformationUnitTests { User findOne(Long primaryKey); } - interface FooRepositoryCustom { + interface FooSuperInterfaceWithGenerics { + boolean exists(T id); + } + interface FooRepositoryCustom extends FooSuperInterfaceWithGenerics { User save(User user); }