Example usage for java.beans PropertyDescriptor createPropertyEditor

List of usage examples for java.beans PropertyDescriptor createPropertyEditor

Introduction

In this page you can find the example usage for java.beans PropertyDescriptor createPropertyEditor.

Prototype

@SuppressWarnings("deprecation")
public PropertyEditor createPropertyEditor(Object bean) 

Source Link

Document

Constructs an instance of a property editor using the current property editor class.

Usage

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 w  w  .ja  v  a 2  s.  c  o 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: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 . ja  v a  2s  . 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.tinygroup.beanwrapper.TypeConverterDelegate.java

/**
 * Find a default editor for the given type.
 * @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
 *//* w w  w.  j  a va 2 s. c  o  m*/
protected PropertyEditor findDefaultEditor(Class requiredType, PropertyDescriptor descriptor) {
    PropertyEditor editor = null;
    if (descriptor != null) {
        if (JdkVersion.isAtLeastJava15()) {
            editor = descriptor.createPropertyEditor(this.targetObject);
        } else {
            Class editorClass = descriptor.getPropertyEditorClass();
            if (editorClass != null) {
                editor = (PropertyEditor) BeanUtils.instantiateClass(editorClass);
            }
        }
    }
    if (editor == null && requiredType != null) {
        // No custom editor -> check BeanWrapperImpl's default editors.
        editor = (PropertyEditor) this.propertyEditorRegistry.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)) {
                // Deprecated global PropertyEditorManager fallback...
                editor = PropertyEditorManager.findEditor(requiredType);
                if (editor == null) {
                    // Regular case as of Spring 2.5
                    unknownEditorTypes.put(requiredType, Boolean.TRUE);
                } else {
                    logger.warn("PropertyEditor [" + editor.getClass().getName()
                            + "] found through deprecated global PropertyEditorManager fallback - "
                            + "consider using a more isolated form of registration, e.g. on the BeanWrapper/BeanFactory!");
                }
            }
        }
    }
    return editor;
}