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

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

Introduction

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

Prototype

public PropertyDescriptor getPropertyDescriptor(Object bean, String name)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException 

Source Link

Document

Retrieve the property descriptor for the specified property of the specified bean, or return null if there is no such descriptor.

Usage

From source file:it.geosolutions.unredd.onlinestats.ppio.JAXBStatisticConfigurationPPIO.java

private Object populate(Map m, Class c) throws InstantiationException, IllegalAccessException,
        InvocationTargetException, NoSuchMethodException {
    Object o = c.newInstance();/*from   w ww  .j a  v a 2 s. c o  m*/
    for (Object eo : m.entrySet()) {
        Entry e = (Entry) eo;
        Object v = e.getValue();
        Class<?> ec = v.getClass();
        if (Map.class.isAssignableFrom(ec)) {
            PropertyUtilsBean pub = new PropertyUtilsBean();
            String ek = (String) e.getKey();
            PropertyDescriptor pd = pub.getPropertyDescriptor(o, ek);
            Class pt = pd.getPropertyType();
            if (List.class.isAssignableFrom(pt)) {
                Class k = Object.class;
                if (ek.equals("classifications")) {
                    k = ClassificationLayer.class;
                    //} else if (ek.equals("topics")) {
                    //   k = String.class;
                    //} else if (ek.equals("stats")) {
                    //   k = StatsType.class;
                }
                List l = new ArrayList();
                // This is the point where I give up.
                Object ojeto = populate((Map) e.getValue(), k);
                l.add(ojeto);
                m.put(ek, l);
            } else {
                Object p = populate((Map) e.getValue(), pt);
                m.put(ek, p);
            }
        }
    }
    BeanUtils.populate(o, m);
    return o;
}

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 {// w w  w  . j  a  va  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:org.apache.empire.spring.EmpireRecord.java

@Override
protected void setBeanValue(Object bean, String property, Column column) {
    try {/*from  ww  w  .j av a  2s .c  o  m*/
        // Get descriptor
        Object value;
        PropertyUtilsBean pub = BeanUtilsBean.getInstance().getPropertyUtils();
        PropertyDescriptor descriptor = pub.getPropertyDescriptor(bean, property);
        if (descriptor == null) {
            return; // Skip this property setter
        }

        // Get Property Value
        value = pub.getSimpleProperty(bean, property);

        // Check enum
        if (value instanceof Enum<?>)
            value = ((Enum<?>) value).name();

        // Set the record value
        setValue(column, value);

    } catch (IllegalAccessException e) {
        log.error(bean.getClass().getName() + ": unable to get property '" + property + "'");
        throw new BeanPropertyGetException(bean, property, e);
    } catch (NoSuchMethodException e) {
        log.error(bean.getClass().getName() + ": unable to get property '" + property + "'");
        throw new BeanPropertyGetException(bean, property, e);
    } catch (InvocationTargetException e) {
        log.error(bean.getClass().getName() + ": unable to get property '" + property + "'");
        throw new BeanPropertyGetException(bean, property, e);
    }
}