Skip to content

Commit

Permalink
DATACMNS-1008 - Reintroduced lost support for non-generic redeclarati…
Browse files Browse the repository at this point in the history
…ons 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.
  • Loading branch information
odrotbohm committed Mar 14, 2017
1 parent 9195a0e commit 41da78e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,10 @@ private boolean parametersMatch(Method method, Method baseClassMethod) {
continue;
}

if (types[i].equals(parameterType)) {
continue;
}

if (!type.isAssignableFrom(parameterType) || !type.equals(methodParameterTypes[i])) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,12 @@ public void discoversCustomlyImplementedCrudMethodWithGenerics() throws Security
@Test // DATACMNS-912
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));
}

Expand All @@ -244,6 +244,18 @@ public void usesCorrectSaveOverload() throws Exception {
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()) {
Expand Down Expand Up @@ -346,11 +358,13 @@ interface CustomDefaultRepositoryMethodsRepository extends CrudRepository<User,
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;
}
}
Expand All @@ -367,4 +381,15 @@ static class DummyRepositoryImpl<T, ID extends Serializable> implements CrudRepo

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;
}
}
}

0 comments on commit 41da78e

Please sign in to comment.