Skip to content

Commit 1c33206

Browse files
committed
catch ConversionException and ConvertedNotFoundException in BeanWrapper's convertIfNecessary as well, in order to support constructor resolution (SPR-6563)
1 parent 2153b2f commit 1c33206

File tree

4 files changed

+58
-14
lines changed

4 files changed

+58
-14
lines changed

org.springframework.beans/src/main/java/org/springframework/beans/BeanWrapperImpl.java

+16-4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.springframework.core.GenericCollectionTypeResolver;
4343
import org.springframework.core.MethodParameter;
4444
import org.springframework.core.convert.ConversionException;
45+
import org.springframework.core.convert.ConverterNotFoundException;
4546
import org.springframework.core.convert.TypeDescriptor;
4647
import org.springframework.util.Assert;
4748
import org.springframework.util.ObjectUtils;
@@ -417,12 +418,18 @@ public <T> T convertIfNecessary(
417418
try {
418419
return this.typeConverterDelegate.convertIfNecessary(value, requiredType, methodParam);
419420
}
420-
catch (IllegalArgumentException ex) {
421+
catch (ConverterNotFoundException ex) {
422+
throw new ConversionNotSupportedException(value, requiredType, ex);
423+
}
424+
catch (ConversionException ex) {
421425
throw new TypeMismatchException(value, requiredType, ex);
422426
}
423427
catch (IllegalStateException ex) {
424428
throw new ConversionNotSupportedException(value, requiredType, ex);
425429
}
430+
catch (IllegalArgumentException ex) {
431+
throw new TypeMismatchException(value, requiredType, ex);
432+
}
426433
}
427434

428435
/**
@@ -1105,12 +1112,12 @@ public Object run() throws Exception {
11051112
throw new MethodInvocationException(propertyChangeEvent, ex.getTargetException());
11061113
}
11071114
}
1108-
catch (ConversionException ex) {
1115+
catch (ConverterNotFoundException ex) {
11091116
PropertyChangeEvent pce =
11101117
new PropertyChangeEvent(this.rootObject, this.nestedPath + propertyName, oldValue, pv.getValue());
1111-
throw new TypeMismatchException(pce, pd.getPropertyType(), ex);
1118+
throw new ConversionNotSupportedException(pce, pd.getPropertyType(), ex);
11121119
}
1113-
catch (IllegalArgumentException ex) {
1120+
catch (ConversionException ex) {
11141121
PropertyChangeEvent pce =
11151122
new PropertyChangeEvent(this.rootObject, this.nestedPath + propertyName, oldValue, pv.getValue());
11161123
throw new TypeMismatchException(pce, pd.getPropertyType(), ex);
@@ -1120,6 +1127,11 @@ public Object run() throws Exception {
11201127
new PropertyChangeEvent(this.rootObject, this.nestedPath + propertyName, oldValue, pv.getValue());
11211128
throw new ConversionNotSupportedException(pce, pd.getPropertyType(), ex);
11221129
}
1130+
catch (IllegalArgumentException ex) {
1131+
PropertyChangeEvent pce =
1132+
new PropertyChangeEvent(this.rootObject, this.nestedPath + propertyName, oldValue, pv.getValue());
1133+
throw new TypeMismatchException(pce, pd.getPropertyType(), ex);
1134+
}
11231135
catch (IllegalAccessException ex) {
11241136
PropertyChangeEvent pce =
11251137
new PropertyChangeEvent(this.rootObject, this.nestedPath + propertyName, oldValue, pv.getValue());

org.springframework.beans/src/main/java/org/springframework/beans/ConversionNotSupportedException.java

+15-9
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,32 @@
1919
import java.beans.PropertyChangeEvent;
2020

2121
/**
22-
* Exception thrown when no suitable editor can be found to set a bean property.
22+
* Exception thrown when no suitable editor or converter can be found for a bean property.
2323
*
2424
* @author Arjen Poutsma
25+
* @author Juergen Hoeller
2526
* @since 3.0
2627
*/
2728
public class ConversionNotSupportedException extends TypeMismatchException {
2829

29-
public ConversionNotSupportedException(PropertyChangeEvent propertyChangeEvent, Class requiredType) {
30-
super(propertyChangeEvent, requiredType);
31-
}
32-
30+
/**
31+
* Create a new ConversionNotSupportedException.
32+
* @param propertyChangeEvent the PropertyChangeEvent that resulted in the problem
33+
* @param requiredType the required target type (or <code>null</code> if not known)
34+
* @param cause the root cause (may be <code>null</code>)
35+
*/
3336
public ConversionNotSupportedException(PropertyChangeEvent propertyChangeEvent, Class requiredType, Throwable cause) {
3437
super(propertyChangeEvent, requiredType, cause);
3538
}
3639

37-
public ConversionNotSupportedException(Object value, Class requiredType) {
38-
super(value, requiredType);
39-
}
40-
40+
/**
41+
* Create a new ConversionNotSupportedException.
42+
* @param value the offending value that couldn't be converted (may be <code>null</code>)
43+
* @param requiredType the required target type (or <code>null</code> if not known)
44+
* @param cause the root cause (may be <code>null</code>)
45+
*/
4146
public ConversionNotSupportedException(Object value, Class requiredType, Throwable cause) {
4247
super(value, requiredType, cause);
4348
}
49+
4450
}

org.springframework.context/src/test/java/org/springframework/context/conversionservice/TestClient.java

+10
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public class TestClient {
3232

3333
private boolean bool;
3434

35+
private List<String> stringList;
36+
3537
private Resource[] resourceArray;
3638

3739
private List<Resource> resourceList;
@@ -56,6 +58,14 @@ public void setBool(boolean bool) {
5658
this.bool = bool;
5759
}
5860

61+
public List<String> getStringList() {
62+
return stringList;
63+
}
64+
65+
public void setStringList(List<String> stringList) {
66+
this.stringList = stringList;
67+
}
68+
5969
public Resource[] getResourceArray() {
6070
return resourceArray;
6171
}

org.springframework.context/src/test/resources/org/springframework/context/conversionservice/conversionService.xml

+17-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@
1313

1414
<bean id="testClient" class="org.springframework.context.conversionservice.TestClient">
1515
<property name="bool" value="true"/>
16+
<property name="stringList">
17+
<list>
18+
<value>#{'test-' + strValue + '-end'}</value>
19+
<value>#{'test-' + strValue}</value>
20+
<value>#{'test-' + numValue+ '-end'}</value>
21+
<value>#{'test-' + numValue}</value>
22+
</list>
23+
</property>
1624
<property name="resourceArray">
1725
<value>classpath:test.xml</value>
1826
</property>
@@ -36,7 +44,15 @@
3644
<bean class="org.springframework.context.conversionservice.Bar">
3745
<constructor-arg value ="value2" />
3846
</bean>
39-
47+
48+
<bean id="numValue" class="java.lang.Integer">
49+
<constructor-arg value="111"/>
50+
</bean>
51+
52+
<bean id="strValue" class="java.lang.String">
53+
<constructor-arg value="222"/>
54+
</bean>
55+
4056
<context:annotation-config />
4157

4258
</beans>

0 commit comments

Comments
 (0)