Example usage for org.springframework.core.convert TypeDescriptor getElementTypeDescriptor

List of usage examples for org.springframework.core.convert TypeDescriptor getElementTypeDescriptor

Introduction

In this page you can find the example usage for org.springframework.core.convert TypeDescriptor getElementTypeDescriptor.

Prototype

@Nullable
public TypeDescriptor getElementTypeDescriptor() 

Source Link

Document

If this type is an array, returns the array's component type.

Usage

From source file:ch.rasc.wampspring.method.MethodParameterConverter.java

@SuppressWarnings("unchecked")
private Object convertListElements(TypeDescriptor td, Object convertedValue) {
    if (List.class.isAssignableFrom(convertedValue.getClass()) && td.isCollection()
            && td.getElementTypeDescriptor() != null) {
        Class<?> elementType = td.getElementTypeDescriptor().getType();

        Collection<Object> convertedList = new ArrayList<>();
        for (Object record : (List<Object>) convertedValue) {
            Object convertedObject = this.objectMapper.convertValue(record, elementType);
            convertedList.add(convertedObject);
        }// w w w. ja  va2 s . com
        return convertedList;

    }
    return convertedValue;
}

From source file:ch.rasc.wampspring.method.MethodParameterConverter.java

public Object convert(MethodParameter parameter, Object argument) {
    if (argument == null) {
        if (parameter.getParameterType().getName().equals("java.util.Optional")) {
            return OptionalUnwrapper.empty();
        }//from  w w w.ja va 2s  .  co m

        return null;
    }

    Class<?> sourceClass = argument.getClass();
    Class<?> targetClass = parameter.getParameterType();

    TypeDescriptor td = new TypeDescriptor(parameter);

    if (targetClass.isAssignableFrom(sourceClass)) {
        return convertListElements(td, argument);
    }

    if (this.conversionService.canConvert(sourceClass, targetClass)) {
        try {
            return convertListElements(td, this.conversionService.convert(argument, targetClass));
        } catch (Exception e) {

            TypeFactory typeFactory = this.objectMapper.getTypeFactory();
            if (td.isCollection()) {
                JavaType type = CollectionType.construct(td.getType(),
                        typeFactory.constructType(td.getElementTypeDescriptor().getType()));
                return this.objectMapper.convertValue(argument, type);
            } else if (td.isArray()) {
                JavaType type = typeFactory.constructArrayType(td.getElementTypeDescriptor().getType());
                return this.objectMapper.convertValue(argument, type);
            }

            throw e;
        }
    }
    return this.objectMapper.convertValue(argument, targetClass);
}

From source file:net.sf.juffrou.reflect.JuffrouTypeConverterDelegate.java

