From 931a697ec54b90dd56c88ece5e8d23a4b0b91b73 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Thu, 13 Feb 2014 12:06:26 +0100 Subject: [PATCH] DATACMNS-440 - Fixed map value type resolving for Map value types. Previously the map value type resolving algorithm checked the value type to be of type Map again to shortcut the resolution. We now weakened this to an assignment check and eagerly resolve the generic type if its bound on exactly that level already. If no concrete type argument can be found, we fall back to the general generics resolution mechanism. --- .../util/ParameterizedTypeInformation.java | 8 ++++-- .../util/ClassTypeInformationUnitTests.java | 26 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/springframework/data/util/ParameterizedTypeInformation.java b/src/main/java/org/springframework/data/util/ParameterizedTypeInformation.java index c466c275f..42d750ae7 100644 --- a/src/main/java/org/springframework/data/util/ParameterizedTypeInformation.java +++ b/src/main/java/org/springframework/data/util/ParameterizedTypeInformation.java @@ -56,9 +56,13 @@ class ParameterizedTypeInformation extends ParentTypeAwareTypeInformation @SuppressWarnings("deprecation") public TypeInformation getMapValueType() { - if (Map.class.equals(getType())) { + if (Map.class.isAssignableFrom(getType())) { + Type[] arguments = type.getActualTypeArguments(); - return createInfo(arguments[1]); + + if (arguments.length > 1) { + return createInfo(arguments[1]); + } } Class rawType = getType(); diff --git a/src/test/java/org/springframework/data/util/ClassTypeInformationUnitTests.java b/src/test/java/org/springframework/data/util/ClassTypeInformationUnitTests.java index 9975d097f..e9b87faf5 100644 --- a/src/test/java/org/springframework/data/util/ClassTypeInformationUnitTests.java +++ b/src/test/java/org/springframework/data/util/ClassTypeInformationUnitTests.java @@ -25,6 +25,7 @@ import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.SortedMap; import org.hamcrest.Matchers; import org.junit.Test; @@ -296,6 +297,26 @@ public class ClassTypeInformationUnitTests { assertThat(from(MyIterable.class).getComponentType(), is(notNullValue())); } + /** + * @see DATACMNS-440 + */ + @Test + public void detectsSpecialMapAsMapValueType() { + + TypeInformation information = ClassTypeInformation.from(SuperGenerics.class); + + TypeInformation propertyInformation = information.getProperty("seriously"); + assertThat(propertyInformation.getType(), is((Object) SortedMap.class)); + + TypeInformation mapValueType = propertyInformation.getMapValueType(); + assertThat(mapValueType.getType(), is((Object) SortedMap.class)); + assertThat(mapValueType.getComponentType().getType(), is((Object) String.class)); + + TypeInformation nestedValueType = mapValueType.getMapValueType(); + assertThat(nestedValueType.getType(), is((Object) List.class)); + assertThat(nestedValueType.getComponentType().getType(), is((Object) Person.class)); + } + static class StringMapContainer extends MapContainer { } @@ -425,4 +446,9 @@ public class ClassTypeInformationUnitTests { interface MyRawIterable extends Iterable {} interface MyIterable extends Iterable {} + + static class SuperGenerics { + + SortedMap>> seriously; + } }