Browse Source

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.
pull/347/head
Oliver Gierke 9 years ago
parent
commit
10075b7d61
  1. 4
      src/main/java/org/springframework/data/repository/core/support/DefaultRepositoryInformation.java
  2. 37
      src/test/java/org/springframework/data/repository/core/support/DefaultRepositoryInformationUnitTests.java

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

@ -372,6 +372,10 @@ class DefaultRepositoryInformation implements RepositoryInformation { @@ -372,6 +372,10 @@ class DefaultRepositoryInformation implements RepositoryInformation {
continue;
}
if (types[i].equals(parameterType)) {
continue;
}
if (!type.isAssignableFrom(parameterType) || !type.equals(methodParameterTypes[i])) {
return false;
}

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

@ -247,12 +247,12 @@ public class DefaultRepositoryInformationUnitTests { @@ -247,12 +247,12 @@ public class DefaultRepositoryInformationUnitTests {
@Test
public void discoversCustomlyImplementedCrudMethodWithGenericParameters() throws Exception {
SampleRepositoryImpl customImplementation = new SampleRepositoryImpl();
RepositoryMetadata metadata = new DefaultRepositoryMetadata(SampleRepository.class);
GenericsSaveRepositoryImpl customImplementation = new GenericsSaveRepositoryImpl();
RepositoryMetadata metadata = new DefaultRepositoryMetadata(GenericsSaveRepository.class);
RepositoryInformation information = new DefaultRepositoryInformation(metadata, RepositoryFactorySupport.class,
customImplementation.getClass());
Method customBaseRepositoryMethod = SampleRepository.class.getMethod("save", Object.class);
Method customBaseRepositoryMethod = GenericsSaveRepository.class.getMethod("save", Object.class);
assertThat(information.isCustomMethod(customBaseRepositoryMethod), is(true));
}
@ -272,6 +272,18 @@ public class DefaultRepositoryInformationUnitTests { @@ -272,6 +272,18 @@ public class DefaultRepositoryInformationUnitTests {
is(DummyRepositoryImpl.class.getMethod("save", Iterable.class)));
}
@Test // DATACMNS-1008, DATACMNS-912, DATACMNS-854
public void discoversCustomlyImplementedCrudMethodWithoutGenericParameters() throws Exception {
SimpleSaveRepositoryImpl customImplementation = new SimpleSaveRepositoryImpl();
RepositoryMetadata metadata = new DefaultRepositoryMetadata(SimpleSaveRepository.class);
RepositoryInformation information = new DefaultRepositoryInformation(metadata, RepositoryFactorySupport.class,
customImplementation.getClass());
Method customBaseRepositoryMethod = SimpleSaveRepository.class.getMethod("save", Object.class);
assertThat(information.isCustomMethod(customBaseRepositoryMethod), is(true));
}
private static Method getMethodFrom(Class<?> type, String name) {
for (Method method : type.getMethods()) {
@ -374,11 +386,13 @@ public class DefaultRepositoryInformationUnitTests { @@ -374,11 +386,13 @@ public class DefaultRepositoryInformationUnitTests {
List<User> findAll();
}
interface SampleRepository extends CrudRepository<Sample, Long> {}
// DATACMNS-854, DATACMNS-912
interface GenericsSaveRepository extends CrudRepository<Sample, Long> {}
static class SampleRepositoryImpl {
static class GenericsSaveRepositoryImpl {
public <S extends Sample> S save(S entity) {
public <T extends Sample> T save(T entity) {
return entity;
}
}
@ -395,4 +409,15 @@ public class DefaultRepositoryInformationUnitTests { @@ -395,4 +409,15 @@ public class DefaultRepositoryInformationUnitTests {
private @Delegate CrudRepository<T, ID> delegate;
}
// DATACMNS-1008, DATACMNS-854, DATACMNS-912
interface SimpleSaveRepository extends CrudRepository<Sample, Long> {}
static class SimpleSaveRepositoryImpl {
public Sample save(Sample entity) {
return entity;
}
}
}

Loading…
Cancel
Save