diff --git a/src/main/java/com/alibaba/fastjson/TypeReference.java b/src/main/java/com/alibaba/fastjson/TypeReference.java index d8252a46ed..b980770769 100755 --- a/src/main/java/com/alibaba/fastjson/TypeReference.java +++ b/src/main/java/com/alibaba/fastjson/TypeReference.java @@ -24,8 +24,8 @@ * parameters, such as {@code Class} or {@code List}. */ public class TypeReference { - static ConcurrentMap, ConcurrentMap>> classTypeCache - = new ConcurrentHashMap, ConcurrentMap>>(16, 0.75f, 1); + static ConcurrentMap classTypeCache + = new ConcurrentHashMap(16, 0.75f, 1); protected final Type type; @@ -42,7 +42,7 @@ protected TypeReference(){ type = ((ParameterizedType) superClass).getActualTypeArguments()[0]; } - + /** * @since 1.2.9 * @param actualTypeArguments @@ -54,7 +54,7 @@ protected TypeReference(Type... actualTypeArguments){ ParameterizedType argType = (ParameterizedType) ((ParameterizedType) superClass).getActualTypeArguments()[0]; Type rawType = argType.getRawType(); Type[] argTypes = argType.getActualTypeArguments(); - + int actualIndex = 0; for (int i = 0; i < argTypes.length; ++i) { if (argTypes[i] instanceof TypeVariable) { @@ -65,31 +65,15 @@ protected TypeReference(Type... actualTypeArguments){ } } - if (actualTypeArguments.length == 1 && argTypes.length == 1) { - ConcurrentMap> classCache = classTypeCache.get(thisClass); - if (classCache == null) { - classTypeCache.putIfAbsent(thisClass, new ConcurrentHashMap>(16, 0.75f, 1)); - classCache = classTypeCache.get(thisClass); - } - - ConcurrentMap typeCached = classCache.get(argType); - if (typeCached == null) { - classCache.putIfAbsent(argType, new ConcurrentHashMap(16, 0.75f, 1)); - typeCached = classCache.get(argType); - } + Type key = new ParameterizedTypeImpl(argTypes, thisClass, rawType); + Type cachedType = classTypeCache.get(key); + if (cachedType == null) { + classTypeCache.putIfAbsent(key, key); + cachedType = classTypeCache.get(key); + } - Type actualTypeArgument = actualTypeArguments[0]; + type = cachedType; - Type cachedType = typeCached.get(actualTypeArgument); - if (cachedType == null) { - typeCached.putIfAbsent(actualTypeArgument, actualTypeArgument); - cachedType = typeCached.get(actualTypeArgument); - } - - type = cachedType; - } else { - type = new ParameterizedTypeImpl(argTypes, thisClass, rawType); - } } /** diff --git a/src/test/java/com/alibaba/json/bvt/TypeReferenceTest12.java b/src/test/java/com/alibaba/json/bvt/TypeReferenceTest12.java new file mode 100644 index 0000000000..c89d0421d7 --- /dev/null +++ b/src/test/java/com/alibaba/json/bvt/TypeReferenceTest12.java @@ -0,0 +1,33 @@ +package com.alibaba.json.bvt; + +import com.alibaba.fastjson.TypeReference; +import junit.framework.TestCase; + +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; + +/** + * Created by wuwen on 2016/12/7. + */ +public class TypeReferenceTest12 extends TestCase { + + public void test_same() throws Exception { + ParameterizedType type1 = getType(Integer.class); + ParameterizedType type2 = getType(); + + assertEquals(type1.getRawType(), type2.getRawType()); + assertSame(type1.getRawType(), type2.getRawType()); + } + + ParameterizedType getType(Type type) { + return (ParameterizedType)new TypeReference>(type) {}.getType(); + } + + ParameterizedType getType() { + return (ParameterizedType)new TypeReference>() {}.getType(); + } + + public static class Model { + public T value; + } +} diff --git a/src/test/java/com/alibaba/json/bvt/bug/Bug_for_issue_937.java b/src/test/java/com/alibaba/json/bvt/bug/Bug_for_issue_937.java new file mode 100644 index 0000000000..eb905d56f2 --- /dev/null +++ b/src/test/java/com/alibaba/json/bvt/bug/Bug_for_issue_937.java @@ -0,0 +1,54 @@ +package com.alibaba.json.bvt.bug; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.TypeReference; +import junit.framework.TestCase; +import org.junit.Assert; + +/** + * Created by wuwen on 2016/12/7. + */ +public class Bug_for_issue_937 extends TestCase { + + public void test_for_issue() throws Exception { + String json = "{outPara:{name:\"user\"}}"; + Out out = returnOut(json, Info.class); + Assert.assertEquals("user", out.getOutPara().getName()); + } + + public static Out returnOut(String jsonStr, Class c2) { + return JSON.parseObject(jsonStr, new TypeReference>(c2) { + }); + } + + public static class Out { + private T outPara; + + public void setOutPara(T t) { + outPara = t; + } + + public T getOutPara() { + return outPara; + } + + public Out() { + } + + public Out(T t) { + setOutPara(t); + } + } + + public static class Info { + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } +}