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.beangle.model.persist.hibernate.CriterionUtils.java

private static Map<String, Object> converToMap(String prefix, Component component, MatchMode mode) {
    if (null == component) {
        return Collections.EMPTY_MAP;
    }//w ww.ja v  a2s.c om
    Map<String, Object> datas = CollectUtils.newHashMap();
    String attr = "";
    try {
        Map<String, Object> beanMap = PropertyUtils.describe(component);
        for (Iterator<String> iter = beanMap.keySet().iterator(); iter.hasNext();) {
            attr = iter.next();
            Object value = PropertyUtils.getProperty(component, attr);
            if (value == null) {
                continue;
            } else {
                addTrivialAttr(datas, prefix + "." + attr, value, mode);
            }
        }
        return datas;

    } catch (Exception e) {
        logger.error(
                "[converToMap]:error occur in converToMap of component" + component + "with attr named " + attr,
                e);

    }
    return Collections.EMPTY_MAP;
}

From source file:org.beangle.model.query.builder.ConditionUtils.java

/**
 * ????<br>/*from   www.j  a va  2  s  . c  o m*/
 * ????"?"(?component)<br>
 * :null,Collection
 * 
 * @param alias
 * @param entity
 * @param mode
 * @return
 */
public static List<Condition> extractConditions(final String alias, final Entity<?> entity) {
    if (null == entity) {
        return Collections.emptyList();
    }
    final List<Condition> conditions = new ArrayList<Condition>();

    StringBuilder aliasBuilder = new StringBuilder(alias == null ? "" : alias);
    if (aliasBuilder.length() > 0 && !alias.endsWith(".")) {
        aliasBuilder.append(".");
    }
    String attr = "";
    try {
        @SuppressWarnings("unchecked")
        final Set<String> props = PropertyUtils.describe(entity).keySet();
        for (final Iterator<String> iter = props.iterator(); iter.hasNext();) {
            attr = iter.next();
            // ???
            if (!PropertyUtils.isWriteable(entity, attr)) {
                continue;
            }
            final Object value = PropertyUtils.getProperty(entity, attr);
            if (null == value) {
                continue;
            }
            if (!(value instanceof Collection<?>)) {
                addAttrCondition(conditions, alias + attr, value);
            }
        }
    } catch (Exception e) {
        logger.debug("error occur in extractConditions for  bean {} with attr named {}", entity, attr);
    }
    return conditions;
}

From source file:org.beangle.model.query.builder.ConditionUtils.java

private static List<Condition> extractComponent(final String prefix, final Component component) {
    if (null == component) {
        return Collections.emptyList();
    }//  ww  w  .jav a 2s.  c o  m
    final List<Condition> conditions = CollectUtils.newArrayList();
    String attr = "";
    try {
        @SuppressWarnings("unchecked")
        final Set<String> props = PropertyUtils.describe(component).keySet();
        for (final Iterator<String> iter = props.iterator(); iter.hasNext();) {
            attr = iter.next();
            if ("class".equals(attr)) {
                continue;
            }
            if (!PropertyUtils.isWriteable(component, attr)) {
                continue;
            }
            final Object value = PropertyUtils.getProperty(component, attr);
            if (value == null) {
                continue;
            } else if (value instanceof Collection<?>) {
                if (((Collection<?>) value).isEmpty()) {
                    continue;
                }
            } else {
                addAttrCondition(conditions, prefix + "." + attr, value);
            }
        }

    } catch (Exception e) {
        logger.warn("error occur in extractComponent of component:" + component + "with attr named :" + attr);
    }
    return conditions;
}

From source file:org.cloudifysource.esc.driver.provisioning.jclouds.ProvisioningUtils.java

@SuppressWarnings("unchecked")
private static void populateMapFromBean(Map<String, String> map, Object bean, String keyPrefix)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {

    if (bean == null) {
        return;//from   w  ww .  j ava2s .c  om
    }
    if (List.class.isAssignableFrom(bean.getClass())) {
        List<Object> list = (List<Object>) bean;
        int index = 0;
        for (Object object : list) {
            populateMapFromBean(map, object, keyPrefix + "." + index);
            ++index;
        }
    } else if (Map.class.isAssignableFrom(bean.getClass())) {
        Map<String, Object> propertyMap = (Map<String, Object>) bean;
        Set<Entry<String, Object>> entries = propertyMap.entrySet();
        for (Entry<String, Object> propertyEntry : entries) {
            populateMapFromBean(map, propertyEntry.getValue(), keyPrefix + "." + propertyEntry.getKey());
        }
    } else {
        if (org.springframework.beans.BeanUtils.isSimpleProperty(bean.getClass())) {
            if (!Class.class.isAssignableFrom(bean.getClass())) {
                map.put(keyPrefix, bean.toString());
            }
        } else {
            Map<String, Object> propertyMap = PropertyUtils.describe(bean);
            Set<Entry<String, Object>> entries = propertyMap.entrySet();
            for (Entry<String, Object> entry : entries) {
                if (keyPrefix.isEmpty()) {
                    populateMapFromBean(map, entry.getValue(), entry.getKey());
                } else {
                    populateMapFromBean(map, entry.getValue(), keyPrefix + "." + entry.getKey());
                }
            }

        }

    }

}

