Search in sources :

Example 1 with ParameterizedTypeImpl

use of com.alibaba.fastjson.util.ParameterizedTypeImpl in project fastjson by alibaba.

the class ArrayListTypeFieldDeserializer method parseArray.

@SuppressWarnings({ "unchecked", "rawtypes" })
public final void parseArray(DefaultJSONParser parser, Type objectType, Collection array) {
    Type itemType = this.itemType;
    ObjectDeserializer itemTypeDeser = this.deserializer;
    if (objectType instanceof ParameterizedType) {
        if (itemType instanceof TypeVariable) {
            TypeVariable typeVar = (TypeVariable) itemType;
            ParameterizedType paramType = (ParameterizedType) objectType;
            Class<?> objectClass = null;
            if (paramType.getRawType() instanceof Class) {
                objectClass = (Class<?>) paramType.getRawType();
            }
            int paramIndex = -1;
            if (objectClass != null) {
                for (int i = 0, size = objectClass.getTypeParameters().length; i < size; ++i) {
                    TypeVariable item = objectClass.getTypeParameters()[i];
                    if (item.getName().equals(typeVar.getName())) {
                        paramIndex = i;
                        break;
                    }
                }
            }
            if (paramIndex != -1) {
                itemType = paramType.getActualTypeArguments()[paramIndex];
                if (!itemType.equals(this.itemType)) {
                    itemTypeDeser = parser.getConfig().getDeserializer(itemType);
                }
            }
        } else if (itemType instanceof ParameterizedType) {
            ParameterizedType parameterizedItemType = (ParameterizedType) itemType;
            Type[] itemActualTypeArgs = parameterizedItemType.getActualTypeArguments();
            if (itemActualTypeArgs.length == 1 && itemActualTypeArgs[0] instanceof TypeVariable) {
                TypeVariable typeVar = (TypeVariable) itemActualTypeArgs[0];
                ParameterizedType paramType = (ParameterizedType) objectType;
                Class<?> objectClass = null;
                if (paramType.getRawType() instanceof Class) {
                    objectClass = (Class<?>) paramType.getRawType();
                }
                int paramIndex = -1;
                if (objectClass != null) {
                    for (int i = 0, size = objectClass.getTypeParameters().length; i < size; ++i) {
                        TypeVariable item = objectClass.getTypeParameters()[i];
                        if (item.getName().equals(typeVar.getName())) {
                            paramIndex = i;
                            break;
                        }
                    }
                }
                if (paramIndex != -1) {
                    itemActualTypeArgs[0] = paramType.getActualTypeArguments()[paramIndex];
                    itemType = new ParameterizedTypeImpl(itemActualTypeArgs, parameterizedItemType.getOwnerType(), parameterizedItemType.getRawType());
                }
            }
        }
    }
    final JSONLexer lexer = parser.lexer;
    if (lexer.token() == JSONToken.LBRACKET) {
        if (itemTypeDeser == null) {
            itemTypeDeser = deserializer = parser.getConfig().getDeserializer(itemType);
            itemFastMatchToken = deserializer.getFastMatchToken();
        }
        lexer.nextToken(itemFastMatchToken);
        for (int i = 0; ; ++i) {
            if (lexer.isEnabled(Feature.AllowArbitraryCommas)) {
                while (lexer.token() == JSONToken.COMMA) {
                    lexer.nextToken();
                    continue;
                }
            }
            if (lexer.token() == JSONToken.RBRACKET) {
                break;
            }
            Object val = itemTypeDeser.deserialze(parser, itemType, i);
            array.add(val);
            parser.checkListResolve(array);
            if (lexer.token() == JSONToken.COMMA) {
                lexer.nextToken(itemFastMatchToken);
                continue;
            }
        }
        lexer.nextToken(JSONToken.COMMA);
    } else {
        if (itemTypeDeser == null) {
            itemTypeDeser = deserializer = parser.getConfig().getDeserializer(itemType);
        }
        Object val = itemTypeDeser.deserialze(parser, itemType, 0);
        array.add(val);
        parser.checkListResolve(array);
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) JSONLexer(com.alibaba.fastjson.parser.JSONLexer) ParameterizedTypeImpl(com.alibaba.fastjson.util.ParameterizedTypeImpl)

Example 2 with ParameterizedTypeImpl

use of com.alibaba.fastjson.util.ParameterizedTypeImpl in project fastjson by alibaba.

the class FieldInfoTest method test_null.

public void test_null() throws Exception {
    FieldInfo fieldInfo = new FieldInfo("getValue", Entity.class.getMethod("getValue"), null, null, null, 0, 0, 0, null, 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, 0, 0, 0, null, 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, 0, 0, 0, null, null, null);
    ParameterizedTypeImpl actualFieldType = (ParameterizedTypeImpl) fieldInfoOfListField.fieldType;
    Assert.assertEquals(actualFieldType.getActualTypeArguments()[0], ValueObject.class);
    Assert.assertEquals(actualFieldType.getRawType(), List.class);
    Assert.assertEquals(fieldInfoOfListField.fieldClass, List.class);
    Assert.assertEquals(null, ((ParameterizedTypeImpl) type).getOwnerType());
    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, 0, 0, 0, null, null, null);
    Assert.assertEquals(fieldInfoOfSetter.fieldType, ValueObject.class);
    Assert.assertEquals(fieldInfoOfSetter.fieldClass, ValueObject.class);
    fieldInfoOfSetter.toString();
    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, 0, 0, 0, null, null, null);
    Assert.assertEquals(actualFieldType.getActualTypeArguments()[0], ValueObject.class);
    Assert.assertEquals(actualFieldType.getRawType(), List.class);
    Assert.assertEquals(fieldInfoOfListSetter.fieldClass, List.class);
}
Also used : Field(java.lang.reflect.Field) Type(java.lang.reflect.Type) List(java.util.List) Method(java.lang.reflect.Method) ParameterizedTypeImpl(com.alibaba.fastjson.util.ParameterizedTypeImpl) FieldInfo(com.alibaba.fastjson.util.FieldInfo)

Aggregations

ParameterizedTypeImpl (com.alibaba.fastjson.util.ParameterizedTypeImpl)2 Type (java.lang.reflect.Type)2 JSONLexer (com.alibaba.fastjson.parser.JSONLexer)1 FieldInfo (com.alibaba.fastjson.util.FieldInfo)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 TypeVariable (java.lang.reflect.TypeVariable)1 WildcardType (java.lang.reflect.WildcardType)1 List (java.util.List)1