Search in sources :

Example 86 with PropertyEditor

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

the class IntrospectionSupport method convert.

private static Object convert(TypeConverter typeConverter, Class<?> type, Object value) throws URISyntaxException, NoTypeConversionAvailableException {
    if (typeConverter != null) {
        return typeConverter.mandatoryConvertTo(type, value);
    }
    if (type == URI.class) {
        return new URI(value.toString());
    }
    PropertyEditor editor = PropertyEditorManager.findEditor(type);
    if (editor != null) {
        // property editor is not thread safe, so we need to lock
        Object answer;
        synchronized (LOCK) {
            editor.setAsText(value.toString());
            answer = editor.getValue();
        }
        return answer;
    }
    return null;
}
Also used : PropertyEditor(java.beans.PropertyEditor) URI(java.net.URI)

Example 87 with PropertyEditor

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

the class Test4274639 method main.

public static void main(String[] args) {
    TestBean bean = new TestBean(STRING_VALUE);
    if (!STRING_VALUE.equals(bean.getString()))
        throw new Error("unexpected string property: " + bean.getString());
    boolean string = false;
    boolean integer = false;
    for (PropertyDescriptor pd : BeanUtils.getPropertyDescriptors(bean.getClass())) {
        String name = pd.getName();
        System.out.println(" - " + name);
        if (name.equals(STRING_PROPERTY)) {
            // This tests createPropertyEditor such that the PropertyEditor
            // returned will have the bean as the source object.
            Class type = pd.getPropertyEditorClass();
            if (!StringEditor.class.equals(type))
                throw new Error("unexpected property editor type: " + type);
            PropertyEditor editor = pd.createPropertyEditor(bean);
            if (editor == null)
                throw new Error("property editor cannot be created");
            if (STRING_VALUE != editor.getValue())
                throw new Error("unexpected value: " + editor.getValue());
            Object source = ((PropertyEditorSupport) editor).getSource();
            if (source != bean)
                throw new Error("unexpected source: " + source);
            string = true;
        }
        if (name.equals(INTEGER_PROPERTY)) {
            // This tests createPropertyEditor such that the PropertyEditor
            // returned will be just a new instance
            Class type = pd.getPropertyEditorClass();
            if (!IntegerEditor.class.equals(type))
                throw new Error("unexpected property editor type: " + type);
            PropertyEditor editor = pd.createPropertyEditor(bean);
            if (editor == null)
                throw new Error("property editor cannot be created");
            if (INTEGER_VALUE != editor.getValue())
                throw new Error("unexpected value: " + editor.getValue());
            Object source = ((PropertyEditorSupport) editor).getSource();
            if (source != editor)
                throw new Error("unexpected source: " + source);
            integer = true;
        }
    }
    if (!string)
        throw new Error("string property is not tested");
    if (!integer)
        throw new Error("integer property is not tested");
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) PropertyEditor(java.beans.PropertyEditor) PropertyEditorSupport(java.beans.PropertyEditorSupport)

Example 88 with PropertyEditor

use of java.beans.PropertyEditor in project lucene-solr by apache.

the class ParseContextConfig method getValueFromString.

private Object getValueFromString(Class<?> targetType, String text) {
    final PropertyEditor editor = PropertyEditorManager.findEditor(targetType);
    if (editor == null) {
        throw new IllegalArgumentException("Cannot set properties of type " + targetType.getName());
    }
    editor.setAsText(text);
    return editor.getValue();
}
Also used : PropertyEditor(java.beans.PropertyEditor)

Example 89 with PropertyEditor

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

the class JspRuntimeLibrary method getValueFromBeanInfoPropertyEditor.

//*********************************************************************
// PropertyEditor Support
public static Object getValueFromBeanInfoPropertyEditor(Class attrClass, String attrName, String attrValue, Class propertyEditorClass) throws JasperException {
    try {
        PropertyEditor pe = (PropertyEditor) propertyEditorClass.newInstance();
        pe.setAsText(attrValue);
        return pe.getValue();
    } catch (Exception ex) {
        throw new JasperException(Localizer.getMessage("jsp.error.beans.property.conversion", attrValue, attrClass.getName(), attrName, ex.getMessage()));
    }
}
Also used : JasperException(org.apache.sling.scripting.jsp.jasper.JasperException) PropertyEditor(java.beans.PropertyEditor) PrivilegedActionException(java.security.PrivilegedActionException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) JasperException(org.apache.sling.scripting.jsp.jasper.JasperException)

Example 90 with PropertyEditor

use of java.beans.PropertyEditor in project geronimo-xbean by apache.

the class PropertyEditors method findEditor.

/**
 * Locate a property editor for qiven class of object.
 *
 * @param type The target object class of the property.
 * @return The resolved editor, if any.  Returns null if a suitable editor
 *         could not be located.
 */
private static PropertyEditor findEditor(Type type) {
    if (type == null)
        throw new NullPointerException("type is null");
    Class clazz = toClass(type);
    // try to locate this directly from the editor manager first.
    PropertyEditor editor = PropertyEditorManager.findEditor(clazz);
    // we're outta here if we got one.
    if (editor != null) {
        return editor;
    }
    // resolvable
    if (clazz.isArray() && !clazz.getComponentType().isArray()) {
        // do a recursive lookup on the base type
        editor = findEditor(clazz.getComponentType());
        // wrapper this in an array adaptor for real use
        if (editor != null) {
            return new ArrayConverter(clazz, editor);
        }
    }
    // nothing found
    return null;
}
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