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

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

Introduction

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

Prototype

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

Source Link

Document

Return the value of the specified simple property of the specified bean, with no type conversions.

For more details see PropertyUtilsBean.

Usage

From source file:org.kuali.rice.krad.util.ObjectUtils.java

/**
 * This method safely extracts either simple values OR nested values. For example, if the bo is SubAccount, and the
 * fieldName is/*from   w  ww .  j a  v  a2  s.co m*/
 * a21SubAccount.subAccountTypeCode, this thing makes sure it gets the value off the very end attribute, no matter
 * how deeply
 * nested it is. The code would be slightly simpler if this was done recursively, but this is safer, and consumes a
 * constant
 * amount of memory, no matter how deeply nested it goes.
 *
 * @param bo
 * @param fieldName
 * @return The field value if it exists. If it doesnt, and the name is invalid, and
 */
public static Object getNestedValue(Object bo, String fieldName) {

    if (bo == null) {
        throw new IllegalArgumentException("The bo passed in was null.");
    }
    if (StringUtils.isBlank(fieldName)) {
        throw new IllegalArgumentException("The fieldName passed in was blank.");
    }

    // okay, this section of code is to handle sub-object values, like
    // SubAccount.a21SubAccount.subAccountTypeCode. it basically walks
    // through the period-delimited list of names, and ends up with the
    // final value.
    String[] fieldNameParts = fieldName.split("\\.");
    Object currentObject = null;
    Object priorObject = bo;
    for (int i = 0; i < fieldNameParts.length; i++) {
        String fieldNamePart = fieldNameParts[i];

        try {
            if (fieldNamePart.indexOf("]") > 0) {
                currentObject = PropertyUtils.getIndexedProperty(priorObject, fieldNamePart);
            } else {
                currentObject = PropertyUtils.getSimpleProperty(priorObject, fieldNamePart);
            }
        } catch (IllegalAccessException e) {
            throw new RuntimeException("Caller does not have access to the property accessor method.", e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException("Property accessor method threw an exception.", e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException("The accessor method requested for this property cannot be found.", e);
        }

        // materialize the proxy, if it is a proxy
        if (ProxyHelper.isProxy(currentObject)) {
            currentObject = ProxyHelper.getRealObject(currentObject);
        }

        // if a node or the leaf is null, then we're done, there's no need to
        // continue accessing null things
        if (currentObject == null) {
            return currentObject;
        }

        priorObject = currentObject;
    }
    return currentObject;
}

From source file:org.mmadsen.sim.transmissionlab.models.AbstractTLModel.java

public Object getSimpleModelPropertyByName(String property) throws RepastException {
    Object parameter = null;/* ww  w  . j  a  va 2 s .  com*/
    try {
        parameter = PropertyUtils.getSimpleProperty(this, property);
    } catch (Exception ex) {
        throw new RepastException(ex, ex.getMessage());
    }
    return parameter;
}

From source file:org.mule.modules.vertex.impl.SimpleVertexClient.java

/**
 * @see org.mule.modules.vertex.api.VertexClient#calculateTax(TaxTransactionType, Object)
 *///from  w ww  .  j a  va  2s. c om
@Override
@SuppressWarnings("unchecked")
public <T extends TaxTransactionResponseType> T calculateTax(TaxTransactionType type, Object request) {
    try {
        VertexEnvelope vertexEnvelope = new VertexEnvelopeBuilder().withRequest(type, request)
                .withLogin(createLogin()).build();
        VertexEnvelope response = getCalculateTaxService().calculateTax60(vertexEnvelope);
        return (T) PropertyUtils.getSimpleProperty(response, type.getResponseAttr());
    } catch (Exception e) {
        throw new VertexRuntimeException(e);
    }
}

From source file:org.mule.modules.vertex.impl.SimpleVertexClient.java

/**
 * @see org.mule.modules.vertex.api.VertexClient#calculateTaxSync(TaxTransactionSyncType, Object)
 *//*from www.ja va  2s.c o  m*/
@Override
@SuppressWarnings("unchecked")
public <T extends TaxSynchronizationResponseType> T calculateTaxSync(TaxTransactionSyncType type,
        Object request) {
    try {
        VertexEnvelope vertexEnvelope = new VertexEnvelopeBuilder().withRequest(type, request)
                .withLogin(createLogin()).build();
        VertexEnvelope response = getCalculateTaxService().calculateTax60(vertexEnvelope);
        return (T) PropertyUtils.getSimpleProperty(response, type.getResponseAttr());
    } catch (Exception e) {
        throw new VertexRuntimeException(e);
    }
}

From source file:org.mule.modules.vertex.impl.SimpleVertexClient.java

/**
 * @see org.mule.modules.vertex.api.VertexClient#lookupTaxArea(TaxGisType,Object)
 *//*from  w  ww. j  a v  a2  s  .co  m*/
@Override
@SuppressWarnings("unchecked")
public <T extends TaxgisResponseType> T lookupTaxArea(TaxGisType type, Object request) {
    try {
        VertexEnvelope vertexEnvelope = new VertexEnvelopeBuilder().withRequest(type, request)
                .withLogin(createLogin()).build();
        VertexEnvelope response = getLookupTaxAreasService().lookupTaxAreas60(vertexEnvelope);
        return (T) PropertyUtils.getSimpleProperty(response, type.getResponseAttr());
    } catch (Exception e) {
        throw new VertexRuntimeException(e);
    }
}

From source file:org.netbeans.modeler.specification.model.document.property.ElementPropertySet.java

private void createPropertySetInternal(String groupId, final IBaseElementWidget baseElementWidget,
        final Object object, Element element, final Map<String, PropertyChangeListener> propertyChangeHandlers,
        final Map<String, PropertyVisibilityHandler> propertyVisiblityHandlers, boolean replaceProperty) {
    try {/*from   www. j a va  2  s.c  om*/
        if (element != null) {
            for (final Attribute attribute : element.getAttributes()) {
                String attributeGroupId = groupId;
                if (attributeGroupId == null) {
                    attributeGroupId = attribute.getGroupId();
                }
                if (attribute.getClassType() == ITextElement.class) {
                    final String name = attribute.getName();
                    final ITextElement expression = (ITextElement) PropertyUtils.getProperty(object, name);//return must not be null//(TExpression) PropertyUtils.getProperty(object, id) == null ? new TExpression() : (TExpression) PropertyUtils.getProperty(object, id);
                    this.put(attributeGroupId,
                            new ElementCustomPropertySupport(this.getModelerFile(), expression, String.class,
                                    attribute.getId(), "content", attribute.getDisplayName(),
                                    attribute.getShortDescription(),
                                    (PropertyChangeListener<String>) (oldValue, value) -> {
                                        if (expression.getContent() == null
                                                || expression.getContent().isEmpty()) {
                                            try {
                                                PropertyUtils.setProperty(object, name, null);
                                            } catch (IllegalAccessException | InvocationTargetException
                                                    | NoSuchMethodException ex) {
                                                Exceptions.printStackTrace(ex);
                                            }
                                        }
                                        if (propertyChangeHandlers != null
                                                && propertyChangeHandlers.get(name) != null) {
                                            propertyChangeHandlers.get(name).changePerformed(oldValue, value);
                                        }
                                        if (attribute.isRefreshOnChange()) {
                                            baseElementWidget.refreshProperties();
                                        }
                                    },
                                    propertyVisiblityHandlers == null ? null
                                            : propertyVisiblityHandlers.get(attribute.getId())),
                            replaceProperty);

                } else if (Enumy.class.isAssignableFrom(attribute.getClassType())) {
                    EnumComboBoxResolver resolver = Lookup.getDefault().lookup(EnumComboBoxResolver.class);
                    this.put(attributeGroupId, resolver.getPropertySupport(modelerFile, attribute,
                            baseElementWidget, object, propertyChangeHandlers));
                } else if (Embedded.class.isAssignableFrom(attribute.getClassType())) {
                    if (attribute.getGroupId() != null) {
                        groupId = attribute.getGroupId();
                    }
                    //in embedded id is used as category
                    this.createPropertySet(groupId, attribute.getId(), baseElementWidget,
                            PropertyUtils.getSimpleProperty(object, attribute.getName()),
                            propertyChangeHandlers, propertyVisiblityHandlers, true, replaceProperty);
                } else {
                    if (attribute.isReadOnly()) {
                        String value = BeanUtils.getProperty(object, attribute.getName());
                        if (value == null) {
                            BeanUtils.setProperty(object, attribute.getName(), attribute.getValue());
                        }
                        this.put(attributeGroupId,
                                new ElementPropertySupport(object, attribute.getFieldGetter(), null, attribute),
                                replaceProperty);
                    } else {
                        PropertyVisibilityHandler propertyVisibilityHandler = propertyVisiblityHandlers == null
                                ? null
                                : propertyVisiblityHandlers.get(attribute.getId());

                        Serializable visibilityExpression = attribute.getVisibilityExpression();
                        if (propertyVisibilityHandler == null && visibilityExpression != null) {
                            propertyVisibilityHandler = createPropertyVisibilityHandler(modelerFile,
                                    baseElementWidget, object, visibilityExpression);
                        }
                        if (propertyChangeHandlers != null
                                && propertyChangeHandlers.get(attribute.getId()) == null
                                && attribute.getOnChangeEvent() != null
                                && !attribute.getOnChangeEvent().trim().isEmpty()) {
                            propertyChangeHandlers.put(attribute.getId(),
                                    createPropertyChangeHandler(modelerFile, baseElementWidget, object,
                                            attribute.getChangeListenerExpression()));
                        }
                        this.put(attributeGroupId, new ElementCustomPropertySupport(this.getModelerFile(),
                                object, attribute, (PropertyChangeListener<Object>) (oldValue, value) -> {
                                    try {
                                        if (value != null) {
                                            if (value instanceof String) {
                                                if (!((String) value).isEmpty()) {
                                                    BeanUtils.setProperty(object, attribute.getName(), value);
                                                } else {
                                                    BeanUtils.setProperty(object, attribute.getName(), null);
                                                }
                                            } else {
                                                BeanUtils.setProperty(object, attribute.getName(), value);
                                            }
                                        } else {
                                            BeanUtils.setProperty(object, attribute.getName(), null);
                                        }
                                    } catch (IllegalAccessException | InvocationTargetException ex) {
                                        Exceptions.printStackTrace(ex);
                                    }
                                    elementValueChanged(baseElementWidget, attribute, propertyChangeHandlers,
                                            oldValue, value);
                                }, propertyVisibilityHandler), replaceProperty);

                    }
                }
            }
        }
    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException
            | NoSuchFieldException ex) {
        Exceptions.printStackTrace(ex);
    }

}

From source file:org.netxilia.spi.impl.storage.db.CellsMapper.java

/**
 * save a cell's property (including value for sheets of type other than "normal")
 *//*ww w  .j  a v a  2  s  . co m*/
private CellData saveCellProperties(SparseMatrixCollection matrices, SheetDbSession session,
        SheetFullName sheetFullName, SheetType sheetType, CellData cell, Collection<Property> properties)
        throws NotFoundException {
    // make sure the corresponding row exists
    session.getStorageService().getOrCreateRowStorage(session, cell.getReference().getRowIndex());

    for (Property property : properties) {
        if (Property.value == property) {
            // TODO - store rich values

            if (sheetType != SheetType.normal) {
                // create column
                // create row
                String rawValue = cell.getValue() != null ? cell.getValue().getStringValue() : null;
                matrices.put(cell.getReference().getRowIndex(), cell.getReference().getColumnIndex(),
                        property.name(), rawValue);
            }
            // add also type
            matrices.put(cell.getReference().getRowIndex(), cell.getReference().getColumnIndex(),
                    CellData.Property.type.name(),
                    cell.getValue() != null ? cell.getValue().getValueType().name() : null);

        } else {
            // update the other properties
            Object propertyValue = null;
            try {
                propertyValue = PropertyUtils.getSimpleProperty(cell, property.name());
            } catch (Exception e) {
                log.error("Cannot get cell property " + property + ":" + e, e);
            }
            matrices.put(cell.getReference().getRowIndex(), cell.getReference().getColumnIndex(),
                    property.name(), propertyValue != null ? propertyValue.toString() : null);
            // save modification right away
        }
    }

    return cell;
}

From source file:org.netxilia.spi.impl.storage.db.DefaultPropertySerializer.java

@Override
public String serializeProperty(T object, String property) {
    Object propertyValue = null;/*w ww  . j  a v a 2s . c o  m*/
    try {
        propertyValue = PropertyUtils.getSimpleProperty(object, property);
        return propertyValue != null ? propertyValue.toString() : null;
    } catch (Exception e) {
        log.error("Cannot get property " + property + ":" + e, e);
        return null;
    }
}

From source file:org.nmrfx.processor.gui.spectra.DatasetAttributes.java

public Map<String, Object> config() {

    //        BeanInfo info = null;
    //        try {
    //            info = Introspector.getBeanInfo(DatasetAttributes.class);
    //        } catch (IntrospectionException ex) {
    //            Logger.getLogger(DatasetAttributes.class.getName()).log(Level.SEVERE, null, ex);
    //        }// w  w w .java2 s  . c  o  m
    //        if (info != null) {
    //            
    //            for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
    //                System.out.println(pd.getName() + " : " + pd.getReadMethod() + " : " + pd.getWriteMethod());
    //            }
    //        }
    Map<String, Object> data = new HashMap<>();
    String[] beanNames = { "nlvls", "clm", "posColor", "negColor", "posWidth", "negWidth", "lvl", "pos",
            "neg" };
    for (String beanName : beanNames) {
        try {
            if (beanName.contains("Color")) {
                Object colObj = PropertyUtils.getSimpleProperty(this, beanName);
                if (colObj instanceof Color) {
                    String colorName = GUIScripter.toRGBCode((Color) colObj);
                    data.put(beanName, colorName);
                }
            } else {
                data.put(beanName, PropertyUtils.getSimpleProperty(this, beanName));
            }
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) {
            Logger.getLogger(DatasetAttributes.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    return data;
}

From source file:org.nmrfx.processor.gui.spectra.PeakListAttributes.java

public Map<String, Object> config() {
    Map<String, Object> data = new HashMap<>();
    String[] beanNames = { "onColor", "offColor", "drawPeaks", "simPeaks", "labelType", "displayType" };
    for (String beanName : beanNames) {
        try {/*from w ww.  j  a  v  a2s . c  om*/
            if (beanName.contains("Color")) {
                Object colObj = PropertyUtils.getSimpleProperty(this, beanName);
                if (colObj instanceof Color) {
                    String colorName = colObj.toString();
                    data.put(beanName, colorName);
                }
            } else {
                Object obj = PropertyUtils.getSimpleProperty(this, beanName);
                if (obj == null) {
                    data.put(beanName, null);
                } else {
                    data.put(beanName, obj.toString());
                }
            }
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) {
            Logger.getLogger(PeakListAttributes.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    return data;
}