Example usage for java.beans PropertyEditor getValue

List of usage examples for java.beans PropertyEditor getValue

Introduction

In this page you can find the example usage for java.beans PropertyEditor getValue.

Prototype


Object getValue();

Source Link

Document

Gets the property value.

Usage

From source file:org.eclipse.wb.internal.swing.model.property.editor.beans.PropertyEditorWrapper.java

private static void updatePropertyValue(Property property, java.beans.PropertyEditor propertyEditor)
        throws Exception {
    Object value = propertyEditor.getValue();
    String source = propertyEditor.getJavaInitializationString();
    GenericProperty genericProperty = (GenericProperty) property;
    genericProperty.setExpression(source, value);
}

From source file:io.tilt.minka.utils.Defaulter.java

/**
 * @param   props the properties instance to look up keys for 
 * @param   configurable /* w w w  . j  a  v  a2 s.  c o  m*/
 *  applying object with pairs of "default" sufixed static fields in the format "some_value_default"
 *  and instance fields in the propercase format without underscores like "someValue" 
 * 
 * @return  TRUE if all defaults were applied. FALSE if some was not !
 */
public static boolean apply(final Properties props, Object configurable) {
    Validate.notNull(props);
    Validate.notNull(configurable);
    boolean all = true;
    for (final Field staticField : getStaticDefaults(configurable.getClass())) {
        final String name = properCaseIt(staticField.getName());
        if (staticField.getName().endsWith(SUFFIX)) {
            final String nameNoDef = name.substring(0, name.length() - SUFFIX.length());
            try {
                final Field instanceField = configurable.getClass().getDeclaredField(nameNoDef);
                try {
                    final PropertyEditor editor = edit(props, configurable, staticField, instanceField);
                    instanceField.setAccessible(true);
                    instanceField.set(configurable, editor.getValue());
                } catch (IllegalArgumentException | IllegalAccessException e) {
                    all = false;
                    logger.error("Defaulter: object <{}> cannot set value for field: {}",
                            configurable.getClass().getSimpleName(), nameNoDef, e);
                }
            } catch (NoSuchFieldException | SecurityException e) {
                all = false;
                logger.error("Defaulter: object <{}> has no field: {} for default static: {} (reason: {})",
                        configurable.getClass().getSimpleName(), nameNoDef, staticField.getName(),
                        e.getClass().getSimpleName());
            }
        }
    }
    return all;
}

From source file:io.tilt.minka.utils.Defaulter.java

private static PropertyEditor edit(final Properties props, final Object configurable, final Field staticField,
        final Field instanceField) throws IllegalAccessException {

    staticField.setAccessible(true);/*from   w  ww  .  j ava  2s  . c  om*/
    final String name = instanceField.getName();
    final String staticValue = staticField.get(configurable).toString();
    final Object propertyOrDefault = props.getProperty(name, System.getProperty(name, staticValue));
    final String objName = configurable.getClass().getSimpleName();
    final PropertyEditor editor = PropertyEditorManager.findEditor(instanceField.getType());
    final String setLog = "Defaulter: set <{}> field [{}] = '{}' from {} ";
    try {
        editor.setAsText(propertyOrDefault.toString());
        logger.info(setLog, objName, name, editor.getValue(),
                propertyOrDefault != staticValue ? " property " : staticField.getName());
    } catch (Exception e) {
        logger.error(
                "Defaulter: object <{}> field: {} does not accept property or static "
                        + "default value: {} (reason: {})",
                objName, name, propertyOrDefault, e.getClass().getSimpleName());
        try { // at this moment only prop. might've been failed
            editor.setAsText(staticValue);
            logger.info(setLog, objName, name, editor.getValue(), staticField.getName());
        } catch (Exception e2) {
            final StringBuilder sb = new StringBuilder().append("Defaulter: object <").append(objName)
                    .append("> field: ").append(name).append(" does not accept static default value: ")
                    .append(propertyOrDefault).append(" (reason: ").append(e.getClass().getSimpleName())
                    .append(")");
            throw new RuntimeException(sb.toString());
        }

    }

    return editor;
}

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

private static Object convert(Object value, Class<?> type) {
    PropertyEditor editor = PropertyEditorManager.findEditor(type);
    if (editor != null) {
        editor.setAsText(value.toString());
        return editor.getValue();
    }/*from   w w w.java2  s .c o m*/
    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   ww w. jav a 2  s .  c o m*/
    return editor.getValue();
}

From source file:org.apache.camel.util.IntrospectionSupport.java

@SuppressWarnings("unchecked")
private static Object convert(TypeConverter typeConverter, Class type, Object value)
        throws URISyntaxException, NoTypeConversionAvailableException {
    if (typeConverter != null) {
        return typeConverter.mandatoryConvertTo(type, value);
    }//from   w w w. j av  a  2 s.  c om
    PropertyEditor editor = PropertyEditorManager.findEditor(type);
    if (editor != null) {
        editor.setAsText(value.toString());
        return editor.getValue();
    }
    if (type == URI.class) {
        return new URI(value.toString());
    }
    return null;
}

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/*from   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:ar.com.zauber.commons.web.proxy.URLRequestMapperEditorTest.java

/** foo */
public final void testNull() {
    final PropertyEditor pe = new URLRequestMapperEditor();
    pe.setAsText("          ");
    assertNull(pe.getValue());
}

From source file:org.synyx.hades.extensions.beans.DomainClassPropertyEditorUnitTest.java

@Test
public void usesCustomEditorIfConfigured() throws Exception {

    PropertyEditor customEditor = mock(PropertyEditor.class);
    when(customEditor.getValue()).thenReturn(1);

    when(registry.findCustomEditor(Integer.class, null)).thenReturn(customEditor);

    convertsPlainIdTypeCorrectly();//from  www.  ja v  a2 s  . c om

    verify(customEditor, times(1)).setAsText("1");
}

From source file:org.tinygroup.springmvc.support.ConventionRestfulWebArgumentResolver.java

@SuppressWarnings("unchecked")
public Object resolveArgument(MethodParameter methodParameter, NativeWebRequest webRequest) throws Exception {

    if (!isConventionalRestful(webRequest)) {
        return UNRESOLVED;
    }//from  ww  w. j  ava 2 s  . c o  m
    // and the paramName is id
    if (!StringUtils.equals("id", methodParameter.getParameterName())) {
        return UNRESOLVED;
    }

    Map<String, String> uriTemplateVariables = null;
    if (BeanUtils.isSimpleProperty(methodParameter.getParameterType())) {
        uriTemplateVariables = (Map<String, String>) webRequest
                .getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);
    }
    if (uriTemplateVariables != null) {
        String val = uriTemplateVariables.get(methodParameter.getParameterName());
        if (val != null && String.class.equals(methodParameter.getParameterType())) {
            return val;
        } else if (val != null) {
            PropertyEditor editor = getDefaultEditor(methodParameter.getParameterType());
            if (editor != null) {
                editor.setAsText(val);
                return editor.getValue();
            }
        }
    }
    return UNRESOLVED;
}