public static void main(String[] args) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { Company com = new Company(); com.setName(""); com.getName(); // simple BeanUtils.setProperty(com, "name", "Jack"); BeanUtils.getProperty(com, "name"); // indexed BeanUtils.setProperty(com, "product[1]", "NOTES SERVER"); BeanUtils.getProperty(com, "product[1]"); // mapped HashMap am = new HashMap(); am.put("1", "10010"); am.put("2", "10010"); BeanUtils.setProperty(com, "telephone", am); BeanUtils.getProperty(com, "telephone(2)"); // combined BeanUtils.getProperty(com, "employee[1].name"); // copyProperty Company com2 = new Company(); BeanUtils.copyProperties(com2, com); // converter BeanUtils.setProperty(com, "date", new Date()); BeanUtils.setProperty(com, "date", "2013-10-01"); ConvertUtils.register( new Converter() { public <T> T convert(Class<T> type, Object value) { // TODO Auto-generated method stub return null; } }, Date.class); // DynamicBean LazyDynaMap dynaBean = new LazyDynaMap(); dynaBean.set("foo", "bar"); dynaBean.set("customer", "title", "Rose"); dynaBean.set("address", 0, "address1"); System.out.println(dynaBean.get("address", 0)); Map map = dynaBean.getMap(); System.out.println(map.toString()); // returnNull dynaBean.setReturnNull(true); // Restricted dynaBean.setRestricted(true); }
/** * Constructs a PropertyConnector that synchronizes the two bound bean properties as specified by * the given pairs of bean and associated property name. If <code>Bean1#property1Name</code> * changes it updates <code>Bean2#property2Name</code> and vice versa. If a bean does not provide * support for bound properties, changes will not be observed. If a bean property is read-only, * this connector will not listen to the other bean's property and so won't update the read-only * property. * * <p>In case you don't need the PropertyConnector instance, you better use the static method * {@link #connect(Object, String, Object, String)}. This constructor may confuse developers if * you just use the side effects performed in the constructor; this is because it is quite * unconventional to instantiate an object that you never use. * * @param bean1 the bean that owns the first property * @param property1Name the name of the first property * @param bean2 the bean that owns the second property * @param property2Name the name of the second property * @throws NullPointerException if a bean or property name is <code>null</code> * @throws IllegalArgumentException if the beans are identical and the property name are equal, or * if both properties are read-only */ public PropertyConnector(Object bean1, String property1Name, Object bean2, String property2Name) { if (bean1 == null) throw new NullPointerException("Bean1 must not be null."); if (bean2 == null) throw new NullPointerException("Bean2 must not be null."); if (property1Name == null) throw new NullPointerException("PropertyName1 must not be null."); if (property2Name == null) throw new NullPointerException("PropertyName2 must not be null."); if ((bean1 == bean2) && (property1Name.equals(property2Name))) throw new IllegalArgumentException( "Cannot connect a bean property to itself on the same bean."); this.bean1 = bean1; this.bean2 = bean2; this.bean1Class = bean1.getClass(); this.bean2Class = bean2.getClass(); this.property1Name = property1Name; this.property2Name = property2Name; property1Descriptor = getPropertyDescriptor(bean1Class, property1Name); property2Descriptor = getPropertyDescriptor(bean2Class, property2Name); // Used to check if property2 shall be observed, // i.e. if a listener shall be registered with property2. boolean property1Writable = property1Descriptor.getWriteMethod() != null; // Used to check if property1 shall be observed, // i.e. if a listener shall be registered with property1. boolean property2Writable = property2Descriptor.getWriteMethod() != null; // Reject to connect to read-only properties if (!property1Writable && !property2Writable) throw new IllegalArgumentException("Cannot connect two read-only properties."); boolean property1Observable = BeanUtils.supportsBoundProperties(bean1Class); boolean property2Observable = BeanUtils.supportsBoundProperties(bean2Class); // We do not reject the case where two unobservable beans // are connected; this allows a hand-update using #updateProperty1 // and #updateProperty2. // Observe property1 if and only if bean1 provides support for // bound bean properties, and if updates can be written to property2. if (property1Observable && property2Writable) { property1ChangeHandler = new PropertyChangeHandler(bean1, property1Descriptor, bean2, property2Descriptor); addPropertyChangeHandler(bean1, bean1Class, property1ChangeHandler); } else { property1ChangeHandler = null; } // Observe property2 if and only if bean2 provides support for // bound bean properties, and if updates can be written to property1. if (property2Observable && property1Writable) { property2ChangeHandler = new PropertyChangeHandler(bean2, property2Descriptor, bean1, property1Descriptor); addPropertyChangeHandler(bean2, bean2Class, property2ChangeHandler); } else { property2ChangeHandler = null; } }
public static Map createMapUsingProperty(Object[] array, String fieldName) throws ApplicationException { if (array == null) { return null; } try { Map result = new HashMap(); for (int i = 0; i < array.length; i++) { Object current = array[i]; if (current != null) { Object key = BeanUtils.invokeGetter(current, fieldName); result.put(key, current); } } return result; } catch (Throwable th) { LOG.log(Level.SEVERE, th.getMessage(), th); throw new ApplicationException( "An error occurred while creating a map using property " + fieldName + ":" + th.getMessage()); } }
@Test public void clearClassLoaderForSystemClassLoader() throws Exception { BeanUtils.getPropertyDescriptors(ArrayList.class); assertTrue(CachedIntrospectionResults.strongClassCache.containsKey(ArrayList.class)); CachedIntrospectionResults.clearClassLoader(ArrayList.class.getClassLoader()); assertFalse(CachedIntrospectionResults.strongClassCache.containsKey(ArrayList.class)); }
/** * Looks up, lazily initializes and returns a <code>PropertyDescriptor</code> for the given Java * Bean and property name. * * @param beanClass the Java Bean class used to lookup the property from * @param propertyName the name of the property * @return the descriptor for the given bean and property name * @throws PropertyNotFoundException if the property could not be found */ private static PropertyDescriptor getPropertyDescriptor(Class<?> beanClass, String propertyName) { try { return BeanUtils.getPropertyDescriptor(beanClass, propertyName); } catch (IntrospectionException e) { throw new PropertyNotFoundException(propertyName, beanClass, e); } }
private static void invokeMethod( Object object, Method method, Class parameterType, String propertyValue) { logger.debug("match method : {}, {}", method, propertyValue); if (parameterType == String.class) { BeanUtils.safeInvokeMethod(object, method, propertyValue); } else if ((parameterType == Integer.class) || (parameterType == int.class)) { BeanUtils.safeInvokeMethod(object, method, Integer.parseInt(propertyValue)); } else if ((parameterType == Long.class) || (parameterType == long.class)) { BeanUtils.safeInvokeMethod(object, method, Long.parseLong(propertyValue)); } else if ((parameterType == Boolean.class) || (parameterType == boolean.class)) { BeanUtils.safeInvokeMethod(object, method, Boolean.valueOf(propertyValue)); } else { logger.info("cannot process parameterType : [" + parameterType + "]"); } }
private void setValueSilently( Object sourceBean, PropertyDescriptor sourcePropertyDescriptor, Object targetBean, PropertyDescriptor targetPropertyDescriptor, Object newValue) { Object targetValue = BeanUtils.getValue(targetBean, targetPropertyDescriptor); if (targetValue == newValue) { return; } if (property1ChangeHandler != null) { removePropertyChangeHandler(bean1, bean1Class, property1ChangeHandler); } if (property2ChangeHandler != null) { removePropertyChangeHandler(bean2, bean2Class, property2ChangeHandler); } try { // Set the new value in the target bean. BeanUtils.setValue(targetBean, targetPropertyDescriptor, newValue); } catch (PropertyVetoException e) { // Silently ignore this situation here, will be handled below. } // The target bean setter may have modified the new value. // Read the value set in the target bean. targetValue = BeanUtils.getValue(targetBean, targetPropertyDescriptor); // If the new value and the value read differ, // update the source bean's value. // This ignores that the source bean setter may modify the value again. // But we won't end in a loop. if (!BindingUtils.equals(targetValue, newValue)) { boolean sourcePropertyWritable = sourcePropertyDescriptor.getWriteMethod() != null; if (sourcePropertyWritable) { try { BeanUtils.setValue(sourceBean, sourcePropertyDescriptor, targetValue); } catch (PropertyVetoException e) { // Ignore. The value set is a modified variant // of a value that had been accepted before. } } } if (property1ChangeHandler != null) { addPropertyChangeHandler(bean1, bean1Class, property1ChangeHandler); } if (property2ChangeHandler != null) { addPropertyChangeHandler(bean2, bean2Class, property2ChangeHandler); } }
public static List<String> getInstancePropertyNames(ClassNode cNode) { List<PropertyNode> pList = BeanUtils.getAllProperties(cNode, false, false, true); List<String> result = new ArrayList<String>(pList.size()); for (PropertyNode pNode : pList) { result.add(pNode.getName()); } return result; }
/** Map the argument text into Boolean.TRUE or Boolean.FALSE using Boolean.valueOf. */ @Override public void setAsText(final String text) { if (BeanUtils.isNull(text)) { setValue(null); return; } Object newValue = Boolean.valueOf(text); setValue(newValue); }
public AuthenticationManager(AbstractResource properties, AbstractResource configuration) throws RemoteException { try { BeanUtils utils = new BeanUtils(configuration, properties); this.auth = utils.getAuthenticationProvider(); Set<QName> set = this.auth.getSupportedAuthenticationProfiles(); if ((set == null) || (set.size() < 1)) { throw new Exception( "The authentication provider must support at least 1 valid authentication profile."); } else if (!AuthenticationProfile.isValid(set)) { throw new Exception( "The authentication provider supports an unknown authentication profile."); } } catch (Exception ex) { throw new RemoteException( "Error instantiating AuthenticationProvider: " + ex.getMessage(), ex); } }
/** * Returns the value of the property named in the transformer's constructor for the object * provided. If any object in the property path leading up to the target property is <code>null * </code> then the outcome will be based on the value of the <code>ignoreNull</code> attribute. * By default, <code>ignoreNull</code> is <code>false</code> and would result in an <code> * IllegalArgumentException</code> if an object in the property path leading up to the target * property is <code>null</code>. * * @param object The object to be transformed. * @return The value of the property named in the transformer's constructor for the object * provided. * @throws IllegalArgumentException If an IllegalAccessException, InvocationTargetException, or * NoSuchMethodException is thrown when trying to access the property specified on the object * provided. Or if an object in the property path provided is <code>null</code> and <code> * ignoreNull</code> is set to <code>false</code>. */ public Object transform(Object object) { Object propertyValue = null; try { propertyValue = PropertyUtils.getProperty(object, propertyName); } catch (IllegalArgumentException e) { final String errorMsg = "Problem during transformation. Null value encountered in property path..."; if (ignoreNull) { log.warn("WARNING: " + errorMsg + e); } else { IllegalArgumentException iae = new IllegalArgumentException(errorMsg); if (!BeanUtils.initCause(iae, e)) { log.error(errorMsg, e); } throw iae; } } catch (IllegalAccessException e) { final String errorMsg = "Unable to access the property provided."; IllegalArgumentException iae = new IllegalArgumentException(errorMsg); if (!BeanUtils.initCause(iae, e)) { log.error(errorMsg, e); } throw iae; } catch (InvocationTargetException e) { final String errorMsg = "Exception occurred in property's getter"; IllegalArgumentException iae = new IllegalArgumentException(errorMsg); if (!BeanUtils.initCause(iae, e)) { log.error(errorMsg, e); } throw iae; } catch (NoSuchMethodException e) { final String errorMsg = "No property found for name [" + propertyName + "]"; IllegalArgumentException iae = new IllegalArgumentException(errorMsg); if (!BeanUtils.initCause(iae, e)) { log.error(errorMsg, e); } throw iae; } return propertyValue; }
/** * A property in the observed bean has changed. First checks, if this listener should handle the * event, because the event's property name is the one to be observed or the event indicates * that any property may have changed. In case the event provides no new value, it is read from * the source bean. * * @param evt the property change event to be handled */ public void propertyChange(PropertyChangeEvent evt) { String sourcePropertyName = sourcePropertyDescriptor.getName(); if ((evt.getPropertyName() == null) || (evt.getPropertyName().equals(sourcePropertyName))) { Object newValue = evt.getNewValue(); if (newValue == null) { newValue = BeanUtils.getValue(sourceBean, sourcePropertyDescriptor); } setValueSilently( sourceBean, sourcePropertyDescriptor, targetBean, targetPropertyDescriptor, newValue); } }
@SuppressWarnings("unchecked") public static <T> T toPrimitiveArrayIfPossible(Class<T> clz, Object value) { if (clz == Typ.intArray) { return (T) iarray(value); } else if (clz == Typ.byteArray) { return (T) barray(value); } else if (clz == Typ.charArray) { return (T) carray(value); } else if (clz == Typ.shortArray) { return (T) sarray(value); } else if (clz == Typ.longArray) { return (T) larray(value); } else if (clz == Typ.floatArray) { return (T) farray(value); } else if (clz == Typ.doubleArray) { return (T) darray(value); } else if (value.getClass() == clz) { return (T) value; } else { int index = 0; Object newInstance = Array.newInstance(clz.getComponentType(), Boon.len(value)); Iterator<Object> iterator = iterator(Typ.object, value); while (iterator.hasNext()) { Object item = iterator.next(); if (clz.getComponentType().isAssignableFrom(item.getClass())) { BeanUtils.idx(newInstance, index, item); } else { item = coerce(clz.getComponentType(), item); BeanUtils.idx(newInstance, index, item); } index++; } return (T) newInstance; } }
/** * Find a default editor for the given type. * * @param requiredType the type to find an editor for * @return the corresponding editor, or {@code null} if none */ private PropertyEditor findDefaultEditor(Class<?> requiredType) { PropertyEditor editor = null; if (requiredType != null) { // No custom editor -> check BeanWrapperImpl's default editors. editor = this.propertyEditorRegistry.getDefaultEditor(requiredType); if (editor == null && String.class != requiredType) { // No BeanWrapper default editor -> check standard JavaBean editor. editor = BeanUtils.findEditorByConvention(requiredType); } } return editor; }
@Override public void setAsText(final String text) { if (BeanUtils.isNull(text)) { setValue(null); } else { try { setValue(new BigDecimal(text)); } catch (NumberFormatException e) { throw new IllegalArgumentException("Failed to parse"); } } }
public static Object toArrayGuessType(Collection<?> value) { Class<?> componentType = Reflection.getComponentType(value); Object array = Array.newInstance(componentType, value.size()); @SuppressWarnings("unchecked") Iterator<Object> iterator = (Iterator<Object>) value.iterator(); int index = 0; while (iterator.hasNext()) { BeanUtils.idx(array, index, iterator.next()); index++; } return array; }
/** {@inheritDoc} */ @Override Object getProperty(SqlProcessContext ctx, Object obj, String item) { String prefix = null; String suffix = null; String name = null; int ix = item.indexOf("?"); if (ix >= 0) { prefix = item.substring(0, ix); suffix = item.substring(ix + 1); name = prefix + suffix; } else { ix = item.indexOf("@"); if (ix >= 0) { prefix = item.substring(0, ix); suffix = SqlProcessContext.getFeature(item.substring(ix + 1)); name = prefix + suffix; } else { name = item; } } Object result = (BeanUtils.checkProperty(obj, name)) ? BeanUtils.getProperty(obj, name) : null; if (result != null || prefix == null || suffix == null) return result; suffix = SqlUtils.firstLowerCase(suffix); Object o = (BeanUtils.checkProperty(obj, suffix)) ? BeanUtils.getProperty(obj, suffix) : null; if (o == null || !(o instanceof Map)) { suffix = SqlProcessContext.getFeature(SqlFeature.OPERATOR_ATTRIBUTE_IN_MAP); o = (BeanUtils.checkProperty(obj, suffix)) ? BeanUtils.getProperty(obj, suffix) : null; if (o == null || !(o instanceof Map)) { return null; } } Map map = (Map) o; return map.get(prefix); }
/** * Initialize the mapping metadata for the given class. * * @param mappedClass the mapped class. */ protected void initialize(Class<?> mappedClass) { this.mappedClass = mappedClass; this.mappedFields = new HashMap<>(); PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(mappedClass); for (PropertyDescriptor pd : pds) { if (pd.getWriteMethod() != null || pd.getReadMethod() != null) { this.mappedFields.put(pd.getName().toLowerCase(), pd); String underscoredName = underscoreName(pd.getName()); if (!pd.getName().toLowerCase().equals(underscoredName)) { this.mappedFields.put(underscoredName, pd); } } } }
@Override protected IDatabaseConnection getConnection() throws Exception { Connection conn = dataSource.getConnection(); IDatabaseConnection connection = new DatabaseConnection( conn, dbType.equalsIgnoreCase("oracle") ? testProperties.getProperty("db.username") : null); DatabaseConfig config = connection.getConfig(); if (containsProperty(testProperties, DATATYPE_FACTORY)) { Class clazz = Class.forName(testProperties.getProperty(DATATYPE_FACTORY)); config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, BeanUtils.getInstance(clazz)); } return connection; }
/** * Find a default editor for the given type. * * @param requiredType the type to find an editor for * @param descriptor the JavaBeans descriptor for the property * @return the corresponding editor, or <code>null</code> if none */ protected PropertyEditor findDefaultEditor(Class requiredType, TypeDescriptor typeDescriptor) { PropertyEditor editor = null; if (typeDescriptor instanceof PropertyTypeDescriptor) { PropertyDescriptor pd = ((PropertyTypeDescriptor) typeDescriptor).getPropertyDescriptor(); editor = pd.createPropertyEditor(this.targetObject); } if (editor == null && requiredType != null) { // No custom editor -> check BeanWrapperImpl's default editors. editor = this.propertyEditorRegistry.getDefaultEditor(requiredType); if (editor == null && !String.class.equals(requiredType)) { // No BeanWrapper default editor -> check standard JavaBean editor. editor = BeanUtils.findEditorByConvention(requiredType); } } return editor; }
/** * Extract the values for all attributes in the struct. * * <p>Utilizes public setters and result set metadata. * * @see java.sql.ResultSetMetaData */ public Object fromStruct(STRUCT struct) throws SQLException { Assert.state(this.mappedClass != null, "Mapped class was not specified"); Object mappedObject = BeanUtils.instantiateClass(this.mappedClass); BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(mappedObject); initBeanWrapper(bw); ResultSetMetaData rsmd = struct.getDescriptor().getMetaData(); Object[] attr = struct.getAttributes(); int columnCount = rsmd.getColumnCount(); for (int index = 1; index <= columnCount; index++) { String column = JdbcUtils.lookupColumnName(rsmd, index).toLowerCase(); PropertyDescriptor pd = (PropertyDescriptor) this.mappedFields.get(column); if (pd != null) { Object value = attr[index - 1]; if (logger.isDebugEnabled()) { logger.debug( "Mapping column '" + column + "' to property '" + pd.getName() + "' of type " + pd.getPropertyType()); } if (bw.isWritableProperty(pd.getName())) { try { bw.setPropertyValue(pd.getName(), value); } catch (Exception e) { e.printStackTrace(); } } else { // Если нету сеттера для проперти не нужно кидать ошибку! logger.warn( "Unable to access the setter for " + pd.getName() + ". Check that " + "set" + StringUtils.capitalize(pd.getName()) + " is declared and has public access."); } } } return mappedObject; }
// Adds to each Service template the general criteria for the CSA // (each service template can have is own set of criteria, but if a global criteria is defined // each component must use it.) private void generalizeCSACriteria() { ArrayList<ServiceTemplate> comps = new ArrayList<ServiceTemplate>(); ArrayList<Criterion> crit = new ArrayList<Criterion>(); comps = this.data.getServiceTemplates(); crit = this.data.getCriteria(); for (ServiceTemplate c : comps) { for (Criterion cr : crit) { try { Criterion temp = (Criterion) BeanUtils.cloneBean(cr); c.addCrit(temp); } catch (IllegalAccessException | InstantiationException | InvocationTargetException | NoSuchMethodException e) { e.printStackTrace(); } } } }
// Adds to each service template the general requirements for the CSA // (if a part of the CSA does not fulfill the requirement the whole will not fullfill it either) private void generalizeCSARequirements() { ArrayList<ServiceTemplate> comps = new ArrayList<ServiceTemplate>(); ArrayList<Requirement> reqs = new ArrayList<Requirement>(); comps = this.data.getServiceTemplates(); reqs = this.data.getRequirements(); for (ServiceTemplate c : comps) { for (Requirement r : reqs) { Requirement temp = new Requirement(); try { BeanUtils.copyProperties(temp, r); // System.out.println(r.toString()); // System.out.println(temp.toString()); c.addReq(temp); } catch (IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); } } } }
@Override public <T extends Annotation> T getAnnotationOnField(String name, Class<T> clz) { FieldEx field = BeanUtils.getField(obj.getClass(), name); if (field == null) return null; return field.getAnnotation(clz); }
/** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { BeanUtils.registBean("com.propn.golf.ioc"); Di di = BeanUtils.getInstance(Di.class); di.ipaa.sayHello(); di.ipab.sayHello(); }
/** {@inheritDoc} */ @Override Class<?> getFieldType(SqlProcessContext ctx, Class<?> attributeType, String attributeName) { if (attributeName.indexOf("@") >= 0 || attributeName.indexOf("?") >= 0) return String.class; return BeanUtils.getFieldType(attributeType, attributeName); }
/** * Used to remove this class' PropertyChangeHandler from the given bean if it is not {@code null}. * * @param bean the bean to remove the property change listener from * @param listener the property change listener to be removed * @throws PropertyUnboundException if the bean does not support bound properties * @throws PropertyNotBindableException if the property change handler cannot be removed * successfully */ private static void removePropertyChangeHandler( Object bean, Class<?> beanClass, PropertyChangeListener listener) { if (bean != null) { BeanUtils.removePropertyChangeListener(bean, beanClass, listener); } }
/** * Reads the value of the first bean property and sets it as new value of the second bean * property. * * @see #updateProperty1() */ public void updateProperty2() { Object property1Value = BeanUtils.getValue(bean1, property1Descriptor); setValueSilently(bean1, property1Descriptor, bean2, property2Descriptor, property1Value); }
public DbConnection(Connection connection, String schema) throws DatabaseUnitException { super( ((PoolableConnection) ((Connection) BeanUtils.getProperty(connection, "delegate"))) .getDelegate()); wrapperConnection = connection; }
/** * Convert the value to the required type (if necessary from a String), for the specified * property. * * @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); // No custom editor but custom ConversionService specified? ConversionService conversionService = this.propertyEditorRegistry.getConversionService(); if (editor == null && conversionService != null && convertedValue != null) { TypeDescriptor sourceTypeDesc = TypeDescriptor.forObject(convertedValue); TypeDescriptor targetTypeDesc = typeDescriptor.forElementType(requiredType); if (conversionService.canConvert(sourceTypeDesc, targetTypeDesc)) { return (T) conversionService.convert(convertedValue, sourceTypeDesc, targetTypeDesc); } } // 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.getMethodParameter() != null) { Class elemType = GenericCollectionTypeResolver.getCollectionParameterType( typeDescriptor.getMethodParameter()); if (elemType != null && Enum.class.isAssignableFrom(elemType)) { convertedValue = StringUtils.commaDelimitedListToStringArray((String) convertedValue); } } if (editor == null) { editor = findDefaultEditor(requiredType, typeDescriptor); } convertedValue = doConvertValue(oldValue, convertedValue, requiredType, editor); } 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); } else if (convertedValue instanceof Map) { // Convert keys and values to respective target type, if determined. convertedValue = convertToTypedMap((Map) convertedValue, propertyName, requiredType, typeDescriptor); } if (convertedValue.getClass().isArray() && Array.getLength(convertedValue) == 1) { convertedValue = Array.get(convertedValue, 0); } 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 (!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); } } if (!ClassUtils.isAssignableValue(requiredType, convertedValue)) { // 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"); throw new IllegalArgumentException(msg.toString()); } else { msg.append(": no matching editors or conversion strategy found"); throw new IllegalStateException(msg.toString()); } } } return (T) convertedValue; }