Skip to content

Commit 5f9f699

Browse files
committed
refined PropertyEditor exposure for the ConversionService case
1 parent 0057481 commit 5f9f699

3 files changed

Lines changed: 21 additions & 14 deletions

File tree

‎org.springframework.context/src/main/java/org/springframework/validation/AbstractPropertyBindingResult.java‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,8 @@ protected Object formatFieldValue(String field, Object value) {
117117
if (this.conversionService != null) {
118118
// Try custom formatter...
119119
TypeDescriptor fieldDesc = getPropertyAccessor().getPropertyTypeDescriptor(fixedField);
120-
TypeDescriptor stringDesc = TypeDescriptor.valueOf(String.class);
121-
if (fieldDesc != null && this.conversionService.canConvert(fieldDesc, stringDesc)) {
122-
return this.conversionService.convert(value, fieldDesc, stringDesc);
120+
if (fieldDesc != null && this.conversionService.canConvert(fieldDesc, TypeDescriptor.STRING)) {
121+
return this.conversionService.convert(value, fieldDesc, TypeDescriptor.STRING);
123122
}
124123
}
125124
return value;
@@ -153,7 +152,9 @@ public PropertyEditor findEditor(String field, Class valueType) {
153152
TypeDescriptor td = (field != null ?
154153
getPropertyAccessor().getPropertyTypeDescriptor(fixedField(field)) :
155154
TypeDescriptor.valueOf(valueType));
156-
editor = new ConvertingPropertyEditorAdapter(this.conversionService, td);
155+
if (this.conversionService.canConvert(TypeDescriptor.STRING, td)) {
156+
editor = new ConvertingPropertyEditorAdapter(this.conversionService, td);
157+
}
157158
}
158159
return editor;
159160
}

‎org.springframework.core/src/main/java/org/springframework/core/convert/support/ConvertingPropertyEditorAdapter.java‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@
3131
*/
3232
public class ConvertingPropertyEditorAdapter extends PropertyEditorSupport {
3333

34-
private static final TypeDescriptor stringDescriptor = TypeDescriptor.valueOf(String.class);
35-
3634
private final ConversionService conversionService;
3735

3836
private final TypeDescriptor targetDescriptor;
3937

38+
private final boolean canConvertToString;
39+
4040

4141
/**
4242
* Create a new ConvertingPropertyEditorAdapter for a given
@@ -50,17 +50,23 @@ public ConvertingPropertyEditorAdapter(ConversionService conversionService, Type
5050
Assert.notNull(targetDescriptor, "TypeDescriptor must not be null");
5151
this.conversionService = conversionService;
5252
this.targetDescriptor = targetDescriptor;
53+
this.canConvertToString = conversionService.canConvert(this.targetDescriptor, TypeDescriptor.STRING);
5354
}
5455

5556

5657
@Override
5758
public void setAsText(String text) throws IllegalArgumentException {
58-
setValue(this.conversionService.convert(text, stringDescriptor, this.targetDescriptor));
59+
setValue(this.conversionService.convert(text, TypeDescriptor.STRING, this.targetDescriptor));
5960
}
6061

6162
@Override
6263
public String getAsText() {
63-
return (String) this.conversionService.convert(getValue(), this.targetDescriptor, stringDescriptor);
64+
if (this.canConvertToString) {
65+
return (String) this.conversionService.convert(getValue(), this.targetDescriptor, TypeDescriptor.STRING);
66+
}
67+
else {
68+
return null;
69+
}
6470
}
6571

6672
}

‎org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/tags/form/ValueFormatter.java‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2007 the original author or authors.
2+
* Copyright 2002-2009 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -59,16 +59,16 @@ public static String getDisplayString(Object value, PropertyEditor propertyEdito
5959
if (propertyEditor != null && !(value instanceof String)) {
6060
try {
6161
propertyEditor.setValue(value);
62-
return getDisplayString(propertyEditor.getAsText(), htmlEscape);
62+
String text = propertyEditor.getAsText();
63+
if (text != null) {
64+
return getDisplayString(text, htmlEscape);
65+
}
6366
}
6467
catch (Throwable ex) {
6568
// The PropertyEditor might not support this value... pass through.
66-
return getDisplayString(value, htmlEscape);
6769
}
6870
}
69-
else {
70-
return getDisplayString(value, htmlEscape);
71-
}
71+
return getDisplayString(value, htmlEscape);
7272
}
7373

7474
}

0 commit comments

Comments
 (0)