Skip to content

Commit 41da78e

Browse files
committed
DATACMNS-1008 - Reintroduced lost support for non-generic redeclarations of generic CRUD methods.
The changes for DATACMNS-854 and DATACMNS-912 dropped the support for the simpler redeclaration of generic methods like CrudRepository.save(…) which as of the changes requires to be redeclared like <T extends Foo> T save(T entity). Previously a simple redeclaration like Foo save(Foo entity) was sufficient. This commit reintroduces the check for a direct match of the parameter types and shortcuts the more detailed check that's necessary in case of type variables being involved. Related tickets: DATACMNS-854, DATACMNS-912.
1 parent 9195a0e commit 41da78e

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

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

+4
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,10 @@ private boolean parametersMatch(Method method, Method baseClassMethod) {
372372
continue;
373373
}
374374

375+
if (types[i].equals(parameterType)) {
376+
continue;
377+
}
378+
375379
if (!type.isAssignableFrom(parameterType) || !type.equals(methodParameterTypes[i])) {
376380
return false;
377381
}

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

+31-6
Original file line numberDiff line numberDiff line change
@@ -223,12 +223,12 @@ public void discoversCustomlyImplementedCrudMethodWithGenerics() throws Security
223223
@Test // DATACMNS-912
224224
public void discoversCustomlyImplementedCrudMethodWithGenericParameters() throws Exception {
225225

226-
SampleRepositoryImpl customImplementation = new SampleRepositoryImpl();
227-
RepositoryMetadata metadata = new DefaultRepositoryMetadata(SampleRepository.class);
226+
GenericsSaveRepositoryImpl customImplementation = new GenericsSaveRepositoryImpl();
227+
RepositoryMetadata metadata = new DefaultRepositoryMetadata(GenericsSaveRepository.class);
228228
RepositoryInformation information = new DefaultRepositoryInformation(metadata, RepositoryFactorySupport.class,
229229
customImplementation.getClass());
230230

231-
Method customBaseRepositoryMethod = SampleRepository.class.getMethod("save", Object.class);
231+
Method customBaseRepositoryMethod = GenericsSaveRepository.class.getMethod("save", Object.class);
232232
assertThat(information.isCustomMethod(customBaseRepositoryMethod), is(true));
233233
}
234234

@@ -244,6 +244,18 @@ public void usesCorrectSaveOverload() throws Exception {
244244
is(DummyRepositoryImpl.class.getMethod("save", Iterable.class)));
245245
}
246246

247+
@Test // DATACMNS-1008, DATACMNS-912, DATACMNS-854
248+
public void discoversCustomlyImplementedCrudMethodWithoutGenericParameters() throws Exception {
249+
250+
SimpleSaveRepositoryImpl customImplementation = new SimpleSaveRepositoryImpl();
251+
RepositoryMetadata metadata = new DefaultRepositoryMetadata(SimpleSaveRepository.class);
252+
RepositoryInformation information = new DefaultRepositoryInformation(metadata, RepositoryFactorySupport.class,
253+
customImplementation.getClass());
254+
255+
Method customBaseRepositoryMethod = SimpleSaveRepository.class.getMethod("save", Object.class);
256+
assertThat(information.isCustomMethod(customBaseRepositoryMethod), is(true));
257+
}
258+
247259
private static Method getMethodFrom(Class<?> type, String name) {
248260

249261
for (Method method : type.getMethods()) {
@@ -346,11 +358,13 @@ interface CustomDefaultRepositoryMethodsRepository extends CrudRepository<User,
346358
List<User> findAll();
347359
}
348360

349-
interface SampleRepository extends CrudRepository<Sample, Long> {}
361+
// DATACMNS-854, DATACMNS-912
362+
363+
interface GenericsSaveRepository extends CrudRepository<Sample, Long> {}
350364

351-
static class SampleRepositoryImpl {
365+
static class GenericsSaveRepositoryImpl {
352366

353-
public <S extends Sample> S save(S entity) {
367+
public <T extends Sample> T save(T entity) {
354368
return entity;
355369
}
356370
}
@@ -367,4 +381,15 @@ static class DummyRepositoryImpl<T, ID extends Serializable> implements CrudRepo
367381

368382
private @Delegate CrudRepository<T, ID> delegate;
369383
}
384+
385+
// DATACMNS-1008, DATACMNS-854, DATACMNS-912
386+
387+
interface SimpleSaveRepository extends CrudRepository<Sample, Long> {}
388+
389+
static class SimpleSaveRepositoryImpl {
390+
391+
public Sample save(Sample entity) {
392+
return entity;
393+
}
394+
}
370395
}

0 commit comments

Comments
 (0)