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);
}
}
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);
}
Aggregations