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.
This commit is contained in:
Oliver Gierke
2017-03-09 10:58:19 +01:00
parent b194f7794d
commit be4498659d
4 changed files with 30 additions and 18 deletions

View File

@@ -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<Class<?>> 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<Class<?>> 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());
}
}
}

View File

@@ -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;
* <li>{@code java.util.concurrent.CompletableFuture}</li>
* <li>{@code org.springframework.util.concurrent.ListenableFuture<}</li>
* <li>{@code javaslang.control.Option} - as of 1.13</li>
* <li>{@code javaslang.collection.Seq}, {@code javaslang.collection.Map}, {@code javaslang.collection.Set} - as of
* <li>Reactive wrappers supported by {@link ReactiveWrappers}</li>
* 1.13</li>
* </ul>
*
* @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<Class<?>> getAllowedPageableTypes() {
return Collections.unmodifiableSet(ALLOWED_PAGEABLE_TYPES);
}