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

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

Introduction

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

Prototype

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

Source Link

Document

Return the value of the specified property of the specified bean, no matter which property reference format is used, with no type conversions.

For more details see PropertyUtilsBean.

Usage

From source file:fm.pattern.minimal.Reflection.java

/**
 * Returns the specified <em>property</em> value on the given <em>instance</em>.
 * //from ww w .  jav a 2  s. c o m
 * @param instance The object instance.
 * @param property The property to get.
 * 
 * @return An Object representing the property value, or null if no property value exists.
 */
public static Object get(Object instance, String property) {
    try {
        return PropertyUtils.getProperty(instance, property);
    } catch (Exception e) {
        return null;
    }
}

From source file:com.nec.nsgui.action.cifs.CommonUtil.java

/**
* set the corresponding message for the specified Property of all the object in List
* @param objList - the list of object/* w w  w.jav  a 2  s .c  o m*/
* @param protertyName - the target Property
* @param value_msgKey - the value and the corresponding message's key in the Resource file
* @param msgResources - MessageResources
* @param request - http servlet request
*/
static public void setMsgInObj(List objList, String protertyName, HashMap value_msgKey,
        MessageResources msgResources, HttpServletRequest request) throws Exception {
    int objNumbers = objList.size();
    Object object;
    Object objValue;
    for (int i = 0; i < objNumbers; i++) {
        object = objList.get(i);
        try {
            objValue = PropertyUtils.getProperty(object, protertyName);
        } catch (Exception e) {
            throw e;
        }
        if (value_msgKey.containsKey(objValue)) {
            //need change the value to the corresponding message
            PropertyUtils.setProperty(object, protertyName, msgResources.getMessage(request.getLocale(),
                    (String) value_msgKey.get(objValue.toString())));
        }
    }
}

From source file:com.aosa.main.utils.tools.AOSACopyUtil.java

/**
 * Description<code>Copy properties of orig to dest Exception the Entity and Collection Type</code>      <br>
 * By mutou at 2011-8-30 ?11:35:29   <br>
 * Object      <br>//  w  ww .j  a  v a  2 s. c  om
 * @param dest
 * @param orig
 * @return the dest bean
 * @throws
 */
@SuppressWarnings("rawtypes")
public static Object copyProperties(Object dest, Object orig) {
    if (dest == null || orig == null) {
        return dest;
    }
    PropertyDescriptor[] destDesc = PropertyUtils.getPropertyDescriptors(dest);
    try {
        for (int i = 0; i < destDesc.length; i++) {
            Class destType = destDesc[i].getPropertyType();
            Class origType = PropertyUtils.getPropertyType(orig, destDesc[i].getName());
            if (destType != null && destType.equals(origType) && !destType.equals(Class.class)) {
                if (!Collection.class.isAssignableFrom(origType)) {
                    try {
                        Object value = PropertyUtils.getProperty(orig, destDesc[i].getName());
                        PropertyUtils.setProperty(dest, destDesc[i].getName(), value);
                    } catch (Exception ex) {
                    }
                }
            }
        }
        return dest;
    } catch (Exception ex) {
        throw new AOSARuntimeException(ex);
    }
}

From source file:com.ponysdk.core.export.util.PropertyUtil.java

public static String getProperty(final Object bean, final String property)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    Object propertyValue = NA;//from w  ww. ja va2s .  com
    if (property != null) {
        final String[] tokens = property.split("\\.");
        propertyValue = getPropertyValue(bean, tokens[0]);
        for (int i = 1; i < tokens.length; i++) {
            if (tokens[i].equals("toString")) {
                propertyValue = propertyValue.toString();
            } else if (PropertyUtils.isReadable(propertyValue, tokens[i])) {
                propertyValue = PropertyUtils.getProperty(propertyValue, tokens[i]);
            } else {
                if (propertyValue instanceof List<?>) {
                    final List<?> propertyList = (List<?>) propertyValue;
                    final List<Object> values = new ArrayList<>();
                    for (final Object object : propertyList) {
                        values.add(getPropertyValue(object, tokens[i]));
                    }
                    if (values.isEmpty())
                        propertyValue = NA;
                    else
                        propertyValue = values;
                } else if (propertyValue instanceof Map<?, ?>) {
                    final Map<?, ?> propertyMap = (Map<?, ?>) propertyValue;
                    final List<Object> values = new ArrayList<>();
                    for (final Object object : propertyMap.values()) {
                        values.add(getPropertyValue(object, tokens[i]));
                    }
                    propertyValue = values;
                } else {
                    propertyValue = NA;
                }
            }
        }
    }
    return String.valueOf(propertyValue == null ? NA : propertyValue);
}