@SuppressWarnings("unchecked")
private Collection convertToTypedCollection(Collection original, String propertyName, Class requiredType,
        TypeDescriptor typeDescriptor) {

    if (!Collection.class.isAssignableFrom(requiredType)) {
        return original;
    }// w  w w. j  av  a 2  s .co  m

    boolean approximable = CollectionFactory.isApproximableCollectionType(requiredType);
    if (!approximable && !canCreateCopy(requiredType)) {
        if (logger.isDebugEnabled()) {
            logger.debug("Custom Collection type [" + original.getClass().getName()
                    + "] does not allow for creating a copy - injecting original Collection as-is");
        }
        return original;
    }

    boolean originalAllowed = requiredType.isInstance(original);
    typeDescriptor = typeDescriptor.narrow(original);
    TypeDescriptor elementType = typeDescriptor.getElementTypeDescriptor();
    if (elementType == null && originalAllowed
            && !this.propertyEditorRegistry.hasCustomEditorForElement(null, propertyName)) {
        return original;
    }

    Iterator it;
    try {
        it = original.iterator();
        if (it == null) {
            if (logger.isDebugEnabled()) {
                logger.debug("Collection of type [" + original.getClass().getName()
                        + "] returned null Iterator - injecting original Collection as-is");
            }
            return original;
        }
    } catch (Throwable ex) {
        if (logger.isDebugEnabled()) {
            logger.debug("Cannot access Collection of type [" + original.getClass().getName()
                    + "] - injecting original Collection as-is: " + ex);
        }
        return original;
    }

    Collection convertedCopy;
    try {
        if (approximable) {
            convertedCopy = CollectionFactory.createApproximateCollection(original, original.size());
        } else {
            convertedCopy = (Collection) requiredType.newInstance();
        }
    } catch (Throwable ex) {
        if (logger.isDebugEnabled()) {
            logger.debug("Cannot create copy of Collection type [" + original.getClass().getName()
                    + "] - injecting original Collection as-is: " + ex);
        }
        return original;
    }

    int i = 0;
    for (; it.hasNext(); i++) {
        Object element = it.next();
        String indexedPropertyName = buildIndexedPropertyName(propertyName, i);
        Object convertedElement = convertIfNecessary(indexedPropertyName, null, element,
                (elementType != null ? elementType.getType() : null), elementType);
        try {
            convertedCopy.add(convertedElement);
        } catch (Throwable ex) {
            if (logger.isDebugEnabled()) {
                logger.debug("Collection type [" + original.getClass().getName()
                        + "] seems to be read-only - injecting original Collection as-is: " + ex);
            }
            return original;
        }
        originalAllowed = originalAllowed && (element == convertedElement);
    }
    return (originalAllowed ? original : convertedCopy);
}

From source file:net.sf.juffrou.reflect.JuffrouTypeConverterDelegate.java

/**
 * Convert the value to the required type (if necessary from a String), for the specified property.
 * /*from  w w w .  j  a va  2  s.  co  m*/
 * @param propertyName
 *            name of the property
 * @param oldValue
 *            the previous value, if available (may be <code>null</code>)
 * @param newValue
 *            the proposed new value
 * @param requiredType
 *            the type we must convert to (or <code>null</code> if not known, for example in case of a collection
 *            element)
 * @param typeDescriptor
 *            the descriptor for the target property or field
 * @return the new value, possibly the result of type conversion
 * @throws IllegalArgumentException
 *             if type conversion failed
 */
