Extracted AbstractJsonHttpMessageConverter from GsonHttpMessageConverter

Generic type resolution algorithm in GenericTypeResolver shared between Jackson and Gson.

Issue: SPR-15381
This commit is contained in:
Juergen Hoeller
2017-04-10 15:36:29 +02:00
parent ea98ee820a
commit e5fdd4cd1d
12 changed files with 337 additions and 659 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,8 +18,6 @@ package org.springframework.core;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.Collection;
import java.util.Date;
@@ -109,26 +107,6 @@ public class BridgeMethodResolverTests {
assertFalse("Should not be bridge method", BridgeMethodResolver.isBridgeMethodFor(bridge, other, MyBar.class));
}
@Test
@Deprecated
public void testCreateTypeVariableMap() throws Exception {
Map<TypeVariable, Type> typeVariableMap = GenericTypeResolver.getTypeVariableMap(MyBar.class);
TypeVariable<?> barT = findTypeVariable(InterBar.class, "T");
assertEquals(String.class, typeVariableMap.get(barT));
typeVariableMap = GenericTypeResolver.getTypeVariableMap(MyFoo.class);
TypeVariable<?> fooT = findTypeVariable(Foo.class, "T");
assertEquals(String.class, typeVariableMap.get(fooT));
typeVariableMap = GenericTypeResolver.getTypeVariableMap(ExtendsEnclosing.ExtendsEnclosed.ExtendsReallyDeepNow.class);
TypeVariable<?> r = findTypeVariable(Enclosing.Enclosed.ReallyDeepNow.class, "R");
TypeVariable<?> s = findTypeVariable(Enclosing.Enclosed.class, "S");
TypeVariable<?> t = findTypeVariable(Enclosing.class, "T");
assertEquals(Long.class, typeVariableMap.get(r));
assertEquals(Integer.class, typeVariableMap.get(s));
assertEquals(String.class, typeVariableMap.get(t));
}
@Test
public void testDoubleParameterization() throws Exception {
Method objectBridge = MyBoo.class.getDeclaredMethod("foo", Object.class);
@@ -228,14 +206,6 @@ public class BridgeMethodResolverTests {
assertEquals(bridgedMethod, BridgeMethodResolver.findBridgedMethod(bridgeMethod));
}
@Test
@Deprecated
public void testSPR2454() throws Exception {
Map<TypeVariable, Type> typeVariableMap = GenericTypeResolver.getTypeVariableMap(YourHomer.class);
TypeVariable<?> variable = findTypeVariable(MyHomer.class, "L");
assertEquals(AbstractBounded.class, ((ParameterizedType) typeVariableMap.get(variable)).getRawType());
}
@Test
public void testSPR2603() throws Exception {
Method objectBridge = YourHomer.class.getDeclaredMethod("foo", Bounded.class);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,11 +17,7 @@
package org.springframework.core;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -76,71 +72,11 @@ public class GenericTypeResolverTests {
resolveReturnTypeArgument(findMethod(MyTypeWithMethods.class, "object"), MyInterfaceType.class));
}
@Test
@Deprecated
public void testResolveType() {
Method intMessageMethod = findMethod(MyTypeWithMethods.class, "readIntegerInputMessage", MyInterfaceType.class);
MethodParameter intMessageMethodParam = new MethodParameter(intMessageMethod, 0);
assertEquals(MyInterfaceType.class,
resolveType(intMessageMethodParam.getGenericParameterType(), new HashMap<>()));
Method intArrMessageMethod = findMethod(MyTypeWithMethods.class, "readIntegerArrayInputMessage",
MyInterfaceType[].class);
MethodParameter intArrMessageMethodParam = new MethodParameter(intArrMessageMethod, 0);
assertEquals(MyInterfaceType[].class,
resolveType(intArrMessageMethodParam.getGenericParameterType(), new HashMap<>()));
Method genericArrMessageMethod = findMethod(MySimpleTypeWithMethods.class, "readGenericArrayInputMessage",
Object[].class);
MethodParameter genericArrMessageMethodParam = new MethodParameter(genericArrMessageMethod, 0);
Map<TypeVariable, Type> varMap = getTypeVariableMap(MySimpleTypeWithMethods.class);
assertEquals(Integer[].class, resolveType(genericArrMessageMethodParam.getGenericParameterType(), varMap));
}
@Test
public void testBoundParameterizedType() {
assertEquals(B.class, resolveTypeArgument(TestImpl.class, TestIfc.class));
}
@Test
@Deprecated
public void testGetTypeVariableMap() throws Exception {
Map<TypeVariable, Type> map;
map = GenericTypeResolver.getTypeVariableMap(MySimpleInterfaceType.class);
assertThat(map.toString(), equalTo("{T=class java.lang.String}"));
map = GenericTypeResolver.getTypeVariableMap(MyCollectionInterfaceType.class);
assertThat(map.toString(), equalTo("{T=java.util.Collection<java.lang.String>}"));
map = GenericTypeResolver.getTypeVariableMap(MyCollectionSuperclassType.class);
assertThat(map.toString(), equalTo("{T=java.util.Collection<java.lang.String>}"));
map = GenericTypeResolver.getTypeVariableMap(MySimpleTypeWithMethods.class);
assertThat(map.toString(), equalTo("{T=class java.lang.Integer}"));
map = GenericTypeResolver.getTypeVariableMap(TopLevelClass.class);
assertThat(map.toString(), equalTo("{}"));
map = GenericTypeResolver.getTypeVariableMap(TypedTopLevelClass.class);
assertThat(map.toString(), equalTo("{T=class java.lang.Integer}"));
map = GenericTypeResolver.getTypeVariableMap(TypedTopLevelClass.TypedNested.class);
assertThat(map.size(), equalTo(2));
Type t = null;
Type x = null;
for (Map.Entry<TypeVariable, Type> entry : map.entrySet()) {
if (entry.getKey().toString().equals("T")) {
t = entry.getValue();
}
else {
x = entry.getValue();
}
}
assertThat(t, equalTo((Type) Integer.class));
assertThat(x, equalTo((Type) Long.class));
}
@Test
public void getGenericsCannotBeResolved() throws Exception {
// SPR-11030