Package com.thoughtworks.xstream.converters

Examples of com.thoughtworks.xstream.converters.ConversionException


                    final Object value = converter.fromString(reader.getAttribute(attrAlias));
                    if (type.isPrimitive()) {
                        type = Primitives.box(type);
                    }
                    if (value != null && !type.isAssignableFrom(value.getClass())) {
                        throw new ConversionException("Cannot convert type "
                            + value.getClass().getName()
                            + " to type "
                            + type.getName());
                    }
                    seenFields.add(new FastField(classDefiningField, attrName));
                    reflectionProvider.writeField(result, attrName, value, classDefiningField);
                }
            }
        }

        Map<String, Collection<? super Object>> implicitCollectionsForCurrentObject = null;
        while (reader.hasMoreChildren()) {
            reader.moveDown();

            final String originalNodeName = reader.getNodeName();
            final Class<?> explicitDeclaringClass = readDeclaringClass(reader);
            final Class<?> fieldDeclaringClass = explicitDeclaringClass == null ? resultType : explicitDeclaringClass;
            final String fieldName = mapper.realMember(fieldDeclaringClass, originalNodeName);
            final Mapper.ImplicitCollectionMapping implicitCollectionMapping = mapper
                .getImplicitCollectionDefForFieldName(fieldDeclaringClass, fieldName);
            final Object value;
            String implicitFieldName = null;
            Field field = null;
            Class<?> type = null;
            if (implicitCollectionMapping == null) {
                // no item of an implicit collection for this name ... do we have a field?
                field = reflectionProvider.getFieldOrNull(fieldDeclaringClass, fieldName);
                if (field == null) {
                    // it is not a field ... do we have a field alias?
                    final Class<?> itemType = mapper.getItemTypeForItemFieldName(resultType, fieldName);
                    if (itemType != null) {
                        final String classAttribute = HierarchicalStreams.readClassAttribute(reader, mapper);
                        if (classAttribute != null) {
                            type = mapper.realClass(classAttribute);
                        } else {
                            type = itemType;
                        }
                    } else {
                        // it is not an alias ... do we have an element of an implicit
                        // collection based on type only?
                        try {
                            type = mapper.realClass(originalNodeName);
                            implicitFieldName = mapper.getFieldNameForItemTypeAndName(context.getRequiredType(), type,
                                originalNodeName);
                        } catch (final CannotResolveClassException e) {
                            // type stays null ...
                        }
                        if (type == null || type != null && implicitFieldName == null) {
                            // either not a type or element is a type alias, but does not
                            // belong to an implicit field
                            handleUnknownField(explicitDeclaringClass, fieldName, resultType, originalNodeName);

                            // element is unknown in declaring class, ignore it now
                            type = null;
                        }
                    }
                    if (type == null) {
                        // no type, no value
                        value = null;
                    } else {
                        if (Map.Entry.class.equals(type)) {
                            // it is an element of an implicit map with two elements now for
                            // key and value
                            reader.moveDown();
                            final Object key = context.convertAnother(result, HierarchicalStreams.readClassType(reader,
                                mapper));
                            reader.moveUp();
                            reader.moveDown();
                            final Object v = context.convertAnother(result, HierarchicalStreams.readClassType(reader,
                                mapper));
                            reader.moveUp();
                            value = Collections.singletonMap(key, v).entrySet().iterator().next();
                        } else {
                            // recurse info hierarchy
                            value = context.convertAnother(result, type);
                        }
                    }
                } else {
                    boolean fieldAlreadyChecked = false;

                    // we have a field, but do we have to address a hidden one?
                    if (explicitDeclaringClass == null) {
                        while (field != null
                            && !(fieldAlreadyChecked = shouldUnmarshalField(field)
                                && mapper.shouldSerializeMember(field.getDeclaringClass(), fieldName))) {
                            field = reflectionProvider.getFieldOrNull(field.getDeclaringClass().getSuperclass(),
                                fieldName);
                        }
                    }
                    if (field != null
                        && (fieldAlreadyChecked || shouldUnmarshalField(field)
                            && mapper.shouldSerializeMember(field.getDeclaringClass(), fieldName))) {

                        final String classAttribute = HierarchicalStreams.readClassAttribute(reader, mapper);
                        if (classAttribute != null) {
                            type = mapper.realClass(classAttribute);
                        } else {
                            type = mapper.defaultImplementationOf(field.getType());
                        }
                        // TODO the reflection provider should already return the proper field
                        value = unmarshallField(context, result, type, field);
                        final Class<?> definedType = field.getType();
                        if (!definedType.isPrimitive()) {
                            type = definedType;
                        }
                    } else {
                        value = null;
                    }
                }
            } else {
                // we have an implicit collection with defined names
                implicitFieldName = implicitCollectionMapping.getFieldName();
                type = implicitCollectionMapping.getItemType();
                if (type == null) {
                    final String classAttribute = HierarchicalStreams.readClassAttribute(reader, mapper);
                    type = mapper.realClass(classAttribute != null ? classAttribute : originalNodeName);
                }
                value = context.convertAnother(result, type);
            }

            if (value != null && !type.isAssignableFrom(value.getClass())) {
                throw new ConversionException("Cannot convert type "
                    + value.getClass().getName()
                    + " to type "
                    + type.getName());
            }
View Full Code Here


                @SuppressWarnings("unchecked")
                final Map.Entry<Object, Object> entry = (Map.Entry<Object, Object>)object;
                return map.put(entry.getKey(), entry.getValue()) == null;
            }

            throw new ConversionException("Element of type "
                + object.getClass().getName()
                + " is not defined as entry for map of type "
                + map.getClass().getName());
        }