@SuppressWarnings("unchecked")
public <T> T convertIfNecessary(String propertyName, Object oldValue, Object newValue, Class<T> requiredType,
        TypeDescriptor typeDescriptor) throws IllegalArgumentException {

    Object convertedValue = newValue;

    // Custom editor for this type?
    PropertyEditor editor = this.propertyEditorRegistry.findCustomEditor(requiredType, propertyName);

    ConversionFailedException firstAttemptEx = null;

    // No custom editor but custom ConversionService specified?
    ConversionService conversionService = this.propertyEditorRegistry.getConversionService();
    if (editor == null && conversionService != null && convertedValue != null && typeDescriptor != null) {
        TypeDescriptor sourceTypeDesc = TypeDescriptor.forObject(newValue);
        TypeDescriptor targetTypeDesc = typeDescriptor;
        if (conversionService.canConvert(sourceTypeDesc, targetTypeDesc)) {
            try {
                return (T) conversionService.convert(convertedValue, sourceTypeDesc, targetTypeDesc);
            } catch (ConversionFailedException ex) {
                // fallback to default conversion logic below
                firstAttemptEx = ex;
            }
        }
    }

    // Value not of required type?
    if (editor != null
            || (requiredType != null && !ClassUtils.isAssignableValue(requiredType, convertedValue))) {
        if (requiredType != null && Collection.class.isAssignableFrom(requiredType)
                && convertedValue instanceof String) {
            TypeDescriptor elementType = typeDescriptor.getElementTypeDescriptor();
            if (elementType != null && Enum.class.isAssignableFrom(elementType.getType())) {
                convertedValue = StringUtils.commaDelimitedListToStringArray((String) convertedValue);
            }
        }
        if (editor == null) {
            editor = findDefaultEditor(requiredType);
        }
        convertedValue = doConvertValue(oldValue, convertedValue, requiredType, editor);
    }

    boolean standardConversion = false;

    if (requiredType != null) {
        // Try to apply some standard type conversion rules if appropriate.

        if (convertedValue != null) {
            if (requiredType.isArray()) {
                // Array required -> apply appropriate conversion of elements.
                if (convertedValue instanceof String
                        && Enum.class.isAssignableFrom(requiredType.getComponentType())) {
                    convertedValue = StringUtils.commaDelimitedListToStringArray((String) convertedValue);
                }
                return (T) convertToTypedArray(convertedValue, propertyName, requiredType.getComponentType());
            } else if (convertedValue instanceof Collection) {
                // Convert elements to target type, if determined.
                convertedValue = convertToTypedCollection((Collection) convertedValue, propertyName,
                        requiredType, typeDescriptor);
                standardConversion = true;
            } else if (convertedValue instanceof Map) {
                // Convert keys and values to respective target type, if determined.
                convertedValue = convertToTypedMap((Map) convertedValue, propertyName, requiredType,
                        typeDescriptor);
                standardConversion = true;
            }
            if (convertedValue.getClass().isArray() && Array.getLength(convertedValue) == 1) {
                convertedValue = Array.get(convertedValue, 0);
                standardConversion = true;
            }
            if (String.class.equals(requiredType)
                    && ClassUtils.isPrimitiveOrWrapper(convertedValue.getClass())) {
                // We can stringify any primitive value...
                return (T) convertedValue.toString();
            } else if (convertedValue instanceof String && !requiredType.isInstance(convertedValue)) {
                if (firstAttemptEx == null && !requiredType.isInterface() && !requiredType.isEnum()) {
                    try {
                        Constructor strCtor = requiredType.getConstructor(String.class);
                        return (T) BeanUtils.instantiateClass(strCtor, convertedValue);
                    } catch (NoSuchMethodException ex) {
                        // proceed with field lookup
                        if (logger.isTraceEnabled()) {
                            logger.trace("No String constructor found on type [" + requiredType.getName() + "]",
                                    ex);
                        }
                    } catch (Exception ex) {
                        if (logger.isDebugEnabled()) {
                            logger.debug(
                                    "Construction via String failed for type [" + requiredType.getName() + "]",
                                    ex);
                        }
                    }
                }
                String trimmedValue = ((String) convertedValue).trim();
                if (requiredType.isEnum() && "".equals(trimmedValue)) {
                    // It's an empty enum identifier: reset the enum value to null.
                    return null;
                }
                convertedValue = attemptToConvertStringToEnum(requiredType, trimmedValue, convertedValue);
                standardConversion = true;
            }
        }

        if (!ClassUtils.isAssignableValue(requiredType, convertedValue)) {
            if (firstAttemptEx != null) {
                throw firstAttemptEx;
            }
            // Definitely doesn't match: throw IllegalArgumentException/IllegalStateException
            StringBuilder msg = new StringBuilder();
            msg.append("Cannot convert value of type [").append(ClassUtils.getDescriptiveType(newValue));
            msg.append("] to required type [").append(ClassUtils.getQualifiedName(requiredType)).append("]");
            if (propertyName != null) {
                msg.append(" for property '").append(propertyName).append("'");
            }
            if (editor != null) {
                msg.append(": PropertyEditor [").append(editor.getClass().getName())
                        .append("] returned inappropriate value of type [")
                        .append(ClassUtils.getDescriptiveType(convertedValue)).append("]");
                throw new IllegalArgumentException(msg.toString());
            } else {
                msg.append(": no matching editors or conversion strategy found");
                throw new IllegalStateException(msg.toString());
            }
        }
    }

    if (firstAttemptEx != null) {
        if (editor == null && !standardConversion && requiredType != null
                && !Object.class.equals(requiredType)) {
            throw firstAttemptEx;
        }
        logger.debug("Original ConversionService attempt failed - ignored since "
                + "PropertyEditor based conversion eventually succeeded", firstAttemptEx);
    }

    return (T) convertedValue;
}

