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

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

Introduction

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

Prototype

public static Map describe(Object bean)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException 

Source Link

Document

Return the entire set of properties for which the specified bean provides a read method.

For more details see PropertyUtilsBean.

Usage

From source file:org.squale.welcom.outils.jdbc.wrapper.ResultSetUtils.java

/**
 * Effectue un populate sans tenir compte de la case
 * //from   w ww.j  av a 2  s  .  co  m
 * @param bean : Bean A populer
 * @param properties : Liste des propertie a modifier
 * @throws IllegalAccessException : Probleme sur l'accs a l'attribut pas le getter
 * @throws InvocationTargetException : Probleme sur le populate
 */
public static void populateIgnoreCase(final Object bean, final Map properties)
        throws IllegalAccessException, InvocationTargetException {
    try {
        final Hashtable realName = new Hashtable();

        // Recuperation de la table des correspondances
        final Map map = PropertyUtils.describe(bean);
        Iterator it = map.keySet().iterator();

        while (it.hasNext()) {
            final String element = (String) it.next();
            realName.put(element.toUpperCase(), element);
        }

        // Reaffecte les bons Noms
        final HashMap propertiesRealName = new HashMap();
        it = properties.keySet().iterator();

        while (it.hasNext()) {
            final String element = (String) it.next();

            if (realName.containsKey(element.toUpperCase())) {
                propertiesRealName.put(realName.get(element.toUpperCase()), properties.get(element));
            }
        }

        BeanUtils.populate(bean, propertiesRealName);
    } catch (final IllegalAccessException e) {
        log.error(e, e);
        BeanUtils.populate(bean, properties);
    } catch (final InvocationTargetException e) {
        log.error(e, e);
        BeanUtils.populate(bean, properties);
    } catch (final NoSuchMethodException e) {
        log.error(e, e);
        BeanUtils.populate(bean, properties);
    }
}

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

/**
 * Genere les colonnes//from  w w  w. jav a2 s . com
 * 
 * @param c class
 */
public void genAllCols(final Class c) {
    try {
        final Map map = PropertyUtils.describe(c);
        final Field fields[] = (Field[]) map.get("declaredFields");

        for (int i = 0; i < fields.length; i++) {
            final Col col = new Col();
            col.setKey(fields[i].getName());
            col.setProperty(fields[i].getName());
            addCellAtCurrentLine(col);
        }

        if (cols == null) {
            cols = new Vector();
        }

        Collections.sort(cols);
    } catch (final Exception e) {
        log.error("Impossible de recupere la liste des elements par defaut", e);
    }
}

From source file:org.xmatthew.spy2servers.jmx.AbstractComponentViewMBean.java

