Example usage for org.apache.commons.beanutils WrapDynaBean get

List of usage examples for org.apache.commons.beanutils WrapDynaBean get

Introduction

In this page you can find the example usage for org.apache.commons.beanutils WrapDynaBean get.

Prototype

public Object get(String name) 

Source Link

Document

Return the value of a simple property with the specified name.

Usage

From source file:de.hotware.hibernate.query.intelligent.structure.Util.java

public static Object getProperty(CachedInfo cachedInfo, Object bean, String property) {
    Object curObject = bean;/*from   ww w.j ava2 s . com*/
    if (property != null && !property.equals("")) {
        for (String split : SPLIT_PATTERN.split(property)) {
            WrapDynaClass clazz = cachedInfo.getCachedClass(curObject.getClass());
            WrapDynaBean dynaBean = new WrapDynaBean(curObject, clazz);
            curObject = dynaBean.get(split);
        }
    }
    return curObject;
}

From source file:com.expressui.core.util.ReflectionUtil.java

/**
 * Asks if the bean's properties are empty. boolean properties that are false and numbers
 * that are zero are considered empty. String values that are zero-length are considered empty.
 * All other property types must be null to be considered empty.
 *
 * @param bean bean to check//  w w  w.j a va 2 s.  c om
 * @return true if bean has no values
 */
public static boolean isBeanEmpty(Object bean) {
    if (bean == null) {
        return true;
    }

    WrapDynaBean wrapDynaBean = new WrapDynaBean(bean);
    DynaProperty[] properties = wrapDynaBean.getDynaClass().getDynaProperties();
    for (DynaProperty property : properties) {
        String propertyName = property.getName();
        Class propertyType = property.getType();

        Object value = wrapDynaBean.get(propertyName);
        if (propertyType.isPrimitive()) {
            if (value instanceof Number && !value.toString().equals("0")) {
                return false;
            } else if (value instanceof Boolean && !value.toString().equals("false")) {
                return false;
            } else if (!value.toString().isEmpty()) {
                return false;
            }
        } else if (value != null) {
            if (!(value instanceof Collection)) {
                String convertedStringValue = ConvertUtils.convert(value);
                if (!StringUtil.isEmpty(convertedStringValue)) {
                    return false;
                }
            }
        }
    }

    return true;
}

From source file:org.faster.util.Beans.java

/**
 * ??/*from www. j a v  a  2  s.  c  o m*/
 *
 * @param dest 
 * @param orig 
 * @param ignoreNullValue ?
 * @param propertyNames ??
 */
public static void slicePopulate(Object dest, Object orig, boolean ignoreNullValue, String... propertyNames) {
    WrapDynaBean destBean = new WrapDynaBean(dest);
    WrapDynaBean origBean = new WrapDynaBean(orig);
    for (String propertyName : propertyNames) {
        Object value = origBean.get(propertyName);
        if (ignoreNullValue && isNullOrEmpty(value)) {
            continue;
        }

        destBean.set(propertyName, value);
    }
}

From source file:org.faster.util.Beans.java

@SuppressWarnings("unchecked")
public static <T> T slice(T orig, boolean ignoreNullValue, String... propertyNames) {
    if (propertyNames == null || propertyNames.length == 0) {
        return orig;
    }/*from  w ww  . j ava2 s.c  o m*/

    T dest = (T) Beans.newInstance(orig.getClass());
    WrapDynaBean destBean = new WrapDynaBean(dest);
    WrapDynaBean origBean = new WrapDynaBean(orig);
    for (String propertyName : propertyNames) {
        Object value = origBean.get(propertyName);
        if (ignoreNullValue && Beans.isNullOrEmpty(value)) {
            continue;
        }

        destBean.set(propertyName, value);
    }
    return dest;
}

From source file:org.gbif.registry.ws.guice.StringTrimInterceptor.java

