Example usage for org.apache.commons.beanutils PropertyUtils getPropertyType

List of usage examples for org.apache.commons.beanutils PropertyUtils getPropertyType

Introduction

In this page you can find the example usage for org.apache.commons.beanutils PropertyUtils getPropertyType.

Prototype

public static Class getPropertyType(Object bean, String name)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException 

Source Link

Document

Return the Java Class representing the property type of the specified property, or null if there is no such property for the specified bean.

For more details see PropertyUtilsBean.

Usage

From source file:org.skfiy.typhon.spi.GMConsoleProvider.java

/**
 *
 * @param uid//from  w w  w  .  j  av  a 2  s .  c o  m
 * @param propertyName
 * @param val
 * @throws javax.management.MBeanException
 */
public void changeProperty(final String uid, final String propertyName, final String val)
        throws MBeanException {
    try {
        invoke(transcoding(uid), new Handler() {

            @Override
            void execute() {
                Player player = SessionUtils.getPlayer();
                try {
                    Class<?> propertyType = PropertyUtils.getPropertyType(player, propertyName);
                    if (propertyType == null) { // Not found property
                        throw new IllegalArgumentException("Not found property[" + propertyName + "]");
                    }

                    if (propertyType == Byte.class || propertyType == Byte.TYPE) {
                        BeanUtils.setProperty(player, propertyName, Byte.valueOf(val));
                    } else if (propertyType == Character.class || propertyType == Character.TYPE) {
                        BeanUtils.setProperty(player, propertyName, val.charAt(0));
                    } else if (propertyType == Boolean.class || propertyType == Boolean.TYPE) {
                        BeanUtils.setProperty(player, propertyName, Boolean.valueOf(val));
                    } else if (propertyType == Integer.class || propertyType == Integer.TYPE) {
                        BeanUtils.setProperty(player, propertyName, Integer.valueOf(val));
                    } else if (propertyType == Long.class || propertyType == Long.TYPE) {
                        BeanUtils.setProperty(player, propertyName, Long.valueOf(val));
                    } else if (propertyType == Float.class || propertyType == Float.TYPE) {
                        BeanUtils.setProperty(player, propertyName, Float.valueOf(val));
                    } else if (propertyType == Double.class || propertyType == Double.TYPE) {
                        BeanUtils.setProperty(player, propertyName, Double.valueOf(val));
                    } else {
                        BeanUtils.setProperty(player, propertyName, val);
                    }

                } catch (Exception ex) {
                    throw new IllegalArgumentException(ex.getMessage());
                }
            }

        });
    } catch (Exception e) {
        throw new MBeanException(e, e.getMessage());
    }
}

From source file:org.snaker.designer.utils.BeanUtil.java

