Example usage for org.apache.commons.beanutils ConvertUtils convert

List of usage examples for org.apache.commons.beanutils ConvertUtils convert

Introduction

In this page you can find the example usage for org.apache.commons.beanutils ConvertUtils convert.

Prototype

public static Object convert(String values[], Class clazz) 

Source Link

Document

Convert an array of specified values to an array of objects of the specified class (if possible).

For more details see ConvertUtilsBean.

Usage

From source file:org.xwiki.properties.internal.converter.ConvertUtilsConverter.java

/**
 * @param <T> the type in which the provided value has to be converted
 * @param targetType the type in which the provided value has to be converted
 * @param sourceValue the value to convert
 * @return the converted value// w  w w.j a v  a 2s . co m
 */
private <T> T convert(Class<T> targetType, Object sourceValue) {
    T result;

    // We can't use Class#cast(Object) because ConvertUtils#convert always return Object form of the targetType even
    // if targetType is a primitive. When using casting syntax Object form is implicitly converter to proper
    // primitive type.
    try {
        result = (T) ConvertUtils.convert(sourceValue, targetType);
    } catch (ConversionException ex) {
        throw new org.xwiki.properties.converter.ConversionException("Error while performing type conversion",
                ex);
    }

    // BeanUtils converters will return the passed value if no converter has been found. Thus we need to check
    // that the returned value is compatible with the expected type and raise a ConversionException if not.
    if (!TypeUtils.isAssignable(targetType, result.getClass())) {
        throw new org.xwiki.properties.converter.ConversionException(
                String.format("Failed to find a Converter to convert from [%s] to [%s]",
                        sourceValue.getClass().getName(), targetType.getName()));
    }

    return result;
}

From source file:pico.commons.beans.BeanInfo.java

/**
*    Setter  .//from   w  w  w  .j  av  a2  s  . c  om
* @param bean   Bean 
* @param propertyName Property 
* @param param Method Setter 
*/
public void invokeSetter(Object bean, String propertyName, Object param) {
    Method method = (Method) setMethods.get(propertyName);
    if (method == null)
        throw new BeanNotAccessException(
                "There is no WRITEABLE property named '" + propertyName + "' in class '" + className + "'");

    try {
        if (param != null) {
            if (targetClass.isAssignableFrom(param.getClass()))
                method.invoke(bean, new Object[] { param });
            else {
                Class<?> clsParam = setTypes.get(propertyName);
                //Object tmp = Convertor.convert(clsParam, param);   // Convertor    
                Object tmp = ConvertUtils.convert(param, clsParam);
                method.invoke(bean, new Object[] { tmp });
            }
        }
    } catch (IllegalAccessException e) {
        throw new BeanNotAccessException("failed Method invoke. ", e);
    } catch (IllegalArgumentException e) {
        throw new BeanNotAccessException("failed Method invoke. ", e);
    } catch (InvocationTargetException e) {
        throw new BeanNotAccessException("failed Method invoke. ", e);
    }
}

From source file:pico.commons.beans.BeanInfo.java

/**
*    Field  .// w  w w.j a  v  a 2 s . c o  m
* @param bean   Bean 
* @param propertyName Property 
* @param param Method Setter 
*/
public void setField(Object bean, String propertyName, Object param) {
    Field field = (Field) fields.get(propertyName);
    if (field == null)
        throw new BeanNotAccessException(
                "There is no FIELD property named '" + propertyName + "' in class '" + className + "'");

    try {
        if (param != null) {
            if (targetClass.isAssignableFrom(param.getClass()))
                field.set(bean, param);
            else {
                Class<?> clsParam = fieldTypes.get(propertyName);
                //Object tmp = Convertor.convert(clsParam, param);
                Object tmp = ConvertUtils.convert(param, clsParam);
                field.set(bean, tmp);
            }
        }
    } catch (IllegalAccessException e) {
        throw new BeanNotAccessException("failed Method invoke. ", e);
    } catch (IllegalArgumentException e) {
        throw new BeanNotAccessException("failed Method invoke. ", e);
    }
}

From source file:ro.nextreports.server.api.client.jdbc.ResultSet.java

@Override
public boolean getBoolean(int columnIndex) throws SQLException {
    //      Boolean res = (Boolean) getObject(columnIndex);
    //      return res.booleanValue();
    return (Boolean) ConvertUtils.convert(getObject(columnIndex), Boolean.class);
}

From source file:ro.nextreports.server.api.client.jdbc.ResultSet.java

@Override
public byte getByte(int columnIndex) throws SQLException {
    //      Byte res = (Byte) getObject(columnIndex);
    //      return res.byteValue();
    return (Byte) ConvertUtils.convert(getObject(columnIndex), Byte.class);
}

From source file:ro.nextreports.server.api.client.jdbc.ResultSet.java

@Override
public short getShort(int columnIndex) throws SQLException {
    //      Short res = (Short) getObject(columnIndex);
    //      return res.shortValue();
    //      return ((Number) getObject(columnIndex)).shortValue();
    return (Short) ConvertUtils.convert(getObject(columnIndex), Short.class);
}

From source file:ro.nextreports.server.api.client.jdbc.ResultSet.java

@Override
public int getInt(int columnIndex) throws SQLException {
    //      Integer res = (Integer) getObject(columnIndex);
    //      return res.intValue();
    //      return ((Number) getObject(columnIndex)).intValue();
    return (Integer) ConvertUtils.convert(getObject(columnIndex), Integer.class);
}

From source file:ro.nextreports.server.api.client.jdbc.ResultSet.java

@Override
public long getLong(int columnIndex) throws SQLException {
    //      Long res = (Long) getObject(columnIndex);
    //      return res.longValue();
    return (Long) ConvertUtils.convert(getObject(columnIndex), Long.class);
}

From source file:ro.nextreports.server.api.client.jdbc.ResultSet.java

@Override
public float getFloat(int columnIndex) throws SQLException {
    //      Float res = (Float) getObject(columnIndex);
    //      return res.floatValue();
    return (Float) ConvertUtils.convert(getObject(columnIndex), Float.class);
}

From source file:ro.nextreports.server.api.client.jdbc.ResultSet.java

@Override
public double getDouble(int columnIndex) throws SQLException {
    //      Double res = (Double) getObject(columnIndex);
    //      return res.doubleValue();
    return (Double) ConvertUtils.convert(getObject(columnIndex), Double.class);
}