private void trimStringsOf(Object target, int level) {
    if (target != null && level <= MAX_RECURSION) {
        LOG.debug("Trimming class: {}", target.getClass());

        WrapDynaBean wrapped = new WrapDynaBean(target);
        DynaClass dynaClass = wrapped.getDynaClass();
        for (DynaProperty dynaProp : dynaClass.getDynaProperties()) {
            if (String.class.isAssignableFrom(dynaProp.getType())) {
                String prop = dynaProp.getName();
                String orig = (String) wrapped.get(prop);
                if (orig != null) {
                    String trimmed = Strings.emptyToNull(orig.trim());
                    if (!Objects.equal(orig, trimmed)) {
                        LOG.debug("Overriding value of [{}] from [{}] to [{}]", prop, orig, trimmed);
                        wrapped.set(prop, trimmed);
                    }/*from  w w w  .ja  va  2s .  c o m*/
                }
            } else {
                try {
                    // trim everything in the registry model package (assume that Dataset resides in the correct package here)
                    Object property = wrapped.get(dynaProp.getName());
                    if (property != null && Dataset.class.getPackage() == property.getClass().getPackage()) {
                        trimStringsOf(property, level + 1);
                    }

                } catch (IllegalArgumentException e) {
                    // expected for non accessible properties
                }
            }
        }
    }
}

From source file:org.gbif.registry2.ws.guice.StringTrimInterceptor.java

private void trimStringsOf(Object target) {
    WrapDynaBean wrapped = new WrapDynaBean(target);
    DynaClass dynaClass = wrapped.getDynaClass();
    for (DynaProperty dynaProp : dynaClass.getDynaProperties()) {
        // Only operate on strings
        if (String.class.isAssignableFrom(dynaProp.getType())) {
            String prop = dynaProp.getName();
            String orig = (String) wrapped.get(prop);
            if (orig != null) {
                String trimmed = Strings.emptyToNull(orig.trim());
                if (!Objects.equal(orig, trimmed)) {
                    LOG.debug("Overriding value of [{}] from [{}] to [{}]", prop, orig, trimmed);
                    wrapped.set(prop, trimmed);
                }/*w  w w.  j  a  va  2 s.co m*/
            }
        }
    }
}

From source file:org.latticesoft.util.common.BeanUtil.java

public static Object getAttribute(Object bean, String attribute) {
    Object retVal = null;//from ww w  .  j  a  v  a  2  s.  c  o  m
    if (bean == null || attribute == null)
        return null;
    if (bean instanceof Map) {
        Map map = (Map) bean;
        retVal = map.get(attribute);
    } else if (bean instanceof WrapDynaBean) {
        WrapDynaBean w = (WrapDynaBean) bean;
        retVal = w.get(attribute);
    } else {
        WrapDynaBean w = new WrapDynaBean(bean);
        retVal = w.get(attribute);
    }
    return retVal;
}

From source file:org.latticesoft.util.common.MiscUtil.java

/**
 * Evaluate others element//w w w  .  java  2 s  .  c  o m
 */
private static Object evaluateOthersElement(Map map, Object currentObject, String elementName,
        boolean isObjectFirst) {

    if (log.isDebugEnabled()) {
        log.debug("<<isOthers>>");
    }

    // from map directly or attribute
    // element is an attribute
    // previousElement.currentAttribute
    if (currentObject == null) {
        // 1st element have to retrieve from map
        currentObject = map.get(elementName);
    } else {
        // not the 1st element
        if (currentObject instanceof Map) {
            Map m = (Map) currentObject;
            currentObject = m.get(elementName);
        } else {
            try {
                WrapDynaBean bean = new WrapDynaBean(currentObject);
                currentObject = bean.get(elementName);
            } catch (Exception e) {
                // if it is not an attribute an occur will occur
                // note that the attribute must have a getter method
                if (log.isDebugEnabled()) {
                    log.debug(e);
                }
            }
        } // if
    }
    return currentObject;
}

