Search in sources :

Example 1 with SingleValueConverter

use of com.thoughtworks.xstream.converters.SingleValueConverter in project hudson-2.x by hudson.

the class RobustReflectionConverter method doUnmarshal.

public Object doUnmarshal(final Object result, final HierarchicalStreamReader reader, final UnmarshallingContext context) {
    final SeenFields seenFields = new SeenFields();
    Iterator it = reader.getAttributeNames();
    // Remember outermost Saveable encountered, for reporting below
    if (result instanceof Saveable && context.get("Saveable") == null)
        context.put("Saveable", result);
    // Process attributes before recursing into child elements.
    while (it.hasNext()) {
        String attrAlias = (String) it.next();
        String attrName = mapper.attributeForAlias(attrAlias);
        Class classDefiningField = determineWhichClassDefinesField(reader);
        boolean fieldExistsInClass = fieldDefinedInClass(result, attrName);
        if (fieldExistsInClass) {
            Field field = reflectionProvider.getField(result.getClass(), attrName);
            SingleValueConverter converter = mapper.getConverterFromAttribute(field.getDeclaringClass(), attrName, field.getType());
            Class type = field.getType();
            if (converter == null) {
                converter = mapper.getConverterFromItemType(type);
            }
            if (converter != null) {
                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());
                }
                reflectionProvider.writeField(result, attrName, value, classDefiningField);
                seenFields.add(classDefiningField, attrName);
            }
        }
    }
    Map implicitCollectionsForCurrentObject = null;
    while (reader.hasMoreChildren()) {
        reader.moveDown();
        try {
            String fieldName = mapper.realMember(result.getClass(), reader.getNodeName());
            boolean implicitCollectionHasSameName = mapper.getImplicitCollectionDefForFieldName(result.getClass(), reader.getNodeName()) != null;
            Class classDefiningField = determineWhichClassDefinesField(reader);
            boolean fieldExistsInClass = !implicitCollectionHasSameName && fieldDefinedInClass(result, fieldName);
            Class type = determineType(reader, fieldExistsInClass, result, fieldName, classDefiningField);
            final Object value;
            if (fieldExistsInClass) {
                Field field = reflectionProvider.getField(result.getClass(), fieldName);
                value = unmarshalField(context, result, type, field);
                // TODO the reflection provider should have returned the proper field in first place ....
                Class definedType = reflectionProvider.getFieldType(result, fieldName, classDefiningField);
                if (!definedType.isPrimitive()) {
                    type = definedType;
                }
            } else {
                value = context.convertAnother(result, type);
            }
            if (value != null && !type.isAssignableFrom(value.getClass())) {
                LOGGER.warning("Cannot convert type " + value.getClass().getName() + " to type " + type.getName());
            // behave as if we didn't see this element
            } else {
                if (fieldExistsInClass) {
                    reflectionProvider.writeField(result, fieldName, value, classDefiningField);
                    seenFields.add(classDefiningField, fieldName);
                } else {
                    implicitCollectionsForCurrentObject = writeValueToImplicitCollection(context, value, implicitCollectionsForCurrentObject, result, fieldName);
                }
            }
        } catch (NonExistentFieldException e) {
            LOGGER.log(WARNING, "Skipping a non-existent field " + e.getFieldName());
            addErrorInContext(context, e);
        } catch (CannotResolveClassException e) {
            LOGGER.log(WARNING, "Skipping a non-existent type " + e.getMessage());
            addErrorInContext(context, e);
        } catch (LinkageError e) {
            LOGGER.log(WARNING, "Failed to resolve a type " + e.getMessage());
            addErrorInContext(context, e);
        }
        reader.moveUp();
    }
    // Report any class/field errors in Saveable objects
    if (context.get("ReadError") != null && context.get("Saveable") == result) {
        OldDataMonitor.report((Saveable) result, (ArrayList<Throwable>) context.get("ReadError"));
        context.put("ReadError", null);
    }
    return result;
}
Also used : ConversionException(com.thoughtworks.xstream.converters.ConversionException) Saveable(hudson.model.Saveable) NonExistentFieldException(com.thoughtworks.xstream.converters.reflection.NonExistentFieldException) CannotResolveClassException(com.thoughtworks.xstream.mapper.CannotResolveClassException) Field(java.lang.reflect.Field) Iterator(java.util.Iterator) SingleValueConverter(com.thoughtworks.xstream.converters.SingleValueConverter) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with SingleValueConverter

