Search in sources :

Example 81 with PropertyEditor

use of java.beans.PropertyEditor in project spring-framework by spring-projects.

the class OptionTagTests method withCustomObjectAndEditorSelected.

@Test
public void withCustomObjectAndEditorSelected() throws Exception {
    final PropertyEditor floatEditor = new SimpleFloatEditor();
    floatEditor.setValue(new Float("12.34"));
    String selectName = "testBean.someNumber";
    BindStatus bindStatus = new BindStatus(getRequestContext(), selectName, false) {

        @Override
        public PropertyEditor getEditor() {
            return floatEditor;
        }
    };
    getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, bindStatus);
    this.tag.setValue(new Float(12.34));
    this.tag.setLabel("12.34f");
    int result = this.tag.doStartTag();
    assertEquals(BodyTag.EVAL_BODY_BUFFERED, result);
    result = this.tag.doEndTag();
    assertEquals(Tag.EVAL_PAGE, result);
    String output = getOutput();
    assertOptionTagOpened(output);
    assertOptionTagClosed(output);
    assertContainsAttribute(output, "selected", "selected");
    assertBlockTagContains(output, "12.34f");
}
Also used : PropertyEditor(java.beans.PropertyEditor) StringArrayPropertyEditor(org.springframework.beans.propertyeditors.StringArrayPropertyEditor) BindStatus(org.springframework.web.servlet.support.BindStatus) Test(org.junit.Test)

Example 82 with PropertyEditor

use of java.beans.PropertyEditor in project spring-framework by spring-projects.

the class ResourceArrayPropertyEditorTests method testVanillaResource.

@Test
public void testVanillaResource() throws Exception {
    PropertyEditor editor = new ResourceArrayPropertyEditor();
    editor.setAsText("classpath:org/springframework/core/io/support/ResourceArrayPropertyEditor.class");
    Resource[] resources = (Resource[]) editor.getValue();
    assertNotNull(resources);
    assertTrue(resources[0].exists());
}
Also used : Resource(org.springframework.core.io.Resource) PropertyEditor(java.beans.PropertyEditor) Test(org.junit.Test)

Example 83 with PropertyEditor

use of java.beans.PropertyEditor in project spring-framework by spring-projects.

the class AbstractBeanFactory method registerCustomEditors.

/**
	 * Initialize the given PropertyEditorRegistry with the custom editors
	 * that have been registered with this BeanFactory.
	 * <p>To be called for BeanWrappers that will create and populate bean
	 * instances, and for SimpleTypeConverter used for constructor argument
	 * and factory method type conversion.
	 * @param registry the PropertyEditorRegistry to initialize
	 */
protected void registerCustomEditors(PropertyEditorRegistry registry) {
    PropertyEditorRegistrySupport registrySupport = (registry instanceof PropertyEditorRegistrySupport ? (PropertyEditorRegistrySupport) registry : null);
    if (registrySupport != null) {
        registrySupport.useConfigValueEditors();
    }
    if (!this.propertyEditorRegistrars.isEmpty()) {
        for (PropertyEditorRegistrar registrar : this.propertyEditorRegistrars) {
            try {
                registrar.registerCustomEditors(registry);
            } catch (BeanCreationException ex) {
                Throwable rootCause = ex.getMostSpecificCause();
                if (rootCause instanceof BeanCurrentlyInCreationException) {
                    BeanCreationException bce = (BeanCreationException) rootCause;
                    if (isCurrentlyInCreation(bce.getBeanName())) {
                        if (logger.isDebugEnabled()) {
                            logger.debug("PropertyEditorRegistrar [" + registrar.getClass().getName() + "] failed because it tried to obtain currently created bean '" + ex.getBeanName() + "': " + ex.getMessage());
                        }
                        onSuppressedException(ex);
                        continue;
                    }
                }
                throw ex;
            }
        }
    }
    if (!this.customEditors.isEmpty()) {
        for (Map.Entry<Class<?>, Class<? extends PropertyEditor>> entry : this.customEditors.entrySet()) {
            Class<?> requiredType = entry.getKey();
            Class<? extends PropertyEditor> editorClass = entry.getValue();
            registry.registerCustomEditor(requiredType, BeanUtils.instantiateClass(editorClass));
        }
    }
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) PropertyEditorRegistrySupport(org.springframework.beans.PropertyEditorRegistrySupport) PropertyEditorRegistrar(org.springframework.beans.PropertyEditorRegistrar) PropertyEditor(java.beans.PropertyEditor) BeanCurrentlyInCreationException(org.springframework.beans.factory.BeanCurrentlyInCreationException) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 84 with PropertyEditor

use of java.beans.PropertyEditor in project spring-framework by spring-projects.

the class CustomEditorConfigurer method postProcessBeanFactory.

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    if (this.propertyEditorRegistrars != null) {
        for (PropertyEditorRegistrar propertyEditorRegistrar : this.propertyEditorRegistrars) {
            beanFactory.addPropertyEditorRegistrar(propertyEditorRegistrar);
        }
    }
    if (this.customEditors != null) {
        for (Map.Entry<Class<?>, Class<? extends PropertyEditor>> entry : this.customEditors.entrySet()) {
            Class<?> requiredType = entry.getKey();
            Class<? extends PropertyEditor> propertyEditorClass = entry.getValue();
            beanFactory.registerCustomEditor(requiredType, propertyEditorClass);
        }
    }
}
Also used : PropertyEditorRegistrar(org.springframework.beans.PropertyEditorRegistrar) PropertyEditor(java.beans.PropertyEditor) Map(java.util.Map)

Example 85 with PropertyEditor

use of java.beans.PropertyEditor in project liquibase by liquibase.

the class CsvToBean method getPropertyEditorValue.

private PropertyEditor getPropertyEditorValue(Class<?> cls) {
    if (editorMap == null) {
        editorMap = new HashMap<Class<?>, PropertyEditor>();
    }
    PropertyEditor editor = editorMap.get(cls);
    if (editor == null) {
        editor = PropertyEditorManager.findEditor(cls);
        addEditorToMap(cls, editor);
    }
    return editor;
}
Also used : PropertyEditor(java.beans.PropertyEditor)

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