Package com.thoughtworks.xstream.core.util

Examples of com.thoughtworks.xstream.core.util.FastField


                final Object value) {
                if (!mapper.shouldSerializeMember(definedIn, fieldName)) {
                    return;
                }

                final FastField field = new FastField(definedIn, fieldName);
                final String alias = mapper.serializedMember(definedIn, fieldName);
                if (!defaultFieldDefinition.containsKey(alias)) {
                    final Class lookupType = sourceType;
                    defaultFieldDefinition.put(
                        alias, reflectionProvider.getField(lookupType, fieldName));
View Full Code Here


                        exception.add("target type", type.getName());
                        throw exception;
                    }

                    reflectionProvider.writeField(result, fieldName, value, declaringClass);
                    if (!seenFields.add(new FastField(declaringClass, fieldName))) {
                        throw new DuplicateFieldException(fieldName
                            + " ["
                            + declaringClass.getName()
                            + "]");
                    }
                }
            }
        }

        if (valueField != null) {
            final Class classDefiningField = valueField.getDeclaringClass();
            final String fieldName = valueField.getName();
            if (fieldName == null
                || !reflectionProvider.fieldDefinedInClass(fieldName, resultType)) {
                final ConversionException exception = new ConversionException(
                    "Cannot assign value to field of type");
                exception.add("element", reader.getNodeName());
                exception.add("field", fieldName);
                exception.add("target type", context.getRequiredType().getName());
                throw exception;
            }

            Class type;
            final String classAttribute = HierarchicalStreams
                .readClassAttribute(reader, mapper);
            if (classAttribute != null) {
                type = mapper.realClass(classAttribute);
            } else {
                type = mapper.defaultImplementationOf(reflectionProvider.getFieldType(
                    result, fieldName, classDefiningField));
            }

            final Field field = reflectionProvider.getField(classDefiningField, fieldName);
            final Object value = context.convertAnother(
                result, type,
                mapper.getLocalConverter(field.getDeclaringClass(), field.getName()));

            final Class definedType = reflectionProvider.getFieldType(
                result, fieldName, classDefiningField);
            if (!definedType.isPrimitive()) {
                type = definedType;
            }

            if (value != null && !type.isAssignableFrom(value.getClass())) {
                final ConversionException exception = new ConversionException(
                    "Cannot assign object to type");
                exception.add("object type", value.getClass().getName());
                exception.add("target type", type.getName());
                throw exception;
            }

            reflectionProvider.writeField(result, fieldName, value, classDefiningField);
            if (!seenFields.add(new FastField(classDefiningField, fieldName))) {
                throw new DuplicateFieldException(fieldName
                    + " ["
                    + classDefiningField.getName()
                    + "]");
            }
View Full Code Here

                        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 implicitCollectionsForCurrentObject = null;
        while (reader.hasMoreChildren()) {
            reader.moveDown();

            String originalNodeName = reader.getNodeName();
            Class classDefiningField = determineWhichClassDefinesField(reader);
            Class fieldDeclaringClass = classDefiningField == null
                ? result.getClass()
                : classDefiningField;
            String fieldName = mapper.realMember(fieldDeclaringClass, originalNodeName);
            Mapper.ImplicitCollectionMapping implicitCollectionMapping = mapper
                .getImplicitCollectionDefForFieldName(fieldDeclaringClass, fieldName);
            boolean fieldExistsInClass = implicitCollectionMapping == null
                && reflectionProvider.fieldDefinedInClass(fieldName, fieldDeclaringClass);
            Class type = implicitCollectionMapping == null || implicitCollectionMapping.getItemType() == null
                ? determineType(
                    reader, fieldExistsInClass, result, fieldName, classDefiningField)
                : implicitCollectionMapping.getItemType();
            final Object value;
            if (fieldExistsInClass) {
                Field field = reflectionProvider.getField(classDefiningField != null ? classDefiningField : result.getClass(), fieldName);
                if ((Modifier.isTransient(field.getModifiers()) && !shouldUnmarshalTransientFields())
                        || !mapper.shouldSerializeMember(classDefiningField != null ? classDefiningField : result.getClass(), fieldName)) {
                    reader.moveUp();
                    continue;
                }
                value = unmarshallField(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 {
                if (Map.Entry.class.equals(type)) {
                    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 {
                    value = type != null ? context.convertAnother(result, type) : null;
                }
            }
           
            if (value != null && !type.isAssignableFrom(value.getClass())) {
                throw new ConversionException("Cannot convert type " + value.getClass().getName() + " to type " + type.getName());
            }

            if (fieldExistsInClass) {
                reflectionProvider.writeField(result, fieldName, value, classDefiningField);
                seenFields.add(new FastField(classDefiningField, fieldName));
            } else if (type != null) {
                implicitCollectionsForCurrentObject = writeValueToImplicitCollection(context, value, implicitCollectionsForCurrentObject, result, originalNodeName);
            }

            reader.moveUp();
View Full Code Here

        super(wrapped);
        readResolve();
    }

    public void registerLocalConverter(Class definedIn, String fieldName, Converter converter) {
        localConverters.put(new FastField(definedIn, fieldName), converter);
    }
View Full Code Here

    public void registerLocalConverter(Class definedIn, String fieldName, Converter converter) {
        localConverters.put(new FastField(definedIn, fieldName), converter);
    }

    public Converter getLocalConverter(Class definedIn, String fieldName) {
        return (Converter)localConverters.get(new FastField(definedIn, fieldName));
    }
View Full Code Here

        fieldToAliasMap.put(key(type, fieldName), alias);
        aliasToFieldMap.put(key(type, alias), fieldName);
    }

    private Object key(Class type, String name) {
        return new FastField(type, name);
    }
View Full Code Here

TOP

Related Classes of com.thoughtworks.xstream.core.util.FastField

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.