From source file:net.yasion.common.core.bean.wrapper.impl.ExtendedBeanWrapperImpl.java

private Object newValue(Class<?> type, TypeDescriptor desc, String name) {
    try {//  ww w  .  j a v  a 2  s.c o m
        if (type.isArray()) {
            Class<?> componentType = type.getComponentType();
            // #TO#DO# - only handles 2-dimensional arrays
            if (componentType.isArray()) {
                Object array = Array.newInstance(componentType, 1);
                Array.set(array, 0, Array.newInstance(componentType.getComponentType(), 0));
                return array;
            } else {
                return Array.newInstance(componentType, 0);
            }
        } else if (Collection.class.isAssignableFrom(type)) {
            TypeDescriptor elementDesc = (desc != null ? desc.getElementTypeDescriptor() : null);
            return CollectionFactory.createCollection(type,
                    (elementDesc != null ? elementDesc.getType() : null), 16);
        } else if (Map.class.isAssignableFrom(type)) {
            TypeDescriptor keyDesc = (desc != null ? desc.getMapKeyTypeDescriptor() : null);
            return CollectionFactory.createMap(type, (keyDesc != null ? keyDesc.getType() : null), 16);
        } else {
            return type.newInstance();
        }
    } catch (Exception ex) {
        // #TO#DO#: Root cause exception context is lost here; just exception message preserved.
        // Should we throw another exception type that preserves context instead?
        throw new NullValueInNestedPathException(getRootClass(), this.nestedPath + name,
                "Could not instantiate property type [" + type.getName()
                        + "] to auto-grow nested property path: " + ex);
    }
}

From source file:org.springframework.beans.AbstractNestablePropertyAccessor.java

private Object newValue(Class<?> type, @Nullable TypeDescriptor desc, String name) {
    try {//from   w w  w.  j av  a2s  .  co m
        if (type.isArray()) {
            Class<?> componentType = type.getComponentType();
            // TODO - only handles 2-dimensional arrays
            if (componentType.isArray()) {
                Object array = Array.newInstance(componentType, 1);
                Array.set(array, 0, Array.newInstance(componentType.getComponentType(), 0));
                return array;
            } else {
                return Array.newInstance(componentType, 0);
            }
        } else if (Collection.class.isAssignableFrom(type)) {
            TypeDescriptor elementDesc = (desc != null ? desc.getElementTypeDescriptor() : null);
            return CollectionFactory.createCollection(type,
                    (elementDesc != null ? elementDesc.getType() : null), 16);
        } else if (Map.class.isAssignableFrom(type)) {
            TypeDescriptor keyDesc = (desc != null ? desc.getMapKeyTypeDescriptor() : null);
            return CollectionFactory.createMap(type, (keyDesc != null ? keyDesc.getType() : null), 16);
        } else {
            Constructor<?> ctor = type.getDeclaredConstructor();
            if (Modifier.isPrivate(ctor.getModifiers())) {
                throw new IllegalAccessException("Auto-growing not allowed with private constructor: " + ctor);
            }
            return BeanUtils.instantiateClass(ctor);
        }
    } catch (Throwable ex) {
        throw new NullValueInNestedPathException(getRootClass(), this.nestedPath + name,
                "Could not instantiate property type [" + type.getName()
                        + "] to auto-grow nested property path",
                ex);
    }
}

From source file:org.springframework.beans.BeanWrapperImpl.java

