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:com.mulesoft.mql.impl.SelectEvaluator.java

private Object transformToPojo(String clsName, Map<String, Object> vars) {
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    try {/*from  www .  j a va 2 s  . c om*/
        Class<?> cls = cl.loadClass(clsName);
        Constructor<?> constructor;
        try {
            constructor = cls.getConstructor();
        } catch (NoSuchMethodException e) {
            throw new QueryException(
                    MessageFormat.format("Class {0} did not have an empty constructor.", clsName), e);
        }

        Object t = constructor.newInstance();
        for (Map.Entry<String, Serializable> e : compiledExpressions.entrySet()) {
            PropertyUtils.setProperty(t, e.getKey(), MVEL.executeExpression(e.getValue(), vars));
        }
        return t;
    } catch (ClassNotFoundException e1) {
        throw new QueryException(MessageFormat.format("Select class {0} was not found.", clsName), e1);
    } catch (Exception e) {
        throw new QueryException(e);
    }
}

From source file:egovframework.com.ext.ldapumt.service.impl.ObjectMapper.java

/**
 * ContextAdapter?  ? vo .//w  w  w.  j av a  2s.  co m
 */
public Object mapFromContext(Object arg0) throws NamingException {
    DirContextAdapter adapter = (DirContextAdapter) arg0;
    Attributes attrs = adapter.getAttributes();

    LdapObject vo = null;

    try {
        vo = (LdapObject) type.newInstance();
    } catch (Exception e2) {
        throw new RuntimeException(e2);
    }

    vo.setDn(adapter.getDn().toString());

    BeanInfo beanInfo;
    try {
        beanInfo = Introspector.getBeanInfo(type);
    } catch (IntrospectionException e1) {
        throw new RuntimeException(e1);
    }

    PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();

    for (PropertyDescriptor descriptor : propertyDescriptors) {
        if (attrs.get(descriptor.getName()) != null)
            try {
                Class<?> o = descriptor.getPropertyType();
                if (o == int.class)
                    PropertyUtils.setProperty(vo, descriptor.getName(),
                            Integer.valueOf((String) attrs.get(descriptor.getName()).get()));
                if (o == String.class)
                    PropertyUtils.setProperty(vo, descriptor.getName(),
                            (String) attrs.get(descriptor.getName()).get());
                if (o == Boolean.class)
                    PropertyUtils.setProperty(vo, descriptor.getName(),
                            ((String) attrs.get(descriptor.getName()).get()).equals("Y"));

            } catch (Exception e) {
                throw new RuntimeException(e);
            }
    }

    return vo;
}

From source file:edu.umn.msi.tropix.common.test.BeanTest.java

public static void testBeanProperties(final Object testBean, final HashMultimap<Class<?>, Object> typeObjects) {
    try {/*from  ww w.  j  a  va 2  s  .c o m*/
        @SuppressWarnings("unchecked")
        final Map<String, ?> propertyMap = PropertyUtils.describe(testBean);
        for (final String propertyName : propertyMap.keySet()) {
            if (propertyName.equals("class") || !PropertyUtils.isWriteable(testBean, propertyName)) {
                continue;
            }
            final Class<?> type = PropertyUtils.getPropertyType(testBean, propertyName);
            Collection<?> objects = null;
            for (final Class<?> typeQuery : typeObjects.keySet()) {
                if (typeQuery.isAssignableFrom(type)) {
                    objects = typeObjects.get(typeQuery);
                }
            }
            boolean useEquals = true;
            if (objects == null) {
                useEquals = false;
                try {
                    objects = Lists.<Object>newArrayList(EasyMock.createMock(type));
                } catch (final Exception e) {
                    // Cannot instantiate mock of this type
                    continue;
                }
            }
            for (final Object expectedObject : objects) {
                PropertyUtils.setProperty(testBean, propertyName, expectedObject);
                final Object object = PropertyUtils.getProperty(testBean, propertyName);
                if (useEquals) {
                    assert object.equals(expectedObject) : "Expected " + expectedObject + " obtained " + object;
                } else {
                    assert object == expectedObject : "Expected " + expectedObject + " obtained " + object;
                }
            }
        }
    } catch (final Exception e) {
        throw new IllegalStateException(e);
    }
}

From source file:javax.faces.component.AbstractUIComponentPropertyTest.java

@Test
public void testExplicitValue() throws Exception {
    for (T testValue : _testValues) {
        PropertyUtils.setProperty(_component, _property, testValue);
        Assert.assertEquals(testValue, PropertyUtils.getProperty(_component, _property));
    }/*from w  w  w .ja va2  s .co  m*/
}

From source file:de.awtools.bean.DefaultPropertyMapper.java

