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

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

Introduction

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

Prototype

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

Source Link

Document

Retrieve the property descriptor for the specified property of the specified bean, or return null if there is no such descriptor.

For more details see PropertyUtilsBean.

Usage

From source file:org.kie.workbench.common.forms.adf.engine.backend.formGeneration.util.BackendPropertyValueExtractor.java

@Override
protected Object readValue(Object model, String propertyName) {
    try {/*  w  w w .  ja v  a 2  s.co m*/
        if (PropertyUtils.getPropertyDescriptor(model, propertyName) != null) {
            return PropertyUtils.getProperty(model, propertyName);
        }
    } catch (Exception e) {
        logger.warn("Error getting property '{}' from object '{}'", propertyName, model);
        logger.warn("Caused by:", e);
    }
    return null;
}

From source file:org.kie.workbench.common.forms.dynamic.backend.server.context.generation.dynamic.impl.fieldProcessors.NestedFormFieldValueProcessor.java

protected void writeValues(Map<String, Object> values, Object model) {
    if (model == null) {
        return;// w  w w.j av a  2  s.co m
    }
    values.forEach((property, value) -> {
        try {
            if (property.equals(MapModelRenderingContext.FORM_ENGINE_OBJECT_IDX)
                    || property.equals(MapModelRenderingContext.FORM_ENGINE_EDITED_OBJECT)) {
                return;
            }
            if (PropertyUtils.getPropertyDescriptor(model, property) != null) {
                BeanUtils.setProperty(model, property, value);
            }
        } catch (Exception e) {
            getLogger().warn("Error modifying object '{}': cannot set value '{}' to property '{}'", model,
                    value, property);
            getLogger().warn("Caused by:", e);
        }
    });
}

From source file:org.kie.workbench.common.forms.dynamic.backend.server.context.generation.dynamic.impl.fieldProcessors.NestedFormFieldValueProcessor.java

protected Object readValue(String property, Object model) {
    try {//from  w w  w  .  j  av  a2 s  . c o  m
        if (PropertyUtils.getPropertyDescriptor(model, property) != null) {
            return PropertyUtils.getProperty(model, property);
        }
    } catch (Exception e) {
        getLogger().warn("Error getting property '{}' from object '{}'", property, model);
        getLogger().warn("Caused by:", e);
    }
    return null;
}

From source file:org.kie.workbench.common.forms.dynamic.backend.server.context.generation.dynamic.impl.marshalling.models.ModelMarshaller.java

private Object readValue(String property) {
    if (model != null) {
        try {/* ww w.  j  a  v a 2s  . com*/
            if (PropertyUtils.getPropertyDescriptor(model, property) != null) {
                return PropertyUtils.getProperty(model, property);
            }
        } catch (Exception e) {
            logger.warn("Error getting property '{}' from object '{}'", property, model);
            logger.warn("Caused by:", e);
        }
    }
    return null;
}

From source file:org.kie.workbench.common.forms.dynamic.backend.server.context.generation.dynamic.impl.marshalling.models.ModelMarshaller.java

private void writeValue(String property, Object value) {
    try {/*w  ww .  j a v a 2  s. c o  m*/
        if (PropertyUtils.getPropertyDescriptor(model, property) != null) {
            BeanUtils.setProperty(model, property, value);
        }
    } catch (Exception e) {
        logger.warn("Error modifying object '{}': cannot set value '{}' to property '{}'", model, value,
                property);
        logger.warn("Caused by:", e);
    }
}

From source file:org.kordamp.ezmorph.bean.BeanMorpher.java

