public void add()

in xstream/src/java/com/thoughtworks/xstream/mapper/ImplicitCollectionMapper.java [103:151]


    public void add(final Class<?> definedIn, final String fieldName, final String itemFieldName, Class<?> itemType,
            final String keyFieldName) {
        Field field = null;
        if (definedIn != null) {
            Class<?> declaredIn = definedIn;
            while (declaredIn != Object.class) {
                try {
                    field = declaredIn.getDeclaredField(fieldName);
                    if (!Modifier.isStatic(field.getModifiers())) {
                        break;
                    }
                    field = null;
                } catch (final SecurityException e) {
                    throw new InitializationException("Access denied for field with implicit collection", e);
                } catch (final NoSuchFieldException e) {
                    declaredIn = declaredIn.getSuperclass();
                }
            }
        }
        if (field == null) {
            throw new InitializationException("No field \"" + fieldName + "\" for implicit collection");
        } else if (Map.class.isAssignableFrom(field.getType())) {
            if (itemFieldName == null && keyFieldName == null) {
                itemType = Map.Entry.class;
            }
        } else if (!Collection.class.isAssignableFrom(field.getType())) {
            final Class<?> fieldType = field.getType();
            if (!fieldType.isArray()) {
                throw new InitializationException("Field \"" + fieldName + "\" declares no collection or array");
            } else {
                Class<?> componentType = fieldType.getComponentType();
                componentType = componentType.isPrimitive() ? Primitives.box(componentType) : componentType;
                if (itemType == null) {
                    itemType = componentType;
                } else {
                    itemType = itemType.isPrimitive() ? Primitives.box(itemType) : itemType;
                    if (!componentType.isAssignableFrom(itemType)) {
                        throw new InitializationException("Field \""
                            + fieldName
                            + "\" declares an array, but the array type is not compatible with "
                            + itemType.getName());

                    }
                }
            }
        }
        final ImplicitCollectionMapperForClass mapper = getOrCreateMapper(definedIn);
        mapper.add(new ImplicitCollectionMappingImpl(fieldName, itemType, itemFieldName, keyFieldName));
    }