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/187/head
Oliver Gierke 9 years ago
parent
commit
fbd560ee4f
  1. 6
      src/main/java/org/springframework/data/repository/core/support/DefaultRepositoryInformation.java
  2. 32
      src/test/java/org/springframework/data/repository/core/support/DefaultRepositoryInformationUnitTests.java

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

@ -246,6 +246,12 @@ class DefaultRepositoryInformation implements RepositoryInformation { @@ -246,6 +246,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

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

@ -18,6 +18,8 @@ package org.springframework.data.repository.core.support; @@ -18,6 +18,8 @@ package org.springframework.data.repository.core.support;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import lombok.experimental.Delegate;
import java.io.Serializable;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
@ -257,6 +259,7 @@ public class DefaultRepositoryInformationUnitTests { @@ -257,6 +259,7 @@ public class DefaultRepositoryInformationUnitTests {
/**
* @see DATACMNS-939
* @throws Exception
*/
@Test
public void ignoresStaticMethod() throws SecurityException, NoSuchMethodException {
@ -285,6 +288,22 @@ public class DefaultRepositoryInformationUnitTests { @@ -285,6 +288,22 @@ public class DefaultRepositoryInformationUnitTests {
assertThat(information.getQueryMethods(), not(hasItem(method)));
}
/**
* @see DATACMNS-943
* @throws Exception
*/
@Test
public void usesCorrectSaveOverload() throws Exception {
RepositoryMetadata metadata = new DefaultRepositoryMetadata(DummyRepository.class);
RepositoryInformation information = new DefaultRepositoryInformation(metadata, DummyRepositoryImpl.class, null);
Method method = DummyRepository.class.getMethod("save", Iterable.class);
assertThat(information.getTargetClassMethod(method),
is(DummyRepositoryImpl.class.getMethod("save", Iterable.class)));
}
private static Method getMethodFrom(Class<?> type, String name) {
for (Method method : type.getMethods()) {
@ -338,7 +357,7 @@ public class DefaultRepositoryInformationUnitTests { @@ -338,7 +357,7 @@ public class DefaultRepositoryInformationUnitTests {
@Override
public Iterator<User> iterator() {
return Collections.<User>emptySet().iterator();
return Collections.<User> emptySet().iterator();
}
}
@ -401,4 +420,15 @@ public class DefaultRepositoryInformationUnitTests { @@ -401,4 +420,15 @@ public class DefaultRepositoryInformationUnitTests {
}
static class Sample {}
interface DummyRepository extends CrudRepository<User, Integer> {
@Override
<S extends User> List<S> save(Iterable<S> entites);
}
static class DummyRepositoryImpl<T, ID extends Serializable> implements CrudRepository<T, ID> {
private @Delegate CrudRepository<T, ID> delegate;
}
}

Loading…
Cancel
Save