public Object morph(Object sourceBean) {
    if (sourceBean == null) {
        return null;
    }//from   ww w. j a  va 2  s .  c o m
    if (!supports(sourceBean.getClass())) {
        throw new MorphException("unsupported class: " + sourceBean.getClass().getName());
    }

    Object targetBean = null;

    try {
        targetBean = beanClass.newInstance();
        PropertyDescriptor[] targetPds = PropertyUtils.getPropertyDescriptors(beanClass);
        for (int i = 0; i < targetPds.length; i++) {
            PropertyDescriptor targetPd = targetPds[i];
            String name = targetPd.getName();
            if (targetPd.getWriteMethod() == null) {
                log.info("Property '" + beanClass.getName() + "." + name + "' has no write method. SKIPPED.");
                continue;
            }

            Class sourceType = null;
            if (sourceBean instanceof DynaBean) {
                DynaBean dynaBean = (DynaBean) sourceBean;
                DynaProperty dynaProperty = dynaBean.getDynaClass().getDynaProperty(name);
                if (dynaProperty == null) {
                    log.warn("DynaProperty '" + name + "' does not exist. SKIPPED.");
                    continue;
                }
                sourceType = dynaProperty.getType();
            } else {
                PropertyDescriptor sourcePd = PropertyUtils.getPropertyDescriptor(sourceBean, name);
                if (sourcePd == null) {
                    log.warn("Property '" + sourceBean.getClass().getName() + "." + name
                            + "' does not exist. SKIPPED.");
                    continue;
                } else if (sourcePd.getReadMethod() == null) {
                    log.warn("Property '" + sourceBean.getClass().getName() + "." + name
                            + "' has no read method. SKIPPED.");
                    continue;
                }
                sourceType = sourcePd.getPropertyType();
            }

            Class targetType = targetPd.getPropertyType();
            Object value = PropertyUtils.getProperty(sourceBean, name);
            setProperty(targetBean, name, sourceType, targetType, value);
        }
    } catch (MorphException me) {
        throw me;
    } catch (Exception e) {
        throw new MorphException(e);
    }

    return targetBean;
}

From source file:org.kuali.coeus.common.impl.krms.KcKrmsFactBuilderServiceHelper.java

/**
 * /*from   w ww .ja  v a 2s.  c  om*/
 * This method gets all terms from the <code>factsObject</code>and add it to the KRMS FactsBuilder 
 * @param factsBuilder
 * @param factsObject
 * @param contextId
 * @param factTermNameSpace
 */
public void addObjectMembersAsFacts(Facts.Builder factsBuilder, Object factsObject, String contextId,
        String factTermNS) {
    TermRepositoryService termRepositoryService = KcServiceLocator.getService("termRepositoryService");
    List<TermSpecificationDefinition> termSpecs = (List<TermSpecificationDefinition>) termRepositoryService
            .findAllTermSpecificationsByContextId(contextId);
    for (TermSpecificationDefinition termSpecificationDefinition : termSpecs) {

        if (isPropertyType(termSpecificationDefinition)) {
            String termNS = termSpecificationDefinition.getNamespace();
            if (termNS.equals(factTermNS)) {
                String factKey = termSpecificationDefinition.getName();
                if (factsObject != null) {
                    Class factsClazz = factsObject.getClass();
                    PropertyDescriptor propDescriptor = null;
                    try {
                        propDescriptor = PropertyUtils.getPropertyDescriptor(factsObject, factKey);
                        if (propDescriptor != null) {
                            Object propertyValue = null;
                            Method readMethod = propDescriptor.getReadMethod();
                            if (readMethod != null) {
                                propertyValue = propDescriptor.getReadMethod().invoke(factsObject);
                            }
                            if (propertyValue != null
                                    && propertyValue.getClass().isAssignableFrom(ScaleTwoDecimal.class)) {
                                propertyValue = ((ScaleTwoDecimal) propertyValue).bigDecimalValue();
                            }
                            factsBuilder.addFact(factKey, propertyValue);
                        }
                    } catch (IllegalArgumentException e) {
                        LOG.error("KRMS Fact for " + factKey + " has not been added to fact builder", e);
                    } catch (IllegalAccessException e) {
                        LOG.error("KRMS Fact for " + factKey + " has not been added to fact builder", e);
                    } catch (InvocationTargetException e) {
                        LOG.error("KRMS Fact for " + factKey + " has not been added to fact builder", e);
                    } catch (NoSuchMethodException e) {
                        LOG.error("KRMS Fact for " + factKey + " has not been added to fact builder", e);
                    }
                } else {
                    factsBuilder.addFact(factKey, null);
                }
            }
        }
    }
}

