@SuppressWarnings("unchecked") protected Collection convertToTypedCollection( Collection original, String propertyName, Class requiredType, TypeDescriptor typeDescriptor) { boolean originalAllowed = requiredType.isInstance(original); if (!originalAllowed && !Collection.class.isAssignableFrom(requiredType)) { return original; } MethodParameter methodParam = typeDescriptor.getMethodParameter(); Class elementType = null; if (methodParam != null) { elementType = GenericCollectionTypeResolver.getCollectionParameterType(methodParam); } 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 (CollectionFactory.isApproximableCollectionType(requiredType)) { 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); if (methodParam != null) { methodParam.increaseNestingLevel(); } Object convertedElement = convertIfNecessary(indexedPropertyName, null, element, elementType, typeDescriptor); if (methodParam != null) { methodParam.decreaseNestingLevel(); } 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); }
@SuppressWarnings("unchecked") private Collection<?> convertToTypedCollection( Collection<?> original, String propertyName, Class<?> requiredType, TypeDescriptor typeDescriptor) { if (!Collection.class.isAssignableFrom(requiredType)) { return original; } 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.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<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); }