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

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

Introduction

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

Prototype

public 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.

Usage

From source file:com.datatorrent.stram.moduleexperiment.InjectConfigTest.java

@Test
public void testBeanUtils() throws Exception {
    // http://www.cowtowncoder.com/blog/archives/2011/02/entry_440.html

    BeanUtilsTestBean testBean = new BeanUtilsTestBean();
    testBean.url = new URL("http://localhost:12345/context");

    ObjectMapper mapper = new ObjectMapper();
    Map<String, Object> properties = mapper.convertValue(testBean, Map.class);
    System.out.println("testBean source: " + properties);

    BeanUtilsTestBean testBean2 = new BeanUtilsTestBean();
    testBean2.string2 = "testBean2";
    Assert.assertFalse("contains transientProperty", properties.containsKey("transientProperty"));
    Assert.assertTrue("contains string2", properties.containsKey("string2"));
    properties.remove("string2"); // remove null
    //properties.put("string3", "");

    BeanUtilsBean bub = new BeanUtilsBean();
    try {//from  ww  w  . j  av  a 2 s . c  om
        bub.getProperty(testBean, "invalidProperty");
        Assert.fail("exception expected");
    } catch (Exception e) {
        Assert.assertTrue(e.getMessage().contains("Unknown property 'invalidProperty'"));
    }
    bub.setProperty(properties, "mapProperty.someKey", "someValue");

    JsonNode sourceTree = mapper.convertValue(testBean2, JsonNode.class);
    JsonNode updateTree = mapper.convertValue(properties, JsonNode.class);
    merge(sourceTree, updateTree);

    //   mapper.readerForUpdating(testBean2).readValue(sourceTree);
    //   Assert.assertEquals("preserve existing value", "testBean2", testBean2.string2);
    //   Assert.assertEquals("map property", "someValue", testBean2.mapProperty.get("someKey"));

    //   System.out.println("testBean cloned: " + mapper.convertValue(testBean2, Map.class));

    PropertyUtilsBean propertyUtilsBean = BeanUtilsBean.getInstance().getPropertyUtils();
    //PropertyDescriptor pd = propertyUtilsBean.getPropertyDescriptor(testBean2, "mapProperty.someKey2");

    // set value on non-existing property
    try {
        propertyUtilsBean.setProperty(testBean, "nonExistingProperty.someProperty", "ddd");
        Assert.fail("should throw exception");
    } catch (NoSuchMethodException e) {
        Assert.assertTrue("" + e, e.getMessage().contains("Unknown property 'nonExistingProperty'"));
    }

    // set value on read-only property
    try {
        testBean.getMapProperty().put("s", "s1Val");
        PropertyDescriptor pd = propertyUtilsBean.getPropertyDescriptor(testBean, "mapProperty");
        Class<?> type = propertyUtilsBean.getPropertyType(testBean, "mapProperty.s");

        propertyUtilsBean.setProperty(testBean, "mapProperty", Integer.valueOf(1));
        Assert.fail("should throw exception");
    } catch (Exception e) {
        Assert.assertTrue("" + e, e.getMessage().contains("Property 'mapProperty' has no setter method"));
    }

    // type mismatch
    try {
        propertyUtilsBean.setProperty(testBean, "intProp", "s1");
        Assert.fail("should throw exception");
    } catch (Exception e) {
        Assert.assertEquals(e.getClass(), IllegalArgumentException.class);
    }

    try {
        propertyUtilsBean.setProperty(testBean, "intProp", "1");
    } catch (IllegalArgumentException e) {
        // BeanUtils does not report invalid properties, but it handles type conversion, which above doesn't
        Assert.assertEquals("", 0, testBean.getIntProp());
        bub.setProperty(testBean, "intProp", "1"); // by default beanutils ignores conversion error
        Assert.assertEquals("", 1, testBean.getIntProp());
    }
}

From source file:com.linkedin.databus.core.util.ConfigLoader.java

private void fillBeanFromMap(Object bean, Map<?, ?> map)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    PropertyUtilsBean propUtils = _beanUtilsBean.getPropertyUtils();

    for (Map.Entry<?, ?> entry : map.entrySet()) {
        String keyStr = entry.getKey().toString();
        Object value = entry.getValue();
        if (value instanceof Map<?, ?>) {
            Object subBean = propUtils.getProperty(bean, keyStr);
            if (null != subBean)
                fillBeanFromMap(subBean, (Map<?, ?>) value);
        } else if (value instanceof List<?>) {
            fillPropertyFromList(bean, keyStr, (List<?>) value);
        } else {/* w ww . j  a  v  a2 s . c om*/
            propUtils.setProperty(bean, keyStr, value);
        }
    }
}

From source file:org.opencms.ui.dialogs.CmsUserDataDialog.java

/**
 * Submits the dialog.<p>//from   w ww. j  a  va2s . c  om
 */
void submit() {

    try {
        // Special user info attributes may have been set since the time the dialog was instantiated,
        // and we don't want to overwrite them, so we read the user again.
        m_user = m_context.getCms().readUser(m_user.getId());
        if (isValid()) {
            m_binder.commit();
            PropertyUtilsBean propUtils = new PropertyUtilsBean();
            for (CmsAccountInfo info : OpenCms.getWorkplaceManager().getAccountInfos()) {
                if (info.isEditable()) {
                    if (info.isAdditionalInfo()) {
                        m_user.setAdditionalInfo(info.getAddInfoKey(),
                                m_infos.getItemProperty(info).getValue());
                    } else {
                        propUtils.setProperty(m_user, info.getField().name(),
                                m_infos.getItemProperty(info).getValue());
                    }
                }
            }
            m_context.getCms().writeUser(m_user);
            m_context.finish(Collections.<CmsUUID>emptyList());
            m_context.updateUserInfo();
        }
    } catch (Exception e) {
        m_context.error(e);
    }
}