From source file:org.kuali.coeus.common.impl.version.SequenceUtils.java

/**
 * This method retrieves the getter for a field on the passed in object.
 * /*from   w w  w.j a  v a 2 s  . co  m*/
 * @param parent the object to retrieve the getter from
 * @param field the field to retrieve the getter for
 * @return the getter.  Will never return null
 * @throws GetterException if there is a problem retrieving the "getter" for a field
 */
private Method findReadMethod(Object parent, Field field) throws GetterException {

    final PropertyDescriptor pd;
    try {
        pd = PropertyUtils.getPropertyDescriptor(parent, field.getName());
        if (pd == null) {
            throw new GetterException(
                    String.format("The property descriptor for field [%s] on class [%s] could not be found",
                            field.getName(), parent.getClass().getName()));
        }
    } catch (IllegalAccessException e) {
        throw new GetterException(e);
    } catch (InvocationTargetException e) {
        throw new GetterException(e);
    } catch (NoSuchMethodException e) {
        throw new GetterException(e);
    }
    final Method getter = pd.getReadMethod();
    if (getter == null) {
        throw new GetterException(String.format("No getter defined for field [%s] on class [%s]",
                field.getName(), parent.getClass().getName()));
    }

    return getter;
}

From source file:org.kuali.coeus.sys.framework.model.KcPersistableBusinessObjectBase.java

@Override
protected void postPersist() {
    if (temporaryExtension != null) {
        @SuppressWarnings("unchecked")
        final List<String> fieldNames = getPersistenceStructureService().listPrimaryKeyFieldNames(getClass());
        try {/*ww  w.  j  a  v a 2 s  .  c  o  m*/
            for (String fieldName : fieldNames) {
                try {
                    Method thisGetter = PropertyUtils
                            .getReadMethod(PropertyUtils.getPropertyDescriptor(this, fieldName));
                    Method extensionSetter = PropertyUtils
                            .getWriteMethod(PropertyUtils.getPropertyDescriptor(temporaryExtension, fieldName));
                    extensionSetter.invoke(temporaryExtension, thisGetter.invoke(this));
                } catch (NoSuchMethodException nsme) {
                    throw new PersistenceBrokerException(
                            "Could not find accessor for " + fieldName + " in an extension object", nsme);
                } catch (InvocationTargetException ite) {
                    throw new PersistenceBrokerException(
                            "Could not invoke accessor for " + fieldName + " on an extension object", ite);
                } catch (IllegalAccessException iae) {
                    throw new PersistenceBrokerException(
                            "Illegal access when invoking " + fieldName + " accessor on an extension object",
                            iae);
                }
            }
        } finally {
            extension = temporaryExtension;
            temporaryExtension = null;
        }
    }
}

From source file:org.kuali.kfs.gl.web.TestDataGenerator.java

/**
 * If the actual transaction implementation has a "setUniversityFiscalYear" method, use that to set the 
 * fiscal year to the value of TestUtils.getFiscalYearForTesting()
 * @param transaction transaction to try to set fiscal year on
 *//*from  w  w w.  jav  a  2s.c  om*/
protected void setFiscalYear(Transaction transaction) {
    try {
        final PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(transaction,
                KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR);
        if (propertyDescriptor.getReadMethod() != null) {
            PropertyUtils.setProperty(transaction, KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR,
                    TestUtils.getFiscalYearForTesting());
        }
    } catch (IllegalAccessException iae) {
        LOG.info("Could test universityFiscalYear property on fixture of transaction type: "
                + transaction.getClass().getName(), iae);
    } catch (InvocationTargetException ite) {
        LOG.info("Could test universityFiscalYear property on fixture of transaction type: "
                + transaction.getClass().getName(), ite);
    } catch (NoSuchMethodException nsme) {
        LOG.info("Could test universityFiscalYear property on fixture of transaction type: "
                + transaction.getClass().getName(), nsme);
    }

}