use of com.thoughtworks.xstream.converters.SingleValueConverter in project hudson-2.x by hudson.

the class RobustReflectionConverter method doMarshal.

protected void doMarshal(final Object source, final HierarchicalStreamWriter writer, final MarshallingContext context) {
    final Set seenFields = new HashSet();
    final Set seenAsAttributes = new HashSet();
    // Attributes might be preferred to child elements ...
    reflectionProvider.visitSerializableFields(source, new ReflectionProvider.Visitor() {

        public void visit(String fieldName, Class type, Class definedIn, Object value) {
            SingleValueConverter converter = mapper.getConverterFromItemType(fieldName, type, definedIn);
            if (converter == null)
                converter = mapper.getConverterFromItemType(fieldName, type);
            if (converter == null)
                converter = mapper.getConverterFromItemType(type);
            if (converter != null) {
                if (value != null) {
                    final String str = converter.toString(value);
                    if (str != null) {
                        writer.addAttribute(mapper.aliasForAttribute(fieldName), str);
                    }
                }
                seenAsAttributes.add(fieldName);
            }
        }
    });
    // Child elements not covered already processed as attributes ...
    reflectionProvider.visitSerializableFields(source, new ReflectionProvider.Visitor() {

        public void visit(String fieldName, Class fieldType, Class definedIn, Object newObj) {
            if (!seenAsAttributes.contains(fieldName) && newObj != null) {
                Mapper.ImplicitCollectionMapping mapping = mapper.getImplicitCollectionDefForFieldName(source.getClass(), fieldName);
                if (mapping != null) {
                    if (mapping.getItemFieldName() != null) {
                        Collection list = (Collection) newObj;
                        for (Iterator iter = list.iterator(); iter.hasNext(); ) {
                            Object obj = iter.next();
                            writeField(fieldName, mapping.getItemFieldName(), mapping.getItemType(), definedIn, obj);
                        }
                    } else {
                        context.convertAnother(newObj);
                    }
                } else {
                    writeField(fieldName, fieldName, fieldType, definedIn, newObj);
                    seenFields.add(fieldName);
                }
            }
        }

        private void writeField(String fieldName, String aliasName, Class fieldType, Class definedIn, Object newObj) {
            try {
                if (!mapper.shouldSerializeMember(definedIn, aliasName)) {
                    return;
                }
                ExtendedHierarchicalStreamWriterHelper.startNode(writer, mapper.serializedMember(definedIn, aliasName), fieldType);
                Class actualType = newObj.getClass();
                Class defaultType = mapper.defaultImplementationOf(fieldType);
                if (!actualType.equals(defaultType)) {
                    String serializedClassName = mapper.serializedClass(actualType);
                    if (!serializedClassName.equals(mapper.serializedClass(defaultType))) {
                        writer.addAttribute(mapper.aliasForSystemAttribute("class"), serializedClassName);
                    }
                }
                if (seenFields.contains(aliasName)) {
                    writer.addAttribute(mapper.aliasForAttribute("defined-in"), mapper.serializedClass(definedIn));
                }
                Field field = reflectionProvider.getField(definedIn, fieldName);
                marshallField(context, newObj, field);
                writer.endNode();
            } catch (RuntimeException e) {
                // intercept an exception so that the stack trace shows how we end up marshalling the object in question
                throw new RuntimeException("Failed to serialize " + definedIn.getName() + "#" + fieldName + " for " + source.getClass(), e);
            }
        }
    });
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) Field(java.lang.reflect.Field) PureJavaReflectionProvider(com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider) ReflectionProvider(com.thoughtworks.xstream.converters.reflection.ReflectionProvider) Iterator(java.util.Iterator) SingleValueConverter(com.thoughtworks.xstream.converters.SingleValueConverter) Collection(java.util.Collection) HashSet(java.util.HashSet)

Aggregations

SingleValueConverter (com.thoughtworks.xstream.converters.SingleValueConverter)2 Field (java.lang.reflect.Field)2 Iterator (java.util.Iterator)2 ConversionException (com.thoughtworks.xstream.converters.ConversionException)1 NonExistentFieldException (com.thoughtworks.xstream.converters.reflection.NonExistentFieldException)1 PureJavaReflectionProvider (com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider)1 ReflectionProvider (com.thoughtworks.xstream.converters.reflection.ReflectionProvider)1 CannotResolveClassException (com.thoughtworks.xstream.mapper.CannotResolveClassException)1 Saveable (hudson.model.Saveable)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1