View Full Code Here

        // this is an array as it's a non final value that's accessed from an anonymous inner class.
        final Class<?>[] currentType = new Class<?>[1];

        final String attributeName = mapper.aliasForSystemAttribute(ATTRIBUTE_SERIALIZATION);
        if (attributeName != null && !ATTRIBUTE_VALUE_CUSTOM.equals(reader.getAttribute(attributeName))) {
            throw new ConversionException("Cannot deserialize object with new readObject()/writeObject() methods");
        }

        final CustomObjectInputStream.StreamCallback callback = new CustomObjectInputStream.StreamCallback() {
            @Override
            public Object readFromStream() {
                reader.moveDown();
                final Class<?> type = HierarchicalStreams.readClassType(reader, mapper);
                final Object value = context.convertAnother(result, type);
                reader.moveUp();
                return value;
            }

            @Override
            public Map<String, Object> readFieldsFromStream() {
                final Map<String, Object> fields = new HashMap<String, Object>();
                reader.moveDown();
                if (reader.getNodeName().equals(ELEMENT_FIELDS)) {
                    // Maintain compatibility with XStream 1.1.0
                    while (reader.hasMoreChildren()) {
                        reader.moveDown();
                        if (!reader.getNodeName().equals(ELEMENT_FIELD)) {
                            throw new ConversionException("Expected <"
                                + ELEMENT_FIELD
                                + "/> element inside <"
                                + ELEMENT_FIELD
                                + "/>");
                        }
                        final String name = reader.getAttribute(ATTRIBUTE_NAME);
                        final Class<?> type = mapper.realClass(reader.getAttribute(ATTRIBUTE_CLASS));
                        final Object value = context.convertAnother(result, type);
                        fields.put(name, value);
                        reader.moveUp();
                    }
                } else if (reader.getNodeName().equals(ELEMENT_DEFAULT)) {
                    // New format introduced in XStream 1.1.1
                    final ObjectStreamClass objectStreamClass = ObjectStreamClass.lookup(currentType[0]);
                    while (reader.hasMoreChildren()) {
                        reader.moveDown();
                        final String name = mapper.realMember(currentType[0], reader.getNodeName());
                        if (mapper.shouldSerializeMember(currentType[0], name)) {
                            final String classAttribute = HierarchicalStreams.readClassAttribute(reader, mapper);
                            Class<?> type;
                            if (classAttribute != null) {
                                type = mapper.realClass(classAttribute);
                            } else {
                                final ObjectStreamField field = objectStreamClass.getField(name);
                                if (field == null) {
                                    throw new MissingFieldException(currentType[0].getName(), name);
                                }
                                type = field.getType();
                            }
                            final Object value = context.convertAnother(result, type);
                            fields.put(name, value);
                        }
                        reader.moveUp();
                    }
                } else {
                    throw new ConversionException("Expected <"
                        + ELEMENT_FIELDS
                        + "/> or <"
                        + ELEMENT_DEFAULT
                        + "/> element when calling ObjectInputStream.readFields()");
                }
                reader.moveUp();
                return fields;
            }

            @Override
            public void defaultReadObject() {
                if (!reader.hasMoreChildren()) {
                    return;
                }
                reader.moveDown();
                if (!reader.getNodeName().equals(ELEMENT_DEFAULT)) {
                    throw new ConversionException("Expected <" + ELEMENT_DEFAULT + "/> element in readObject() stream");
                }
                while (reader.hasMoreChildren()) {
                    reader.moveDown();

                    final String fieldName = mapper.realMember(currentType[0], reader.getNodeName());
View Full Code Here

    public void callReadObject(final Class<?> type, final Object object, final ObjectInputStream stream) {
        try {
            final Method readObjectMethod = getMethod(type, "readObject", false, ObjectInputStream.class);
            readObjectMethod.invoke(object, stream);
        } catch (final IllegalAccessException e) {
            throw new ConversionException("Could not call " + object.getClass().getName() + ".readObject()", e);
        } catch (final InvocationTargetException e) {
            throw new ConversionException("Could not call " + object.getClass().getName() + ".readObject()", e
                .getTargetException());
        }
    }
View Full Code Here

    public void callWriteObject(final Class<?> type, final Object instance, final ObjectOutputStream stream) {
        try {
            final Method readObjectMethod = getMethod(type, "writeObject", false, ObjectOutputStream.class);
            readObjectMethod.invoke(instance, stream);
        } catch (final IllegalAccessException e) {
            throw new ConversionException("Could not call " + instance.getClass().getName() + ".writeObject()", e);
        } catch (final InvocationTargetException e) {
            throw new ConversionException("Could not call " + instance.getClass().getName() + ".writeObject()", e
                .getTargetException());
        }
    }
View Full Code Here

        final String s = attribute.toString();
        final String className = attribute.getClass().getName();
        if (s.startsWith(className)) {
            return s.substring(className.length() + 1, s.length() - 1);
        }
        throw new ConversionException("Cannot find name of attribute of type " + className, ex);
    }
View Full Code Here

    @Override
    public Object fromString(final String str) {
        if (attributeMap.containsKey(str)) {
            return attributeMap.get(str);
        }
        throw new ConversionException("Cannot find attribute of type " + type.getName() + " with name " + str);
    }
View Full Code Here

            @SuppressWarnings("resource")
            final CustomObjectOutputStream objectOutput = CustomObjectOutputStream.getInstance(context, callback);
            externalizable.writeExternal(objectOutput);
            objectOutput.popCallback();
        } catch (final IOException e) {
            throw new ConversionException("Cannot serialize " + source.getClass().getName() + " using Externalization",
                e);
        }
    }