public static Class<?> getProperyClass(Object bean, String propertyName) {
    try {//  ww w  . j  av  a 2 s  . c  o  m
        return PropertyUtils.getPropertyType(bean, propertyName);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.squale.welcom.struts.action.WRequestProcessor.java

/**
 * effectue un populate en conction des dates
 * /*from ww  w. j  a v  a  2 s.  c om*/
 * @param form : fomulaire
 * @param request : request
 * @parem element : Nom du champs
 */
private void processPopulateDate(final HttpServletRequest request, final ActionForm form,
        final String element) {
    if (element.endsWith("WDate") && !element.equals("WDate")) {

        final String field = element.substring(0, element.lastIndexOf("WDate"));

        String formattedDate = TagUtils.getDateHeureFromDateTag(request, field);

        final HashMap formatMaps = (HashMap) request.getSession().getAttribute(DATE_FORMAT_KEY);

        try {
            final Class propertyType = PropertyUtils.getPropertyType(form, field);
            if (propertyType != null) {
                boolean isTypeDate = propertyType.equals(Date.class);
                Object emptyValue = null;
                if (!isTypeDate) {
                    emptyValue = "";
                }

                if ((formatMaps != null) && (formatMaps.get(field + "WDate") != null)) {
                    String pattern = (String) formatMaps.get(field + "WDate");

                    if (formatMaps.get(field + "WHour") != null) {
                        pattern = pattern + " " + formatMaps.get(field + "WHour");
                    }

                    if (GenericValidator.isBlankOrNull(formattedDate)) {
                        BeanUtils.setProperty(form, field, emptyValue);
                    } else {
                        Locale locale = (Locale) request.getSession().getAttribute(Globals.LOCALE_KEY);
                        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern, locale);
                        dateFormat.setLenient(false);

                        try {
                            Date newDate = dateFormat.parse(formattedDate);

                            if (dateFormat.format(newDate).equals(formattedDate)) {
                                try {
                                    if (isTypeDate) {
                                        LayoutUtils.setProperty(form, field, newDate);
                                    } else {
                                        LayoutUtils.setProperty(form, field, String.valueOf(newDate.getTime()));
                                    }
                                } catch (SecurityException e1) {
                                    e1.printStackTrace();
                                }
                            } else {
                                BeanUtils.setProperty(form, field, emptyValue);
                            }
                        } catch (ParseException e) {
                            e.printStackTrace();
                            LayoutUtils.setProperty(form, field, emptyValue);
                        }
                    }
                } else {
                    if (formattedDate == null) {
                        formattedDate = "";
                    }
                    BeanUtils.setProperty(form, field, formattedDate);
                }

            }

        } catch (final JspException e1) {
            e1.printStackTrace();
        } catch (final IllegalAccessException e3) {
            e3.printStackTrace();
        } catch (final InvocationTargetException e3) {
            e3.printStackTrace();
        } catch (final NoSuchMethodException e3) {
            e3.printStackTrace();
        }

    }
}

From source file:org.squale.welcom.taglib.table.InternalTableUtil.java

/**
 * Remiez a zero de l'attribut case a coch pass en parametre
 * /*from  www . ja va2  s. c o  m*/
 * @param request : requets
 * @param form formulaire
 * @param attribute attribut
 */
private static void razTheCheckBoxAttribute(final HttpServletRequest request, final ActionForm form,
        final String attribute) {
    try {
        // Recupere le type de l'attribut
        Class propertyType = PropertyUtils.getPropertyType(form, attribute);
        if (String.class.equals(propertyType)) {
            BeanUtils.setProperty(form, attribute, "false");
        } else if (Boolean.class.equals(propertyType)) {
            BeanUtils.setProperty(form, attribute, Boolean.FALSE);
        } else if (Boolean.TYPE.equals(propertyType)) {
            BeanUtils.setProperty(form, attribute, "false");
        } else {
            log.warn("Impossible de reinitialiser la checkbox de la liste, type de l'attribut : " + attribute
                    + "=" + propertyType);
        }
    } catch (IllegalAccessException e) {
        log.error(e, e);
    } catch (InvocationTargetException e) {
        log.error(e, e);
    } catch (NoSuchMethodException e) {
        log.error(e, e);
    }
}

From source file:org.tdar.core.service.ReflectionService.java

/**
 * Cast the value to the object type of the field and call the setter. Validate the propert in the process.
 * //ww  w  .  j a v a2  s.c  om
 * @param beanToProcess
 * @param name
 * @param value
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public void validateAndSetProperty(Object beanToProcess, String name, String value) {
    List<String> errorValueList = Arrays.asList(name, value);
    try {
        logger.trace("processing: {} - {} --> {}", beanToProcess, name, value);
        Class propertyType = PropertyUtils.getPropertyType(beanToProcess, name);

        // handle types should we be testing column length?
        if (propertyType.isEnum()) {
            try {
                BeanUtils.setProperty(beanToProcess, name, Enum.valueOf(propertyType, value));
            } catch (IllegalArgumentException e) {
                logger.debug("cannot set property:", e);
                throw new TdarRecoverableRuntimeException("reflectionService.not_valid_value", e,
                        errorValueList);
            }
        } else {
            if (Integer.class.isAssignableFrom(propertyType)) {
                try {
                    Double dbl = Double.valueOf(value);
                    if (dbl == Math.floor(dbl)) {
                        value = new Integer((int) Math.floor(dbl)).toString();
                    }
                } catch (NumberFormatException nfe) {
                    throw new TdarRecoverableRuntimeException("reflectionService.expecting_integer",
                            errorValueList);
                }
            }
            if (Long.class.isAssignableFrom(propertyType)) {
                try {
                    Double dbl = Double.valueOf(value);
                    if (dbl == Math.floor(dbl)) {
                        value = new Long((long) Math.floor(dbl)).toString();
                    }
                } catch (NumberFormatException nfe) {
                    throw new TdarRecoverableRuntimeException("reflectionService.expecting_big_integer",
                            errorValueList);
                }
            }
            if (Float.class.isAssignableFrom(propertyType)) {
                try {
                    Float.parseFloat(value);
                } catch (NumberFormatException nfe) {
                    throw new TdarRecoverableRuntimeException("reflectionService.expecting_floating_point",
                            errorValueList);
                }
            }
            BeanUtils.setProperty(beanToProcess, name, value);
        }
    } catch (Exception e1) {
        if (e1 instanceof TdarRecoverableRuntimeException) {
            throw (TdarRecoverableRuntimeException) e1;
        }
        logger.debug("error processing bulk upload: {}", e1);
        throw new TdarRecoverableRuntimeException("reflectionService.expecting_generic", errorValueList);
    }
}

From source file:org.tohu.domain.DomainModelSupportTest.java

private void setTextProperty(String property, String value) {
    try {//  w ww  .j  a va 2 s  . co  m
        logger.debug("Setting " + property);
        Class<?> propertyClass = PropertyUtils.getPropertyType(data, property);
        Object v = DomainModelSupport.answerToObject(Question.TYPE_TEXT, value, propertyClass);
        PropertyUtils.setProperty(data, property, v);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.tohu.domain.DomainModelSupportTest.java

private void setNumberProperty(String property, Long value) {
    try {/*from   w  ww  .  j  a va2 s.com*/
        logger.debug("Setting " + property);
        Class<?> propertyClass = PropertyUtils.getPropertyType(data, property);
        Object v = DomainModelSupport.answerToObject(Question.TYPE_NUMBER, value, propertyClass);
        PropertyUtils.setProperty(data, property, v);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.tohu.domain.DomainModelSupportTest.java

private void setDecimalProperty(String property, BigDecimal value) {
    try {/*  w  w w . jav  a 2 s. c  o  m*/
        logger.debug("Setting " + property);
        Class<?> propertyClass = PropertyUtils.getPropertyType(data, property);
        Object v = DomainModelSupport.answerToObject(Question.TYPE_DECIMAL, value, propertyClass);
        PropertyUtils.setProperty(data, property, v);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.tohu.domain.DomainModelSupportTest.java

private void setBooleanProperty(String property, Boolean value) {
    try {/* w  w  w .j a va  2  s . c o  m*/
        logger.debug("Setting " + property);
        Class<?> propertyClass = PropertyUtils.getPropertyType(data, property);
        Object v = DomainModelSupport.answerToObject(Question.TYPE_BOOLEAN, value, propertyClass);
        PropertyUtils.setProperty(data, property, v);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.tohu.domain.DomainModelSupportTest.java

private void setDateProperty(String property, Date value) {
    try {/* www.  j  av a2s .co  m*/
        logger.debug("Setting " + property);
        Class<?> propertyClass = PropertyUtils.getPropertyType(data, property);
        Object v = DomainModelSupport.answerToObject(Question.TYPE_DATE, value, propertyClass);
        PropertyUtils.setProperty(data, property, v);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}