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

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

Introduction

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

Prototype

@Nullable
public TypeDescriptor getMapKeyTypeDescriptor() 

Source Link

Document

If this type is a Map and its key type is parameterized, returns the map's key type.

Usage

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

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

    if (!Map.class.isAssignableFrom(requiredType)) {
        return original;
    }//  w w  w .  ja  v a  2  s .  com

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

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

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

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

    while (it.hasNext()) {
        Map.Entry entry = (Map.Entry) it.next();
        Object key = entry.getKey();
        Object value = entry.getValue();
        String keyedPropertyName = buildKeyedPropertyName(propertyName, key);
        Object convertedKey = convertIfNecessary(keyedPropertyName, null, key,
                (keyType != null ? keyType.getType() : null), keyType);
        Object convertedValue = convertIfNecessary(keyedPropertyName, null, value,
                (valueType != null ? valueType.getType() : null), valueType);
        try {
            convertedCopy.put(convertedKey, convertedValue);
        } catch (Throwable ex) {
            if (logger.isDebugEnabled()) {
                logger.debug("Map type [" + original.getClass().getName()
                        + "] seems to be read-only - injecting original Map as-is: " + ex);
            }
            return original;
        }
        originalAllowed = originalAllowed && (key == convertedKey) && (value == convertedValue);
    }
    return (originalAllowed ? original : convertedCopy);
}

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

private Object newValue(Class<?> type, TypeDescriptor desc, String name) {
    try {//w w w .java2s  .  c  om
        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   www.j a  v  a2  s . 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 {//ww  w . jav  a 2 s  . c  o  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 {
            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

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

    if (!Map.class.isAssignableFrom(requiredType)) {
        return original;
    }/*from w w  w. ja v a 2s .  c  om*/

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

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

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

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

    while (it.hasNext()) {
        Map.Entry<?, ?> entry = (Map.Entry<?, ?>) it.next();
        Object key = entry.getKey();
        Object value = entry.getValue();
        String keyedPropertyName = buildKeyedPropertyName(propertyName, key);
        Object convertedKey = convertIfNecessary(keyedPropertyName, null, key,
                (keyType != null ? keyType.getType() : null), keyType);
        Object convertedValue = convertIfNecessary(keyedPropertyName, null, value,
                (valueType != null ? valueType.getType() : null), valueType);
        try {
            convertedCopy.put(convertedKey, convertedValue);
        } catch (Throwable ex) {
            if (logger.isDebugEnabled()) {
                logger.debug("Map type [" + original.getClass().getName()
                        + "] seems to be read-only - injecting original Map as-is: " + ex);
            }
            return original;
        }
        originalAllowed = originalAllowed && (key == convertedKey) && (value == convertedValue);
    }
    return (originalAllowed ? original : convertedCopy);
}