Search in sources :

Example 11 with PropertyEditor

use of java.beans.PropertyEditor in project Smartcity-Smarthouse by TechnionYP5777.

the class StringConverter method convert.

/**
     * Convert a string into any class
     * 
     * @param targetType
     * @param text
     * @return the new converted Object
     */
public static Object convert(final Class<?> targetType, final String text) {
    if (targetType.isPrimitive()) {
        if (text == null)
            return targetType.equals(Character.TYPE) ? '\0' : targetType.equals(Boolean.TYPE) ? false : 0;
        if (targetType.equals(Character.TYPE))
            return text.charAt(0);
    }
    final PropertyEditor editor = PropertyEditorManager.findEditor(targetType);
    if (editor == null) {
        log.warn("Converting an uknown Object to null: [targetType: " + targetType.getCanonicalName() + "]");
        return null;
    }
    editor.setAsText(text);
    return editor.getValue();
}
Also used : PropertyEditor(java.beans.PropertyEditor)

Example 12 with PropertyEditor

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

the class Configurator method convertValue.

/**
     * Convert a value
     *
     * @param clazz             the class
     * @param value             the value
     * @param replaceProperties whether to replace system properties
     * @param trim              whether to trim string value
     * @return the value or null if there is no editor
     * @throws Throwable for any error
     */
@SuppressWarnings("unchecked")
public static Object convertValue(Class<?> clazz, Object value, boolean replaceProperties, boolean trim) throws Throwable {
    if (clazz == null)
        return value;
    if (value == null)
        return null;
    Class<?> valueClass = value.getClass();
    // If we have a string, trim and replace any system properties when requested
    if (valueClass == String.class) {
        String string = (String) value;
        if (trim)
            string = string.trim();
        if (replaceProperties)
            value = PropertiesValueResolver.replaceProperties(string);
    }
    if (clazz.isAssignableFrom(valueClass))
        return value;
    // First see if this is an Enum
    if (clazz.isEnum()) {
        Class<? extends Enum> eclazz = clazz.asSubclass(Enum.class);
        return Enum.valueOf(eclazz, value.toString());
    }
    // Next look for a property editor
    if (valueClass == String.class) {
        PropertyEditor editor = PropertyEditorManager.findEditor(clazz);
        if (editor != null) {
            editor.setAsText((String) value);
            return editor.getValue();
        }
    }
    // Try a static clazz.valueOf(value)
    try {
        Method method = clazz.getMethod("valueOf", valueClass);
        int modifiers = method.getModifiers();
        if (Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers) && clazz.isAssignableFrom(method.getReturnType()))
            return method.invoke(null, value);
    } catch (Exception ignored) {
    }
    if (valueClass == String.class) {
        try {
            Constructor constructor = clazz.getConstructor(valueClass);
            if (Modifier.isPublic(constructor.getModifiers()))
                return constructor.newInstance(value);
        } catch (Exception ignored) {
        }
    }
    return value;
}
Also used : Constructor(java.lang.reflect.Constructor) PropertyEditor(java.beans.PropertyEditor) Method(java.lang.reflect.Method)

Example 13 with PropertyEditor

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

the class URLEditorTests method testSetAsTextWithNull.

@Test
public void testSetAsTextWithNull() throws Exception {
    PropertyEditor urlEditor = new URLEditor();
    urlEditor.setAsText(null);
    assertNull(urlEditor.getValue());
    assertEquals("", urlEditor.getAsText());
}
Also used : PropertyEditor(java.beans.PropertyEditor) Test(org.junit.Test)

Example 14 with PropertyEditor

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

the class CustomEditorTests method testFileEditorWithAbsolutePath.

@Test
public void testFileEditorWithAbsolutePath() {
    PropertyEditor fileEditor = new FileEditor();
    // testing on Windows
    if (new File("C:/myfile.txt").isAbsolute()) {
        fileEditor.setAsText("C:/myfile.txt");
        assertEquals(new File("C:/myfile.txt"), fileEditor.getValue());
    }
    // testing on Unix
    if (new File("/myfile.txt").isAbsolute()) {
        fileEditor.setAsText("/myfile.txt");
        assertEquals(new File("/myfile.txt"), fileEditor.getValue());
    }
}
Also used : PropertyEditor(java.beans.PropertyEditor) File(java.io.File) Test(org.junit.Test)

Example 15 with PropertyEditor

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

the class CustomEditorTests method testLocaleEditor.

@Test
public void testLocaleEditor() {
    PropertyEditor localeEditor = new LocaleEditor();
    localeEditor.setAsText("en_CA");
    assertEquals(Locale.CANADA, localeEditor.getValue());
    assertEquals("en_CA", localeEditor.getAsText());
    localeEditor = new LocaleEditor();
    assertEquals("", localeEditor.getAsText());
}
Also used : PropertyEditor(java.beans.PropertyEditor) Test(org.junit.Test)

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