Search in sources :

Example 41 with PropertyEditor

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

the class ResourceArrayPropertyEditorTests method testSystemPropertyReplacement.

@Test
public void testSystemPropertyReplacement() {
    PropertyEditor editor = new ResourceArrayPropertyEditor();
    System.setProperty("test.prop", "foo");
    try {
        editor.setAsText("${test.prop}-${bar}");
        Resource[] resources = (Resource[]) editor.getValue();
        assertEquals("foo-${bar}", resources[0].getFilename());
    } finally {
        System.getProperties().remove("test.prop");
    }
}
Also used : Resource(org.springframework.core.io.Resource) PropertyEditor(java.beans.PropertyEditor) Test(org.junit.Test)

Example 42 with PropertyEditor

use of java.beans.PropertyEditor in project jdk8u_jdk by JetBrains.

the class PropertyEditorFinder method find.

@Override
public PropertyEditor find(Class<?> type) {
    Class<?> predefined;
    synchronized (this.registry) {
        predefined = this.registry.get(type);
    }
    PropertyEditor editor = instantiate(predefined, null);
    if (editor == null) {
        editor = super.find(type);
        if ((editor == null) && (null != type.getEnumConstants())) {
            editor = new EnumEditor(type);
        }
    }
    return editor;
}
Also used : PropertyEditor(java.beans.PropertyEditor) EnumEditor(com.sun.beans.editors.EnumEditor)

Example 43 with PropertyEditor

use of java.beans.PropertyEditor in project tomee by apache.

the class Converter method convert.

public static Object convert(final Object value, Class<?> targetType, final String name) {
    if (value == null) {
        if (targetType.equals(Boolean.TYPE)) {
            return false;
        }
        return value;
    }
    final Class<? extends Object> actualType = value.getClass();
    if (targetType.isPrimitive()) {
        targetType = PrimitiveTypes.valueOf(targetType.toString().toUpperCase()).getWraper();
    }
    if (targetType.isAssignableFrom(actualType)) {
        return value;
    }
    if (Number.class.isAssignableFrom(actualType) && Number.class.isAssignableFrom(targetType)) {
        return value;
    }
    if (!(value instanceof String)) {
        final String message = String.format("Expected type '%s' for '%s'. Found '%s'", targetType.getName(), name, actualType.getName());
        throw new IllegalArgumentException(message);
    }
    final String stringValue = (String) value;
    if (Enum.class.isAssignableFrom(targetType)) {
        final Class<? extends Enum> enumType = (Class<? extends Enum>) targetType;
        try {
            return Enum.valueOf(enumType, stringValue);
        } catch (final IllegalArgumentException e) {
            try {
                return Enum.valueOf(enumType, stringValue.toUpperCase());
            } catch (final IllegalArgumentException e1) {
                return Enum.valueOf(enumType, stringValue.toLowerCase());
            }
        }
    }
    try {
        // Force static initializers to run
        Class.forName(targetType.getName(), true, targetType.getClassLoader());
    } catch (final ClassNotFoundException e) {
        e.printStackTrace();
    }
    final PropertyEditor editor = Editors.get(targetType);
    if (editor == null) {
        final Object result = create(targetType, stringValue);
        if (result != null) {
            return result;
        }
    }
    if (editor == null) {
        final String message = String.format("Cannot convert to '%s' for '%s'. No PropertyEditor", targetType.getName(), name);
        throw new IllegalArgumentException(message);
    }
    editor.setAsText(stringValue);
    return editor.getValue();
}
Also used : PropertyEditor(java.beans.PropertyEditor)

Example 44 with PropertyEditor

use of java.beans.PropertyEditor in project tomee by apache.

the class Editors method get.

public static PropertyEditor get(final Class<?> type) {
    final PropertyEditor editor = PropertyEditorManager.findEditor(type);
    if (editor != null) {
        return editor;
    }
    final Class<Editors> c = Editors.class;
    try {
        final Class<?> editorClass = c.getClassLoader().loadClass(c.getName().replace("Editors", type.getSimpleName() + "Editor"));
        PropertyEditorManager.registerEditor(type, editorClass);
        return PropertyEditorManager.findEditor(type);
    } catch (final ClassNotFoundException e) {
        return null;
    }
}
Also used : PropertyEditor(java.beans.PropertyEditor)

Example 45 with PropertyEditor

use of java.beans.PropertyEditor in project tomee by apache.

the class ClientInjectionProcessor method convert.

private Object convert(final Class type, Object value) {
    if (type == Object.class || !(value instanceof String)) {
        return value;
    }
    final String stringValue = (String) value;
    final PropertyEditor editor = findEditor(type);
    if (editor != null) {
        editor.setAsText(stringValue);
        value = editor.getValue();
    }
    return value;
}
Also used : PropertyEditor(java.beans.PropertyEditor) AccessibleObject(java.lang.reflect.AccessibleObject)

Aggregations

PropertyEditor (java.beans.PropertyEditor)98 Test (org.junit.Test)59 TestBean (org.springframework.tests.sample.beans.TestBean)10 File (java.io.File)9 URI (java.net.URI)8 ITestBean (org.springframework.tests.sample.beans.ITestBean)6 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)6 Path (java.nio.file.Path)5 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 StringArrayPropertyEditor (org.springframework.beans.propertyeditors.StringArrayPropertyEditor)4 IOException (java.io.IOException)3 URL (java.net.URL)3 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)3 Resource (org.springframework.core.io.Resource)3 BooleanTestBean (org.springframework.tests.sample.beans.BooleanTestBean)3 NumberTestBean (org.springframework.tests.sample.beans.NumberTestBean)3 BindStatus (org.springframework.web.servlet.support.BindStatus)3 StringReader (java.io.StringReader)2