bug fixed for generic

This commit is contained in:
高铁
2016-04-03 12:36:01 +08:00
parent a04c47b9c6
commit 3bd528016c
6 changed files with 43 additions and 60 deletions
@@ -536,16 +536,7 @@ public class JavaBeanDeserializer implements ObjectDeserializer {
Map<String, Object> fieldValues) {
JSONLexer lexer = parser.lexer; // xxx
FieldDeserializer fieldDeserializer = feildDeserializerMap.get(key);
if (fieldDeserializer == null) {
for (Map.Entry<String, FieldDeserializer> entry : feildDeserializerMap.entrySet()) {
if (entry.getKey().equalsIgnoreCase(key)) {
fieldDeserializer = entry.getValue();
break;
}
}
}
FieldDeserializer fieldDeserializer = smartMatch(key);
if (fieldDeserializer == null) {
parseExtra(parser, object, key);
@@ -168,10 +168,10 @@ public class ObjectArrayCodec implements ObjectSerializer, ObjectDeserializer {
componentClass = Object.class;
}
} else {
componentClass = Object.class;
componentClass = TypeUtils.getClass(typeVar.getBounds()[0]);
}
} else {
componentClass = (Class) componentType;
componentClass = TypeUtils.getClass(componentType);
}
} else {
Class clazz = (Class) type;
@@ -471,7 +471,7 @@ public class DeserializeBeanInfo {
continue;
}
beanInfo.add(new FieldInfo(propertyName, method, null, clazz, type, annotation));
beanInfo.add(new FieldInfo(propertyName, method, null, clazz, type, 0, 0, annotation, null));
}
}
}
@@ -19,23 +19,19 @@ public class FieldInfo implements Comparable<FieldInfo> {
public final Method method;
public final Field field;
private int ordinal = 0;
private int ordinal = 0;
public final Class<?> fieldClass;
public final Type fieldType;
public final Class<?> declaringClass;
private boolean getOnly = false;
private int serialzeFeatures;
private String label = "";
private JSONField fieldAnnotation;
private JSONField methodAnnotation;
private boolean getOnly = false;
private int serialzeFeatures;
private String label = "";
private JSONField fieldAnnotation;
private JSONField methodAnnotation;
public final char[] name_chars;
public FieldInfo(String name, Class<?> declaringClass, Class<?> fieldClass, Type fieldType, Field field){
this(name, declaringClass, fieldClass, fieldType, field, 0, 0);
}
public FieldInfo(String name, Class<?> declaringClass, Class<?> fieldClass, Type fieldType, Field field, int ordinal, int serialzeFeatures){
this.name = name;
this.declaringClass = declaringClass;
@@ -53,25 +49,6 @@ public class FieldInfo implements Comparable<FieldInfo> {
}
}
public FieldInfo(String name, Method method, Field field){
this(name, method, field, 0, 0);
}
public FieldInfo(String name, Method method, Field field, int ordinal, int serialzeFeatures){
this(name, method, field, ordinal, serialzeFeatures, null, null, null);
}
public FieldInfo(String name, Method method, Field field, int ordinal, int serialzeFeatures, String label, JSONField methodAnnotation, JSONField fieldAnnotation){
this(name, method, field, null, null, ordinal, serialzeFeatures, methodAnnotation, fieldAnnotation);
if (label != null && label.length() > 0) {
this.label = label;
}
}
public FieldInfo(String name, Method method, Field field, Class<?> clazz, Type type, JSONField methodAnnotation){
this(name, method, field, clazz, type, 0, 0, methodAnnotation, null);
}
public FieldInfo(String name, Method method, Field field, Class<?> clazz, Type type, int ordinal, int serialzeFeatures, JSONField fieldAnnotation, JSONField methodAnnotation){
this.name = name;
this.method = method;
@@ -394,4 +371,11 @@ public class FieldInfo implements Comparable<FieldInfo> {
public int getSerialzeFeatures() {
return serialzeFeatures;
}
public void setLabel(String label) {
if (label != null && label.length() > 0) {
this.label = label;
}
}
}
@@ -1086,8 +1086,9 @@ public class TypeUtils {
}
}
fieldInfoMap.put(propertyName, new FieldInfo(propertyName, method, null, ordinal, serialzeFeatures,
annotation.label(), annotation, null));
FieldInfo fieldInfo = new FieldInfo(propertyName, method, null, clazz, null, ordinal, serialzeFeatures, annotation, null);
fieldInfo.setLabel(label);
fieldInfoMap.put(propertyName, fieldInfo);
continue;
}
@@ -1169,8 +1170,9 @@ public class TypeUtils {
}
}
fieldInfoMap.put(propertyName,
new FieldInfo(propertyName, method, field, ordinal, serialzeFeatures, label, annotation, fieldAnnotation));
FieldInfo fieldInfo = new FieldInfo(propertyName, method, field, clazz, null, ordinal, serialzeFeatures, annotation, fieldAnnotation);
fieldInfo.setLabel(label);
fieldInfoMap.put(propertyName, fieldInfo);
}
if (methodName.startsWith("is")) {
@@ -1237,8 +1239,9 @@ public class TypeUtils {
}
}
fieldInfoMap.put(propertyName,
new FieldInfo(propertyName, method, field, ordinal, serialzeFeatures, label, annotation, fieldAnnotation));
FieldInfo fieldInfo = new FieldInfo(propertyName, method, field, clazz, null, ordinal, serialzeFeatures, annotation, fieldAnnotation);
fieldInfo.setLabel(label);
fieldInfoMap.put(propertyName, fieldInfo);
}
}
@@ -1277,8 +1280,9 @@ public class TypeUtils {
}
if (!fieldInfoMap.containsKey(propertyName)) {
fieldInfoMap.put(propertyName,
new FieldInfo(propertyName, null, field, ordinal, serialzeFeatures, label, null, fieldAnnotation));
FieldInfo fieldInfo = new FieldInfo(propertyName, null, field, clazz, null, ordinal, serialzeFeatures, null, fieldAnnotation);
fieldInfo.setLabel(label);
fieldInfoMap.put(propertyName, fieldInfo);
}
}
@@ -1461,6 +1465,11 @@ public class TypeUtils {
if (type instanceof ParameterizedType) {
return getClass(((ParameterizedType) type).getRawType());
}
if (type instanceof TypeVariable) {
Type boundType = ((TypeVariable<?>) type).getBounds()[0];
return (Class<?>) boundType;
}
return Object.class;
}
@@ -5,28 +5,27 @@ import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.List;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.util.FieldInfo;
import com.alibaba.fastjson.util.ParameterizedTypeImpl;
import junit.framework.TestCase;
public class FieldInfoTest extends TestCase {
public void test_null() throws Exception {
FieldInfo fieldInfo = new FieldInfo("getValue", Entity.class.getMethod("getValue"), null);
FieldInfo fieldInfo = new FieldInfo("getValue", Entity.class.getMethod("getValue"), null, null, null, 0, 0, null, null);
Assert.assertEquals(null, fieldInfo.getAnnotation());
Field field = GenericFieldEntity.class.getField("value");
Type type = new ParameterizedTypeImpl(new Type[] { ValueObject.class }, null, GenericFieldEntity.class);
FieldInfo fieldInfoOfField = new FieldInfo("value", null, field, GenericFieldEntity.class, type, null);
FieldInfo fieldInfoOfField = new FieldInfo("value", null, field, GenericFieldEntity.class, type, 0, 0, null, null);
Assert.assertEquals(fieldInfoOfField.fieldType, ValueObject.class);
Assert.assertEquals(fieldInfoOfField.fieldClass, ValueObject.class);
field = GenericListFieldEntity.class.getField("value");
type = new ParameterizedTypeImpl(new Type[] { ValueObject.class }, null, GenericListFieldEntity.class);
FieldInfo fieldInfoOfListField = new FieldInfo("value", null, field, GenericListFieldEntity.class, type, null);
FieldInfo fieldInfoOfListField = new FieldInfo("value", null, field, GenericListFieldEntity.class, type, 0, 0, null, null);
ParameterizedTypeImpl actualFieldType = (ParameterizedTypeImpl) fieldInfoOfListField.fieldType;
Assert.assertEquals(actualFieldType.getActualTypeArguments()[0], ValueObject.class);
Assert.assertEquals(actualFieldType.getRawType(), List.class);
@@ -35,7 +34,7 @@ public class FieldInfoTest extends TestCase {
Method method = GenericSetterEntity.class.getMethod("setValue", Object.class);
type = new ParameterizedTypeImpl(new Type[] { ValueObject.class }, null, GenericSetterEntity.class);
FieldInfo fieldInfoOfSetter = new FieldInfo("value", method, null, GenericSetterEntity.class, type, null);
FieldInfo fieldInfoOfSetter = new FieldInfo("value", method, null, GenericSetterEntity.class, type, 0, 0, null, null);
Assert.assertEquals(fieldInfoOfSetter.fieldType, ValueObject.class);
Assert.assertEquals(fieldInfoOfSetter.fieldClass, ValueObject.class);
@@ -43,7 +42,7 @@ public class FieldInfoTest extends TestCase {
method = GenericListSetterEntity.class.getMethod("setValue", List.class);
type = new ParameterizedTypeImpl(new Type[] { ValueObject.class }, null, GenericListSetterEntity.class);
FieldInfo fieldInfoOfListSetter = new FieldInfo("value", method, null, GenericListSetterEntity.class, type, null);
FieldInfo fieldInfoOfListSetter = new FieldInfo("value", method, null, GenericListSetterEntity.class, type, 0, 0, null, null);
Assert.assertEquals(actualFieldType.getActualTypeArguments()[0], ValueObject.class);
Assert.assertEquals(actualFieldType.getRawType(), List.class);
Assert.assertEquals(fieldInfoOfListSetter.fieldClass, List.class);