private Object newValue(Class<?> type, TypeDescriptor desc, String name) {
    try {/*from w ww . j a v a 2 s  .c om*/
        if (type.isArray()) {
            Class<?> componentType = type.getComponentType();
            // TODO - only handles 2-dimensional arrays
            if (componentType.isArray()) {
                Object array = Array.newInstance(componentType, 1);
                Array.set(array, 0, Array.newInstance(componentType.getComponentType(), 0));
                return array;
            } else {
                return Array.newInstance(componentType, 0);
            }
        } else if (Collection.class.isAssignableFrom(type)) {
            TypeDescriptor elementDesc = (desc != null ? desc.getElementTypeDescriptor() : null);
            return CollectionFactory.createCollection(type,
                    (elementDesc != null ? elementDesc.getType() : null), 16);
        } else if (Map.class.isAssignableFrom(type)) {
            TypeDescriptor keyDesc = (desc != null ? desc.getMapKeyTypeDescriptor() : null);
            return CollectionFactory.createMap(type, (keyDesc != null ? keyDesc.getType() : null), 16);
        } else {
            return type.newInstance();
        }
    } catch (Exception ex) {
        // TODO: Root cause exception context is lost here; just exception message preserved.
        // Should we throw another exception type that preserves context instead?
        throw new NullValueInNestedPathException(getRootClass(), this.nestedPath + name,
                "Could not instantiate property type [" + type.getName()
                        + "] to auto-grow nested property path: " + ex);
    }
}

From source file:org.springframework.beans.TypeConverterDelegate.java

/**
 * Convert the value to the required type (if necessary from a String),
 * for the specified property./*from ww w . j ava 2  s.co  m*/
 * @param propertyName name of the property
 * @param oldValue the previous value, if available (may be {@code null})
 * @param newValue the proposed new value
 * @param requiredType the type we must convert to
 * (or {@code null} if not known, for example in case of a collection element)
 * @param typeDescriptor the descriptor for the target property or field
 * @return the new value, possibly the result of type conversion
 * @throws IllegalArgumentException if type conversion failed
 */
