Browse Source

DATACMNS-1571 - Fixed specialization in ParameterizedTypeInformation.

DATACMNS-783 introduced code to specialize a raw type containing generics with the type information from it's context so that it can be established if only the raw type has been e.g. stored as type hint in a store (SomeType<T> stored as SomeType and T needing to be reestablished from the context). That application of the context unfortunately did not consider that the raw type might carry generics information itself, like `SomeType extends Foo<String, Integer>`.

We've now changed the specialization algorithm so that the the target type is being looked at in the context of the type used for the assignment (e.g. `Foo<T, ID> foo`). If that assignment fully resolves the declared type, we just outright use the given type as it by definition carries all generics information plus better type information in the first place.

If the type to specialize on is only partially expanding the generics we now create a synthetic type to merge the generics information. I.e. a `SomeType<T> extends Foo<T, Integer>` stored as SomeType would still resolve ID to Integer but capture T from the context including all potentially declared bounds.

Related tickets: DATACMNS-783, DATACMNS-1138.
pull/391/head
Oliver Drotbohm 7 years ago
parent
commit
85e257fc82
  1. 15
      src/main/java/org/springframework/data/util/ParameterizedTypeInformation.java
  2. 10
      src/main/java/org/springframework/data/util/TypeDiscoverer.java

15
src/main/java/org/springframework/data/util/ParameterizedTypeInformation.java

@ -166,7 +166,20 @@ class ParameterizedTypeInformation<T> extends ParentTypeAwareTypeInformation<T> @@ -166,7 +166,20 @@ class ParameterizedTypeInformation<T> extends ParentTypeAwareTypeInformation<T>
@Override
@SuppressWarnings("unchecked")
public TypeInformation<? extends T> specialize(ClassTypeInformation<?> type) {
return isResolvedCompletely() ? (TypeInformation<? extends T>) type : super.specialize(type);
if (isResolvedCompletely()) {
return (TypeInformation<? extends T>) type;
}
TypeInformation<?> asSupertype = type.getSuperTypeInformation(getType());
if (asSupertype == null || !ParameterizedTypeInformation.class.isInstance(asSupertype)) {
return super.specialize(type);
}
return ((ParameterizedTypeInformation<?>) asSupertype).isResolvedCompletely() //
? (TypeInformation<? extends T>) type //
: super.specialize(type);
}
/*

10
src/main/java/org/springframework/data/util/TypeDiscoverer.java

@ -485,13 +485,15 @@ class TypeDiscoverer<S> implements TypeInformation<S> { @@ -485,13 +485,15 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
@SuppressWarnings("unchecked")
public TypeInformation<? extends S> specialize(ClassTypeInformation<?> type) {
Assert.notNull(type, "Type must not be null!");
Assert.isTrue(getType().isAssignableFrom(type.getType()),
String.format("%s must be assignable from %s", getType(), type.getType()));
() -> String.format("%s must be assignable from %s", getType(), type.getType()));
List<TypeInformation<?>> arguments = getTypeArguments();
List<TypeInformation<?>> typeArguments = getTypeArguments();
return (TypeInformation<? extends S>) (arguments.isEmpty() ? type
: createInfo(new SyntheticParamterizedType(type, arguments)));
return (TypeInformation<? extends S>) (typeArguments.isEmpty() //
? type //
: type.createInfo(new SyntheticParamterizedType(type, getTypeArguments())));
}
@Nullable

Loading…
Cancel
Save