diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/core/RepositoryInformation.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/core/RepositoryInformation.java index 37d120f0b..a619dad7c 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/core/RepositoryInformation.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/core/RepositoryInformation.java @@ -43,11 +43,18 @@ public interface RepositoryInformation extends RepositoryMetadata { * Returns whether the given method is a custom repository method. * * @param method - * @param baseClass * @return */ boolean isCustomMethod(Method method); + /** + * Returns whether the given method is a query method. + * + * @param method + * @return + */ + boolean isQueryMethod(Method method); + /** * Returns all methods considered to be query methods. * diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/core/support/DefaultRepositoryInformation.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/core/support/DefaultRepositoryInformation.java index 1220c8d30..02dfb6aa2 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/core/support/DefaultRepositoryInformation.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/core/support/DefaultRepositoryInformation.java @@ -22,6 +22,7 @@ import java.io.Serializable; import java.lang.reflect.Method; import java.lang.reflect.Type; import java.lang.reflect.TypeVariable; +import java.util.Collections; import java.util.HashSet; import java.util.Map; import java.util.Set; @@ -101,7 +102,6 @@ class DefaultRepositoryInformation extends AbstractRepositoryMetadata implements * @see org.springframework.data.repository.support.RepositoryInformation#getRepositoryBaseClass() */ public Class getRepositoryBaseClass() { - return this.repositoryBaseClass; } @@ -152,7 +152,7 @@ class DefaultRepositoryInformation extends AbstractRepositoryMetadata implements * (non-Javadoc) * @see org.springframework.data.repository.support.RepositoryInformation#getQueryMethods() */ - public Iterable getQueryMethods() { + public Set getQueryMethods() { Set result = new HashSet(); @@ -163,7 +163,7 @@ class DefaultRepositoryInformation extends AbstractRepositoryMetadata implements } } - return result; + return Collections.unmodifiableSet(result); } /* @@ -174,6 +174,14 @@ class DefaultRepositoryInformation extends AbstractRepositoryMetadata implements return isTargetClassMethod(method, customImplementationClass); } + /* + * (non-Javadoc) + * @see org.springframework.data.repository.core.RepositoryInformation#isQueryMethod(java.lang.reflect.Method) + */ + public boolean isQueryMethod(Method method) { + return getQueryMethods().contains(method); + } + /** * Returns whether the given method is a method covered by the base implementation. * diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/repository/core/support/DefaultRepositoryInformationUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/repository/core/support/DefaultRepositoryInformationUnitTests.java index 51b8c6f72..1bf3ecfbf 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/repository/core/support/DefaultRepositoryInformationUnitTests.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/repository/core/support/DefaultRepositoryInformationUnitTests.java @@ -142,6 +142,21 @@ public class DefaultRepositoryInformationUnitTests { assertThat(queryMethods, not(hasItem(intermediateMethod))); } + /** + * @see DATACMNS-193 + */ + @Test + public void detectsQueryMethodCorrectly() { + + RepositoryMetadata metadata = new DefaultRepositoryMetadata(ConcreteRepository.class); + RepositoryInformation information = new DefaultRepositoryInformation(metadata, CrudRepository.class, null); + + Method queryMethod = getMethodFrom(ConcreteRepository.class, "findBySomethingDifferent"); + + assertThat(information.getQueryMethods(), hasItem(queryMethod)); + assertThat(information.isQueryMethod(queryMethod), is(true)); + } + private Method getMethodFrom(Class type, String name) { for (Method method : type.getMethods()) { if (method.getName().equals(name)) { diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/DummyRepositoryInformation.java b/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/DummyRepositoryInformation.java index a7b4e6477..08b6fb2fc 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/DummyRepositoryInformation.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/DummyRepositoryInformation.java @@ -18,6 +18,7 @@ package org.springframework.data.repository.support; import java.io.Serializable; import java.lang.reflect.Method; import java.util.Collections; +import java.util.Set; import org.springframework.data.repository.core.RepositoryInformation; import org.springframework.data.repository.core.RepositoryMetadata; @@ -59,11 +60,15 @@ public final class DummyRepositoryInformation implements RepositoryInformation { return false; } - public Iterable getQueryMethods() { + public boolean isQueryMethod(Method method) { + return false; + } + + public Set getQueryMethods() { return Collections.emptySet(); } public Method getTargetClassMethod(Method method) { return method; } -} \ No newline at end of file +}