@SuppressWarnings("unchecked")
@Nullable
public <T> T convertIfNecessary(@Nullable String propertyName, @Nullable Object oldValue,
        @Nullable Object newValue, @Nullable Class<T> requiredType, @Nullable TypeDescriptor typeDescriptor)
        throws IllegalArgumentException {

    // Custom editor for this type?
    PropertyEditor editor = this.propertyEditorRegistry.findCustomEditor(requiredType, propertyName);

    ConversionFailedException conversionAttemptEx = null;

    // No custom editor but custom ConversionService specified?
    ConversionService conversionService = this.propertyEditorRegistry.getConversionService();
    if (editor == null && conversionService != null && newValue != null && typeDescriptor != null) {
        TypeDescriptor sourceTypeDesc = TypeDescriptor.forObject(newValue);
        if (conversionService.canConvert(sourceTypeDesc, typeDescriptor)) {
            try {
                return (T) conversionService.convert(newValue, sourceTypeDesc, typeDescriptor);
            } catch (ConversionFailedException ex) {
                // fallback to default conversion logic below
                conversionAttemptEx = ex;
            }
        }
    }

    Object convertedValue = newValue;

    // Value not of required type?
    if (editor != null
            || (requiredType != null && !ClassUtils.isAssignableValue(requiredType, convertedValue))) {
        if (typeDescriptor != null && requiredType != null && Collection.class.isAssignableFrom(requiredType)
                && convertedValue instanceof String) {
            TypeDescriptor elementTypeDesc = typeDescriptor.getElementTypeDescriptor();
            if (elementTypeDesc != null) {
                Class<?> elementType = elementTypeDesc.getType();
                if (Class.class == elementType || Enum.class.isAssignableFrom(elementType)) {
                    convertedValue = StringUtils.commaDelimitedListToStringArray((String) convertedValue);
                }
            }
        }
        if (editor == null) {
            editor = findDefaultEditor(requiredType);
        }
        convertedValue = doConvertValue(oldValue, convertedValue, requiredType, editor);
    }

    boolean standardConversion = false;

    if (requiredType != null) {
        // Try to apply some standard type conversion rules if appropriate.

        if (convertedValue != null) {
            if (Object.class == requiredType) {
                return (T) convertedValue;
            } else if (requiredType.isArray()) {
                // Array required -> apply appropriate conversion of elements.
                if (convertedValue instanceof String
                        && Enum.class.isAssignableFrom(requiredType.getComponentType())) {
                    convertedValue = StringUtils.commaDelimitedListToStringArray((String) convertedValue);
                }
                return (T) convertToTypedArray(convertedValue, propertyName, requiredType.getComponentType());
            } else if (convertedValue instanceof Collection) {
                // Convert elements to target type, if determined.
                convertedValue = convertToTypedCollection((Collection<?>) convertedValue, propertyName,
                        requiredType, typeDescriptor);
                standardConversion = true;
            } else if (convertedValue instanceof Map) {
                // Convert keys and values to respective target type, if determined.
                convertedValue = convertToTypedMap((Map<?, ?>) convertedValue, propertyName, requiredType,
                        typeDescriptor);
                standardConversion = true;
            }
            if (convertedValue.getClass().isArray() && Array.getLength(convertedValue) == 1) {
                convertedValue = Array.get(convertedValue, 0);
                standardConversion = true;
            }
            if (String.class == requiredType && ClassUtils.isPrimitiveOrWrapper(convertedValue.getClass())) {
                // We can stringify any primitive value...
                return (T) convertedValue.toString();
            } else if (convertedValue instanceof String && !requiredType.isInstance(convertedValue)) {
                if (conversionAttemptEx == null && !requiredType.isInterface() && !requiredType.isEnum()) {
                    try {
                        Constructor<T> strCtor = requiredType.getConstructor(String.class);
                        return BeanUtils.instantiateClass(strCtor, convertedValue);
                    } catch (NoSuchMethodException ex) {
                        // proceed with field lookup
                        if (logger.isTraceEnabled()) {
                            logger.trace("No String constructor found on type [" + requiredType.getName() + "]",
                                    ex);
                        }
                    } catch (Exception ex) {
                        if (logger.isDebugEnabled()) {
                            logger.debug(
                                    "Construction via String failed for type [" + requiredType.getName() + "]",
                                    ex);
                        }
                    }
                }
                String trimmedValue = ((String) convertedValue).trim();
                if (requiredType.isEnum() && "".equals(trimmedValue)) {
                    // It's an empty enum identifier: reset the enum value to null.
                    return null;
                }
                convertedValue = attemptToConvertStringToEnum(requiredType, trimmedValue, convertedValue);
                standardConversion = true;
            } else if (convertedValue instanceof Number && Number.class.isAssignableFrom(requiredType)) {
                convertedValue = NumberUtils.convertNumberToTargetClass((Number) convertedValue,
                        (Class<Number>) requiredType);
                standardConversion = true;
            }
        } else {
            // convertedValue == null
            if (requiredType == Optional.class) {
                convertedValue = Optional.empty();
            }
        }

        if (!ClassUtils.isAssignableValue(requiredType, convertedValue)) {
            if (conversionAttemptEx != null) {
                // Original exception from former ConversionService call above...
                throw conversionAttemptEx;
            } else if (conversionService != null && typeDescriptor != null) {
                // ConversionService not tried before, probably custom editor found
                // but editor couldn't produce the required type...
                TypeDescriptor sourceTypeDesc = TypeDescriptor.forObject(newValue);
                if (conversionService.canConvert(sourceTypeDesc, typeDescriptor)) {
                    return (T) conversionService.convert(newValue, sourceTypeDesc, typeDescriptor);
                }
            }

            // Definitely doesn't match: throw IllegalArgumentException/IllegalStateException
            StringBuilder msg = new StringBuilder();
            msg.append("Cannot convert value of type '").append(ClassUtils.getDescriptiveType(newValue));
            msg.append("' to required type '").append(ClassUtils.getQualifiedName(requiredType)).append("'");
            if (propertyName != null) {
                msg.append(" for property '").append(propertyName).append("'");
            }
            if (editor != null) {
                msg.append(": PropertyEditor [").append(editor.getClass().getName())
                        .append("] returned inappropriate value of type '")
                        .append(ClassUtils.getDescriptiveType(convertedValue)).append("'");
                throw new IllegalArgumentException(msg.toString());
            } else {
                msg.append(": no matching editors or conversion strategy found");
                throw new IllegalStateException(msg.toString());
            }
        }
    }

    if (conversionAttemptEx != null) {
        if (editor == null && !standardConversion && requiredType != null && Object.class != requiredType) {
            throw conversionAttemptEx;
        }
        logger.debug("Original ConversionService attempt failed - ignored since "
                + "PropertyEditor based conversion eventually succeeded", conversionAttemptEx);
    }

    return (T) convertedValue;
}

