use of java.beans.PropertyEditor in project spring-framework by spring-projects.
the class AbstractPropertyBindingResult method findEditor.
/**
* This implementation exposes a PropertyEditor adapter for a Formatter,
* if applicable.
*/
@Override
@Nullable
public PropertyEditor findEditor(@Nullable String field, @Nullable Class<?> valueType) {
Class<?> valueTypeForLookup = valueType;
if (valueTypeForLookup == null) {
valueTypeForLookup = getFieldType(field);
}
PropertyEditor editor = super.findEditor(field, valueTypeForLookup);
if (editor == null && this.conversionService != null) {
TypeDescriptor td = null;
if (field != null && getTarget() != null) {
TypeDescriptor ptd = getPropertyAccessor().getPropertyTypeDescriptor(fixedField(field));
if (ptd != null && (valueType == null || valueType.isAssignableFrom(ptd.getType()))) {
td = ptd;
}
}
if (td == null) {
td = TypeDescriptor.valueOf(valueTypeForLookup);
}
if (this.conversionService.canConvert(TypeDescriptor.valueOf(String.class), td)) {
editor = new ConvertingPropertyEditorAdapter(this.conversionService, td);
}
}
return editor;
}
use of java.beans.PropertyEditor in project spring-framework by spring-projects.
the class AbstractPropertyBindingResult method getCustomEditor.
/**
* Retrieve the custom PropertyEditor for the given field, if any.
* @param fixedField the fully qualified field name
* @return the custom PropertyEditor, or {@code null}
*/
@Nullable
protected PropertyEditor getCustomEditor(String fixedField) {
Class<?> targetType = getPropertyAccessor().getPropertyType(fixedField);
PropertyEditor editor = getPropertyAccessor().findCustomEditor(targetType, fixedField);
if (editor == null) {
editor = BeanUtils.findEditorByConvention(targetType);
}
return editor;
}
use of java.beans.PropertyEditor in project spring-framework by spring-projects.
the class DataBinderTests method testBindingWithFormatterAgainstFields.
@Test
void testBindingWithFormatterAgainstFields() {
TestBean tb = new TestBean();
DataBinder binder = new DataBinder(tb);
FormattingConversionService conversionService = new FormattingConversionService();
DefaultConversionService.addDefaultConverters(conversionService);
conversionService.addFormatterForFieldType(Float.class, new NumberStyleFormatter());
binder.setConversionService(conversionService);
binder.initDirectFieldAccess();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("myFloat", "1,2");
LocaleContextHolder.setLocale(Locale.GERMAN);
try {
binder.bind(pvs);
assertThat(tb.getMyFloat()).isEqualTo(Float.valueOf(1.2f));
assertThat(binder.getBindingResult().getFieldValue("myFloat")).isEqualTo("1,2");
PropertyEditor editor = binder.getBindingResult().findEditor("myFloat", Float.class);
assertThat(editor).isNotNull();
editor.setValue(1.4f);
assertThat(editor.getAsText()).isEqualTo("1,4");
editor = binder.getBindingResult().findEditor("myFloat", null);
assertThat(editor).isNotNull();
editor.setAsText("1,6");
assertThat(editor.getValue()).isEqualTo(1.6f);
} finally {
LocaleContextHolder.resetLocaleContext();
}
}
use of java.beans.PropertyEditor in project spring-framework by spring-projects.
the class ResourceEditorTests method sunnyDay.
@Test
void sunnyDay() {
PropertyEditor editor = new ResourceEditor();
editor.setAsText("classpath:org/springframework/core/io/ResourceEditorTests.class");
Resource resource = (Resource) editor.getValue();
assertThat(resource).isNotNull();
assertThat(resource.exists()).isTrue();
}
use of java.beans.PropertyEditor in project spring-framework by spring-projects.
the class ResourceEditorTests method systemPropertyReplacementWithUnresolvablePlaceholder.
@Test
void systemPropertyReplacementWithUnresolvablePlaceholder() {
PropertyEditor editor = new ResourceEditor();
System.setProperty("test.prop", "foo");
try {
editor.setAsText("${test.prop}-${bar}");
Resource resolved = (Resource) editor.getValue();
assertThat(resolved.getFilename()).isEqualTo("foo-${bar}");
} finally {
System.getProperties().remove("test.prop");
}
}
Aggregations