Skip to content

Commit be44986

Browse files
committed
DATACMNS-1005 - Polishing.
Simplified type check by using Set as method signature to avoid unnecessary manual array wrapping. Simplification in the test assertions. A bit of Javadoc, corrected imports and author tags. Original pull request: #200.
1 parent b194f77 commit be44986

File tree

4 files changed

+30
-18
lines changed

4 files changed

+30
-18
lines changed

src/main/java/org/springframework/data/repository/query/QueryMethod.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2008-2015 the original author or authors.
2+
* Copyright 2008-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,7 +18,6 @@
1818
import static org.springframework.data.repository.util.ClassUtils.*;
1919

2020
import java.lang.reflect.Method;
21-
import java.util.Arrays;
2221
import java.util.Set;
2322

2423
import org.springframework.data.domain.Page;
@@ -40,6 +39,7 @@
4039
*
4140
* @author Oliver Gierke
4241
* @author Thomas Darimont
42+
* @author Maciek Opała
4343
*/
4444
public class QueryMethod {
4545

@@ -80,8 +80,7 @@ public QueryMethod(Method method, RepositoryMetadata metadata, ProjectionFactory
8080
if (hasParameterOfType(method, Pageable.class)) {
8181

8282
if (!isStreamQuery()) {
83-
final Set<Class<?>> allowedPageableTypes = QueryExecutionConverters.getAllowedPageableTypes();
84-
assertReturnTypeAssignable(method, allowedPageableTypes.toArray(new Class<?>[allowedPageableTypes.size()]));
83+
assertReturnTypeAssignable(method, QueryExecutionConverters.getAllowedPageableTypes());
8584
}
8685

8786
if (hasParameterOfType(method, Sort.class)) {
@@ -90,7 +89,8 @@ public QueryMethod(Method method, RepositoryMetadata metadata, ProjectionFactory
9089
}
9190
}
9291

93-
Assert.notNull(this.parameters, String.format("Parameters extracted from method '%s' must not be null!", method.getName()));
92+
Assert.notNull(this.parameters,
93+
String.format("Parameters extracted from method '%s' must not be null!", method.getName()));
9494

9595
if (isPageQuery()) {
9696
Assert.isTrue(this.parameters.hasPageableParameter(),
@@ -279,20 +279,21 @@ private static Class<? extends Object> potentiallyUnwrapReturnTypeFor(Method met
279279
return method.getReturnType();
280280
}
281281

282-
private static void assertReturnTypeAssignable(Method method, Class<?>... types) {
282+
private static void assertReturnTypeAssignable(Method method, Set<Class<?>> types) {
283283

284284
Assert.notNull(method, "Method must not be null!");
285285
Assert.notEmpty(types, "Types must not be null or empty!");
286286

287287
TypeInformation<?> returnType = ClassTypeInformation.fromReturnTypeOf(method);
288-
returnType = QueryExecutionConverters.isSingleValue(returnType.getType()) ? returnType.getComponentType() : returnType;
288+
returnType = QueryExecutionConverters.isSingleValue(returnType.getType()) ? returnType.getComponentType()
289+
: returnType;
289290

290291
for (Class<?> type : types) {
291292
if (type.isAssignableFrom(returnType.getType())) {
292293
return;
293294
}
294295
}
295296

296-
throw new IllegalStateException("Method has to have one of the following return types! " + Arrays.toString(types));
297+
throw new IllegalStateException("Method has to have one of the following return types! " + types.toString());
297298
}
298-
}
299+
}

src/main/java/org/springframework/data/repository/util/QueryExecutionConverters.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2016 the original author or authors.
2+
* Copyright 2014-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,8 +21,6 @@
2121
import lombok.Getter;
2222
import lombok.RequiredArgsConstructor;
2323
import lombok.Value;
24-
import org.springframework.data.domain.Page;
25-
import org.springframework.data.domain.Slice;
2624
import scala.Function0;
2725
import scala.Option;
2826
import scala.runtime.AbstractFunction0;
@@ -43,6 +41,8 @@
4341
import org.springframework.core.convert.converter.Converter;
4442
import org.springframework.core.convert.converter.GenericConverter;
4543
import org.springframework.core.convert.support.ConfigurableConversionService;
44+
import org.springframework.data.domain.Page;
45+
import org.springframework.data.domain.Slice;
4646
import org.springframework.scheduling.annotation.AsyncResult;
4747
import org.springframework.util.Assert;
4848
import org.springframework.util.ClassUtils;
@@ -62,11 +62,14 @@
6262
* <li>{@code java.util.concurrent.CompletableFuture}</li>
6363
* <li>{@code org.springframework.util.concurrent.ListenableFuture<}</li>
6464
* <li>{@code javaslang.control.Option} - as of 1.13</li>
65+
* <li>{@code javaslang.collection.Seq}, {@code javaslang.collection.Map}, {@code javaslang.collection.Set} - as of
6566
* <li>Reactive wrappers supported by {@link ReactiveWrappers}</li>
67+
* 1.13</li>
6668
* </ul>
6769
*
6870
* @author Oliver Gierke
6971
* @author Mark Paluch
72+
* @author Maciek Opała
7073
* @since 1.8
7174
* @see ReactiveWrappers
7275
*/
@@ -191,6 +194,12 @@ public static boolean isSingleValue(Class<?> type) {
191194
return false;
192195
}
193196

197+
/**
198+
* Returns the types that are supported on paginating query methods. Will include custom collection types of e.g.
199+
* Javaslang.
200+
*
201+
* @return
202+
*/
194203
public static Set<Class<?>> getAllowedPageableTypes() {
195204
return Collections.unmodifiableSet(ALLOWED_PAGEABLE_TYPES);
196205
}

src/test/java/org/springframework/data/repository/query/QueryMethodUnitTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
*
4747
* @author Oliver Gierke
4848
* @author Thomas Darimont
49+
* @author Maciek Opała
4950
*/
5051
public class QueryMethodUnitTests {
5152

src/test/java/org/springframework/data/repository/util/QueryExecutionConvertersUnitTests.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package org.springframework.data.repository.util;
1717

18-
import static org.hamcrest.CoreMatchers.*;
18+
import static org.hamcrest.Matchers.*;
1919
import static org.junit.Assert.*;
2020
import static org.springframework.data.repository.util.QueryExecutionConverters.*;
2121

@@ -41,10 +41,13 @@
4141
import java.util.concurrent.CompletableFuture;
4242
import java.util.concurrent.Future;
4343

44+
import org.hamcrest.Matchers;
4445
import org.junit.Before;
4546
import org.junit.Test;
4647
import org.reactivestreams.Publisher;
4748
import org.springframework.core.convert.support.DefaultConversionService;
49+
import org.springframework.data.domain.Page;
50+
import org.springframework.data.domain.Slice;
4851
import org.springframework.util.ReflectionUtils;
4952
import org.springframework.util.concurrent.ListenableFuture;
5053

@@ -55,6 +58,7 @@
5558
*
5659
* @author Oliver Gierke
5760
* @author Mark Paluch
61+
* @author Maciek Opała
5862
*/
5963
public class QueryExecutionConvertersUnitTests {
6064

@@ -273,11 +277,8 @@ public void unwrapsJavaslangCollectionsToJavaOnes() {
273277
@Test // DATACMNS-1005
274278
public void registersAllowedPageabletypes() {
275279

276-
final Set<Class<?>> allowedPageableTypes = QueryExecutionConverters.getAllowedPageableTypes();
277-
assertThat(allowedPageableTypes, hasItem(Page.class));
278-
assertThat(allowedPageableTypes, hasItem(Slice.class));
279-
assertThat(allowedPageableTypes, hasItem(List.class));
280-
assertThat(allowedPageableTypes, hasItem(Seq.class));
280+
Set<Class<?>> allowedPageableTypes = QueryExecutionConverters.getAllowedPageableTypes();
281+
assertThat(allowedPageableTypes, Matchers.<Class<?>> hasItems(Page.class, Slice.class, List.class, Seq.class));
281282
}
282283

283284
@SuppressWarnings("unchecked")

0 commit comments

Comments
 (0)