From source file:me.yyam.beanutils.BeanUtilEx.java

/**
 * IntrospectorPropertyDescriptor Bean --> Map
 * @param obj//from  w w  w .j a  v a2s .co  m
 * @return
 */
public static Map transBean2Map(Object obj) throws Exception {
    if (obj == null) {
        return null;
    }
    Map<String, Object> map = new HashMap<>();
    PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(obj);
    for (PropertyDescriptor property : propertyDescriptors) {
        String key = property.getName();
        if (!key.equals("class")) {
            try {
                Object value = PropertyUtils.getProperty(obj, key);
                if (value == null) {
                    map.put(key, value);
                } else {
                    if (value instanceof List) {
                        List list = new ArrayList();
                        for (Object v : (List) value) {
                            list.add(transBean2Map(v));
                        }
                        map.put(key, list);
                    } else {
                        if (value instanceof Enum) {
                            value = value.toString();
                        }
                        if (isPrimitive(value)) {
                            map.put(key, value);
                        } else {
                            Map cmap = transBean2Map(value);
                            map.put(key, cmap);
                        }
                    }
                }
            } catch (NoSuchMethodException e) {
                System.out.println(e.toString());
            }
        }
    }
    return map;
}

From source file:com.gs.obevo.db.impl.platforms.sybaseiq.iqload.BeanDataExtractor.java

@Override
public Object extractValue(Object obj, String fieldName) {
    try {//from w  w w  .jav a2  s  .  c om
        return PropertyUtils.getProperty(obj, fieldName);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:es.pode.adminusuarios.negocio.servicios.AltaGrupoException.java

/**
 * Finds the root cause of the parent exception
 * by traveling up the exception tree/*from  w  w w .j ava  2  s .c o m*/
 */
private static Throwable findRootCause(Throwable th) {
    if (th != null) {
        // Lets reflectively get any JMX or EJB exception causes.
        try {
            Throwable targetException = null;
            // java.lang.reflect.InvocationTargetException
            // or javax.management.ReflectionException
            String exceptionProperty = "targetException";
            if (PropertyUtils.isReadable(th, exceptionProperty)) {
                targetException = (Throwable) PropertyUtils.getProperty(th, exceptionProperty);
            } else {
                exceptionProperty = "causedByException";
                //javax.ejb.EJBException
                if (PropertyUtils.isReadable(th, exceptionProperty)) {
                    targetException = (Throwable) PropertyUtils.getProperty(th, exceptionProperty);
                }
            }
            if (targetException != null) {
                th = targetException;
            }
        } catch (Exception ex) {
            // just print the exception and continue
            ex.printStackTrace();
        }

        if (th.getCause() != null) {
            th = th.getCause();
            th = findRootCause(th);
        }
    }
    return th;
}

From source file:fr.mael.microrss.util.js.MicroRSSGlobalFunctions.java

private static boolean matches(Scriptable thisObj, Object[] args, String name, boolean contains) {
    Article article = getArticle(thisObj);
    if (article == null || args == null || args.length < 1) {
        return false;
    }/*from   w  w  w .  ja va2  s .com*/
    Object val;
    try {
        val = PropertyUtils.getProperty(article, name);
        if (val == null) {
            return false;
        }
        String comparison = args[0].toString();
        if (contains) {
            comparison = ".*" + comparison + ".*";
        }
        return val.toString().matches(comparison);
    } catch (Exception e) {
        LOG.error("Error getting property " + name, e);
        return false;
    }

}

From source file:com.nh.core.utils.reflaection.CollectionMapper.java

/**
 * ????(Getter), ??List./*w ww.j ava  2 s .c  o m*/
 * 
 * @param collection ???.
 * @param propertyName ??????.
 */
public static List extractToList(final Collection collection, final String propertyName) {
    List<Object> list = new ArrayList<Object>();

    try {
        for (Object obj : collection) {
            list.add(PropertyUtils.getProperty(obj, propertyName));
        }
    } catch (Exception e) {
        throw ReflectionUtils.convertReflectionExceptionToUnchecked(e);
    }

    return list;
}

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

/**
 * ????(Getter), ??List.//from   w w  w. j  a  v  a 2s.c  o m
 * 
 * @param collection ???.
 * @param propertyName ??????.
 */
public static List extractToList(final Collection collection, final String propertyName) {
    List list = new ArrayList();

    try {
        for (Object obj : collection) {
            list.add(PropertyUtils.getProperty(obj, propertyName));
        }
    } catch (Exception e) {
        throw ReflectionUtils.convertReflectionExceptionToUnchecked(e);
    }

    return list;
}