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

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

Introduction

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

Prototype

public static Method getWriteMethod(PropertyDescriptor descriptor) 

Source Link

Document

Return an accessible property setter method for this property, if there is one; otherwise return null.

For more details see PropertyUtilsBean.

Usage

From source file:ca.sqlpower.object.undo.PropertyChangeEdit.java

/**
 * Sets the value of the property to be the old value
 *//*www .  j a v a 2 s.  c  o  m*/
@Override
public void undo() throws CannotUndoException {
    logger.debug("Undoing Property change: Setting " + sourceEvent.getPropertyName() + " from "
            + sourceEvent.getNewValue() + " to " + sourceEvent.getOldValue());
    super.undo();
    try {
        final PropertyDescriptor propertyDescriptor = PropertyUtils
                .getPropertyDescriptor(sourceEvent.getSource(), sourceEvent.getPropertyName());
        logger.debug("Found property descriptor " + propertyDescriptor);
        if (logger.isDebugEnabled()) {
            PropertyDescriptor[] propertyDescriptors = PropertyUtils
                    .getPropertyDescriptors(sourceEvent.getSource());
            logger.debug("Descriptor has write method " + propertyDescriptor.getWriteMethod());
        }
        Method setter = PropertyUtils.getWriteMethod(propertyDescriptor);
        logger.info("Found setter: " + setter.getName());
        setter.invoke(sourceEvent.getSource(), sourceEvent.getOldValue());

    } catch (Exception ex) {
        CannotUndoException wrapper = new CannotUndoException();
        wrapper.initCause(ex);
        throw wrapper;
    }
}

From source file:ca.sqlpower.object.undo.PropertyChangeEdit.java

/**
 * Sets the value of the property to be the new value
 *///w ww  .ja v a  2 s.c  o m
@Override
public void redo() throws CannotRedoException {
    logger.debug("Undoing Property change: Setting " + sourceEvent.getPropertyName() + " from "
            + sourceEvent.getOldValue() + " to " + sourceEvent.getNewValue());
    super.redo();
    try {
        Method setter = PropertyUtils.getWriteMethod(
                PropertyUtils.getPropertyDescriptor(sourceEvent.getSource(), sourceEvent.getPropertyName()));
        logger.info("Found setter: " + setter.getName());
        setter.invoke(sourceEvent.getSource(), sourceEvent.getNewValue());

    } catch (Exception ex) {
        CannotRedoException wrapper = new CannotRedoException();
        wrapper.initCause(ex);
        throw wrapper;
    }
}

From source file:edu.ku.brc.af.ui.forms.DataSetterForObj.java

public void setFieldValue(Object dataObj, String fieldName, Object data) {
    //log.debug("fieldName["+fieldName+"] dataObj["+dataObj+"] data ["+data+"]");
    try {//w  w w .  j  a v a  2  s.co m
        PropertyDescriptor descr = PropertyUtils.getPropertyDescriptor(dataObj, fieldName.trim());
        if (descr != null) {
            // Check to see if the class of the data we have is different than the one we are trying to set into
            // This typically happens when we have a TextField with a number and it needs to be converted from a 
            // String representation of the number to the actually numeric type like from String to Integer or Short
            Object dataVal = data;
            Class<?> fieldClass = descr.getPropertyType();
            if (data != null) {

                if (dataVal.getClass() != fieldClass && !fieldClass.isAssignableFrom(dataVal.getClass())) {
                    dataVal = UIHelper.convertDataFromString(dataVal.toString(), fieldClass);
                }
            } /*else // Data is Null
              {
              if (fieldClass.isAssignableFrom(Collection.class) || fieldClass.isAssignableFrom(Set.class))
              {
                  return;
              }
              }*/
            if (dataVal instanceof String && ((String) dataVal).isEmpty()) {
                dataVal = null;
            }
            Method setter = PropertyUtils.getWriteMethod(descr);
            if (setter != null && dataObj != null) {
                args[0] = dataVal;
                //log.debug("fieldname["+fieldName+"] dataObj["+dataObj+"] data ["+dataVal+"] ("+(dataVal != null ? dataVal.getClass().getSimpleName() : "")+")");
                setter.invoke(dataObj, dataVal);
            } else {
                log.error("dataObj was null - Trouble setting value field named["
                        + (fieldName != null ? fieldName.trim() : "null") + "] in data object ["
                        + (dataObj != null ? dataObj.getClass().toString() : "null") + "]");
            }
        } else {
            log.error("We could not find a field named[" + fieldName.trim() + "] in data object ["
                    + dataObj.getClass().toString() + "]");
        }
    } catch (java.lang.reflect.InvocationTargetException ex) {
        log.error("Trouble setting value field named[" + (fieldName != null ? fieldName.trim() : "null")
                + "] in data object [" + (dataObj != null ? dataObj.getClass().toString() : "null") + "]");
        log.error(ex);
        ex.printStackTrace();
        edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
        edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(DataSetterForObj.class, ex);

    } catch (Exception ex) {
        log.error("Trouble setting value field named[" + (fieldName != null ? fieldName.trim() : "null")
                + "] in data object [" + (dataObj != null ? dataObj.getClass().toString() : "null") + "]");
        log.error(ex);
        ex.printStackTrace();
        edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
        edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(DataSetterForObj.class, ex);
    }
}