From source file:org.codehaus.plexus.redback.struts2.action.admin.SystemInfoAction.java

@SuppressWarnings("unchecked")
private void dumpObjectReaders(List<Object> seenObjects, StringBuilder sb, Object obj, String indent,
        int depth) {
    sb.append(obj.toString()).append(LN);
    String name = null;//from w  w  w  .j  av  a  2 s .  c o  m

    try {
        Map<String, Object> readers = PropertyUtils.describe(obj);
        for (Map.Entry<String, Object> readerEntry : readers.entrySet()) {
            name = (String) readerEntry.getKey();

            if (ignoredReaders.contains(name)) {
                // skip this reader.
                continue;
            }

            sb.append(indent);
            sb.append(name).append(':');

            Object value = readerEntry.getValue();
            if (value == null) {
                sb.append(NULL).append(LN);
            } else {
                dumpObjectSwitchboard(seenObjects, sb, value, INDENT + indent, depth);
            }
        }
    } catch (Throwable e) {
        sb.append(LN).append(indent);
        sb.append("Unable to read bean [").append(obj.getClass().getName());
        if (StringUtils.isNotBlank(name)) {
            sb.append(".get").append(StringUtils.capitalize(name)).append("()");
        }
        sb.append("]: ").append('(').append(e.getClass().getName()).append(") ");
        sb.append(e.getMessage()).append(LN);
    }
}

From source file:org.codehaus.redback.integration.reports.CsvUserList.java

@SuppressWarnings("unchecked")
private void writeCsvRow(PrintWriter out, User user) throws ReportException {
    try {//from   w  ww.  j ava 2 s .  c o  m
        boolean hasPreviousField = false;
        Map<String, Object> propMap = PropertyUtils.describe(user);
        for (String propName : fields.keySet()) {
            Object propValue = propMap.get(propName);

            if (hasPreviousField) {
                out.print(",");
            }

            if (propValue != null) {
                out.print(escapeCell(propValue.toString()));
            }
            hasPreviousField = true;
        }
        out.println();
    } catch (IllegalAccessException e) {
        String emsg = "Unable to produce " + getName() + " report.";
        log.error(emsg, e);
        throw new ReportException(emsg, e);
    } catch (InvocationTargetException e) {
        String emsg = "Unable to produce " + getName() + " report.";
        log.error(emsg, e);
        throw new ReportException(emsg, e);
    } catch (NoSuchMethodException e) {
        String emsg = "Unable to produce " + getName() + " report.";
        log.error(emsg, e);
        throw new ReportException(emsg, e);
    }
}

From source file:org.ghost4j.AbstractComponent.java

@SuppressWarnings("unchecked")
public Map<String, Object> extractSettings()
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {

    Map<String, Object> result = PropertyUtils.describe(this);

    if (result.get("maxProcessCount") != null) {
        result.remove("maxProcessCount");
    }/*www  .j  a  v a2s  .co m*/

    return result;
}

From source file:org.ikasan.configurationService.service.ConfigurationFactoryDefaultImpl.java

/**
 * @param runtimeConfiguration/* w w  w  .  jav  a  2s.  c  om*/
 * @return
 */
