From 7ac97bfeae5e7e9debe748198a90f53649729081 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 1d11fa7300..09ecc8ae1a 100644 --- a/src/main/java/org/springframework/data/util/ParameterizedTypeInformation.java +++ b/src/main/java/org/springframework/data/util/ParameterizedTypeInformation.java @@ -55,9 +55,13 @@ public ParameterizedTypeInformation(ParameterizedType type, TypeDiscoverer pa @Override 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 71b6e26686..edae81bd5b 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.List; import java.util.Map; import java.util.Set; +import java.util.SortedMap; import org.junit.Test; import org.springframework.data.mapping.Person; @@ -295,6 +296,26 @@ public void returnsNullForRawTypesOnly() { 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 { } @@ -424,4 +445,9 @@ interface Identifiable { interface MyRawIterable extends Iterable {} interface MyIterable extends Iterable {} + + static class SuperGenerics { + + SortedMap>> seriously; + } }