Skip to content

Commit 8ebe52d

Browse files
committed
Complete Vavr collection detection on TypeInformation and CustomConversions.
Moved Vavr collection converters into a type in the utility package. Register the converters via CustomConversions.registerConvertersIn(…) to make sure that all the Spring Data object mapping converters automatically benefit from a ConversionService that is capable of translating between Java-native collections and Vavr ones. Issue #2511.
1 parent 23c153a commit 8ebe52d

File tree

6 files changed

+105
-65
lines changed

6 files changed

+105
-65
lines changed

src/main/java/org/springframework/data/convert/CustomConversions.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2021 the original author or authors.
2+
* Copyright 2011-2022 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.
@@ -16,24 +16,14 @@
1616
package org.springframework.data.convert;
1717

1818
import java.lang.annotation.Annotation;
19-
import java.util.ArrayList;
20-
import java.util.Arrays;
21-
import java.util.Collection;
22-
import java.util.Collections;
23-
import java.util.HashSet;
24-
import java.util.LinkedHashSet;
25-
import java.util.List;
26-
import java.util.Map;
27-
import java.util.Optional;
28-
import java.util.Set;
19+
import java.util.*;
2920
import java.util.concurrent.ConcurrentHashMap;
3021
import java.util.function.Function;
3122
import java.util.function.Predicate;
3223
import java.util.stream.Collectors;
3324

3425
import org.apache.commons.logging.Log;
3526
import org.apache.commons.logging.LogFactory;
36-
3727
import org.springframework.core.GenericTypeResolver;
3828
import org.springframework.core.annotation.AnnotationUtils;
3929
import org.springframework.core.convert.converter.Converter;
@@ -46,6 +36,7 @@
4636
import org.springframework.data.mapping.model.SimpleTypeHolder;
4737
import org.springframework.data.util.Predicates;
4838
import org.springframework.data.util.Streamable;
39+
import org.springframework.data.util.VavrCollectionConverters;
4940
import org.springframework.lang.Nullable;
5041
import org.springframework.util.Assert;
5142
import org.springframework.util.ObjectUtils;
@@ -176,6 +167,7 @@ public void registerConvertersIn(ConverterRegistry conversionService) {
176167
Assert.notNull(conversionService, "ConversionService must not be null!");
177168

178169
converters.forEach(it -> registerConverterIn(it, conversionService));
170+
VavrCollectionConverters.getConvertersToRegister().forEach(it -> registerConverterIn(it, conversionService));
179171
}
180172

