catch ConversionException and ConvertedNotFoundException in BeanWrapper's convertIfNecessary as well, in order to support constructor resolution (SPR-6563)

This commit is contained in:
Juergen Hoeller 2009-12-15 12:53:36 +00:00
parent 2153b2fbd5
commit 1c33206042
4 changed files with 58 additions and 14 deletions

View File

@ -42,6 +42,7 @@ import org.springframework.core.CollectionFactory;
import org.springframework.core.GenericCollectionTypeResolver; import org.springframework.core.GenericCollectionTypeResolver;
import org.springframework.core.MethodParameter; import org.springframework.core.MethodParameter;
import org.springframework.core.convert.ConversionException; import org.springframework.core.convert.ConversionException;
import org.springframework.core.convert.ConverterNotFoundException;
import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.TypeDescriptor;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
@ -417,12 +418,18 @@ public class BeanWrapperImpl extends AbstractPropertyAccessor implements BeanWra
try { try {
return this.typeConverterDelegate.convertIfNecessary(value, requiredType, methodParam); return this.typeConverterDelegate.convertIfNecessary(value, requiredType, methodParam);
} }
catch (IllegalArgumentException ex) { catch (ConverterNotFoundException ex) {
throw new ConversionNotSupportedException(value, requiredType, ex);
}
catch (ConversionException ex) {
throw new TypeMismatchException(value, requiredType, ex); throw new TypeMismatchException(value, requiredType, ex);
} }
catch (IllegalStateException ex) { catch (IllegalStateException ex) {
throw new ConversionNotSupportedException(value, requiredType, ex); throw new ConversionNotSupportedException(value, requiredType, ex);
} }
catch (IllegalArgumentException ex) {
throw new TypeMismatchException(value, requiredType, ex);
}
} }
/** /**
@ -1105,12 +1112,12 @@ public class BeanWrapperImpl extends AbstractPropertyAccessor implements BeanWra
throw new MethodInvocationException(propertyChangeEvent, ex.getTargetException()); throw new MethodInvocationException(propertyChangeEvent, ex.getTargetException());
} }
} }
catch (ConversionException ex) { catch (ConverterNotFoundException ex) {
PropertyChangeEvent pce = PropertyChangeEvent pce =
new PropertyChangeEvent(this.rootObject, this.nestedPath + propertyName, oldValue, pv.getValue()); new PropertyChangeEvent(this.rootObject, this.nestedPath + propertyName, oldValue, pv.getValue());
throw new TypeMismatchException(pce, pd.getPropertyType(), ex); throw new ConversionNotSupportedException(pce, pd.getPropertyType(), ex);
} }
catch (IllegalArgumentException ex) { catch (ConversionException ex) {
PropertyChangeEvent pce = PropertyChangeEvent pce =
new PropertyChangeEvent(this.rootObject, this.nestedPath + propertyName, oldValue, pv.getValue()); new PropertyChangeEvent(this.rootObject, this.nestedPath + propertyName, oldValue, pv.getValue());
throw new TypeMismatchException(pce, pd.getPropertyType(), ex); throw new TypeMismatchException(pce, pd.getPropertyType(), ex);
@ -1120,6 +1127,11 @@ public class BeanWrapperImpl extends AbstractPropertyAccessor implements BeanWra
new PropertyChangeEvent(this.rootObject, this.nestedPath + propertyName, oldValue, pv.getValue()); new PropertyChangeEvent(this.rootObject, this.nestedPath + propertyName, oldValue, pv.getValue());
throw new ConversionNotSupportedException(pce, pd.getPropertyType(), ex); throw new ConversionNotSupportedException(pce, pd.getPropertyType(), ex);
} }
catch (IllegalArgumentException ex) {
PropertyChangeEvent pce =
new PropertyChangeEvent(this.rootObject, this.nestedPath + propertyName, oldValue, pv.getValue());
throw new TypeMismatchException(pce, pd.getPropertyType(), ex);
}
catch (IllegalAccessException ex) { catch (IllegalAccessException ex) {
PropertyChangeEvent pce = PropertyChangeEvent pce =
new PropertyChangeEvent(this.rootObject, this.nestedPath + propertyName, oldValue, pv.getValue()); new PropertyChangeEvent(this.rootObject, this.nestedPath + propertyName, oldValue, pv.getValue());

View File

@ -19,26 +19,32 @@ package org.springframework.beans;
import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeEvent;
/** /**
* Exception thrown when no suitable editor can be found to set a bean property. * Exception thrown when no suitable editor or converter can be found for a bean property.
* *
* @author Arjen Poutsma * @author Arjen Poutsma
* @author Juergen Hoeller
* @since 3.0 * @since 3.0
*/ */
public class ConversionNotSupportedException extends TypeMismatchException { public class ConversionNotSupportedException extends TypeMismatchException {
public ConversionNotSupportedException(PropertyChangeEvent propertyChangeEvent, Class requiredType) { /**
super(propertyChangeEvent, requiredType); * Create a new ConversionNotSupportedException.
} * @param propertyChangeEvent the PropertyChangeEvent that resulted in the problem
* @param requiredType the required target type (or <code>null</code> if not known)
* @param cause the root cause (may be <code>null</code>)
*/
public ConversionNotSupportedException(PropertyChangeEvent propertyChangeEvent, Class requiredType, Throwable cause) { public ConversionNotSupportedException(PropertyChangeEvent propertyChangeEvent, Class requiredType, Throwable cause) {
super(propertyChangeEvent, requiredType, cause); super(propertyChangeEvent, requiredType, cause);
} }
public ConversionNotSupportedException(Object value, Class requiredType) { /**
super(value, requiredType); * Create a new ConversionNotSupportedException.
} * @param value the offending value that couldn't be converted (may be <code>null</code>)
* @param requiredType the required target type (or <code>null</code> if not known)
* @param cause the root cause (may be <code>null</code>)
*/
public ConversionNotSupportedException(Object value, Class requiredType, Throwable cause) { public ConversionNotSupportedException(Object value, Class requiredType, Throwable cause) {
super(value, requiredType, cause); super(value, requiredType, cause);
} }
} }

View File

@ -32,6 +32,8 @@ public class TestClient {
private boolean bool; private boolean bool;
private List<String> stringList;
private Resource[] resourceArray; private Resource[] resourceArray;
private List<Resource> resourceList; private List<Resource> resourceList;
@ -56,6 +58,14 @@ public class TestClient {
this.bool = bool; this.bool = bool;
} }
public List<String> getStringList() {
return stringList;
}
public void setStringList(List<String> stringList) {
this.stringList = stringList;
}
public Resource[] getResourceArray() { public Resource[] getResourceArray() {
return resourceArray; return resourceArray;
} }

View File

@ -13,6 +13,14 @@
<bean id="testClient" class="org.springframework.context.conversionservice.TestClient"> <bean id="testClient" class="org.springframework.context.conversionservice.TestClient">
<property name="bool" value="true"/> <property name="bool" value="true"/>
<property name="stringList">
<list>
<value>#{'test-' + strValue + '-end'}</value>
<value>#{'test-' + strValue}</value>
<value>#{'test-' + numValue+ '-end'}</value>
<value>#{'test-' + numValue}</value>
</list>
</property>
<property name="resourceArray"> <property name="resourceArray">
<value>classpath:test.xml</value> <value>classpath:test.xml</value>
</property> </property>
@ -37,6 +45,14 @@
<constructor-arg value ="value2" /> <constructor-arg value ="value2" />
</bean> </bean>
<bean id="numValue" class="java.lang.Integer">
<constructor-arg value="111"/>
</bean>
<bean id="strValue" class="java.lang.String">
<constructor-arg value="222"/>
</bean>
<context:annotation-config /> <context:annotation-config />
</beans> </beans>