Skip to content

Commit 7ea8c9d

Browse files
committed
DATACMNS-836 - Polishing.
RepositoryMetadata now exposes an ….isReactiveRepository() that's implemented by inspecting the backing repository interface for any reactive wrapper type appearing in method signatures. That allows us to move down the use of a ConversionService down to ReactiveRepositoryInformation.WrapperConversionMatch. Made a couple of methods static that could be. Simplified some logic using streams. A bit better wording in assertions. Some formatting when using streams.
1 parent c8ea914 commit 7ea8c9d

17 files changed

+352
-273
lines changed

src/main/java/org/springframework/data/repository/core/RepositoryMetadata.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2015 the original author or authors.
2+
* Copyright 2011-2016 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.
@@ -83,4 +83,12 @@ public interface RepositoryMetadata {
8383
* @since 1.11
8484
*/
8585
Set<Class<?>> getAlternativeDomainTypes();
86+
87+
/**
88+
* Returns whether the repository is a reactive one, i.e. if it uses reactive types in one of its methods.
89+
*
90+
* @return
91+
* @since 2.0
92+
*/
93+
boolean isReactiveRepository();
8694
}

src/main/java/org/springframework/data/repository/core/support/AbstractRepositoryMetadata.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2015 the original author or authors.
2+
* Copyright 2011-2016 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.
@@ -25,6 +25,7 @@
2525
import org.springframework.data.repository.core.CrudMethods;
2626
import org.springframework.data.repository.core.RepositoryMetadata;
2727
import org.springframework.data.repository.util.QueryExecutionConverters;
28+
import org.springframework.data.repository.util.ReactiveWrappers;
2829
import org.springframework.data.util.ClassTypeInformation;
2930
import org.springframework.data.util.ReflectionUtils;
3031
import org.springframework.data.util.TypeInformation;
@@ -126,6 +127,15 @@ public Set<Class<?>> getAlternativeDomainTypes() {
126127
return Collections.emptySet();
127128
}
128129

