From 79286440c50123e6d1b49ecd0d6e53c960fab410 Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Tue, 4 Dec 2018 14:16:02 +0100 Subject: [PATCH] DATACMNS-1435 - Fixed Vavr Map component and value type detection. --- .../data/util/TypeDiscoverer.java | 31 ++++++++++--------- .../util/ClassTypeInformationUnitTests.java | 15 +++++++-- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/springframework/data/util/TypeDiscoverer.java b/src/main/java/org/springframework/data/util/TypeDiscoverer.java index e85f37b15..569c0893b 100644 --- a/src/main/java/org/springframework/data/util/TypeDiscoverer.java +++ b/src/main/java/org/springframework/data/util/TypeDiscoverer.java @@ -30,15 +30,7 @@ import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.lang.reflect.TypeVariable; import java.lang.reflect.WildcardType; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; import java.util.concurrent.ConcurrentHashMap; import org.springframework.beans.BeanUtils; @@ -58,14 +50,10 @@ class TypeDiscoverer implements TypeInformation { static { - ClassLoader classLoader = TypeDiscoverer.class.getClassLoader(); - Set> mapTypes = new HashSet>(); mapTypes.add(Map.class); - - try { - mapTypes.add(ClassUtils.forName("javaslang.collection.Map", classLoader)); - } catch (ClassNotFoundException o_O) {} + tryToAddClassTo("javaslang.collection.Map", mapTypes); + tryToAddClassTo("io.vavr.collection.Map", mapTypes); MAP_TYPES = Collections.unmodifiableSet(mapTypes); } @@ -583,6 +571,19 @@ class TypeDiscoverer implements TypeInformation { return hashCode; } + /** + * Tries to load the class with the given name and adds it to the given {@link Set} of classes if present. + * + * @param className must not be {@literal null} or empty. + * @param classes must not be {@literal null}. + */ + private static final void tryToAddClassTo(String className, Set> classes) { + + try { + classes.add(ClassUtils.forName(className, TypeDiscoverer.class.getClassLoader())); + } catch (ClassNotFoundException o_O) {} + } + /** * A synthetic {@link ParameterizedType}. * diff --git a/src/test/java/org/springframework/data/util/ClassTypeInformationUnitTests.java b/src/test/java/org/springframework/data/util/ClassTypeInformationUnitTests.java index 2f17a0574..789a98a2b 100644 --- a/src/test/java/org/springframework/data/util/ClassTypeInformationUnitTests.java +++ b/src/test/java/org/springframework/data/util/ClassTypeInformationUnitTests.java @@ -386,7 +386,16 @@ public class ClassTypeInformationUnitTests { @Test // DATACMNS-940 public void detectsJavaslangMapComponentAndValueType() { - ClassTypeInformation information = ClassTypeInformation.from(SampleMap.class); + ClassTypeInformation information = ClassTypeInformation.from(JavaslangSampleMap.class); + + assertThat(information.getComponentType().getType(), is(typeCompatibleWith(String.class))); + assertThat(information.getMapValueType().getType(), is(typeCompatibleWith(Integer.class))); + } + + @Test // DATACMNS-1434 + public void detectsVavrMapComponentAndValueType() { + + ClassTypeInformation information = ClassTypeInformation.from(VavrSampleMap.class); assertThat(information.getComponentType().getType(), is(typeCompatibleWith(String.class))); assertThat(information.getMapValueType().getType(), is(typeCompatibleWith(Integer.class))); @@ -605,7 +614,9 @@ public class ClassTypeInformationUnitTests { static interface SampleTraversable extends Traversable {} - static interface SampleMap extends javaslang.collection.Map {} + static interface JavaslangSampleMap extends javaslang.collection.Map {} + + static interface VavrSampleMap extends io.vavr.collection.Map {} // DATACMNS-1138