Search in sources :

Example 71 with PropertyEditor

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;
}
Also used : ConvertingPropertyEditorAdapter(org.springframework.core.convert.support.ConvertingPropertyEditorAdapter) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) PropertyEditor(java.beans.PropertyEditor) Nullable(org.springframework.lang.Nullable)

Example 72 with PropertyEditor

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;
}
Also used : PropertyEditor(java.beans.PropertyEditor) Nullable(org.springframework.lang.Nullable)

Example 73 with PropertyEditor

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();
    }
}
Also used : DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) NumberStyleFormatter(org.springframework.format.number.NumberStyleFormatter) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) PropertyEditor(java.beans.PropertyEditor) FormattingConversionService(org.springframework.format.support.FormattingConversionService) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) Test(org.junit.jupiter.api.Test)

Example 74 with PropertyEditor

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();
}
Also used : PropertyEditor(java.beans.PropertyEditor) Test(org.junit.jupiter.api.Test)

Example 75 with PropertyEditor

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");
    }
}
Also used : PropertyEditor(java.beans.PropertyEditor) Test(org.junit.jupiter.api.Test)

Aggregations

PropertyEditor (java.beans.PropertyEditor)167 Test (org.junit.jupiter.api.Test)59 HashMap (java.util.HashMap)14 Test (org.junit.Test)13 Map (java.util.Map)11 File (java.io.File)10 URI (java.net.URI)9 TestBean (org.springframework.beans.testfixture.beans.TestBean)9 IOException (java.io.IOException)7 Path (java.nio.file.Path)7 Resource (org.springframework.core.io.Resource)7 Method (java.lang.reflect.Method)6 URL (java.net.URL)6 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)6 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)6 PropertyDescriptor (java.beans.PropertyDescriptor)5 Field (java.lang.reflect.Field)4 ArrayList (java.util.ArrayList)4 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)4 StringArrayPropertyEditor (org.springframework.beans.propertyeditors.StringArrayPropertyEditor)4