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

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

Introduction

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

Prototype

public static void setProperty(Object bean, String name, Object value)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException 

Source Link

Document

Set 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:org.sonatype.nexus.plugins.crowd.client.BaseCrowdLocator.java

/**
 * Set a bean property if (and only if) the attribute exists and is
 * non-blank on the provided entity.// w ww . j  a  va2s  . c om
 * 
 * @param bean the bean on which to set a property
 * @param property the property name to set
 * @param entity the Crowd entity from which to retreive the attribute
 * @param attributeName the attribute name
 * @throws IllegalAccessException if something goes wrong
 * @throws InvocationTargetException if something goes wrong
 * @throws NoSuchMethodException if something goes wrong
 */
private static void setIfAttributeExists(Object bean, String property, SOAPEntity entity, String attributeName)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    SOAPAttribute attr = entity.getAttribute(attributeName);
    if (attr != null && attr.getValues().length == 1) {
        PropertyUtils.setProperty(bean, property, attr.getValues()[0]);
    }
}

From source file:org.specrunner.parameters.core.AccessImpl.java

@Override
public void set(Object target, String name, Object... args) throws Exception {
    if (target == null) {
        return;//www.  j  a v  a  2 s  .  c  o  m
    }
    if (field != null) {
        field.set(target, prepare(args[0]));
    } else if (property != null) {
        PropertyUtils.setProperty(target, name, prepare(args[0]));
    } else if (method != null) {
        method.invoke(target, prepare(args));
    }
}

From source file:org.specrunner.plugins.core.objects.AbstractPluginObject.java

/**
 * Set the field with the given value./*  ww w  .  j  a va  2  s . c o  m*/
 * 
 * @param context
 *            The test context.
 * @param table
 *            The table.
 * @param row
 *            The row corresponding to the object.
 * @param instance
 *            The object instance.
 * @param f
 *            The field information.
 * @param value
 *            The value to be set in instance for field 'f'.
 * @throws Exception
 *             On set value errors.
 */
protected void setValue(IContext context, TableAdapter table, RowAdapter row, Object instance, Field f,
        Object value) throws Exception {
    // populate inner objects
    Object current = instance;
    for (int i = 0; i < f.names.length - 1; i++) {
        Object tmp = PropertyUtils.getProperty(current, f.names[i]);
        if (tmp == null) {
            if (creator != null) {
                tmp = creatorInstance.create(context, table, row, f.types[i]);
            }
            if (tmp == null) {
                if (f.types[i] == List.class) {
                    tmp = new LinkedList<Object>();
                } else {
                    tmp = f.types[i].newInstance();
                }
            }
            PropertyUtils.setProperty(current, f.names[i], tmp);
        }
        current = tmp;
    }
    if (value == null) {
        setObject(instance, f, null);
    } else {
        Class<?> st = f.getSpecificType();
        if (st == boolean.class || st == Boolean.class) {
            setBoolean(instance, f, value);
        } else if (st == char.class || st == Character.class) {
            setChar(instance, f, value);
        } else if (st == short.class || st == Short.class) {
            setShort(instance, f, value);
        } else if (st == int.class || st == Integer.class) {
            setInteger(instance, f, value);
        } else if (st == long.class || st == Long.class) {
            setLong(instance, f, value);
        } else if (st == float.class || st == Float.class) {
            setFloat(instance, f, value);
        } else if (st == double.class || st == Double.class) {
            setDouble(instance, f, value);
        } else if (SRServices.getObjectManager().isBound(st)) {
            setEntity(instance, f, value);
        } else {
            setObject(instance, f, value);
        }
    }
}

From source file:org.specrunner.plugins.core.objects.AbstractPluginObject.java

/**
 * Sets a boolean to a field.//w  w  w. j  a v a 2s. c  om
 * 
 * @param instance
 *            The object instance.
 * @param f
 *            The field information.
 * @param value
 *            The value to be set.
 * @throws Exception
 *             On setting errors.
 */
protected void setBoolean(Object instance, Field f, Object value) throws Exception {
    PropertyUtils.setProperty(instance, f.getFullName(), Boolean.valueOf(String.valueOf(value)));
}

From source file:org.specrunner.plugins.core.objects.AbstractPluginObject.java

/**
 * Sets a char to a field.//from   w ww.  ja  v  a 2 s .c  o  m
 * 
 * @param instance
 *            The object instance.
 * @param f
 *            The field information.
 * @param value
 *            The value to be set.
 * @throws Exception
 *             On setting errors.
 */
protected void setChar(Object instance, Field f, Object value) throws Exception {
    PropertyUtils.setProperty(instance, f.getFullName(), Character.valueOf(String.valueOf(value).charAt(0)));
}

From source file:org.specrunner.plugins.core.objects.AbstractPluginObject.java

/**
 * Sets a short to a field.//from w w  w  . ja  v  a2  s  . c  o  m
 * 
 * @param instance
 *            The object instance.
 * @param f
 *            The field information.
 * @param value
 *            The value to be set.
 * @throws Exception
 *             On setting errors.
 */
protected void setShort(Object instance, Field f, Object value) throws Exception {
    PropertyUtils.setProperty(instance, f.getFullName(), Short.valueOf(String.valueOf(value)));
}

From source file:org.specrunner.plugins.core.objects.AbstractPluginObject.java

/**
 * Sets an integer to a field.//from   w  w  w  .  j  a v a  2 s  .com
 * 
 * @param instance
 *            The object instance.
 * @param f
 *            The field information.
 * @param value
 *            The value to be set.
 * @throws Exception
 *             On setting errors.
 */
protected void setInteger(Object instance, Field f, Object value) throws Exception {
    PropertyUtils.setProperty(instance, f.getFullName(), Integer.valueOf(String.valueOf(value)));
}

From source file:org.specrunner.plugins.core.objects.AbstractPluginObject.java

/**
 * Sets a long to a field.//from   www  . j  av  a2s.com
 * 
 * @param instance
 *            The object instance.
 * @param f
 *            The field information.
 * @param value
 *            The value to be set.
 * @throws Exception
 *             On setting errors.
 */
protected void setLong(Object instance, Field f, Object value) throws Exception {
    PropertyUtils.setProperty(instance, f.getFullName(), Long.valueOf(String.valueOf(value)));
}

From source file:org.specrunner.plugins.core.objects.AbstractPluginObject.java

/**
 * Sets a float to a field.// ww  w.  jav a  2s.com
 * 
 * @param instance
 *            The object instance.
 * @param f
 *            The field information.
 * @param value
 *            The value to be set.
 * @throws Exception
 *             On setting errors.
 */
protected void setFloat(Object instance, Field f, Object value) throws Exception {
    PropertyUtils.setProperty(instance, f.getFullName(), Float.valueOf(String.valueOf(value)));
}

From source file:org.specrunner.plugins.core.objects.AbstractPluginObject.java

/**
 * Sets a double to a field.//  w  w w  .  j  a v  a 2 s .  c o m
 * 
 * @param instance
 *            The object instance.
 * @param f
 *            The field information.
 * @param value
 *            The value to be set.
 * @throws Exception
 *             On setting errors.
 */
protected void setDouble(Object instance, Field f, Object value) throws Exception {
    PropertyUtils.setProperty(instance, f.getFullName(), Double.valueOf(String.valueOf(value)));
}