Convenient forType methods for ParameterizedTypeReference

Issue: SPR-16054

(cherry picked from commit 53091c7)
This commit is contained in:
Juergen Hoeller
2017-10-09 13:59:01 +02:00
parent ab081c4fab
commit a4803d8b0f
4 changed files with 79 additions and 18 deletions

View File

@@ -53,6 +53,10 @@ public abstract class ParameterizedTypeReference<T> {
this.type = parameterizedType.getActualTypeArguments()[0];
}
private ParameterizedTypeReference(Type type) {
this.type = type;
}
public Type getType() {
return this.type;
@@ -75,6 +79,19 @@ public abstract class ParameterizedTypeReference<T> {
}
/**
* Build a {@code ParameterizedTypeReference} wrapping the given type.
* @param type a generic type (possibly obtained via reflection,
* e.g. from {@link java.lang.reflect.Method#getGenericReturnType()})
* @return a corresponding reference which may be passed into
* {@code ParameterizedTypeReference}-accepting methods
* @since 4.3.12
*/
public static <T> ParameterizedTypeReference<T> forType(Type type) {
return new ParameterizedTypeReference<T>(type) {
};
}
private static Class<?> findParameterizedTypeReferenceSubclass(Class<?> child) {
Class<?> parent = child.getSuperclass();
if (Object.class == parent) {

View File

@@ -1309,6 +1309,19 @@ public class ResolvableType implements Serializable {
return forType(type, variableResolver);
}
/**
* Return a {@link ResolvableType} for the specified {@link ParameterizedTypeReference}.
* Note: The resulting {@link ResolvableType} may not be {@link Serializable}.
* @param typeReference the reference to obtain the source type from
* @return a {@link ResolvableType} for the specified {@link ParameterizedTypeReference}
* @since 4.3.12
* @see #forType(Type)
*/
public static ResolvableType forType(ParameterizedTypeReference<?> typeReference) {
return forType(typeReference.getType(), null, null);
}
/**
* Return a {@link ResolvableType} for the specified {@link Type} backed by a given
* {@link VariableResolver}.
@@ -1545,7 +1558,7 @@ public class ResolvableType implements Serializable {
}
WildcardType wildcardType = (WildcardType) resolveToWildcard.type;
Kind boundsType = (wildcardType.getLowerBounds().length > 0 ? Kind.LOWER : Kind.UPPER);
Type[] bounds = boundsType == Kind.UPPER ? wildcardType.getUpperBounds() : wildcardType.getLowerBounds();
Type[] bounds = (boundsType == Kind.UPPER ? wildcardType.getUpperBounds() : wildcardType.getLowerBounds());
ResolvableType[] resolvableBounds = new ResolvableType[bounds.length];
for (int i = 0; i < bounds.length; i++) {
resolvableBounds[i] = ResolvableType.forType(bounds[i], type.variableResolver);