Skip to content

Commit 79a74bc

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 112bd60 commit 79a74bc

File tree

4 files changed

+30
-20
lines changed

4 files changed

+30
-20
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.
@@ -20,8 +20,6 @@
2020
import lombok.AccessLevel;
2121
import lombok.RequiredArgsConstructor;
2222
import lombok.Value;
23-
import org.springframework.data.domain.Page;
24-
import org.springframework.data.domain.Slice;
2523
import scala.Function0;
2624
import scala.Option;
2725
import scala.runtime.AbstractFunction0;
@@ -41,6 +39,8 @@
4139
import org.springframework.core.convert.converter.Converter;
4240
import org.springframework.core.convert.converter.GenericConverter;
4341
import org.springframework.core.convert.support.ConfigurableConversionService;
42+
import org.springframework.data.domain.Page;
43+
import org.springframework.data.domain.Slice;
4444
import org.springframework.scheduling.annotation.AsyncResult;
4545
import org.springframework.util.Assert;
4646
import org.springframework.util.ClassUtils;
@@ -60,10 +60,13 @@
6060
* <li>{@code java.util.concurrent.CompletableFuture}</li>
6161
* <li>{@code org.springframework.util.concurrent.ListenableFuture<}</li>
6262
* <li>{@code javaslang.control.Option} - as of 1.13</li>
63+
* <li>{@code javaslang.collection.Seq}, {@code javaslang.collection.Map}, {@code javaslang.collection.Set} - as of
64+
* 1.13</li>
6365
* </ul>
6466
*
6567
* @author Oliver Gierke
6668
* @author Mark Paluch
69+
* @author Maciek Opała
6770
* @since 1.8
6871
*/
6972
public abstract class QueryExecutionConverters {
@@ -152,6 +155,12 @@ public static boolean isSingleValue(Class<?> type) {
152155
return false;
153156
}
154157

158+
/**
159+
* Returns the types that are supported on paginating query methods. Will include custom collection types of e.g.
160+
* Javaslang.
161+
*
162+
* @return
163+
*/
155164
public static Set<Class<?>> getAllowedPageableTypes() {
156165
return Collections.unmodifiableSet(ALLOWED_PAGEABLE_TYPES);
157166
}

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 & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,14 @@
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

2222
import javaslang.collection.HashMap;
2323
import javaslang.collection.HashSet;
2424
import javaslang.collection.Seq;
2525
import javaslang.collection.Traversable;
26-
import org.springframework.data.domain.Page;
27-
import org.springframework.data.domain.Slice;
2826
import scala.Option;
2927

3028
import java.lang.reflect.Method;
@@ -36,9 +34,12 @@
3634
import java.util.concurrent.CompletableFuture;
3735
import java.util.concurrent.Future;
3836

37+
import org.hamcrest.Matchers;
3938
import org.junit.Before;
4039
import org.junit.Test;
4140
import org.springframework.core.convert.support.DefaultConversionService;
41+
import org.springframework.data.domain.Page;
42+
import org.springframework.data.domain.Slice;
4243
import org.springframework.util.ReflectionUtils;
4344
import org.springframework.util.concurrent.ListenableFuture;
4445

@@ -49,6 +50,7 @@
4950
*
5051
* @author Oliver Gierke
5152
* @author Mark Paluch
53+
* @author Maciek Opała
5254
*/
5355
public class QueryExecutionConvertersUnitTests {
5456

@@ -225,11 +227,8 @@ public void unwrapsJavaslangCollectionsToJavaOnes() {
225227
@Test // DATACMNS-1005
226228
public void registersAllowedPageabletypes() {
227229

228-
final Set<Class<?>> allowedPageableTypes = QueryExecutionConverters.getAllowedPageableTypes();
229-
assertThat(allowedPageableTypes, hasItem(Page.class));
230-
assertThat(allowedPageableTypes, hasItem(Slice.class));
231-
assertThat(allowedPageableTypes, hasItem(List.class));
232-
assertThat(allowedPageableTypes, hasItem(Seq.class));
230+
Set<Class<?>> allowedPageableTypes = QueryExecutionConverters.getAllowedPageableTypes();
231+
assertThat(allowedPageableTypes, Matchers.<Class<?>> hasItems(Page.class, Slice.class, List.class, Seq.class));
233232
}
234233

235234
@SuppressWarnings("unchecked")

0 commit comments

Comments
 (0)