From fa48fe21bd1c0ef255b53faac0343a4ff329a0ce Mon Sep 17 00:00:00 2001 From: wenshao Date: Wed, 7 Nov 2012 23:10:44 +0800 Subject: [PATCH] bug fixed for generic types --- pom.xml | 2 +- .../com/alibaba/fastjson/util/FieldInfo.java | 33 +++++++++++++++++++ .../json/bvt/bug/Bug_for_huangchun.java | 16 +++++++++ .../json/bvt/bug/Bug_for_jinghui70.java | 33 +++++++++++++++++++ 4 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 src/test/java/com/alibaba/json/bvt/bug/Bug_for_huangchun.java create mode 100644 src/test/java/com/alibaba/json/bvt/bug/Bug_for_jinghui70.java diff --git a/pom.xml b/pom.xml index 73763ddf3..e92c0e628 100644 --- a/pom.xml +++ b/pom.xml @@ -73,7 +73,7 @@ org.apache.maven.plugins maven-compiler-plugin - 2.3.1 + 2.5.1 UTF-8 1.5 diff --git a/src/main/java/com/alibaba/fastjson/util/FieldInfo.java b/src/main/java/com/alibaba/fastjson/util/FieldInfo.java index 5ec366167..e0d2e0199 100644 --- a/src/main/java/com/alibaba/fastjson/util/FieldInfo.java +++ b/src/main/java/com/alibaba/fastjson/util/FieldInfo.java @@ -2,6 +2,7 @@ package com.alibaba.fastjson.util; import java.lang.annotation.Annotation; import java.lang.reflect.Field; +import java.lang.reflect.GenericDeclaration; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.ParameterizedType; @@ -71,6 +72,16 @@ public class FieldInfo implements Comparable { fieldType = field.getGenericType(); this.declaringClass = field.getDeclaringClass(); } + + if (clazz != null && fieldClass == Object.class && fieldType instanceof TypeVariable) { + TypeVariable tv = (TypeVariable) fieldType; + Type genericFieldType = getInheritGenericType(clazz, tv); + if (genericFieldType != null) { + this.fieldClass = TypeUtils.getClass(genericFieldType); + this.fieldType = genericFieldType; + return; + } + } Type genericFieldType = getFieldType(clazz, type, fieldType); @@ -109,6 +120,28 @@ public class FieldInfo implements Comparable { return fieldType; } + + public static Type getInheritGenericType(Class clazz, TypeVariable tv) { + Type type = null; + GenericDeclaration gd = tv.getGenericDeclaration(); + do { + type = clazz.getGenericSuperclass(); + if (type instanceof ParameterizedType) { + ParameterizedType ptype = (ParameterizedType) type; + if (ptype.getRawType() == gd) { + TypeVariable[] tvs = gd.getTypeParameters(); + Type[] types = ptype.getActualTypeArguments(); + for (int i = 0; i < tvs.length; i++) { + if (tvs[i] == tv) + return types[i]; + } + return null; + } + } + clazz = TypeUtils.getClass(type); + } while (type != null); + return null; + } public String toString() { return this.name; diff --git a/src/test/java/com/alibaba/json/bvt/bug/Bug_for_huangchun.java b/src/test/java/com/alibaba/json/bvt/bug/Bug_for_huangchun.java new file mode 100644 index 000000000..59e93982f --- /dev/null +++ b/src/test/java/com/alibaba/json/bvt/bug/Bug_for_huangchun.java @@ -0,0 +1,16 @@ +package com.alibaba.json.bvt.bug; + +import junit.framework.TestCase; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; + + +public class Bug_for_huangchun extends TestCase { + public void test_serialize_url() throws Exception { + JSONObject json = new JSONObject(); + json.put("info", " 问题链接 "); + String text = JSON.toJSONString(json); + System.out.println(text); + } +} diff --git a/src/test/java/com/alibaba/json/bvt/bug/Bug_for_jinghui70.java b/src/test/java/com/alibaba/json/bvt/bug/Bug_for_jinghui70.java new file mode 100644 index 000000000..eaa0ab67d --- /dev/null +++ b/src/test/java/com/alibaba/json/bvt/bug/Bug_for_jinghui70.java @@ -0,0 +1,33 @@ +package com.alibaba.json.bvt.bug; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import com.alibaba.fastjson.JSON; + +public class Bug_for_jinghui70 extends TestCase { + + public static abstract class IdObject { + + private I id; + + public I getId() { + return id; + } + + public void setId(I id) { + this.id = id; + } + } + + public static class Child extends IdObject { + + } + + public void test_generic() throws Exception { + String str = "{\"id\":0}"; + + Child child = JSON.parseObject(str, Child.class); + Assert.assertEquals(Long.class, child.getId().getClass()); + } +}