From source file:org.springframework.beans.TypeConverterDelegate.java

@SuppressWarnings("unchecked")
private Collection<?> convertToTypedCollection(Collection<?> original, @Nullable String propertyName,
        Class<?> requiredType, @Nullable TypeDescriptor typeDescriptor) {

    if (!Collection.class.isAssignableFrom(requiredType)) {
        return original;
    }/* ww  w  .ja v  a  2s  .  co  m*/

    boolean approximable = CollectionFactory.isApproximableCollectionType(requiredType);
    if (!approximable && !canCreateCopy(requiredType)) {
        if (logger.isDebugEnabled()) {
            logger.debug("Custom Collection type [" + original.getClass().getName()
                    + "] does not allow for creating a copy - injecting original Collection as-is");
        }
        return original;
    }

    boolean originalAllowed = requiredType.isInstance(original);
    TypeDescriptor elementType = (typeDescriptor != null ? typeDescriptor.getElementTypeDescriptor() : null);
    if (elementType == null && originalAllowed
            && !this.propertyEditorRegistry.hasCustomEditorForElement(null, propertyName)) {
        return original;
    }

    Iterator<?> it;
    try {
        it = original.iterator();
    } catch (Throwable ex) {
        if (logger.isDebugEnabled()) {
            logger.debug("Cannot access Collection of type [" + original.getClass().getName()
                    + "] - injecting original Collection as-is: " + ex);
        }
        return original;
    }

    Collection<Object> convertedCopy;
    try {
        if (approximable) {
            convertedCopy = CollectionFactory.createApproximateCollection(original, original.size());
        } else {
            convertedCopy = (Collection<Object>) ReflectionUtils.accessibleConstructor(requiredType)
                    .newInstance();
        }
    } catch (Throwable ex) {
        if (logger.isDebugEnabled()) {
            logger.debug("Cannot create copy of Collection type [" + original.getClass().getName()
                    + "] - injecting original Collection as-is: " + ex);
        }
        return original;
    }

    int i = 0;
    for (; it.hasNext(); i++) {
        Object element = it.next();
        String indexedPropertyName = buildIndexedPropertyName(propertyName, i);
        Object convertedElement = convertIfNecessary(indexedPropertyName, null, element,
                (elementType != null ? elementType.getType() : null), elementType);
        try {
            convertedCopy.add(convertedElement);
        } catch (Throwable ex) {
            if (logger.isDebugEnabled()) {
                logger.debug("Collection type [" + original.getClass().getName()
                        + "] seems to be read-only - injecting original Collection as-is: " + ex);
            }
            return original;
        }
        originalAllowed = originalAllowed && (element == convertedElement);
    }
    return (originalAllowed ? original : convertedCopy);
}