Example usage for java.beans PropertyEditorManager findEditor

List of usage examples for java.beans PropertyEditorManager findEditor

Introduction

In this page you can find the example usage for java.beans PropertyEditorManager findEditor.

Prototype

public static PropertyEditor findEditor(Class<?> targetType) 

Source Link

Document

Locate a value editor for a given target type.

Usage

From source file:com.easyget.commons.csv.bean.CsvToBean.java

private PropertyEditor getPropertyEditorValue(Class<?> cls) {
    if (editorMap == null) {
        editorMap = new HashMap<>();
    }//  w  w w.jav a2  s  .c  om

    PropertyEditor editor = editorMap.get(cls);

    if (editor == null) {
        editor = PropertyEditorManager.findEditor(cls);
        addEditorToMap(cls, editor);
    }

    return editor;
}

From source file:org.fusesource.meshkeeper.util.internal.IntrospectionSupport.java

public static String convertToString(Object value, Class<?> type) {
    PropertyEditor editor = PropertyEditorManager.findEditor(type);
    if (editor != null) {
        editor.setValue(value);//  w  ww.j  a  v a 2s  .  c o  m
        return editor.getAsText();
    }
    return null;
}

From source file:org.springframework.faces.mvc.bind.ReverseDataBinder.java

/**
 * Find a default editor for the given type. This code is based on <tt>TypeConverterDelegate.findDefaultEditor</tt>
 * from Spring 2.5.6./*  w  ww .  j a v  a 2  s .  c  o m*/
 * @param requiredType the type to find an editor for
 * @param descriptor the JavaBeans descriptor for the property
 * @return the corresponding editor, or <code>null</code> if none
 * 
 * @author Juergen Hoeller
 * @author Rob Harrop
 */
protected PropertyEditor findDefaultEditor(PropertyEditorRegistrySupport propertyEditorRegistrySupport,
        Object targetObject, Class requiredType, PropertyDescriptor descriptor) {

    PropertyEditor editor = null;
    if (descriptor != null) {
        if (JdkVersion.isAtLeastJava15()) {
            editor = descriptor.createPropertyEditor(targetObject);
        } else {
            Class editorClass = descriptor.getPropertyEditorClass();
            if (editorClass != null) {
                editor = (PropertyEditor) BeanUtils.instantiateClass(editorClass);
            }
        }
    }

    if (editor == null && requiredType != null) {
        // No custom editor -> check default editors.
        editor = propertyEditorRegistrySupport.getDefaultEditor(requiredType);
        if (editor == null && !String.class.equals(requiredType)) {
            // No BeanWrapper default editor -> check standard JavaBean editor.
            editor = BeanUtils.findEditorByConvention(requiredType);
            if (editor == null && !unknownEditorTypes.containsKey(requiredType)) {
                // Global PropertyEditorManager fallback...
                editor = PropertyEditorManager.findEditor(requiredType);
                if (editor == null) {
                    // Regular case as of Spring 2.5
                    unknownEditorTypes.put(requiredType, Boolean.TRUE);
                }
            }
        }
    }
    return editor;
}

From source file:org.fusesource.meshkeeper.util.internal.IntrospectionSupport.java

private static boolean isSettableType(Class<?> clazz) {
    if (PropertyEditorManager.findEditor(clazz) != null) {
        return true;
    }//w w  w. jav a  2 s .  c  o  m

    return false;
}

From source file:io.fabric8.devops.ProjectConfigs.java

/**
 * Configures the given {@link ProjectConfig} with a map of key value pairs from
 * something like a JBoss Forge command// w  ww  .  j  av  a2  s.co m
 */