181173
/**

src/main/java/org/springframework/data/repository/util/QueryExecutionConverters.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2021 the original author or authors.
2+
* Copyright 2014-2022 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.
@@ -43,6 +43,7 @@
4343
import org.springframework.data.util.StreamUtils;
4444
import org.springframework.data.util.Streamable;
4545
import org.springframework.data.util.TypeInformation;
46+
import org.springframework.data.util.VavrCollectionConverters;
4647
import org.springframework.lang.Nullable;
4748
import org.springframework.scheduling.annotation.AsyncResult;
4849
import org.springframework.util.Assert;
@@ -99,7 +100,7 @@ public abstract class QueryExecutionConverters {
99100

100101
if (VAVR_PRESENT) {
101102

102-
WRAPPER_TYPES.add(VavrCollections.ToJavaConverter.INSTANCE.getWrapperType());
103+
WRAPPER_TYPES.add(VavrTraversableUnwrapper.INSTANCE.getWrapperType());
103104
UNWRAPPERS.add(VavrTraversableUnwrapper.INSTANCE);
104105

105106
// Try support
@@ -196,7 +197,7 @@ public static void registerConvertersIn(ConfigurableConversionService conversion
196197
NullableWrapperConverters.registerConvertersIn(conversionService);
197198

198199
if (VAVR_PRESENT) {
199-
conversionService.addConverter(VavrCollections.FromJavaConverter.INSTANCE);
200+
conversionService.addConverter(VavrCollectionConverters.FromJavaConverter.INSTANCE);
200201
}
201202

202203
conversionService.addConverter(new NullableWrapperToCompletableFutureConverter());
@@ -391,19 +392,25 @@ private enum VavrTraversableUnwrapper implements Converter<Object, Object> {
391392

392393
INSTANCE;
393394

395+
private static final TypeDescriptor OBJECT_DESCRIPTOR = TypeDescriptor.valueOf(Object.class);
396+
394397
@Nullable
395398
@Override
396-
@SuppressWarnings("unchecked")
399+
@SuppressWarnings("null")
397400
public Object convert(Object source) {
398401

399402
if (source instanceof io.vavr.collection.Traversable) {
400-
return VavrCollections.ToJavaConverter.INSTANCE.convert(source);
403+
return VavrCollectionConverters.ToJavaConverter.INSTANCE //
404+
.convert(source, TypeDescriptor.forObject(source), OBJECT_DESCRIPTOR);
401405
}
402406

403407
return source;
404408
}
405-
}
406409

410+
public WrapperType getWrapperType() {
411+
return WrapperType.multiValue(io.vavr.collection.Traversable.class);
412+
}
413+
}
407414

408415
private static class IterableToStreamableConverter implements ConditionalGenericConverter {
409416

src/main/java/org/springframework/data/util/TypeDiscoverer.java

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2021 the original author or authors.
2+
* Copyright 2011-2022 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.
@@ -23,16 +23,7 @@
2323
import java.lang.reflect.Type;
2424
import java.lang.reflect.TypeVariable;
2525
import java.lang.reflect.WildcardType;
26-
import java.util.ArrayList;
27-
import java.util.Arrays;
28-
import java.util.Collection;
29-
import java.util.Collections;
30-
import java.util.HashMap;
31-
import java.util.HashSet;
32-
import java.util.List;
33-
import java.util.Map;
34-
import java.util.Optional;
35-
import java.util.Set;
26+
import java.util.*;
3627
import java.util.concurrent.ConcurrentHashMap;
3728
import java.util.stream.Collectors;
3829

@@ -347,23 +338,10 @@ public boolean isCollectionLike() {
347338

348339
return rawType.isArray() //
349340
|| Iterable.class.equals(rawType) //
350-
|| Streamable.class.isAssignableFrom(rawType)
341+
|| Streamable.class.isAssignableFrom(rawType) //
351342
|| isCollection();
352343
}
353344

354-
private boolean isCollection() {
355-
356-
Class<S> type = getType();
357-
358-
for (Class<?> collectionType : COLLECTION_TYPES) {
359-
if (collectionType.isAssignableFrom(type)) {
360-
return true;
361-
}
362-
}
363-
364-
return false;
365-
}
366-
367345
@Nullable
368346
public final TypeInformation<?> getComponentType() {
369347
return componentType.orElse(null);
@@ -386,7 +364,7 @@ protected TypeInformation<?> doGetComponentType() {
386364
return getTypeArgument(Iterable.class, 0);
387365
}
388366

389-
if(isNullableWrapper()) {
367+
if (isNullableWrapper()) {
390368
return getTypeArgument(rawType, 0);
391369
}
392370

@@ -541,6 +519,24 @@ public int hashCode() {
541519
return hashCode;
542520
}
543521

522+
/**
523+
* Returns whether the current type is considered a collection.
524+
*
525+
* @return
526+
*/
527+
private boolean isCollection() {
528+
529+
var type = getType();
530+
531+
for (Class<?> collectionType : COLLECTION_TYPES) {
532+
if (collectionType.isAssignableFrom(type)) {
533+
return true;
534+
}
535+
}
536+
537+
return false;
538+
}
539+
544540
/**
545541
* A synthetic {@link ParameterizedType}.
546542
*

src/main/java/org/springframework/data/repository/util/VavrCollections.java renamed to src/main/java/org/springframework/data/util/VavrCollectionConverters.java

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 the original author or authors.
2+
* Copyright 2022 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.
@@ -13,46 +13,79 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.data.repository.util;
16+
package org.springframework.data.util;
1717

1818
import io.vavr.collection.LinkedHashMap;
1919
import io.vavr.collection.LinkedHashSet;
2020
import io.vavr.collection.Traversable;
2121

22+
import java.util.Arrays;
2223
import java.util.Collection;
2324
import java.util.Collections;
2425
import java.util.HashSet;
2526
import java.util.List;
2627
import java.util.Map;
2728
import java.util.Set;
29+
import java.util.stream.Collectors;
2830

2931
import org.springframework.core.convert.TypeDescriptor;
3032
import org.springframework.core.convert.converter.ConditionalGenericConverter;
31-
import org.springframework.core.convert.converter.Converter;
32-
import org.springframework.data.repository.util.QueryExecutionConverters.WrapperType;
3333
import org.springframework.lang.NonNull;
3434
import org.springframework.lang.Nullable;
35+
import org.springframework.util.ClassUtils;
3536

3637
/**
3738
* Converter implementations to map from and to Vavr collections.
3839
*
3940
* @author Oliver Gierke
4041
* @author Christoph Strobl
41-
* @since 2.0
42+
* @since 2.7
4243
*/
43-
class VavrCollections {
44+
public class VavrCollectionConverters {
4445

45-
public enum ToJavaConverter implements Converter<Object, Object> {
46+
private static final boolean VAVR_PRESENT = ClassUtils.isPresent("io.vavr.control.Option",
47+
NullableWrapperConverters.class.getClassLoader());
4648

47-
INSTANCE;
49+
public static Collection<Object> getConvertersToRegister() {
4850

49-
public WrapperType getWrapperType() {
50-
return WrapperType.multiValue(io.vavr.collection.Traversable.class);
51+
if (!VAVR_PRESENT) {
52+
return Collections.emptyList();
5153
}
5254

55+
return Arrays.asList(ToJavaConverter.INSTANCE, FromJavaConverter.INSTANCE);
56+
}
57+
58+
public enum ToJavaConverter implements ConditionalGenericConverter {
59+
60+
INSTANCE;
61+
62+
private static final TypeDescriptor TRAVERSAL_TYPE = TypeDescriptor.valueOf(Traversable.class);
63+
private static final Set<Class<?>> COLLECTIONS_AND_MAP = new HashSet<>(
64+
Arrays.asList(Collection.class, List.class, Set.class, Map.class));
65+
5366
@NonNull
5467
@Override
55-
public Object convert(Object source) {
68+
public Set<ConvertiblePair> getConvertibleTypes() {
69+
70+
return COLLECTIONS_AND_MAP.stream()
71+
.map(it -> new ConvertiblePair(Traversable.class, it))
72+
.collect(Collectors.toSet());
73+
}
74+
75+
@Override
76+
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
77+
78+
return sourceType.isAssignableTo(TRAVERSAL_TYPE)
79+
&& COLLECTIONS_AND_MAP.contains(targetType.getType());
80+
}
81+
82+
@Nullable
83+
@Override
84+
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
85+
86+
if (source == null) {
87+
return null;
88+
}
5689

5790
if (source instanceof io.vavr.collection.Seq) {
5891
return ((io.vavr.collection.Seq<?>) source).asJava();

src/test/java/org/springframework/data/convert/CustomConversionsUnitTests.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2021 the original author or authors.
2+
* Copyright 2011-2022 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.
@@ -22,14 +22,14 @@
2222
import java.util.Arrays;
2323
import java.util.Collections;
2424
import java.util.Date;
25+
import java.util.List;
2526
import java.util.Locale;
2627
import java.util.Map;
2728
import java.util.function.Predicate;
2829

2930
import org.jmolecules.ddd.types.Association;
3031
import org.jmolecules.ddd.types.Identifier;
3132
import org.junit.jupiter.api.Test;
32-
3333
import org.springframework.aop.framework.ProxyFactory;
3434
import org.springframework.core.convert.converter.Converter;
3535
import org.springframework.core.convert.converter.ConverterFactory;
@@ -261,6 +261,18 @@ void addsIdentifierConvertersByDefault() {
261261
assertThat(conversions.hasCustomReadTarget(String.class, Identifier.class)).isTrue();
262262
}
263263

264+
@Test // GH-2511
265+
void registersVavrConverters() {
266+
267+
ConfigurableConversionService conversionService = new DefaultConversionService();
268+
269+
new CustomConversions(StoreConversions.NONE, Collections.emptyList())
270+
.registerConvertersIn(conversionService);
271+
272+
assertThat(conversionService.canConvert(io.vavr.collection.List.class, List.class)).isTrue();
273+
assertThat(conversionService.canConvert(List.class, io.vavr.collection.List.class)).isTrue();
274+
}
275+
264276
private static Class<?> createProxyTypeFor(Class<?> type) {
265277

266278
var factory = new ProxyFactory();

src/test/java/org/springframework/data/util/TypeDiscovererUnitTests.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,34 +179,34 @@ void detectsSubTypes() {
179179
assertThat(type.isSubTypeOf(String.class)).isFalse();
180180
}
181181

182-
@Test
182+
@Test // #2511
183183
void considerVavrMapToBeAMap() {
184184

185-
TypeInformation<io.vavr.collection.Map> type = from(io.vavr.collection.Map.class);
185+
var type = from(io.vavr.collection.Map.class);
186186

187187
assertThat(type.isMap()).isTrue();
188188
}
189189

190-
@Test
190+
@Test // #2511
191191
void considerVavrSetToBeCollectionLike() {
192192

193-
TypeInformation<io.vavr.collection.Set> type = from(io.vavr.collection.Set.class);
193+
var type = from(io.vavr.collection.Set.class);
194194

195195
assertThat(type.isCollectionLike()).isTrue();
196196
}
197197

198-
@Test
198+
@Test // #2511
199199
void considerVavrSeqToBeCollectionLike() {
200200

201-
TypeInformation<io.vavr.collection.Seq> type = from(io.vavr.collection.Seq.class);
201+
var type = from(io.vavr.collection.Seq.class);
202202

203203
assertThat(type.isCollectionLike()).isTrue();
204204
}
205205

206-
@Test
206+
@Test // #2511
207207
void considerVavrListToBeCollectionLike() {
208208

209-
TypeInformation<io.vavr.collection.List> type = from(io.vavr.collection.List.class);
209+
var type = from(io.vavr.collection.List.class);
210210

211211
assertThat(type.isCollectionLike()).isTrue();
212212
}

0 commit comments

Comments
 (0)