/**
 * Eventuelle Exceptions, die bei der Injektion auftreten, werden in eine
 * <code>RuntimeException</code> umgewandelt.
 *
 * @see Mapper#setProperty(java.lang.Object, java.lang.Object)
 *//*w ww . j a  va2s .  c o m*/
public void setProperty(final Object domainObject, final Object value) {
    try {
        PropertyUtils.setProperty(domainObject, property, value);
    } catch (IllegalAccessException ex) {
        throw new RuntimeException(ex);
    } catch (InvocationTargetException ex) {
        throw new RuntimeException(ex);
    } catch (NoSuchMethodException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:net.sf.launch4j.binding.OptListBinding.java

public void get(IValidatable bean) {
    try {//from   w w w. j  a  v a  2  s .  c o m
        String[] items = _textArea.getText().split("\n");
        PropertyUtils.setProperty(bean, _property, _button.isSelected() ? items : null);
    } catch (Exception e) {
        throw new BindingException(e);
    }
}

From source file:ca.sqlpower.testutil.TestUtils.java

/**
 * Sets all the settable properties on the given target object which are not
 * in the given ignore set./*w  ww  . java2  s  .  c o m*/
 * <p>
 * TODO merge this with what is in Architect's TestUtils class. This was
 * originally refactored out of there.
 * 
 * @param target
 *            The object to change the properties of
 * @param propertiesToIgnore
 *            The properties of target not to modify or read
 * @return A Map describing the new values of all the non-ignored, readable
 *         properties in target.
 */
public static Map<String, Object> setAllInterestingProperties(Object target, Set<String> propertiesToIgnore,
        NewValueMaker valueMaker) throws Exception {

    PropertyDescriptor props[] = PropertyUtils.getPropertyDescriptors(target);
    for (int i = 0; i < props.length; i++) {
        Object oldVal = null;
        if (PropertyUtils.isReadable(target, props[i].getName()) && props[i].getReadMethod() != null
                && !propertiesToIgnore.contains(props[i].getName())) {
            oldVal = PropertyUtils.getProperty(target, props[i].getName());
        }
        if (PropertyUtils.isWriteable(target, props[i].getName()) && props[i].getWriteMethod() != null
                && !propertiesToIgnore.contains(props[i].getName())) {

            Object newVal = valueMaker.makeNewValue(props[i].getPropertyType(), oldVal, props[i].getName());

            System.out.println("Changing property \"" + props[i].getName() + "\" to \"" + newVal + "\"");
            PropertyUtils.setProperty(target, props[i].getName(), newVal);

        }
    }

    // read them all back at the end in case there were dependencies between properties
    return TestUtils.getAllInterestingProperties(target, propertiesToIgnore);
}

From source file:com.qumoon.commons.BeanMapper.java

public static <T> T convertProto2Bean(com.google.protobuf.GeneratedMessage message, T descObject,
        Class srcClass) {/*from www  . ja va 2 s  . co m*/
    for (Field srcField : srcClass.getDeclaredFields()) {
        Descriptors.FieldDescriptor fd = message.getDescriptorForType().findFieldByName(srcField.getName());
        if (null == fd) {
            continue;
        }
        try {
            String fieldStrValue = String.valueOf(message.getField(fd));
            if (fieldStrValue.isEmpty()) {
                continue;
            }
            if (srcField.getType() == BigDecimal.class) {
                PropertyUtils.setProperty(descObject, srcField.getName(), new BigDecimal(fieldStrValue));
            } else {
                if (srcField.getType() == Date.class) {
                    PropertyUtils.setProperty(descObject, srcField.getName(),
                            new Date((Long) (message.getField(fd))));
                } else {
                    if (srcField.getType() == Byte.class) {
                        PropertyUtils.setProperty(descObject, srcField.getName(), Byte.valueOf(fieldStrValue));
                    } else {
                        PropertyUtils.setProperty(descObject, srcField.getName(), message.getField(fd));
                    }
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        } finally {
            continue;
        }
    }
    return descObject;
}

From source file:com.antonjohansson.lprs.controller.token.TokenSenderProvider.java

private void setProperties(ITokenSender instance) {
    configuration.getSubProperties("token-sender.").stream().forEach(entry -> {
        try {// w ww . jav  a  2s  . c  om
            PropertyUtils.setProperty(instance, entry.getKey(), entry.getValue());
        } catch (Exception e) {
            LOG.error("Exception occurred when setting property '" + entry.getKey()
                    + "' on the ITokenSender implementation", e);
        }
    });
}

From source file:de.topicmapslab.kuria.runtime.table.ColumnBinding.java

/**
 *  {@inheritDoc}//from www  .j  a  v a 2s  . c  o  m
 */
public void setValue(Object instance, Object value)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    PropertyUtils.setProperty(instance, fieldName, value);
}