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:edu.scripps.fl.pubchem.app.util.GroupingIterator.java

public boolean hasNext() {
    array = Array.newInstance(clazz, groupSize);
    int ii = 0;/*from   w w w.  j a v  a  2 s  .  c  o m*/
    for (ii = 0; ii < groupSize; ii++)
        if (iterator.hasNext()) {
            Object obj = iterator.next();
            obj = ConvertUtils.convert(obj, clazz);
            Array.set(array, ii, obj);
        } else
            break;
    if (ii < groupSize) {
        Object dest = Array.newInstance(clazz, ii);
        System.arraycopy(array, 0, dest, 0, ii);
        array = dest;
    }
    return ii > 0;
}

From source file:cn.hxh.springside.mapper.ObjectMapper.java

/**
 * Apache BeanUtils?.//from ww w .  j  av  a  2s.c  o m
 * 
 * @param value ?.
 * @param toType ?.
 */
public static Object convertToObject(String value, Class<?> toType) {
    try {
        return ConvertUtils.convert(value, toType);
    } catch (Exception e) {
        throw ReflectionUtils.convertReflectionExceptionToUnchecked(e);
    }
}

From source file:com.helpinput.propertyeditors.PropertyEditorRegister.java

private static boolean addProperty(Class<? extends PropertyEditor> propertyEditorType, Property propertyAnn,
        Map<Method, Object> setMethodAndValues) {

    final String methodName = Commons.getSetterName(propertyAnn.name());

    if (!Utils.hasLength(methodName))
        return false;

    Method method = Utils.findMethod(propertyEditorType, methodName);

    if (method == null || !method.getReturnType().equals(Void.TYPE))
        return false;

    Class<?>[] parameterTypes = method.getParameterTypes();

    if (parameterTypes == null || parameterTypes.length != 1)
        return false;

    try {/*from ww w  .  j a v  a 2 s .c o m*/
        Class<?> parameterType = parameterTypes[0];
        Object realValue = ConvertUtils.convert(propertyAnn.value(), parameterType);
        setMethodAndValues.put(method, realValue);
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

From source file:com.rodaxsoft.mailgun.converters.ListMemberConverter.java

@Override
public <T> T convert(Class<T> type, Object value) {

    ListMember lm = null;/*from   w  ww  .ja v  a2 s  .  c om*/

    if (value instanceof JSONObject) {
        JSONObject jsonObj = (JSONObject) value;

        lm = new ListMember();
        lm.setAddress(jsonObj.getString("address"));
        lm.setName(jsonObj.getString("name"));
        lm.setSubscribed(jsonObj.getBoolean("subscribed"));

        JSONObject varsObj = jsonObj.optJSONObject("vars");

        if (varsObj != null) {

            //Use a converter if varClass defined
            if (varClass != null && ConvertUtils.lookup(JSONObject.class, varClass) != null) {
                lm.setVars(ConvertUtils.convert(varsObj, varClass));
            }

            else {
                lm.setVars(varsObj);
            }
        }

    }

    return type.cast(lm);
}

From source file:net.servicefixture.converter.XMLGregorianCalendarConverter.java

public Object fromObject(Object source) {
    if (source == null) {
        return null;
    }/*from   ww  w .j av  a2 s. c om*/
    if (source instanceof XMLGregorianCalendar) {
        return source;
    } else if (source instanceof Calendar) {
        return calendarToXMLGregorianCalendar((Calendar) source);
    } else if (source instanceof Date) {
        return dateToXMLGregorianCalendar((Date) source);
    } else {
        Date date = (Date) ConvertUtils.convert(source.toString(), Date.class);
        return dateToXMLGregorianCalendar(date);
    }
}

From source file:edu.scripps.fl.curves.plot.ShapeFactory.java

private static int[] intArray(double... values) {
    return (int[]) ConvertUtils.convert(values, int[].class);
}

From source file:net.servicefixture.converter.ObjectConverter.java

/**
 * Converts string data to java object.//from   w  w  w  .  j a va  2s .  co m
 * 
 * @param data
 * @param destType
 * @return
 */
public static Object convert(String data, Class destType, boolean checkType) {
    // Convert EMPTY_TOKEN to an empty array
    if (EMPTY_TOKEN.equals(data) && CollectionBuilderFactory.isCollectionType(destType)) {
        return CollectionBuilderFactory.createCollectionBuilder(null, null, destType).getCollection();
    }

    // Check if it s a jexl token
    if (isExpression(data)) {
        return evaluateJexlToken(extractExpression(data), destType, checkType);
    }
    if (Calendar.class.isAssignableFrom(destType)) {
        destType = Calendar.class;
    }

    // If the destType is not supported by ConvertUtils
    if (ConvertUtils.lookup(destType) == null) {
        return specialConvert(data, destType);
    }
    return ConvertUtils.convert(data, destType);
}

From source file:io.github.benas.projector.processors.AbstractAnnotationProcessor.java

/**
 * Convert the value to field type and set it in the target object.
 *
 * @param target the target object/*from  w ww.  j a  va  2  s  .  c om*/
 * @param field the annotated field
 * @param key the annotation property attribute
 * @param value the value to inject
 * @throws Exception thrown if an exception occurs when trying to set the field value
 */
protected void injectProperty(Object target, Field field, String key, Object value) throws Exception {

    Object typedValue = ConvertUtils.convert(value, field.getType());
    try {
        PropertyUtils.setProperty(target, field.getName(), typedValue);
    } catch (Exception e) {
        throw new Exception("Unable to set property " + key + " on field " + field.getName() + " of type "
                + target.getClass() + ". A setter may be missing for this field.", e);
    }

}

From source file:com.rodaxsoft.mailgun.MailingListManager.java

/**
 * Returns the mailing lists for the account
 * @return A collection of list info objects 
 * @throws ContextedException if a processing error occurs
 * @since 0.2/*from w  w w  .  j  a va2  s .  com*/
 */
Collection<ListInfo> getLists() throws ContextedException {
    final String path = "/lists";
    RESTResponse restResponse = invokeGet(path);

    List<ListInfo> lists = null;

    if (restResponse.getFamily() == SUCCESSFUL) {

        JSONObject jsonObj = toJSONObject(restResponse.getStringResponse());
        JSONArray jsonArray = jsonObj.getJSONArray("items");

        @SuppressWarnings("unchecked")
        Iterator<JSONObject> iter = jsonArray.iterator();

        while (iter.hasNext()) {

            JSONObject anItem = iter.next();

            if (null == lists) {
                lists = new ArrayList<>();
            }

            ListInfo info;
            info = (ListInfo) ConvertUtils.convert(anItem, ListInfo.class);
            lists.add(info);

        }

    }

    return lists;
}

From source file:com.rodaxsoft.mailgun.CampaignManager.java

/**
 * Returns the campaign for the given ID
 * @param campaignId The campaign ID//from w w  w  .  j  av  a 2 s . c  om
 * @return the campaign for the given ID or <code>null</code>
 * @throws ContextedException if a processing error occurs
 */
Campaign getCampaign(String campaignId) throws ContextedException {
    final String domain = getAccount().getDomain();
    final String path = "/" + domain + "/campaigns/" + campaignId;
    RESTResponse restResponse = invokeGet(path);

    Campaign campaign = null;
    if (restResponse.getFamily() == SUCCESSFUL) {
        JSONObject json = toJSONObject(restResponse.getStringResponse());
        campaign = (Campaign) ConvertUtils.convert(json, Campaign.class);
    }

    return campaign;

}