From source file:org.latticesoft.util.common.StringUtil.java

/**
 * Print the properties of an object into a xml string.
 * This is useful in the object's toString method.
 * @param o the object to be converted// w w w .jav a2  s .  c  o  m
 * @param mode one of the above mode
 * @param displayAll display all attributes including those which are null.
 * @return the string-fied xml form of the object
 */
public static String formatObjectToXmlString(Object o, int mode, boolean displayAll) {
    if (o == null)
        return "<NullClass/>";

    StringBuffer sb = new StringBuffer();
    String s = o.getClass().getName();
    String p = o.getClass().getPackage().getName();
    String className = s.substring(p.length() + 1, s.length());

    if (mode == StringUtil.MODE_END_TAG) {
        sb.append("</");
        sb.append(className);
        sb.append(">");
        return sb.toString();
    }

    sb.append("<");
    sb.append(className);

    // list of attributes
    Field f[] = o.getClass().getDeclaredFields();
    WrapDynaBean dyn = null;
    try {
        dyn = new WrapDynaBean(o);
    } catch (Exception e) {
    }

    for (int i = 0; i < f.length; i++) {
        String name = f[i].getName();
        int modifier = f[i].getModifiers();
        if (Modifier.isFinal(modifier) || Modifier.isAbstract(modifier) || Modifier.isInterface(modifier)
                || Modifier.isStatic(modifier)) {
            continue;
        }

        Object value = null;
        try {
            value = dyn.get(name);
        } catch (Exception e) {
            //if (log.isErrorEnabled()) { log.error(e); }
        }
        if (name != null) {
            if ((value != null && !displayAll) || (displayAll)) {
                sb.append(" ");
                sb.append(name);
                sb.append("=\"");
                sb.append(value);
                sb.append("\"");
            }
        }
    }
    switch (mode) {
    default:
    case StringUtil.MODE_FULL_STANDARD:
        sb.append("/>");
        break;
    case StringUtil.MODE_FULL_LONG:
        sb.append("></");
        sb.append(className);
        sb.append(">");
        break;
    case StringUtil.MODE_START_TAG:
        sb.append(">");
        break;
    }
    return sb.toString();
}

From source file:org.latticesoft.util.common.StringUtil.java

public static String formatObjectToString(Object o, boolean includeChild) {
    if (o == null)
        return "";
    if (o == null)
        return "";

    StringBuffer sb = new StringBuffer();
    String className = o.getClass().getName();

    sb.append("[");
    sb.append(className);/*from  ww w.j  ava 2s. c  o  m*/
    sb.append("|");

    // list of attributes
    Field f[] = o.getClass().getDeclaredFields();
    WrapDynaBean dyn = null;
    try {
        dyn = new WrapDynaBean(o);
    } catch (Exception e) {
    }

    for (int i = 0; i < f.length; i++) {
        String name = f[i].getName();
        int modifier = f[i].getModifiers();
        if (Modifier.isFinal(modifier) || Modifier.isAbstract(modifier) || Modifier.isInterface(modifier)
                || Modifier.isStatic(modifier)) {
            continue;
        }
        Object value = null;
        try {
            value = dyn.get(name);
        } catch (Exception e) {
            //if (log.isErrorEnabled()) { log.error(e); }
        }
        if (name != null && value != null) {
            sb.append(name);
            sb.append("=");
            if (value instanceof Map) {
                Map map = (Map) value;
                if (includeChild) {
                    sb.append(value);
                } else {
                    sb.append(map.size());
                }
                sb.append("|");
            } else if (value instanceof Collection) {
                Collection c = (Collection) value;
                if (includeChild) {
                    sb.append(value);
                } else {
                    sb.append(c.size());
                }
                sb.append("|");
            } else {
                sb.append(value);
                sb.append("|");
            }
        }
    }
    sb.deleteCharAt(sb.length() - 1);
    sb.append("]");
    return sb.toString();
}