destinationMap.put(fieldName, list);
}
list.add(mapValue);
} else if (field != null) {
// not a map: store in field value
FieldInfo fieldInfo = FieldInfo.of(field);
if (fieldClass == Object.class) {
// field is an Object: store as ArrayList of element maps
@SuppressWarnings("unchecked")
Collection<Object> list = (Collection<Object>) fieldInfo.getValue(destination);
if (list == null) {
list = new ArrayList<Object>(1);
fieldInfo.setValue(destination, list);
}
list.add(mapValue);
} else {
// field is a Map: store as a single element map
fieldInfo.setValue(destination, mapValue);
}
} else {
// GenericXml: store as ArrayList of elements
GenericXml atom = (GenericXml) destination;
@SuppressWarnings("unchecked")
Collection<Object> list = (Collection<Object>) atom.get(fieldName);
if (list == null) {
list = new ArrayList<Object>(1);
atom.set(fieldName, list);
}
list.add(mapValue);
}
} else if (isArray || Types.isAssignableToOrFrom(fieldClass, Collection.class)) {
// TODO(yanivi): some duplicate code here; isolate into reusable methods
FieldInfo fieldInfo = FieldInfo.of(field);
Object elementValue = null;
Type subFieldType =
isArray ? Types.getArrayComponentType(fieldType) : Types.getIterableParameter(
fieldType);
Class<?> rawArrayComponentType =
Types.getRawArrayComponentType(context, subFieldType);
subFieldType = Data.resolveWildcardTypeOrTypeVariable(context, subFieldType);
Class<?> subFieldClass =
subFieldType instanceof Class<?> ? (Class<?>) subFieldType : null;
if (subFieldType instanceof ParameterizedType) {
subFieldClass = Types.getRawClass((ParameterizedType) subFieldType);
}
if (Data.isPrimitive(subFieldType)) {
elementValue = parseTextContentForElement(parser, context, false, subFieldType);
} else if (subFieldType == null || subFieldClass != null
&& Types.isAssignableToOrFrom(subFieldClass, Map.class)) {
elementValue = Data.newMapInstance(subFieldClass);
int contextSize = context.size();
if (subFieldType != null) {
context.add(subFieldType);
}
Type subValueType =
subFieldType != null && Map.class.isAssignableFrom(subFieldClass)
? Types.getMapValueParameter(subFieldType) : null;
subValueType = Data.resolveWildcardTypeOrTypeVariable(context, subValueType);
isStopped = parseElementInternal(parser,
context,
elementValue,
subValueType,
namespaceDictionary,
customizeParser);
if (subFieldType != null) {
context.remove(contextSize);
}
} else {
elementValue = Types.newInstance(rawArrayComponentType);
int contextSize = context.size();
context.add(fieldType);
isStopped = parseElementInternal(parser,
context,
elementValue,
null,
namespaceDictionary,
customizeParser);
context.remove(contextSize);
}
if (isArray) {
// array field: add new element to array value map
if (field == null) {
arrayValueMap.put(fieldName, rawArrayComponentType, elementValue);
} else {
arrayValueMap.put(field, rawArrayComponentType, elementValue);
}
} else {
// collection: add new element to collection
@SuppressWarnings("unchecked")
Collection<Object> collectionValue = (Collection<Object>) (field == null
? destinationMap.get(fieldName) : fieldInfo.getValue(destination));
if (collectionValue == null) {
collectionValue = Data.newCollectionInstance(fieldType);
setValue(collectionValue,
field,
destination,