private void writeValueToImplicitCollection()

in xstream/src/java/com/thoughtworks/xstream/converters/reflection/AbstractReflectionConverter.java [511:553]


    private void writeValueToImplicitCollection(final Object value,
            final Map<FieldLocation, Collection<? super Object>> implicitCollections, final Object result,
            final FieldLocation fieldLocation) {
        Collection<? super Object> collection = implicitCollections.get(fieldLocation);
        if (collection == null) {
            final Field field = reflectionProvider.getFieldOrNull(fieldLocation.definedIn, fieldLocation.fieldName);
            final Class<?> physicalFieldType = field != null
                ? field.getType()
                : reflectionProvider.getFieldType(result, fieldLocation.fieldName, null);
            if (physicalFieldType.isArray()) {
                collection = new ArraysList(physicalFieldType);
            } else {
                final Class<?> fieldType = mapper.defaultImplementationOf(physicalFieldType);
                if (!(Collection.class.isAssignableFrom(fieldType) || Map.class.isAssignableFrom(fieldType))) {
                    final ObjectAccessException oaex = new ObjectAccessException(
                        "Field is configured for an implicit Collection or Map, but is of an incompatible type");
                    oaex.add("field", result.getClass().getName() + "." + fieldLocation.fieldName);
                    oaex.add("field-type", fieldType.getName());
                    throw oaex;
                }
                if (pureJavaReflectionProvider == null) {
                    pureJavaReflectionProvider = new PureJavaReflectionProvider();
                }
                final Object instance = pureJavaReflectionProvider.newInstance(fieldType);
                if (instance instanceof Collection) {
                    @SuppressWarnings("unchecked")
                    final Collection<? super Object> uncheckedCollection = (Collection<? super Object>)instance;
                    collection = uncheckedCollection;
                } else {
                    final Mapper.ImplicitCollectionMapping implicitCollectionMapping = mapper
                        .getImplicitCollectionDefForFieldName(fieldLocation.definedIn, fieldLocation.fieldName);
                    @SuppressWarnings("unchecked")
                    final Map<Object, Object> map = (Map<Object, Object>)instance;
                    collection = new MappingList(map, implicitCollectionMapping.getKeyFieldName());
                }
                reflectionProvider.writeField(result, fieldLocation.fieldName, instance, field != null
                    ? field.getDeclaringClass()
                    : null);
            }
            implicitCollections.put(fieldLocation, collection);
        }
        collection.add(value);
    }