public Configuration<List<ConfigurationParameter>> createConfiguration(String configurationResourceId,
        Object runtimeConfiguration) {
    if (runtimeConfiguration == null) {
        throw new ConfigurationException("Runtime configuration object cannot be 'null'");
    }

    Configuration<List<ConfigurationParameter>> configuration = new DefaultConfiguration(
            configurationResourceId, new ArrayList<ConfigurationParameter>());

    try {
        Map<String, Object> properties = PropertyUtils.describe(runtimeConfiguration);

        for (Map.Entry<String, Object> entry : properties.entrySet()) {
            String name = entry.getKey();
            Object value = entry.getValue();

            // TODO - is there a cleaner way of ignoring the class property ?
            if (!"class".equals(name)) {
                if (value == null) {
                    Class<?> cls = PropertyUtils.getPropertyType(runtimeConfiguration, name);

                    if (cls.isAssignableFrom(String.class)) {
                        if (isMasked(runtimeConfiguration, name)) {
                            configuration.getParameters()
                                    .add(new ConfigurationParameterMaskedStringImpl(name, null));
                        } else {
                            configuration.getParameters().add(new ConfigurationParameterStringImpl(name, null));
                        }
                    } else if (cls.isAssignableFrom(Long.class)) {
                        configuration.getParameters().add(new ConfigurationParameterLongImpl(name, null));
                    } else if (cls.isAssignableFrom(Integer.class)) {
                        configuration.getParameters().add(new ConfigurationParameterIntegerImpl(name, null));
                    } else if (cls.isAssignableFrom(Boolean.class)) {
                        configuration.getParameters().add(new ConfigurationParameterBooleanImpl(name, null));
                    } else if (cls.isAssignableFrom(List.class)) {
                        configuration.getParameters().add(new ConfigurationParameterListImpl(name, null));
                    } else if (cls.isAssignableFrom(Map.class)) {
                        configuration.getParameters().add(new ConfigurationParameterMapImpl(name, null));
                    } else {
                        logger.warn(
                                "Ignoring unsupported configurationParameter class [" + cls.getName() + "].");
                    }
                } else {
                    if (value instanceof String) {
                        if (isMasked(runtimeConfiguration, name)) {
                            configuration.getParameters()
                                    .add(new ConfigurationParameterMaskedStringImpl(name, (String) value));
                        } else {
                            configuration.getParameters()
                                    .add(new ConfigurationParameterStringImpl(name, (String) value));
                        }
                    } else if (value instanceof Long) {
                        configuration.getParameters()
                                .add(new ConfigurationParameterLongImpl(name, (Long) value));
                    } else if (value instanceof Integer) {
                        configuration.getParameters()
                                .add(new ConfigurationParameterIntegerImpl(name, (Integer) value));
                    } else if (value instanceof Boolean) {
                        configuration.getParameters()
                                .add(new ConfigurationParameterBooleanImpl(name, (Boolean) value));
                    } else if (value instanceof List) {
                        configuration.getParameters()
                                .add(new ConfigurationParameterListImpl(name, (List) value));
                    } else if (value instanceof Map) {
                        configuration.getParameters().add(new ConfigurationParameterMapImpl(name, (Map) value));
                    } else {
                        logger.warn("Ignoring unsupported configurationParameter class ["
                                + value.getClass().getName() + "].");
                    }
                }
            }
        }
    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
        throw new ConfigurationException(e);
    }

    return configuration;
}

From source file:org.kuali.kfs.coa.service.ObjectCodeServiceTest.java

License:asdf

public void testPropertyUtilsDescribe()
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    ObjectCode objectCode = new ObjectCode();
    Map boProps = PropertyUtils.describe(objectCode);
}

From source file:org.kuali.kfs.module.ar.document.CustomerMaintenableImpl.java

private boolean oldAndNewObjectIsEqual(String objectName, Object oldObject, Object newObject) {

    //  if both are null, then they're the same
    if (oldObject == null && newObject == null) {
        return true;
    }//from  w  ww.  j  av  a 2  s .  c o  m

    //  if only one is null, then they're different
    if (oldObject == null || newObject == null) {
        return false;
    }

    //  if they're different classes, then they're different
    if (!oldObject.getClass().getName().equals(newObject.getClass().getName())) {
        return false;
    }

    //  get the list of properties and unconverted values for readable props
    Map<String, Object> oldProps;
    Map<String, Object> newProps;
    try {
        oldProps = PropertyUtils.describe(oldObject);
        newProps = PropertyUtils.describe(newObject);
    } catch (Exception e) {
        throw new RuntimeException("Exception raised while trying to get a list of properties on OldCustomer.",
                e);
    }

    //  compare old to new on all readable properties
    Object oldValue, newValue;
    for (String propName : oldProps.keySet()) {
        oldValue = oldProps.get(propName);
        newValue = newProps.get(propName);

        if (!oldAndNewPropertyIsEqual(propName, oldValue, newValue)) {
            return false;
        }
    }

    //  if we didnt find any differences, then they are the same
    return true;
}