public static void configureProperties(ProjectConfig config, Map map) {
    Class<? extends ProjectConfig> clazz = config.getClass();
    BeanInfo beanInfo = null;
    try {
        beanInfo = Introspector.getBeanInfo(clazz);
    } catch (IntrospectionException e) {
        LOG.warn("Could not introspect " + clazz.getName() + ". " + e, e);
    }
    if (beanInfo != null) {
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor descriptor : propertyDescriptors) {
            Method writeMethod = descriptor.getWriteMethod();
            if (writeMethod != null) {
                String name = descriptor.getName();
                Object value = map.get(name);
                if (value != null) {
                    Object safeValue = null;
                    Class<?> propertyType = descriptor.getPropertyType();
                    if (propertyType.isInstance(value)) {
                        safeValue = value;
                    } else {
                        PropertyEditor editor = descriptor.createPropertyEditor(config);
                        if (editor == null) {
                            editor = PropertyEditorManager.findEditor(propertyType);
                        }
                        if (editor != null) {
                            String text = value.toString();
                            editor.setAsText(text);
                            safeValue = editor.getValue();
                        } else {
                            LOG.warn("Cannot update property " + name + " with value " + value + " of type "
                                    + propertyType.getName() + " on " + clazz.getName());
                        }
                    }
                    if (safeValue != null) {
                        try {
                            writeMethod.invoke(config, safeValue);
                        } catch (Exception e) {
                            LOG.warn("Failed to set property " + name + " with value " + value + " on "
                                    + clazz.getName() + " " + config + ". " + e, e);
                        }

                    }
                }
            }
        }
    }
    String flow = null;
    Object flowValue = map.get("pipeline");
    if (flowValue == null) {
        flowValue = map.get("flow");
    }
    if (flowValue != null) {
        flow = flowValue.toString();
    }
    config.setPipeline(flow);
}

From source file:net.sourceforge.vulcan.spring.SpringBeanXmlEncoder.java

void encodeAsValue(final Element propertyNode, final Object object) {
    if (object == null) {
        propertyNode.addContent(new Element("null"));
        return;//from  w ww.j  a  va 2s. c  om
    } else if (object instanceof Enum<?>) {
        encodeEnum(propertyNode, (Enum<?>) object);
        return;
    }

    final Element valueNode = new Element("value");

    PropertyEditor editor = PropertyEditorManager.findEditor(object.getClass());

    if (editor == null && object instanceof Date) {
        editor = new DateEditor();
    }

    if (editor != null) {
        editor.setValue(object);
        valueNode.setText(editor.getAsText());
    } else {
        valueNode.setText(object.toString());
    }
    propertyNode.addContent(valueNode);
}

From source file:cat.albirar.framework.dynabean.impl.DynaBeanDescriptor.java

/**
 * Search for a {@link PropertyEditor} for the given property or item component if array or collection.
 * @param propDesc The descriptor, required
 *//*  www  .j  a v  a 2  s .  c o m*/
private void resolvePropertyEditorForProperty(DynaBeanPropertyDescriptor propDesc) {
    PropertyEditor pEditor;

    // Ignore String editor
    if (!String.class.equals(propDesc.getItemType())) {
        if ((pEditor = getFactory().getPropertyEditorRegistry().findCustomEditor(propDesc.getItemType(),
                propDesc.getPropertyPath())) == null) {
            // Last, find in
            if ((pEditor = PropertyEditorManager.findEditor(propDesc.getItemType())) == null) {
                getFactory().getPropertyEditorRegistry().registerCustomEditor(propDesc.getItemType(), pEditor);
            }
        }
        propDesc.propertyItemEditor = pEditor;
    }
}

From source file:org.apache.jmeter.testbeans.gui.GenericTestBeanCustomizer.java

/**
 * Create a customizer for a given test bean type.
 *
 * @param testBeanClass/*  w w w  .j  av a 2s  . c om*/
 *            a subclass of TestBean
 * @see org.apache.jmeter.testbeans.TestBean
 */
