diff --git a/src/main/java/org/springframework/data/repository/query/QueryMethod.java b/src/main/java/org/springframework/data/repository/query/QueryMethod.java index 6d93bdd51..9fbe70bb9 100644 --- a/src/main/java/org/springframework/data/repository/query/QueryMethod.java +++ b/src/main/java/org/springframework/data/repository/query/QueryMethod.java @@ -1,5 +1,5 @@ /* - * Copyright 2008-2015 the original author or authors. + * Copyright 2008-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,7 +18,6 @@ package org.springframework.data.repository.query; import static org.springframework.data.repository.util.ClassUtils.*; import java.lang.reflect.Method; -import java.util.Arrays; import java.util.Set; import org.springframework.data.domain.Page; @@ -40,6 +39,7 @@ import org.springframework.util.Assert; * * @author Oliver Gierke * @author Thomas Darimont + * @author Maciek Opała */ public class QueryMethod { @@ -80,8 +80,7 @@ public class QueryMethod { if (hasParameterOfType(method, Pageable.class)) { if (!isStreamQuery()) { - final Set> allowedPageableTypes = QueryExecutionConverters.getAllowedPageableTypes(); - assertReturnTypeAssignable(method, allowedPageableTypes.toArray(new Class[allowedPageableTypes.size()])); + assertReturnTypeAssignable(method, QueryExecutionConverters.getAllowedPageableTypes()); } if (hasParameterOfType(method, Sort.class)) { @@ -90,7 +89,8 @@ public class QueryMethod { } } - Assert.notNull(this.parameters, String.format("Parameters extracted from method '%s' must not be null!", method.getName())); + Assert.notNull(this.parameters, + String.format("Parameters extracted from method '%s' must not be null!", method.getName())); if (isPageQuery()) { Assert.isTrue(this.parameters.hasPageableParameter(), @@ -279,13 +279,14 @@ public class QueryMethod { return method.getReturnType(); } - private static void assertReturnTypeAssignable(Method method, Class... types) { + private static void assertReturnTypeAssignable(Method method, Set> types) { Assert.notNull(method, "Method must not be null!"); Assert.notEmpty(types, "Types must not be null or empty!"); TypeInformation returnType = ClassTypeInformation.fromReturnTypeOf(method); - returnType = QueryExecutionConverters.isSingleValue(returnType.getType()) ? returnType.getComponentType() : returnType; + returnType = QueryExecutionConverters.isSingleValue(returnType.getType()) ? returnType.getComponentType() + : returnType; for (Class type : types) { if (type.isAssignableFrom(returnType.getType())) { @@ -293,6 +294,6 @@ public class QueryMethod { } } - throw new IllegalStateException("Method has to have one of the following return types! " + Arrays.toString(types)); + throw new IllegalStateException("Method has to have one of the following return types! " + types.toString()); } -} \ No newline at end of file +} diff --git a/src/main/java/org/springframework/data/repository/util/QueryExecutionConverters.java b/src/main/java/org/springframework/data/repository/util/QueryExecutionConverters.java index cfdb43593..b7309b727 100644 --- a/src/main/java/org/springframework/data/repository/util/QueryExecutionConverters.java +++ b/src/main/java/org/springframework/data/repository/util/QueryExecutionConverters.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2016 the original author or authors. + * Copyright 2014-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,8 +21,6 @@ import lombok.AccessLevel; import lombok.Getter; import lombok.RequiredArgsConstructor; import lombok.Value; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Slice; import scala.Function0; import scala.Option; import scala.runtime.AbstractFunction0; @@ -43,6 +41,8 @@ import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.converter.GenericConverter; import org.springframework.core.convert.support.ConfigurableConversionService; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Slice; import org.springframework.scheduling.annotation.AsyncResult; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; @@ -62,11 +62,14 @@ import com.google.common.base.Optional; *
  • {@code java.util.concurrent.CompletableFuture}
  • *
  • {@code org.springframework.util.concurrent.ListenableFuture<}
  • *
  • {@code javaslang.control.Option} - as of 1.13
  • + *
  • {@code javaslang.collection.Seq}, {@code javaslang.collection.Map}, {@code javaslang.collection.Set} - as of *
  • Reactive wrappers supported by {@link ReactiveWrappers}
  • + * 1.13 * * * @author Oliver Gierke * @author Mark Paluch + * @author Maciek Opała * @since 1.8 * @see ReactiveWrappers */ @@ -191,6 +194,12 @@ public abstract class QueryExecutionConverters { return false; } + /** + * Returns the types that are supported on paginating query methods. Will include custom collection types of e.g. + * Javaslang. + * + * @return + */ public static Set> getAllowedPageableTypes() { return Collections.unmodifiableSet(ALLOWED_PAGEABLE_TYPES); } diff --git a/src/test/java/org/springframework/data/repository/query/QueryMethodUnitTests.java b/src/test/java/org/springframework/data/repository/query/QueryMethodUnitTests.java index 98b035d38..85457b61e 100644 --- a/src/test/java/org/springframework/data/repository/query/QueryMethodUnitTests.java +++ b/src/test/java/org/springframework/data/repository/query/QueryMethodUnitTests.java @@ -46,6 +46,7 @@ import org.springframework.data.util.Version; * * @author Oliver Gierke * @author Thomas Darimont + * @author Maciek Opała */ public class QueryMethodUnitTests { diff --git a/src/test/java/org/springframework/data/repository/util/QueryExecutionConvertersUnitTests.java b/src/test/java/org/springframework/data/repository/util/QueryExecutionConvertersUnitTests.java index 6f1de0a15..c3a379e4d 100644 --- a/src/test/java/org/springframework/data/repository/util/QueryExecutionConvertersUnitTests.java +++ b/src/test/java/org/springframework/data/repository/util/QueryExecutionConvertersUnitTests.java @@ -15,7 +15,7 @@ */ package org.springframework.data.repository.util; -import static org.hamcrest.CoreMatchers.*; +import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; import static org.springframework.data.repository.util.QueryExecutionConverters.*; @@ -41,10 +41,13 @@ import java.util.Set; import java.util.concurrent.CompletableFuture; import java.util.concurrent.Future; +import org.hamcrest.Matchers; import org.junit.Before; import org.junit.Test; import org.reactivestreams.Publisher; import org.springframework.core.convert.support.DefaultConversionService; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Slice; import org.springframework.util.ReflectionUtils; import org.springframework.util.concurrent.ListenableFuture; @@ -55,6 +58,7 @@ import com.google.common.base.Optional; * * @author Oliver Gierke * @author Mark Paluch + * @author Maciek Opała */ public class QueryExecutionConvertersUnitTests { @@ -273,11 +277,8 @@ public class QueryExecutionConvertersUnitTests { @Test // DATACMNS-1005 public void registersAllowedPageabletypes() { - final Set> allowedPageableTypes = QueryExecutionConverters.getAllowedPageableTypes(); - assertThat(allowedPageableTypes, hasItem(Page.class)); - assertThat(allowedPageableTypes, hasItem(Slice.class)); - assertThat(allowedPageableTypes, hasItem(List.class)); - assertThat(allowedPageableTypes, hasItem(Seq.class)); + Set> allowedPageableTypes = QueryExecutionConverters.getAllowedPageableTypes(); + assertThat(allowedPageableTypes, Matchers.> hasItems(Page.class, Slice.class, List.class, Seq.class)); } @SuppressWarnings("unchecked")