List of usage examples for org.springframework.core CollectionFactory isApproximableMapType
public static boolean isApproximableMapType(@Nullable Class<?> mapType)
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 . jav 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:org.springframework.beans.factory.support.ConstructorResolver.java
/** * Template method for resolving the specified argument which is supposed to be autowired. */// w w w . j av a 2s. c o m @Nullable protected Object resolveAutowiredArgument(MethodParameter param, String beanName, @Nullable Set<String> autowiredBeanNames, TypeConverter typeConverter, boolean fallback) { Class<?> paramType = param.getParameterType(); if (InjectionPoint.class.isAssignableFrom(paramType)) { InjectionPoint injectionPoint = currentInjectionPoint.get(); if (injectionPoint == null) { throw new IllegalStateException("No current InjectionPoint available for " + param); } return injectionPoint; } try { return this.beanFactory.resolveDependency(new DependencyDescriptor(param, true), beanName, autowiredBeanNames, typeConverter); } catch (NoUniqueBeanDefinitionException ex) { throw ex; } catch (NoSuchBeanDefinitionException ex) { if (fallback) { // Single constructor or factory method -> let's return an empty array/collection // for e.g. a vararg or a non-null List/Set/Map parameter. if (paramType.isArray()) { return Array.newInstance(paramType.getComponentType(), 0); } else if (CollectionFactory.isApproximableCollectionType(paramType)) { return CollectionFactory.createCollection(paramType, 0); } else if (CollectionFactory.isApproximableMapType(paramType)) { return CollectionFactory.createMap(paramType, 0); } } throw 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 ww . j ava 2 s. 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); }
From source file:org.tinygroup.beanwrapper.TypeConverterDelegate.java
/** * Convert the value to the required type (if necessary from a String), * for the specified property./* ww w . ja va 2 s . c om*/ * @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 descriptor the JavaBeans descriptor for the property * @param methodParam the method parameter that is the target of the conversion * (may be <code>null</code>) * @return the new value, possibly the result of type conversion * @throws IllegalArgumentException if type conversion failed */ protected Object convertIfNecessary(String propertyName, Object oldValue, Object newValue, Class requiredType, PropertyDescriptor descriptor, MethodParameter methodParam) throws IllegalArgumentException { Object convertedValue = newValue; // Custom editor for this type? PropertyEditor editor = this.propertyEditorRegistry.findCustomEditor(requiredType, propertyName); // Value not of required type? if (editor != null || (requiredType != null && !ClassUtils.isAssignableValue(requiredType, convertedValue))) { if (editor == null) { editor = findDefaultEditor(requiredType, descriptor); } convertedValue = doConvertValue(oldValue, convertedValue, requiredType, editor); } if (requiredType != null) { // Try to apply some standard type conversion rules if appropriate. if (convertedValue != null) { if (String.class.equals(requiredType) && ClassUtils.isPrimitiveOrWrapper(convertedValue.getClass())) { // We can stringify any primitive value... return convertedValue.toString(); } else if (requiredType.isArray()) { // Array required -> apply appropriate conversion of elements. return convertToTypedArray(convertedValue, propertyName, requiredType.getComponentType()); } else if (convertedValue instanceof Collection && CollectionFactory.isApproximableCollectionType(requiredType)) { // Convert elements to target type, if determined. convertedValue = convertToTypedCollection((Collection) convertedValue, propertyName, methodParam); } else if (convertedValue instanceof Map && CollectionFactory.isApproximableMapType(requiredType)) { // Convert keys and values to respective target type, if determined. convertedValue = convertToTypedMap((Map) convertedValue, propertyName, methodParam); } else if (convertedValue instanceof String && !requiredType.isInstance(convertedValue)) { String strValue = ((String) convertedValue).trim(); if (JdkVersion.isAtLeastJava15() && requiredType.isEnum() && "".equals(strValue)) { // It's an empty enum identifier: reset the enum value to null. return null; } // Try field lookup as fallback: for JDK 1.5 enum or custom enum // with values defined as static fields. Resulting value still needs // to be checked, hence we don't return it right away. try { Field enumField = requiredType.getField(strValue); convertedValue = enumField.get(null); } catch (Exception ex) { if (logger.isTraceEnabled()) { logger.trace("Field [" + convertedValue + "] isn't an enum value", ex); } } } } if (!ClassUtils.isAssignableValue(requiredType, convertedValue)) { // Definitely doesn't match: throw IllegalArgumentException. StringBuffer msg = new StringBuffer(); 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 '" + propertyName + "'"); } if (editor != null) { msg.append( ": PropertyEditor [" + editor.getClass().getName() + "] returned inappropriate value"); } else { msg.append(": no matching editors or conversion strategy found"); } throw new IllegalArgumentException(msg.toString()); } } return convertedValue; }