Example usage for org.springframework.beans SimpleTypeConverter convertIfNecessary

List of usage examples for org.springframework.beans SimpleTypeConverter convertIfNecessary

Introduction

In this page you can find the example usage for org.springframework.beans SimpleTypeConverter convertIfNecessary.

Prototype

@Override
    @Nullable
    public <T> T convertIfNecessary(@Nullable Object value, @Nullable Class<T> requiredType)
            throws TypeMismatchException 

Source Link

Usage

From source file:com.conversantmedia.mapreduce.tool.DistributedResourceManager.java

public static void setFieldValue(Field field, Object bean, Object value) throws IllegalAccessException {
    // Set it on the field
    field.setAccessible(true);//from ww w  .j  av a 2 s .  c  om

    // Perform type conversion if possible..
    SimpleTypeConverter converter = new SimpleTypeConverter();
    try {
        value = converter.convertIfNecessary(value, field.getType());
    } catch (ConversionNotSupportedException e) {
        // If conversion isn't supported, ignore and try and set anyway.
    }
    field.set(bean, value);
}

From source file:org.uimafit.factory.ConfigurationParameterFactory.java

/**
 * Convert a value so it can be injected into a UIMA component. UIMA only supports several
 * parameter types. If the value is not of these types, this method can be used to coerce the
 * value into a supported type (typically String). It is also used to convert primitive
 * arrays to object arrays when necessary.
 * /*from  w  w w  . j a  v a 2  s. c om*/
 * @param param the configuration parameter.
 * @param aValue the parameter value.
 * @return the converted value.
 */
protected static Object convertParameterValue(ConfigurationParameter param, Object aValue) {
    Object value = aValue;
    if (value.getClass().isArray() && value.getClass().getComponentType().getName().equals("boolean")) {
        value = ArrayUtils.toObject((boolean[]) value);
    } else if (value.getClass().isArray() && value.getClass().getComponentType().getName().equals("int")) {
        value = ArrayUtils.toObject((int[]) value);
    } else if (value.getClass().isArray() && value.getClass().getComponentType().getName().equals("float")) {
        value = ArrayUtils.toObject((float[]) value);
    } else {
        try {
            if (param.getType().equals(ConfigurationParameter.TYPE_STRING)) {
                SimpleTypeConverter converter = new SimpleTypeConverter();
                PropertyEditorUtil.registerUimaFITEditors(converter);
                if (value.getClass().isArray() || value instanceof Collection) {
                    value = converter.convertIfNecessary(value, String[].class);
                } else {
                    value = converter.convertIfNecessary(value, String.class);
                }
            }
        } catch (TypeMismatchException e) {
            throw new IllegalArgumentException(e.getMessage(), e);
        }
    }

    return value;
}

From source file:org.jdal.swing.report.DefaultReportParameterEditor.java

public Object getValue() throws Exception {
    String strValue = ((JTextField) editor).getText();
    SimpleTypeConverter stc = new SimpleTypeConverter();
    return stc.convertIfNecessary(strValue, clazz);
}

From source file:com.conversantmedia.mapreduce.tool.AnnotatedToolContext.java

@Override
protected void populateExtendedContext(CommandLine line) throws ParseException {
    // Populate the underlying annotated context object
    // with the values from the parsed command line arguments.
    SimpleTypeConverter converter = new SimpleTypeConverter();

    for (Entry<String, Field> e : this.fieldsMap.entrySet()) {
        String optName = e.getKey();
        Field field = e.getValue();
        String defaultValue = defaultValuesMap.get(optName);
        field.setAccessible(true);/*from w  ww  .  j  a  va 2 s  . c o m*/
        try {
            if (line.hasOption(optName)) {
                Object value = line.getOptionValue(optName);
                if (value == null) {
                    value = Boolean.TRUE;
                }
                value = converter.convertIfNecessary(value, field.getType());
                field.set(this.bean, value);
            } else if (StringUtils.isNotBlank(defaultValue)) {
                Object value = converter.convertIfNecessary(defaultValue, field.getType());
                field.set(this.bean, value);
            }
        } catch (IllegalArgumentException | IllegalAccessException e1) {
            throw new ParseException(e1.getMessage());
        }
    }
}

From source file:org.dspace.servicemanager.config.DSpaceConfigurationService.java

/**
 * Convert the value of a given property to a specific object type.
 * <P>/*from  w ww.  ja v a2s .  co m*/
 * Note: in most cases we can just use Configuration get*() methods.
 *
 * @param name Key of the property to convert
 * @param <T> object type
 * @return converted value
 */
private <T> T convert(String name, Class<T> type) {

    // If this key doesn't exist, just return null
    if (!configuration.containsKey(name)) {
        // Special case. For booleans, return false if key doesn't exist
        if (Boolean.class.equals(type) || boolean.class.equals(type))
            return (T) Boolean.FALSE;
        else
            return null;
    }

    // Based on the type of class, call the appropriate
    // method of the Configuration object
    if (type.isArray())
        return (T) configuration.getStringArray(name);
    else if (String.class.equals(type) || type.isAssignableFrom(String.class))
        return (T) configuration.getString(name);
    else if (BigDecimal.class.equals(type))
        return (T) configuration.getBigDecimal(name);
    else if (BigInteger.class.equals(type))
        return (T) configuration.getBigInteger(name);
    else if (Boolean.class.equals(type) || boolean.class.equals(type))
        return (T) Boolean.valueOf(configuration.getBoolean(name));
    else if (Byte.class.equals(type) || byte.class.equals(type))
        return (T) Byte.valueOf(configuration.getByte(name));
    else if (Double.class.equals(type) || double.class.equals(type))
        return (T) Double.valueOf(configuration.getDouble(name));
    else if (Float.class.equals(type) || float.class.equals(type))
        return (T) Float.valueOf(configuration.getFloat(name));
    else if (Integer.class.equals(type) || int.class.equals(type))
        return (T) Integer.valueOf(configuration.getInt(name));
    else if (List.class.equals(type))
        return (T) configuration.getList(name);
    else if (Long.class.equals(type) || long.class.equals(type))
        return (T) Long.valueOf(configuration.getLong(name));
    else if (Short.class.equals(type) || short.class.equals(type))
        return (T) Short.valueOf(configuration.getShort(name));
    else {
        // If none of the above works, try to convert the value to the required type
        SimpleTypeConverter converter = new SimpleTypeConverter();
        return (T) converter.convertIfNecessary(configuration.getProperty(name), type);
    }
}