DATACMNS-867 - RepositoryInvoker.invokeQueryMethod(…) now returns an Optional.

This commit is contained in:
Oliver Gierke
2017-03-22 13:27:21 +01:00
parent 7c33bcf98a
commit ba875187cd
3 changed files with 22 additions and 19 deletions

View File

@@ -135,8 +135,8 @@ public class QuerydslRepositoryInvokerAdapter implements RepositoryInvoker {
* @see org.springframework.data.repository.support.RepositoryInvoker#invokeQueryMethod(java.lang.reflect.Method, org.springframework.util.MultiValueMap, org.springframework.data.domain.Pageable, org.springframework.data.domain.Sort)
*/
@Override
public Object invokeQueryMethod(Method method, MultiValueMap<String, ? extends Object> parameters, Pageable pageable,
Sort sort) {
public Optional<Object> invokeQueryMethod(Method method, MultiValueMap<String, ? extends Object> parameters,
Pageable pageable, Sort sort) {
return delegate.invokeQueryMethod(method, parameters, pageable, sort);
}

View File

@@ -136,23 +136,12 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker {
* @see org.springframework.data.rest.core.invoke.RepositoryInvoker#invokeFindOne(java.io.Serializable)
*/
@Override
@SuppressWarnings("unchecked")
public <T> Optional<T> invokeFindOne(Serializable id) {
Method method = methods.getFindOneMethod()//
.orElseThrow(() -> new IllegalStateException("Repository doesn't have a find-one-method declared!"));
Object invoke = invoke(method, convertId(id));
if (Optional.class.isInstance(invoke)) {
return (Optional<T>) invoke;
}
if (invoke == null) {
return Optional.empty();
}
return conversionService.convert(QueryExecutionConverters.unwrap(invoke), Optional.class);
return returnAsOptional(invoke(method, convertId(id)));
}
/*
@@ -190,8 +179,8 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker {
* @see org.springframework.data.rest.core.invoke.RepositoryInvoker#invokeQueryMethod(java.lang.reflect.Method, java.util.Map, org.springframework.data.domain.Pageable, org.springframework.data.domain.Sort)
*/
@Override
public Object invokeQueryMethod(Method method, MultiValueMap<String, ? extends Object> parameters, Pageable pageable,
Sort sort) {
public Optional<Object> invokeQueryMethod(Method method, MultiValueMap<String, ? extends Object> parameters,
Pageable pageable, Sort sort) {
Assert.notNull(method, "Method must not be null!");
Assert.notNull(parameters, "Parameters must not be null!");
@@ -200,7 +189,7 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker {
ReflectionUtils.makeAccessible(method);
return invoke(method, prepareParameters(method, parameters, pageable, sort));
return returnAsOptional(invoke(method, prepareParameters(method, parameters, pageable, sort)));
}
private Object[] prepareParameters(Method method, MultiValueMap<String, ? extends Object> rawParameters,
@@ -262,6 +251,20 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker {
return (T) ReflectionUtils.invokeMethod(method, repository, arguments);
}
@SuppressWarnings("unchecked")
private <T> Optional<T> returnAsOptional(Object source) {
if (Optional.class.isInstance(source)) {
return (Optional<T>) source;
}
if (source == null) {
return Optional.empty();
}
return conversionService.convert(QueryExecutionConverters.unwrap(source), Optional.class);
}
/**
* Converts the given id into the id type of the backing repository.
*

View File

@@ -99,6 +99,6 @@ public interface RepositoryInvoker extends RepositoryInvocationInformation {
* @return the result of the invoked query method.
* @since 1.11
*/
Object invokeQueryMethod(Method method, MultiValueMap<String, ? extends Object> parameters, Pageable pageable,
Sort sort);
Optional<Object> invokeQueryMethod(Method method, MultiValueMap<String, ? extends Object> parameters,
Pageable pageable, Sort sort);
}