Skip to content

Commit e0fc7de

Browse files
terje2001odrotbohm
authored andcommitted
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.
1 parent 87566df commit e0fc7de

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/main/java/org/springframework/data/repository/core/support/DefaultRepositoryInformation.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,10 @@ private boolean parametersMatch(Method method, Method baseClassMethod) {
356356
return false;
357357
}
358358
} else {
359-
if (!types[i].equals(parameterType)) {
359+
// We must either have an exact match, or,
360+
if (!types[i].equals(parameterType) &&
361+
// the tpes must be assignable _and_ signature definition types must be identical.
362+
!(types[i].isAssignableFrom(parameterType) && types[i].equals(method.getParameterTypes()[i]))) {
360363
return false;
361364
}
362365
}

src/test/java/org/springframework/data/repository/core/support/DefaultRepositoryInformationUnitTests.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,22 @@ public void discoversCustomlyImplementedCrudMethod() throws SecurityException, N
9494
assertThat(information.getTargetClassMethod(source), is(expected));
9595
}
9696

97+
/**
98+
* @see DATACMNS-854
99+
*/
100+
@Test
101+
public void discoversCustomlyImplementedCrudMethodWithGenerics() throws SecurityException, NoSuchMethodException {
102+
103+
RepositoryMetadata metadata = new DefaultRepositoryMetadata(FooRepository.class);
104+
RepositoryInformation information = new DefaultRepositoryInformation(metadata, CrudRepository.class,
105+
customImplementation.getClass());
106+
107+
Method source = FooRepositoryCustom.class.getMethod("exists", Object.class);
108+
Method expected = customImplementation.getClass().getMethod("exists", Object.class);
109+
110+
assertThat(information.getTargetClassMethod(source), is(expected));
111+
}
112+
97113
@Test
98114
public void considersIntermediateMethodsAsFinderMethods() {
99115

@@ -248,8 +264,11 @@ interface FooRepository extends CrudRepository<User, Integer>, FooRepositoryCust
248264
User findOne(Long primaryKey);
249265
}
250266

251-
interface FooRepositoryCustom {
267+
interface FooSuperInterfaceWithGenerics<T> {
268+
boolean exists(T id);
269+
}
252270

271+
interface FooRepositoryCustom extends FooSuperInterfaceWithGenerics<User> {
253272
User save(User user);
254273
}
255274

0 commit comments

Comments
 (0)