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

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

Introduction

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

Prototype

public 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.

Usage

From source file:com.trenako.results.RollingStockResults.java

protected Object safeGetProperty(Object bean, String name) {
    try {//from w w w  .  j  av a  2  s  .  co m
        PropertyUtilsBean pb = BeanUtilsBean.getInstance().getPropertyUtils();
        return pb.getProperty(bean, name);
    } catch (Exception e) {
        return null;
    }
}

From source file:com.pps.webos.util.AppInfoHelper.java

/**
 * Method should replace values in the appinfo.json with values specified by in the UI.
 * Reference materials:/*from  w  w  w. j a  v  a2 s . com*/
 *       org.eclipse.jface.text.FindReplaceDocumentAdapter to replace matches of file with the values of the domain table
 http://dev.eclipse.org/newslists/news.eclipse.platform/msg74246.html
 http://help.eclipse.org/help32/index.jsp?topic=/org.eclipse.platform.doc.isv/reference/api/org/eclipse/core/filebuffers/package-summary.html
 * @param projectName
 * @param appInfo
 */
public void replaceAppInfoAttributes(String projectName, AppInfo appInfo) {

    try {
        /* get workspace location, since the directory structure should only contain 1 appinfo.json
         * logic will just look for the the name of the file **/
        IWorkspace workspace = ResourcesPlugin.getWorkspace();
        IFile file = workspace.getRoot().getProject(projectName).getFile(AppInfoEnum.WEBOS_FILE_APPINFO);

        //%PROJECT_NAME%archive/webos/appinfo.json
        ITextFileBufferManager fileBufferManager = FileBuffers.getTextFileBufferManager();
        IPath location = file.getLocation();
        fileBufferManager.connect(location, LocationKind.NORMALIZE, new NullProgressMonitor());

        ITextFileBuffer textFileBuffer = fileBufferManager.getTextFileBuffer(location, LocationKind.NORMALIZE);
        IDocument document = textFileBuffer.getDocument();

        FindReplaceDocumentAdapter findReplaceDocumentAdapter = new FindReplaceDocumentAdapter(document);

        // for each element in the enumeration
        // call method name from appInfo with MethodUtils
        PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
        Object tObj = null;
        for (AppInfoEnum appInfoEnum : AppInfoEnum.values()) {
            tObj = propertyUtilsBean.getProperty(appInfo, appInfoEnum.getFieldName());

            IRegion regionFind = findReplaceDocumentAdapter.find(0, appInfoEnum.getReplaceVal(), true, false,
                    true, false);
            IRegion regionReplace = findReplaceDocumentAdapter.replace(String.valueOf(tObj), false);
        }

        // commit replaced changes
        textFileBuffer.commit(new NullProgressMonitor(), true);

        // clean up  
        fileBufferManager.disconnect(location, LocationKind.NORMALIZE, new NullProgressMonitor());

    } catch (NullPointerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (CoreException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (BadLocationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:com.linkedin.databus.core.util.ConfigLoader.java

private void fillBeanFromMap(Object bean, Map<?, ?> map)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    PropertyUtilsBean propUtils = _beanUtilsBean.getPropertyUtils();

    for (Map.Entry<?, ?> entry : map.entrySet()) {
        String keyStr = entry.getKey().toString();
        Object value = entry.getValue();
        if (value instanceof Map<?, ?>) {
            Object subBean = propUtils.getProperty(bean, keyStr);
            if (null != subBean)
                fillBeanFromMap(subBean, (Map<?, ?>) value);
        } else if (value instanceof List<?>) {
            fillPropertyFromList(bean, keyStr, (List<?>) value);
        } else {//  www .  ja  v a  2  s .c om
            propUtils.setProperty(bean, keyStr, value);
        }
    }
}

From source file:de.hska.ld.content.service.impl.AbstractContentService.java

public List<String> compare(T oldT, T newT)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    BeanMap map = new BeanMap(oldT);
    PropertyUtilsBean propUtils = new PropertyUtilsBean();
    //StringBuilder sb = new StringBuilder();
    //sb.append("UPDATE process: >> object=[class=" + newT.getClass() + ", " + newT.getId() + "]:");
    List<String> differentProperties = new ArrayList<>();
    for (Object propNameObject : map.keySet()) {
        String propertyName = (String) propNameObject;
        if (propertyName.endsWith("List"))
            continue;
        try {/*from  w ww  .j ava  2  s.  com*/
            Object property1 = propUtils.getProperty(oldT, propertyName);
            Object property2 = propUtils.getProperty(newT, propertyName);
            if (property1 != null) {
                if (!(property1 instanceof List) && !(property1 instanceof Date)) {
                    if (property1.equals(property2)) {
                        //sb.append(" ||" + propertyName + " is equal ||");
                    } else {
                        try {
                            //sb.append(" ||> " + propertyName + " is different (oldValue=\"" + property1 + "\", newValue=\"" + property2 + "\") ||");
                            differentProperties.add(propertyName);
                        } catch (Exception e) {
                            //sb.append(" ||> " + propertyName + " is different (newValue=\"" + property2 + "\") ||");
                            differentProperties.add(propertyName);
                        }
                    }
                }
            } else {
                if (property2 == null) {
                    //sb.append(" ||" + propertyName + " is equal ||");
                } else {
                    if (!(property2 instanceof List) && !(property2 instanceof Date)) {
                        //sb.append(" ||> " + propertyName + " is different (newValue=\"" + property2 + "\") ||");
                        differentProperties.add(propertyName);
                    }
                }
            }
        } catch (Exception e) {
            //sb.append(" ||> Could not compute difference for property with name=" + propertyName + "||");
        }
    }
    //sb.append(" <<");
    //LOGGER.info(sb.toString());
    return differentProperties;
}

From source file:org.opencms.jsp.util.CmsMacroFormatterResolver.java

/**
 * Returns the property value read from the given JavaBean.
 *
 * @param bean the JavaBean to read the property from
 * @param property the property to read//from  ww w .ja v  a 2  s . c  o  m
 *
 * @return the property value read from the given JavaBean
 */
protected Object getMacroBeanValue(Object bean, String property) {

    Object result = null;
    if ((bean != null) && CmsStringUtil.isNotEmptyOrWhitespaceOnly(property)) {
        try {
            PropertyUtilsBean propBean = BeanUtilsBean.getInstance().getPropertyUtils();
            result = propBean.getProperty(bean, property);
        } catch (Exception e) {
            LOG.error("Unable to access property '" + property + "' of '" + bean + "'.", e);
        }
    } else {
        LOG.info("Invalid parameters: property='" + property + "' bean='" + bean + "'.");
    }
    return result;
}

From source file:org.opencms.workplace.CmsAccountInfo.java

/**
 * Returns the account info value for the given user.<p>
 *
 * @param user the user//from ww  w  .ja v  a  2 s .c  o  m
 *
 * @return the value
 */
public String getValue(CmsUser user) {

    String value = null;
    if (isAdditionalInfo()) {
        value = (String) user.getAdditionalInfo(getAddInfoKey());
    } else {
        try {
            PropertyUtilsBean propUtils = new PropertyUtilsBean();
            value = (String) propUtils.getProperty(user, getField().name());
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            LOG.error("Error reading account info field.", e);
        }
    }
    return value;
}

From source file:org.seasar.struts.bean.SuppressPropertyUtilsBeanTest.java

public void testGetProperty() throws Exception {
    List classes = new ArrayList();
    classes.add(FullName.class);
    PropertyUtilsBean propertyUtils = new SuppressPropertyUtilsBean(classes);
    Person person = new Person();
    person.getFullName().setFirstName("aaa");
    person.getFullName().setLastName("bbb");
    person.getAddress().setStreet("ccc");
    person.setAge("20");
    try {//from   www .  j  a v a 2  s  .c o  m
        propertyUtils.getProperty(person, "fullName.firstName");
    } catch (NoSuchMethodException expected) {
    }
    try {
        propertyUtils.getProperty(person, "fullName.lastName");
    } catch (NoSuchMethodException expected) {
    }
    assertEquals("ccc", propertyUtils.getProperty(person, "address.street"));
    assertEquals("20", propertyUtils.getProperty(person, "age"));
}