Browse Source

DATACMNS-943 - We prefer direct matches on repository query method overloads.

We now try to look up a target class method based on concrete name and parameter type before falling back on the more expensive type matches. This also eliminates the possibility of invalid method matches as described in the ticket.
pull/346/head
Oliver Gierke 9 years ago
parent
commit
7ceb761457
  1. 6
      src/main/java/org/springframework/data/repository/core/support/DefaultRepositoryInformation.java
  2. 23
      src/test/java/org/springframework/data/repository/core/support/DefaultRepositoryInformationUnitTests.java

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

@ -243,6 +243,12 @@ class DefaultRepositoryInformation implements RepositoryInformation { @@ -243,6 +243,12 @@ class DefaultRepositoryInformation implements RepositoryInformation {
return method;
}
Method result = findMethod(baseClass, method.getName(), method.getParameterTypes());
if (result != null) {
return result;
}
for (Method baseClassMethod : baseClass.getMethods()) {
// Wrong name

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

@ -254,6 +254,21 @@ public class DefaultRepositoryInformationUnitTests { @@ -254,6 +254,21 @@ public class DefaultRepositoryInformationUnitTests {
assertThat(information.isCustomMethod(customBaseRepositoryMethod), is(true));
}
/**
* @see DATACMNS-943
* @throws Exception
*/
@Test
public void usesCorrectSaveOverload() throws Exception {
RepositoryMetadata metadata = new DefaultRepositoryMetadata(DummyRepository.class);
RepositoryInformation information = new DefaultRepositoryInformation(metadata, CrudRepository.class, null);
Method method = DummyRepository.class.getMethod("save", Iterable.class);
assertThat(information.getTargetClassMethod(method), is(CrudRepository.class.getMethod("save", Iterable.class)));
}
private static Method getMethodFrom(Class<?> type, String name) {
for (Method method : type.getMethods()) {
@ -303,7 +318,7 @@ public class DefaultRepositoryInformationUnitTests { @@ -303,7 +318,7 @@ public class DefaultRepositoryInformationUnitTests {
@Override
public Iterator<User> iterator() {
return Collections.<User>emptySet().iterator();
return Collections.<User> emptySet().iterator();
}
}
@ -366,4 +381,10 @@ public class DefaultRepositoryInformationUnitTests { @@ -366,4 +381,10 @@ public class DefaultRepositoryInformationUnitTests {
}
static class Sample {}
interface DummyRepository extends CrudRepository<User, Integer> {
@Override
<S extends User> List<S> save(Iterable<S> entites);
}
}

Loading…
Cancel
Save