Skip to content

Commit 3337fd3

Browse files
author
Phillip Webb
committed
Refine ResolvableType class
- Support for serialization - Allow programmatic creation of an array from a given component type - Allow programmatic creation with given generics - Extract generics from Class types using Class.getTypeParameters() - Move TypeVariableResolver to an inner class (and make method private) - Refine 'resolve()' algorithm Issue: SPR-10973
1 parent 7ffd05a commit 3337fd3

File tree

6 files changed

+901
-200
lines changed

6 files changed

+901
-200
lines changed

spring-core/src/main/java/org/springframework/core/GenericTypeResolver.java

Lines changed: 16 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,22 @@ public static Class[] resolveTypeArguments(Class<?> clazz, Class<?> genericIfc)
256256
* @deprecated as of Spring 4.0 in favor of {@link ResolvableType}
257257
*/
258258
@Deprecated
259-
public static Class<?> resolveType(Type genericType, Map<TypeVariable, Type> typeVariableMap) {
260-
TypeVariableResolver variableResolver = new TypeVariableMapResolver(typeVariableMap);
261-
Class<?> resolved = ResolvableType.forType(genericType, variableResolver).resolve();
262-
return (resolved == null ? Object.class : resolved);
259+
public static Class<?> resolveType(Type genericType, final Map<TypeVariable, Type> typeVariableMap) {
260+
261+
ResolvableType.VariableResolver variableResolver = new ResolvableType.VariableResolver() {
262+
@Override
263+
public ResolvableType resolveVariable(TypeVariable<?> variable) {
264+
Type type = typeVariableMap.get(variable);
265+
return (type == null ? null : ResolvableType.forType(type));
266+
}
267+
268+
@Override
269+
public Object getSource() {
270+
return typeVariableMap;
271+
}
272+
};
273+
274+
return ResolvableType.forType(genericType, variableResolver).resolve(Object.class);
263275
}
264276

265277
/**
@@ -303,40 +315,4 @@ private static void buildTypeVariableMap(ResolvableType type, Map<TypeVariable,
303315
}
304316
}
305317

306-
307-
/**
308-
* Adapts a {@code typeVariableMap} to a {@link TypeVariableResolver}.
309-
*/
310-
private static class TypeVariableMapResolver implements TypeVariableResolver {
311-
312-
private final Map<TypeVariable, Type> typeVariableMap;
313-
314-
public TypeVariableMapResolver(Map<TypeVariable, Type> typeVariableMap) {
315-
Assert.notNull("TypeVariableMap must not be null");
316-
this.typeVariableMap = typeVariableMap;
317-
}
318-
319-
@Override
320-
public Type resolveVariable(TypeVariable typeVariable) {
321-
return this.typeVariableMap.get(typeVariable);
322-
}
323-
324-
@Override
325-
public boolean equals(Object obj) {
326-
if (this == obj) {
327-
return true;
328-
}
329-
if (obj instanceof TypeVariableMapResolver) {
330-
TypeVariableMapResolver other = (TypeVariableMapResolver) obj;
331-
return this.typeVariableMap.equals(other.typeVariableMap);
332-
}
333-
return false;
334-
}
335-
336-
@Override
337-
public int hashCode() {
338-
return this.typeVariableMap.hashCode();
339-
}
340-
}
341-
342318
}

0 commit comments

Comments
 (0)