GenericTestBeanCustomizer(BeanInfo beanInfo) {
    super();

    this.beanInfo = beanInfo;

    // Get and sort the property descriptors:
    descriptors = beanInfo.getPropertyDescriptors();
    Arrays.sort(descriptors, new PropertyComparator(beanInfo));

    // Obtain the propertyEditors:
    editors = new PropertyEditor[descriptors.length];
    int scriptLanguageIndex = 0;
    int textAreaEditorIndex = 0;
    for (int i = 0; i < descriptors.length; i++) { // Index is also used for accessing editors array
        PropertyDescriptor descriptor = descriptors[i];
        String name = descriptor.getName();

        // Don't get editors for hidden or non-read-write properties:
        if (TestBeanHelper.isDescriptorIgnored(descriptor)) {
            log.debug("Skipping editor for property " + name);
            editors[i] = null;
            continue;
        }

        PropertyEditor propertyEditor;
        Object guiType = descriptor.getValue(GUITYPE);
        if (guiType instanceof TypeEditor) {
            propertyEditor = ((TypeEditor) guiType).getInstance(descriptor);
        } else if (guiType instanceof Class && Enum.class.isAssignableFrom((Class<?>) guiType)) {
            @SuppressWarnings("unchecked") // we check the class type above
            final Class<? extends Enum<?>> enumClass = (Class<? extends Enum<?>>) guiType;
            propertyEditor = new EnumEditor(descriptor, enumClass,
                    (ResourceBundle) descriptor.getValue(GenericTestBeanCustomizer.RESOURCE_BUNDLE));
        } else {
            Class<?> editorClass = descriptor.getPropertyEditorClass();
            if (log.isDebugEnabled()) {
                log.debug("Property " + name + " has editor class " + editorClass);
            }

            if (editorClass != null) {
                try {
                    propertyEditor = (PropertyEditor) editorClass.newInstance();
                } catch (InstantiationException | IllegalAccessException e) {
                    log.error("Can't create property editor.", e);
                    throw new Error(e.toString());
                }
            } else {
                Class<?> c = descriptor.getPropertyType();
                propertyEditor = PropertyEditorManager.findEditor(c);
            }
        }

        if (propertyEditor == null) {
            log.warn("No editor for property: " + name + " type: " + descriptor.getPropertyType() + " in bean: "
                    + beanInfo.getBeanDescriptor().getDisplayName());
            editors[i] = null;
            continue;
        }

        if (log.isDebugEnabled()) {
            log.debug("Property " + name + " has property editor " + propertyEditor);
        }

        validateAttributes(descriptor, propertyEditor);

        if (!propertyEditor.supportsCustomEditor()) {
            propertyEditor = createWrapperEditor(propertyEditor, descriptor);

            if (log.isDebugEnabled()) {
                log.debug("Editor for property " + name + " is wrapped in " + propertyEditor);
            }
        }
        if (propertyEditor instanceof TestBeanPropertyEditor) {
            ((TestBeanPropertyEditor) propertyEditor).setDescriptor(descriptor);
        }

        if (propertyEditor instanceof TextAreaEditor) {
            textAreaEditorIndex = i;
        }
        if (propertyEditor.getCustomEditor() instanceof JScrollPane) {
            scrollerCount++;
        }

        editors[i] = propertyEditor;

        // Initialize the editor with the provided default value or null:
        setEditorValue(i, descriptor.getValue(DEFAULT));

        if (name.equals("scriptLanguage")) {
            scriptLanguageIndex = i;
        }

    }
    // In case of BSF and JSR elements i want to add textAreaEditor as a listener to scriptLanguage ComboBox.
    String beanName = this.beanInfo.getBeanDescriptor().getName();
    if (beanName.startsWith("BSF") || beanName.startsWith("JSR223")) { // $NON-NLS-1$ $NON-NLS-2$
        WrapperEditor we = (WrapperEditor) editors[scriptLanguageIndex];
        TextAreaEditor tae = (TextAreaEditor) editors[textAreaEditorIndex];
        we.addChangeListener(tae);
    }

    // Obtain message formats:
    propertyFieldLabelMessage = new MessageFormat(JMeterUtils.getResString("property_as_field_label")); //$NON-NLS-1$
    propertyToolTipMessage = new MessageFormat(JMeterUtils.getResString("property_tool_tip")); //$NON-NLS-1$

    // Initialize the GUI:
    init();
}

From source file:io.fabric8.apmagent.ApmConfiguration.java

private Object convert(Object value, Class type) throws Exception {
    PropertyEditor editor = PropertyEditorManager.findEditor(type);
    if (editor != null) {
        editor.setAsText(value.toString());
        return editor.getValue();
    }/*  w  w  w  .j a va 2s . c  o m*/
    if (type == URI.class) {
        return new URI(value.toString());
    }
    return null;
}

From source file:com.wavemaker.runtime.server.ServerUtils.java

private static Object convert(String value, Class clazz) {
    PropertyEditor editor = PropertyEditorManager.findEditor(clazz);
    editor.setAsText(value);//from   w w  w .  j  av  a  2s .  c  om
    return editor.getValue();
}