@SuppressWarnings("unchecked")
public String getComponentProperties() {
    if (getComponent() != null) {
        try {/*from w  ww .  j  av  a 2 s  .  co m*/
            Component component = getComponent();
            Map properties = PropertyUtils.describe(component);
            if (properties == null) {
                return null;
            }
            Iterator iter = properties.entrySet().iterator();
            List<String> keyValues = new ArrayList<String>(properties.size() - 1);
            Object key;
            String keyValue;
            while (iter.hasNext()) {
                Map.Entry entry = (Map.Entry) iter.next();
                key = entry.getKey();
                if (((String) key).equals("class")) {
                    continue;
                }

                keyValue = key.toString() + "=" + entry.getValue();
                keyValues.add(keyValue);
            }
            return keyValues.toString();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    return null;
}

From source file:unit.web.util.TestUtil.java

public static void setRandomValues(Object inDataObject, WebForm inForm, boolean setOtherValues,
        List inParamsToIgnore) throws Exception {

    System.out.println("\n<TestUtil> Entered setRandomValues");

    Map theBeanProps = PropertyUtils.describe(inDataObject);
    Iterator theProperties = theBeanProps.entrySet().iterator();

    // loop thru bean properties
    while (theProperties.hasNext()) {

        Map.Entry theEntry = (Map.Entry) theProperties.next();

        if (theEntry.getKey() != null) {

            String thePropertyName = theEntry.getKey().toString();

            PropertyDescriptor thePropertyDescriptor = PropertyUtils.getPropertyDescriptor(inDataObject,
                    thePropertyName);/*w w  w .j  av a 2s  .c  om*/

            Object thePropertyValue = theEntry.getValue();

            // Only override non-set values  (theForm.set...)
            if (thePropertyValue == null && !inParamsToIgnore.contains(thePropertyName)) {

                // check if property is a string
                if (thePropertyDescriptor.getPropertyType().getName().equals("java.lang.String")) {

                    String[] theOptions = inForm.getOptionValues(thePropertyName);

                    if (theOptions.length > 0) {
                        System.out.println("This property is a collection: " + thePropertyName);
                        if (Arrays.asList(theOptions).contains(Constants.Dropdowns.OTHER_OPTION)
                                && setOtherValues == true) {
                            BeanUtils.setProperty(inDataObject, thePropertyName,
                                    Constants.Dropdowns.OTHER_OPTION);
                            System.out.println("Collection PropertyName: " + thePropertyName + "PropertyValue:"
                                    + Constants.Dropdowns.OTHER_OPTION);
                        } else {
                            String newOption = theOptions[theOptions.length - 1];
                            BeanUtils.setProperty(inDataObject, thePropertyName, newOption);
                            System.out.println("Collection PropertyName: " + thePropertyName + "PropertyValue:"
                                    + newOption);
                        }
                    } else {

                        // If we're not setting the other option, skip these
                        if (thePropertyName.indexOf("other") == -1 || setOtherValues == true) {
                            String newRandomValue = GUIDGenerator.getInstance().genNewGuid();
                            BeanUtils.setProperty(inDataObject, thePropertyName, newRandomValue);
                            System.out.println("The PropertyName in other loop: " + thePropertyName
                                    + "\t The PropertyValue: " + newRandomValue);
                        }
                    }
                } else if (thePropertyDescriptor.getPropertyType().getName().equals("java.lang.Long")) {
                    BeanUtils.setProperty(inDataObject, thePropertyName,
                            new Long(ourRandomIntGenerator.draw()));
                } else if (thePropertyDescriptor.getPropertyType().getName().equals("java.lang.Double")) {
                    BeanUtils.setProperty(inDataObject, thePropertyName,
                            new Double(ourRandomIntGenerator.draw()));
                } else {
                    // Ignore for now
                }
            }
        }
    }
    System.out.println("<TestUtil> Exiting setRandomValues\n");
}

From source file:unit.web.util.TestUtil.java

public static void setValuesOnForm(Object inDataObject, WebForm inForm) throws Exception {

    System.out.println("<TestUtil> Entered setValuesOnForm");

    Map theBeanProps = PropertyUtils.describe(inDataObject);
    Iterator theProperties = theBeanProps.entrySet().iterator();

    // loop thru bean properties
    while (theProperties.hasNext()) {

        Map.Entry theEntry = (Map.Entry) theProperties.next();

        if (theEntry.getKey() != null) {
            String thePropertyName = theEntry.getKey().toString();
            Object thePropertyValue = theEntry.getValue();

            if (thePropertyValue != null && inForm.hasParameterNamed(thePropertyName)) {
                System.out.println("Setting value: " + thePropertyName + " to: " + thePropertyValue.toString());
                inForm.getScriptableObject().setParameterValue(thePropertyName, thePropertyValue.toString());
                //WebForm savedForm = new WebForm;
                savePropertyNameValue(thePropertyName, thePropertyValue.toString());
            }/*  ww w  . j a  v  a 2  s .  c  o  m*/
        }
    }
    //Save ParameterName and ParameterValue to copare during populate testing

    System.out.println("<TestUtil> Exited setValuesOnForm");
}