Skip to content

Commit f3986df

Browse files
committed
bug fixed for generic types
1 parent 48365c7 commit f3986df

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

src/main/java/com/alibaba/fastjson/util/FieldInfo.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.lang.annotation.Annotation;
44
import java.lang.reflect.Field;
5+
import java.lang.reflect.GenericDeclaration;
56
import java.lang.reflect.InvocationTargetException;
67
import java.lang.reflect.Method;
78
import java.lang.reflect.ParameterizedType;
@@ -69,9 +70,19 @@ public FieldInfo(String name, Method method, Field field, Class<?> clazz, Type t
6970
fieldType = field.getGenericType();
7071
this.declaringClass = field.getDeclaringClass();
7172
}
73+
74+
if (clazz != null && fieldClass == Object.class && fieldType instanceof TypeVariable) {
75+
TypeVariable<?> tv = (TypeVariable<?>) fieldType;
76+
Type genericFieldType = getInheritGenericType(clazz, tv);
77+
if (genericFieldType != null) {
78+
this.fieldClass = TypeUtils.getClass(genericFieldType);
79+
this.fieldType = genericFieldType;
80+
return;
81+
}
82+
}
7283

7384
Type genericFieldType = getFieldType(clazz, type, fieldType);
74-
85+
7586
if (genericFieldType != fieldType) {
7687
if (genericFieldType instanceof ParameterizedType) {
7788
fieldClass = TypeUtils.getClass(genericFieldType);
@@ -107,6 +118,28 @@ public static Type getFieldType(Class<?> clazz, Type type, Type fieldType) {
107118

108119
return fieldType;
109120
}
121+
122+
public static Type getInheritGenericType(Class<?> clazz, TypeVariable<?> tv) {
123+
Type type = null;
124+
GenericDeclaration gd = tv.getGenericDeclaration();
125+
do {
126+
type = clazz.getGenericSuperclass();
127+
if (type instanceof ParameterizedType) {
128+
ParameterizedType ptype = (ParameterizedType) type;
129+
if (ptype.getRawType() == gd) {
130+
TypeVariable<?>[] tvs = gd.getTypeParameters();
131+
Type[] types = ptype.getActualTypeArguments();
132+
for (int i = 0; i < tvs.length; i++) {
133+
if (tvs[i] == tv)
134+
return types[i];
135+
}
136+
return null;
137+
}
138+
}
139+
clazz = TypeUtils.getClass(type);
140+
} while (type != null);
141+
return null;
142+
}
110143

111144
public String toString() {
112145
return this.name;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.alibaba.json.bvt.bug;
2+
3+
import junit.framework.Assert;
4+
import junit.framework.TestCase;
5+
6+
import com.alibaba.fastjson.JSON;
7+
8+
public class Bug_for_jinghui70 extends TestCase {
9+
10+
public static abstract class IdObject<I> {
11+
12+
private I id;
13+
14+
public I getId() {
15+
return id;
16+
}
17+
18+
public void setId(I id) {
19+
this.id = id;
20+
}
21+
}
22+
23+
public static class Child extends IdObject<Long> {
24+
25+
}
26+
27+
public void test_generic() throws Exception {
28+
String str = "{\"id\":0}";
29+
30+
Child child = JSON.parseObject(str, Child.class);
31+
Assert.assertEquals(Long.class, child.getId().getClass());
32+
}
33+
}

0 commit comments

Comments
 (0)