DATACMNS-1180 - Polishing.

Avoid premature resolution of type in TypeDiscoverer.createInfo(…) and thereby simplify constructor in ParameterizedTypeInformation.
This commit is contained in:
Oliver Gierke
2017-10-26 23:31:12 +02:00
parent ce8d923382
commit 5edcdefe58
3 changed files with 10 additions and 19 deletions

View File

@@ -46,9 +46,9 @@ class ParameterizedTypeInformation<T> extends ParentTypeAwareTypeInformation<T>
* @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<T> extends ParentTypeAwareTypeInformation<T>
* 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<TypeVariable<?>, Type> calculateTypeVariables(ParameterizedType type, Class<?> resolvedType,
TypeDiscoverer<?> parent) {
private static Map<TypeVariable<?>, Type> calculateTypeVariables(ParameterizedType type, TypeDiscoverer<?> parent) {
Class<?> resolvedType = parent.resolveType(type);
TypeVariable<?>[] typeParameters = resolvedType.getTypeParameters();
Type[] arguments = type.getActualTypeArguments();

View File

@@ -114,7 +114,7 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
* @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<S> implements TypeInformation<S> {
return ClassTypeInformation.from((Class<?>) fieldType);
}
Class<S> 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) {

View File

@@ -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<TypeVariable<?>, Type> EMPTY_MAP = Collections.emptyMap();
@Mock ParameterizedType one;
Class<?> resolvedOne = GenericTypeResolver.resolveType(one, Collections.<TypeVariable, Type> emptyMap());
@Before
public void setUp() {
@@ -61,10 +59,8 @@ public class ParameterizedTypeInformationUnitTests {
TypeDiscoverer<String> stringParent = new TypeDiscoverer<String>(String.class, EMPTY_MAP);
TypeDiscoverer<Object> objectParent = new TypeDiscoverer<Object>(Object.class, EMPTY_MAP);
ParameterizedTypeInformation<Object> first = new ParameterizedTypeInformation<Object>(one, resolvedOne,
stringParent);
ParameterizedTypeInformation<Object> second = new ParameterizedTypeInformation<Object>(one, resolvedOne,
objectParent);
ParameterizedTypeInformation<Object> first = new ParameterizedTypeInformation<Object>(one, stringParent);
ParameterizedTypeInformation<Object> second = new ParameterizedTypeInformation<Object>(one, objectParent);
assertThat(first, is(not(second)));
}
@@ -74,10 +70,8 @@ public class ParameterizedTypeInformationUnitTests {
TypeDiscoverer<String> stringParent = new TypeDiscoverer<String>(String.class, EMPTY_MAP);
ParameterizedTypeInformation<Object> first = new ParameterizedTypeInformation<Object>(one, resolvedOne,
stringParent);
ParameterizedTypeInformation<Object> second = new ParameterizedTypeInformation<Object>(one, resolvedOne,
stringParent);
ParameterizedTypeInformation<Object> first = new ParameterizedTypeInformation<Object>(one, stringParent);
ParameterizedTypeInformation<Object> second = new ParameterizedTypeInformation<Object>(one, stringParent);
assertTrue(first.equals(second));
}