From 5edcdefe58838b23ec5081fc084f335694919ff8 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Thu, 26 Oct 2017 23:31:12 +0200 Subject: [PATCH] DATACMNS-1180 - Polishing. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Avoid premature resolution of type in TypeDiscoverer.createInfo(…) and thereby simplify constructor in ParameterizedTypeInformation. --- .../data/util/ParameterizedTypeInformation.java | 9 ++++----- .../springframework/data/util/TypeDiscoverer.java | 6 ++---- .../ParameterizedTypeInformationUnitTests.java | 14 ++++---------- 3 files changed, 10 insertions(+), 19 deletions(-) diff --git a/src/main/java/org/springframework/data/util/ParameterizedTypeInformation.java b/src/main/java/org/springframework/data/util/ParameterizedTypeInformation.java index 798de32e0..707fe4b09 100644 --- a/src/main/java/org/springframework/data/util/ParameterizedTypeInformation.java +++ b/src/main/java/org/springframework/data/util/ParameterizedTypeInformation.java @@ -46,9 +46,9 @@ class ParameterizedTypeInformation extends ParentTypeAwareTypeInformation * @param type must not be {@literal null} * @param parent must not be {@literal null} */ - public ParameterizedTypeInformation(ParameterizedType type, Class resolvedType, TypeDiscoverer parent) { + public ParameterizedTypeInformation(ParameterizedType type, TypeDiscoverer parent) { - super(type, parent, calculateTypeVariables(type, resolvedType, parent)); + super(type, parent, calculateTypeVariables(type, parent)); this.type = type; } @@ -245,13 +245,12 @@ class ParameterizedTypeInformation extends ParentTypeAwareTypeInformation * declared. * * @param type must not be {@literal null}. - * @param resolvedType must not be {@literal null}. * @param parent must not be {@literal null}. * @return will never be {@literal null}. */ - private static Map, Type> calculateTypeVariables(ParameterizedType type, Class resolvedType, - TypeDiscoverer parent) { + private static Map, Type> calculateTypeVariables(ParameterizedType type, TypeDiscoverer parent) { + Class resolvedType = parent.resolveType(type); TypeVariable[] typeParameters = resolvedType.getTypeParameters(); Type[] arguments = type.getActualTypeArguments(); diff --git a/src/main/java/org/springframework/data/util/TypeDiscoverer.java b/src/main/java/org/springframework/data/util/TypeDiscoverer.java index b096ff842..e85f37b15 100644 --- a/src/main/java/org/springframework/data/util/TypeDiscoverer.java +++ b/src/main/java/org/springframework/data/util/TypeDiscoverer.java @@ -114,7 +114,7 @@ class TypeDiscoverer implements TypeInformation { * @param fieldType * @return */ - @SuppressWarnings({ "rawtypes", "unchecked", "deprecation" }) + @SuppressWarnings({ "rawtypes", "unchecked" }) protected TypeInformation createInfo(Type fieldType) { if (fieldType.equals(this.type)) { @@ -125,11 +125,9 @@ class TypeDiscoverer implements TypeInformation { return ClassTypeInformation.from((Class) fieldType); } - Class resolveType = resolveType(fieldType); - if (fieldType instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) fieldType; - return new ParameterizedTypeInformation(parameterizedType, resolveType, this); + return new ParameterizedTypeInformation(parameterizedType, this); } if (fieldType instanceof TypeVariable) { diff --git a/src/test/java/org/springframework/data/util/ParameterizedTypeInformationUnitTests.java b/src/test/java/org/springframework/data/util/ParameterizedTypeInformationUnitTests.java index 406b4d64f..d439436d3 100644 --- a/src/test/java/org/springframework/data/util/ParameterizedTypeInformationUnitTests.java +++ b/src/test/java/org/springframework/data/util/ParameterizedTypeInformationUnitTests.java @@ -34,7 +34,6 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; -import org.springframework.core.GenericTypeResolver; /** * Unit tests for {@link ParameterizedTypeInformation}. @@ -48,7 +47,6 @@ public class ParameterizedTypeInformationUnitTests { static final Map, Type> EMPTY_MAP = Collections.emptyMap(); @Mock ParameterizedType one; - Class resolvedOne = GenericTypeResolver.resolveType(one, Collections. emptyMap()); @Before public void setUp() { @@ -61,10 +59,8 @@ public class ParameterizedTypeInformationUnitTests { TypeDiscoverer stringParent = new TypeDiscoverer(String.class, EMPTY_MAP); TypeDiscoverer objectParent = new TypeDiscoverer(Object.class, EMPTY_MAP); - ParameterizedTypeInformation first = new ParameterizedTypeInformation(one, resolvedOne, - stringParent); - ParameterizedTypeInformation second = new ParameterizedTypeInformation(one, resolvedOne, - objectParent); + ParameterizedTypeInformation first = new ParameterizedTypeInformation(one, stringParent); + ParameterizedTypeInformation second = new ParameterizedTypeInformation(one, objectParent); assertThat(first, is(not(second))); } @@ -74,10 +70,8 @@ public class ParameterizedTypeInformationUnitTests { TypeDiscoverer stringParent = new TypeDiscoverer(String.class, EMPTY_MAP); - ParameterizedTypeInformation first = new ParameterizedTypeInformation(one, resolvedOne, - stringParent); - ParameterizedTypeInformation second = new ParameterizedTypeInformation(one, resolvedOne, - stringParent); + ParameterizedTypeInformation first = new ParameterizedTypeInformation(one, stringParent); + ParameterizedTypeInformation second = new ParameterizedTypeInformation(one, stringParent); assertTrue(first.equals(second)); }