From 1725cbaec18cb1a5c859f4d808a27840c4ffb1f6 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Fri, 11 Aug 2017 15:29:17 +0200 Subject: [PATCH] =?UTF-8?q?DATACMNS-1138=20-=20TypeInformation.specialize(?= =?UTF-8?q?=E2=80=A6)=20now=20only=20specializes=20unresolved=20parameteri?= =?UTF-8?q?zed=20types.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Type specialization - i.e. enrichment of a raw type with a current generic context - is now only done if the current type is not yet resolved completely. This allows wildcarded target references to just fall back to the type to specialize, which will then by definition carry more generics information than the one to be specialized. --- .../util/ParameterizedTypeInformation.java | 9 +++++++ .../util/ClassTypeInformationUnitTests.java | 26 ++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/springframework/data/util/ParameterizedTypeInformation.java b/src/main/java/org/springframework/data/util/ParameterizedTypeInformation.java index d7075911c..88a8a2fc4 100644 --- a/src/main/java/org/springframework/data/util/ParameterizedTypeInformation.java +++ b/src/main/java/org/springframework/data/util/ParameterizedTypeInformation.java @@ -181,6 +181,15 @@ class ParameterizedTypeInformation extends ParentTypeAwareTypeInformation } /* + * (non-Javadoc) + * @see org.springframework.data.util.TypeDiscoverer#specialize(org.springframework.data.util.ClassTypeInformation) + */ + @Override + public TypeInformation specialize(ClassTypeInformation type) { + return isResolvedCompletely() ? type : super.specialize(type); + } + + /* * (non-Javadoc) * @see org.springframework.data.util.ParentTypeAwareTypeInformation#equals(java.lang.Object) */ diff --git a/src/test/java/org/springframework/data/util/ClassTypeInformationUnitTests.java b/src/test/java/org/springframework/data/util/ClassTypeInformationUnitTests.java index c4a4af0fa..1bfcfa7ea 100644 --- a/src/test/java/org/springframework/data/util/ClassTypeInformationUnitTests.java +++ b/src/test/java/org/springframework/data/util/ClassTypeInformationUnitTests.java @@ -106,7 +106,7 @@ public class ClassTypeInformationUnitTests { property = information.getProperty("rawSet"); assertEquals(Set.class, property.getType()); - assertThat(property.getComponentType().getType(), is(Matchers.>equalTo(Object.class))); + assertThat(property.getComponentType().getType(), is(Matchers.> equalTo(Object.class))); assertNull(property.getMapValueType()); } @@ -413,6 +413,18 @@ public class ClassTypeInformationUnitTests { assertThat(information.getProperty("field").getType(), is(typeCompatibleWith(Nested.class))); } + @Test // DATACMNS-1138 + @SuppressWarnings("rawtypes") + public void usesTargetTypeForWildcardedBaseOnSpecialization() { + + ClassTypeInformation wrapper = ClassTypeInformation.from(WildcardedWrapper.class); + ClassTypeInformation concrete = ClassTypeInformation.from(SomeConcrete.class); + + TypeInformation property = wrapper.getProperty("wildcarded"); + + assertThat(property.specialize(concrete), is((TypeInformation) concrete)); + } + static class StringMapContainer extends MapContainer { } @@ -611,4 +623,16 @@ public class ClassTypeInformationUnitTests { static class Nested extends SomeType {} static class Concrete extends SomeType {} + + // DATACMNS-1138 + + static class SomeGeneric { + T value; + } + + static class SomeConcrete extends SomeGeneric {} + + static class WildcardedWrapper { + SomeGeneric wildcarded; + } }