View Full Code Here

                externalizable.readExternal(objectInput);
                objectInput.popCallback();
            }
            return externalizable;
        } catch (final NoSuchMethodException e) {
            throw new ConversionException("Cannot construct " + type.getClass() + ", missing default constructor", e);
        } catch (final InvocationTargetException e) {
            throw new ConversionException("Cannot construct " + type.getClass(), e);
        } catch (final InstantiationException e) {
            throw new ConversionException("Cannot construct " + type.getClass(), e);
        } catch (final IllegalAccessException e) {
            throw new ConversionException("Cannot construct " + type.getClass(), e);
        } catch (final IOException e) {
            throw new ConversionException("Cannot externalize " + type.getClass(), e);
        } catch (final ClassNotFoundException e) {
            throw new ConversionException("Cannot externalize " + type.getClass(), e);
        }
    }
View Full Code Here

        final Callback[] callbacks = hasFactory ? ((Factory)source).getCallbacks() : getCallbacks(source);
        if (callbacks.length > 1) {
            if (hasFactory) {
                callbackIndexMap = createCallbackIndexMap((Factory)source);
            } else {
                final ConversionException exception = new ConversionException(
                    "Cannot handle CGLIB enhanced proxies without factory that have multiple callbacks");
                exception.add("proxy superclass", type.getSuperclass().getName());
                exception.add("number of callbacks", String.valueOf(callbacks.length));
                throw exception;
            }
            writer.startNode("callbacks");
            writer.startNode("mapping");
            context.convertAnother(callbackIndexMap);
View Full Code Here

TOP

Related Classes of com.thoughtworks.xstream.converters.ConversionException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.