List of usage examples for org.springframework.core CollectionFactory isApproximableCollectionType
public static boolean isApproximableCollectionType(@Nullable Class<?> collectionType)
From source file:com.alibaba.citrus.service.form.impl.GroupImpl.java
/** * fields//from w ww. j a va2 s . c o m * <p> * <code>isValidated()</code><code>true</code>group * </p> */ public void mapTo(Object object) { if (isValidated() || object == null) { return; } if (log.isDebugEnabled()) { log.debug("Mapping properties to fields: group=\"{}\", object={}", getName(), ObjectUtil.identityToString(object)); } BeanWrapper bean = new BeanWrapperImpl(object); getForm().getFormConfig().getPropertyEditorRegistrar().registerCustomEditors(bean); for (Field field : getFields()) { String propertyName = field.getFieldConfig().getPropertyName(); if (bean.isReadableProperty(propertyName)) { Object propertyValue = bean.getPropertyValue(propertyName); Class<?> propertyType = bean.getPropertyType(propertyName); PropertyEditor editor = bean.findCustomEditor(propertyType, propertyName); if (editor == null) { editor = BeanUtils.findEditorByConvention(propertyType); } if (editor == null) { if (propertyType.isArray() || CollectionFactory.isApproximableCollectionType(propertyType)) { field.setValues((String[]) bean.convertIfNecessary(propertyValue, String[].class)); } else { field.setValue(bean.convertIfNecessary(propertyValue, String.class)); } } else { editor.setValue(propertyValue); field.setValue(editor.getAsText()); } } else { log.debug("No readable property \"{}\" found in type {}", propertyName, object.getClass().getName()); } } }
From source file:com.alibaba.citrus.service.requestcontext.support.ValueListSupport.java
private <T> T convert(Class<T> type, MethodParameter methodParameter, Object[] values, Object defaultValue) { if (values == null) { values = EMPTY_STRING_ARRAY;/*from www . j av a2 s . co m*/ } Object convertedValue = null; boolean requiresArray = type.isArray() || CollectionFactory.isApproximableCollectionType(type); if (values.length == 0) { if (!type.equals(String.class)) { try { convertedValue = converter.convertIfNecessary(values, type, methodParameter); } catch (TypeMismatchException e) { // ignored for empty value, just returns null } } } else { if (requiresArray) { convertedValue = converter.convertIfNecessary(values, type, methodParameter); } else { Object singleValue = values[0]; if (isEmptyObject(singleValue)) { singleValue = defaultValue; } convertedValue = converter.convertIfNecessary(singleValue, type, methodParameter); } } return type.cast(convertedValue); }
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; }/*from w ww. j a va 2 s. c om*/ 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:org.springframework.beans.factory.support.ConstructorResolver.java
/** * Template method for resolving the specified argument which is supposed to be autowired. *//*from w ww . ja v a 2s . com*/ @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 Collection<?> convertToTypedCollection(Collection<?> original, @Nullable String propertyName, Class<?> requiredType, @Nullable TypeDescriptor typeDescriptor) { if (!Collection.class.isAssignableFrom(requiredType)) { return original; }//from w w w .j a v a 2s .c o 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); }
From source file:org.tinygroup.beanwrapper.TypeConverterDelegate.java
/** * Convert the value to the required type (if necessary from a String), * for the specified property.//from w w w. j a v a2 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 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; }