Skip to content

Commit ba87518

Browse files
committed
DATACMNS-867 - RepositoryInvoker.invokeQueryMethod(…) now returns an Optional.
1 parent 7c33bcf commit ba87518

File tree

3 files changed

+22
-19
lines changed

3 files changed

+22
-19
lines changed

src/main/java/org/springframework/data/querydsl/QuerydslRepositoryInvokerAdapter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ public <T> Optional<T> invokeFindOne(Serializable id) {
135135
* @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)
136136
*/
137137
@Override
138-
public Object invokeQueryMethod(Method method, MultiValueMap<String, ? extends Object> parameters, Pageable pageable,
139-
Sort sort) {
138+
public Optional<Object> invokeQueryMethod(Method method, MultiValueMap<String, ? extends Object> parameters,
139+
Pageable pageable, Sort sort) {
140140
return delegate.invokeQueryMethod(method, parameters, pageable, sort);
141141
}
142142

src/main/java/org/springframework/data/repository/support/ReflectionRepositoryInvoker.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -136,23 +136,12 @@ public boolean hasFindOneMethod() {
136136
* @see org.springframework.data.rest.core.invoke.RepositoryInvoker#invokeFindOne(java.io.Serializable)
137137
*/
138138
@Override
139-
@SuppressWarnings("unchecked")
140139
public <T> Optional<T> invokeFindOne(Serializable id) {
141140

142141
Method method = methods.getFindOneMethod()//
143142
.orElseThrow(() -> new IllegalStateException("Repository doesn't have a find-one-method declared!"));
144143

145-
Object invoke = invoke(method, convertId(id));
146-
147-
if (Optional.class.isInstance(invoke)) {
148-
return (Optional<T>) invoke;
149-
}
150-
151-
if (invoke == null) {
152-
return Optional.empty();
153-
}
154-
155-
return conversionService.convert(QueryExecutionConverters.unwrap(invoke), Optional.class);
144+
return returnAsOptional(invoke(method, convertId(id)));
156145
}
157146

158147
/*
@@ -190,8 +179,8 @@ public void invokeDelete(Serializable id) {
190179
* @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)
191180
*/
192181
@Override
193-
public Object invokeQueryMethod(Method method, MultiValueMap<String, ? extends Object> parameters, Pageable pageable,
194-
Sort sort) {
182+
public Optional<Object> invokeQueryMethod(Method method, MultiValueMap<String, ? extends Object> parameters,
183+
Pageable pageable, Sort sort) {
195184

196185
Assert.notNull(method, "Method must not be null!");
197186
Assert.notNull(parameters, "Parameters must not be null!");
@@ -200,7 +189,7 @@ public Object invokeQueryMethod(Method method, MultiValueMap<String, ? extends O
200189

201190
ReflectionUtils.makeAccessible(method);
202191

203-
return invoke(method, prepareParameters(method, parameters, pageable, sort));
192+
return returnAsOptional(invoke(method, prepareParameters(method, parameters, pageable, sort)));
204193
}
205194

206195
private Object[] prepareParameters(Method method, MultiValueMap<String, ? extends Object> rawParameters,
@@ -262,6 +251,20 @@ private <T> T invoke(Method method, Object... arguments) {
262251
return (T) ReflectionUtils.invokeMethod(method, repository, arguments);
263252
}
264253

254+
@SuppressWarnings("unchecked")
255+
private <T> Optional<T> returnAsOptional(Object source) {
256+
257+
if (Optional.class.isInstance(source)) {
258+
return (Optional<T>) source;
259+
}
260+
261+
if (source == null) {
262+
return Optional.empty();
263+
}
264+
265+
return conversionService.convert(QueryExecutionConverters.unwrap(source), Optional.class);
266+
}
267+
265268
/**
266269
* Converts the given id into the id type of the backing repository.
267270
*

src/main/java/org/springframework/data/repository/support/RepositoryInvoker.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,6 @@ public interface RepositoryInvoker extends RepositoryInvocationInformation {
9999
* @return the result of the invoked query method.
100100
* @since 1.11
101101
*/
102-
Object invokeQueryMethod(Method method, MultiValueMap<String, ? extends Object> parameters, Pageable pageable,
103-
Sort sort);
102+
Optional<Object> invokeQueryMethod(Method method, MultiValueMap<String, ? extends Object> parameters,
103+
Pageable pageable, Sort sort);
104104
}

0 commit comments

Comments
 (0)