From source file:com.timtripcony.AbstractSmartDocumentModel.java

@Override
public boolean isReadOnly(final Object key) {
    boolean result = !isEditMode() || super.isReadOnly(key);
    try {//from   www.java 2s.  co  m
        if (!result) {
            result = PropertyUtils
                    .getWriteMethod(PropertyUtils.getPropertyDescriptor(this, key.toString())) == null;
        }
    } catch (Throwable t) {
        result = super.isReadOnly(key);
    }
    return result;
}

From source file:com.opengamma.language.object.ObjectFunctionProvider.java

protected void loadGetAndSet(final ObjectInfo object, final Collection<MetaFunction> definitions,
        final String category) {
    if (object.getLabel() != null) {
        final Class<?> clazz = object.getObjectClass();
        final Set<String> attributes = new HashSet<String>(object.getDirectAttributes());
        for (PropertyDescriptor prop : PropertyUtils.getPropertyDescriptors(clazz)) {
            final AttributeInfo attribute = object.getDirectAttribute(prop.getName());
            if (attribute != null) {
                if (attribute.isReadable()) {
                    final Method read = PropertyUtils.getReadMethod(prop);
                    if (read != null) {
                        loadObjectGetter(object, attribute, read, definitions, category);
                        attributes.remove(prop.getName());
                    }/*w  w  w.j a  va2 s .c o  m*/
                }
                if (attribute.isWriteable()) {
                    final Method write = PropertyUtils.getWriteMethod(prop);
                    if (write != null) {
                        loadObjectSetter(object, attribute, write, definitions, category);
                        attributes.remove(prop.getName());
                    }
                }
            }
        }
        if (!attributes.isEmpty()) {
            for (String attribute : attributes) {
                throw new OpenGammaRuntimeException(
                        "Attribute " + attribute + " is not exposed on object " + object);
            }
        }
    }
}

From source file:nl.nn.adapterframework.configuration.AttributeCheckingRule.java

public void begin(String uri, String elementName, Attributes attributes) throws Exception {

    Object top = digester.peek();

    for (int i = 0; i < attributes.getLength(); i++) {
        String name = attributes.getLocalName(i);
        if ("".equals(name)) {
            name = attributes.getQName(i);
        }// ww w.  j  a v  a  2 s. c o m
        if (name != null && !name.equals("className")) {
            //            if (log.isDebugEnabled()) {
            //               log.debug(getObjectName(top)+" checking for setter for attribute ["+name+"]");
            //            }
            PropertyDescriptor pd = PropertyUtils.getPropertyDescriptor(top, name);
            Method m = null;
            if (pd != null) {
                m = PropertyUtils.getWriteMethod(pd);
            }
            if (m == null) {
                Locator loc = digester.getDocumentLocator();
                String msg = "line " + loc.getLineNumber() + ", col " + loc.getColumnNumber() + ": "
                        + getObjectName(top) + " does not have an attribute [" + name + "] to set to value ["
                        + attributes.getValue(name) + "]";
                configWarnings.add(log, msg);
            }
        }
    }

}

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 {/*from   w w  w . j  a  v  a 2s  . com*/
            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.kra.bo.KraPersistableBusinessObjectBase.java

/**
 * {@inheritDoc}//from w w  w.j  a v  a  2 s  . co  m
 * @see org.kuali.rice.kns.bo.PersistableBusinessObjectBase#afterInsert(org.apache.ojb.broker.PersistenceBroker)
 */
@Override
@SuppressWarnings("unchecked")
public void afterInsert(PersistenceBroker persistenceBroker) throws PersistenceBrokerException {
    if (temporaryExtension != null) {
        List<String> fieldNames = getPersistenceStructureService().listPrimaryKeyFieldNames(getClass());
        try {
            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.lunifera.runtime.web.vaadin.databinding.component.internal.SimpleAccessorProperty.java

public SimpleAccessorProperty(Class<?> componentClass, String property) {
    super();/*from   ww w .j  av  a  2s.  c o  m*/

    PropertyDescriptor result = getPropertyDescriptor(componentClass, property);
    this.getter = PropertyUtils.getReadMethod(result);
    this.setter = PropertyUtils.getWriteMethod(result);
    this.type = getter.getReturnType();
}

From source file:org.neovera.jdiablo.internal.BeanUtils.java

public static Method getWriteMethod(String fieldName, Class<?> clz) {
    try {//from   www  .  jav a2  s .c  o  m
        return PropertyUtils.getWriteMethod(new PropertyDescriptor(fieldName, clz));
    } catch (IntrospectionException e) {
        _logger.error(e.getMessage(), e);
        throw new RuntimeException(e.getMessage(), e);
    }
}