130+
/*
131+
* (non-Javadoc)
132+
* @see org.springframework.data.repository.core.RepositoryMetadata#isReactiveRepository()
133+
*/
134+
@Override
135+
public boolean isReactiveRepository() {
136+
return ReactiveWrappers.usesReactiveType(repositoryInterface);
137+
}
138+
129139
/**
130140
* Recursively unwraps well known wrapper types from the given {@link TypeInformation}.
131141
*

src/main/java/org/springframework/data/repository/core/support/DefaultRepositoryInformation.java

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,12 @@
4747
*
4848
* @author Oliver Gierke
4949
* @author Thomas Darimont
50+
* @author Mark Paluch
5051
*/
5152
class DefaultRepositoryInformation implements RepositoryInformation {
5253

53-
@SuppressWarnings("rawtypes") private static final TypeVariable<Class<Repository>>[] PARAMETERS = Repository.class
54-
.getTypeParameters();
54+
@SuppressWarnings("rawtypes") //
55+
private static final TypeVariable<Class<Repository>>[] PARAMETERS = Repository.class.getTypeParameters();
5556
private static final String DOMAIN_TYPE_NAME = PARAMETERS[0].getName();
5657
private static final String ID_TYPE_NAME = PARAMETERS[1].getName();
5758

@@ -334,42 +335,13 @@ public Set<Class<?>> getAlternativeDomainTypes() {
334335
return metadata.getAlternativeDomainTypes();
335336
}
336337

337-
/**
338-
* Checks the given method's parameters to match the ones of the given base class method. Matches generic arguments
339-
* against the ones bound in the given repository interface.
340-
*
341-
* @param method
342-
* @param baseClassMethod
343-
* @return
338+
/*
339+
* (non-Javadoc)
340+
* @see org.springframework.data.repository.core.RepositoryMetadata#isReactiveRepository()
344341
*/
345-
private boolean parametersMatch(Method method, Method baseClassMethod) {
346-
347-
Class<?>[] methodParameterTypes = method.getParameterTypes();
348-
Type[] genericTypes = baseClassMethod.getGenericParameterTypes();
349-
Class<?>[] types = baseClassMethod.getParameterTypes();
350-
351-
for (int i = 0; i < genericTypes.length; i++) {
352-
353-
Type genericType = genericTypes[i];
354-
Class<?> type = types[i];
355-
MethodParameter parameter = new MethodParameter(method, i);
356-
Class<?> parameterType = resolveParameterType(parameter, metadata.getRepositoryInterface());
357-
358-
if (genericType instanceof TypeVariable<?>) {
359-
360-
if (!matchesGenericType((TypeVariable<?>) genericType, ResolvableType.forMethodParameter(parameter))) {
361-
return false;
362-
}
363-
364-
continue;
365-
}
366-
367-
if (!type.isAssignableFrom(parameterType) || !type.equals(methodParameterTypes[i])) {
368-
return false;
369-
}
370-
}
371-
372-
return true;
342+
@Override
343+
public boolean isReactiveRepository() {
344+
return metadata.isReactiveRepository();
373345
}
374346

375347
/**
@@ -408,4 +380,42 @@ protected boolean matchesGenericType(TypeVariable<?> variable, ResolvableType pa
408380

409381
return false;
410382
}
383+
384+
/**
385+
* Checks the given method's parameters to match the ones of the given base class method. Matches generic arguments
386+
* against the ones bound in the given repository interface.
387+
*
388+
* @param method
389+
* @param baseClassMethod
390+
* @return
391+
*/
392+
private boolean parametersMatch(Method method, Method baseClassMethod) {
393+
394+
Class<?>[] methodParameterTypes = method.getParameterTypes();
395+
Type[] genericTypes = baseClassMethod.getGenericParameterTypes();
396+
Class<?>[] types = baseClassMethod.getParameterTypes();
397+
398+
for (int i = 0; i < genericTypes.length; i++) {
399+
400+
Type genericType = genericTypes[i];
401+
Class<?> type = types[i];
402+
MethodParameter parameter = new MethodParameter(method, i);
403+
Class<?> parameterType = resolveParameterType(parameter, metadata.getRepositoryInterface());
404+
405+
if (genericType instanceof TypeVariable<?>) {
406+
407+
if (!matchesGenericType((TypeVariable<?>) genericType, ResolvableType.forMethodParameter(parameter))) {
408+
return false;
409+
}
410+
411+
continue;
412+
}
413+
414+
if (!type.isAssignableFrom(parameterType) || !type.equals(methodParameterTypes[i])) {
415+
return false;
416+
}
417+
}
418+
419+
return true;
420+
}
411421
}

src/main/java/org/springframework/data/repository/core/support/QueryExecutionResultHandler.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014 the original author or authors.
2+
* Copyright 2014-2016 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.
@@ -72,14 +72,15 @@ public Object postProcessInvocationResult(Object result, TypeDescriptor returnTy
7272

7373
TypeDescriptor targetType = TypeDescriptor.valueOf(expectedReturnType);
7474

75-
if(conversionService.canConvert(WRAPPER_TYPE, returnTypeDescriptor)
76-
&& !conversionService.canBypassConvert(WRAPPER_TYPE, targetType)) {
75+
if (conversionService.canConvert(WRAPPER_TYPE, returnTypeDescriptor)
76+
&& !conversionService.canBypassConvert(WRAPPER_TYPE, targetType)) {
7777

7878
return conversionService.convert(new NullableWrapper(result), expectedReturnType);
7979
}
8080

81-
if(result != null && conversionService.canConvert(TypeDescriptor.valueOf(result.getClass()), returnTypeDescriptor)
82-
&& !conversionService.canBypassConvert(TypeDescriptor.valueOf(result.getClass()), targetType)) {
81+
if (result != null
82+
&& conversionService.canConvert(TypeDescriptor.valueOf(result.getClass()), returnTypeDescriptor)
83+
&& !conversionService.canBypassConvert(TypeDescriptor.valueOf(result.getClass()), targetType)) {
8384

8485
return conversionService.convert(result, expectedReturnType);
8586
}

0 commit comments

Comments
 (0)