Example usage for org.apache.commons.beanutils WrapDynaBean WrapDynaBean

List of usage examples for org.apache.commons.beanutils WrapDynaBean WrapDynaBean

Introduction

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

Prototype

public WrapDynaBean(Object instance) 

Source Link

Document

Construct a new DynaBean associated with the specified JavaBean instance.

Usage

From source file:es.caib.zkib.jxpath.ri.model.dynabeans.DynaBeanModelTest.java

protected Object createContextBean() {
    return new WrapDynaBean(new TestBean());
}

From source file:net.sf.morph.reflect.reflectors.DynaBeanReflectorTestCase.java

protected List createReflectableObjects() {
    List list = new ArrayList();
    list.add(new WrapDynaBean(new Object()));
    list.add(new WrapDynaBean(TestClass.getEmptyObject()));
    list.add(new WrapDynaBean(TestClass.getPartialObject()));
    list.add(new WrapDynaBean(TestClass.getFullObject()));
    return list;//w ww .ja va2 s. co m
}

From source file:io.cloudex.framework.utils.ObjectUtils.java

/**
 * Wrapper for BeanUtils.populate, validates that bean has all the properties referenced in
 * the properties map/*from ww w .j a  v a  2  s  . c o  m*/
 * @param bean - the bean to populate
 * @param properties - a map of key value pairs used to populate bean
 * @param validate - validate that all provided properties are in bean
 * @throws InstancePopulationException if the population of the bean fails
 */
public static void populate(Object bean, Map<String, ? extends Object> properties, boolean validate)
        throws InstancePopulationException {

    try {

        if (validate) {
            DynaBean dynaBean = new WrapDynaBean(bean);

            // ensure all the properties in the map are present
            for (String property : properties.keySet()) {
                dynaBean.get(property);
                // if no present it will throw IllegalArgumentException
            }
        }

        BeanUtils.populate(bean, properties);

    } catch (Exception e) {
        log.error("Failed to populate instance: " + bean + ", from map: " + properties, e);
        throw new InstancePopulationException("Unable to populate instance: " + bean, e);
    }
}

From source file:com.qagen.osfe.core.utils.BeanPopulator.java

public static void populateBean(List<RowValue> rowValues, Object bean) {
    final WrapDynaBean wrapDynaBean = new WrapDynaBean(bean);

    for (RowValue rowValue : rowValues) {
        final String name = rowValue.getDescription().getName();
        final String value = rowValue.getValue();
        final String type = rowValue.getDescription().getType();
        final String format = rowValue.getDescription().getFormat();
        wrapDynaBean.set(name, getValueAsType(name, value, type, format));
    }//from   w ww  .j a v  a2 s.c o  m
}

From source file:com.expressui.core.util.ReflectionUtil.java

/**
 * Asks if the bean's properties are empty. boolean properties that are false and numbers
 * that are zero are considered empty. String values that are zero-length are considered empty.
 * All other property types must be null to be considered empty.
 *
 * @param bean bean to check//from  w w w .  j  a  va  2s .c  o  m
 * @return true if bean has no values
 */
public static boolean isBeanEmpty(Object bean) {
    if (bean == null) {
        return true;
    }

    WrapDynaBean wrapDynaBean = new WrapDynaBean(bean);
    DynaProperty[] properties = wrapDynaBean.getDynaClass().getDynaProperties();
    for (DynaProperty property : properties) {
        String propertyName = property.getName();
        Class propertyType = property.getType();

        Object value = wrapDynaBean.get(propertyName);
        if (propertyType.isPrimitive()) {
            if (value instanceof Number && !value.toString().equals("0")) {
                return false;
            } else if (value instanceof Boolean && !value.toString().equals("false")) {
                return false;
            } else if (!value.toString().isEmpty()) {
                return false;
            }
        } else if (value != null) {
            if (!(value instanceof Collection)) {
                String convertedStringValue = ConvertUtils.convert(value);
                if (!StringUtil.isEmpty(convertedStringValue)) {
                    return false;
                }
            }
        }
    }

    return true;
}

From source file:com.expressui.core.util.ReflectionUtil.java

/**
 * Finds all properties in the bean that are complex, that is not BeanUtils.isSimpleValueType
 *
 * @param bean bean to reflectively analyze
 * @return collection of properties on the bean
 *///from  www.ja  va  2 s .co m
public static Collection<String> findComplexProperties(Object bean) {
    Collection<String> complexProperties = new ArrayList<String>();

    WrapDynaBean wrapDynaBean = new WrapDynaBean(bean);
    DynaProperty[] properties = wrapDynaBean.getDynaClass().getDynaProperties();
    for (DynaProperty property : properties) {
        String propertyName = property.getName();
        Class propertyType = property.getType();
        if (!BeanUtils.isSimpleValueType(propertyType)) {
            complexProperties.add(propertyName);
        }
    }

    return complexProperties;
}

From source file:jp.terasoluna.fw.beans.JXPathIndexedBeanWrapperImplTest01.java

/**
 * testGetIndexedPropertyValuesJavaBean01() <br>
 * <br>// w  ww  . j av  a  2 s  . c o  m
 * () <br>
 * A <br>
 * <br>
 * () propertyName:"foo.bar.hoge"<br>
 * () this.context:DynaBean<br>
 * ??=<br>
 * foo.bar.hoge="test"<br>
 * ?<br>
 * <br>
 * () Map<String, Object>:Map<String, Object><br>
 * foo.bar.hoge="test"<br>
 * <br>
 * ?DynaBean?<br>
 * ????? <br>
 * @throws Exception ?????
 */
@Test
public void testGetIndexedPropertyValuesDynaBean01() throws Exception {
    // ??
    JXPathIndexedBeanWrapperImpl_JavaBeanStub03 bean = new JXPathIndexedBeanWrapperImpl_JavaBeanStub03();
    // foo
    JXPathIndexedBeanWrapperImpl_JavaBeanStub03.Foo foo = new JXPathIndexedBeanWrapperImpl_JavaBeanStub03.Foo();

    // bar
    JXPathIndexedBeanWrapperImpl_JavaBeanStub03.Bar bar = new JXPathIndexedBeanWrapperImpl_JavaBeanStub03.Bar();
    // hoge
    bar.setHoge("test");

    // foo.bar.hoge
    foo.setBar(bar);
    bean.setFoo(foo);

    WrapDynaBean dynaBean = new WrapDynaBean(bean);

    JXPathIndexedBeanWrapperImpl bw = new JXPathIndexedBeanWrapperImpl(dynaBean);

    // 
    Map<String, Object> result = bw.getIndexedPropertyValues("foo.bar.hoge");

    // 
    assertEquals(1, result.size());
    assertTrue(result.containsKey("foo.bar.hoge"));
    assertEquals("test", result.get("foo.bar.hoge"));
    assertEquals("test", PropertyUtils.getProperty(dynaBean, "foo.bar.hoge"));
    assertEquals("test", BeanUtils.getProperty(dynaBean, "foo.bar.hoge"));
}

From source file:jp.terasoluna.fw.beans.JXPathIndexedBeanWrapperImplTest01.java

/**
 * testGetIndexedPropertyValuesJavaBean02() <br>
 * <br>//from w  w  w.j ava 2s.  c  om
 * () <br>
 * A <br>
 * <br>
 * () propertyName:"foo.bar.hoge"<br>
 * () this.context:DynaBean<br>
 * ??=<br>
 * foo.bar[0].hoge[0]="test0"<br>
 * foo.bar[1]=null<br>
 * foo.bar[2].hoge[0]="test2"<br>
 * foo.bar[2].hoge[1]="test3"<br>
 * foo.bar[2].hoge[2]="test4"<br>
 * ?<br>
 * <br>
 * () Map<String, Object>:Map<String, Object><br>
 * foo.bar[0].hoge[0]="test0"<br>
 * foo.bar[2].hoge[0]="test2"<br>
 * foo.bar[2].hoge[1]="test3"<br>
 * foo.bar[2].hoge[2]="test4"<br>
 * <br>
 * ?DynaBean?<br>
 * ??null????? <br>
 * @throws Exception ?????
 */
@Test
public void testGetIndexedPropertyValuesDynaBean02() throws Exception {
    // ??
    JXPathIndexedBeanWrapperImpl_JavaBeanStub05 bean = new JXPathIndexedBeanWrapperImpl_JavaBeanStub05();

    // foo
    JXPathIndexedBeanWrapperImpl_JavaBeanStub05.Foo foo = new JXPathIndexedBeanWrapperImpl_JavaBeanStub05.Foo();
    bean.setFoo(foo);

    // foo.bar[0]
    JXPathIndexedBeanWrapperImpl_JavaBeanStub05.Bar bar0 = new JXPathIndexedBeanWrapperImpl_JavaBeanStub05.Bar();
    // foo.bar[1]
    JXPathIndexedBeanWrapperImpl_JavaBeanStub05.Bar bar1 = null;
    // foo.bar[2]
    JXPathIndexedBeanWrapperImpl_JavaBeanStub05.Bar bar2 = new JXPathIndexedBeanWrapperImpl_JavaBeanStub05.Bar();
    // foo.bar[]
    List<JXPathIndexedBeanWrapperImpl_JavaBeanStub05.Bar> barList = new ArrayList<JXPathIndexedBeanWrapperImpl_JavaBeanStub05.Bar>();
    // foo.bar[0]
    barList.add(bar0);
    // foo.bar[1]
    barList.add(bar1);
    // foo.bar[2]
    barList.add(bar2);

    // foo.bar
    foo.setBar(barList);

    // foo.bar[0].hoge[0]="test0"
    List<String> hoge0 = new ArrayList<String>();
    hoge0.add("test0");
    bar0.setHoge(hoge0);

    // foo.bar[1]=null

    // foo.bar[2].hoge[0]="test2"
    // foo.bar[2].hoge[1]="test3"
    // foo.bar[2].hoge[2]="test4"
    List<String> hoge2 = new ArrayList<String>();
    hoge2.add("test2");
    hoge2.add("test3");
    hoge2.add("test4");
    bar2.setHoge(hoge2);

    WrapDynaBean dynaBean = new WrapDynaBean(bean);

    JXPathIndexedBeanWrapperImpl bw = new JXPathIndexedBeanWrapperImpl(dynaBean);

    // 
    Map<String, Object> result = bw.getIndexedPropertyValues("foo.bar.hoge");

    // 
    assertEquals(4, result.size());
    assertEquals("test0", result.get("foo.bar[0].hoge[0]"));
    assertEquals("test2", result.get("foo.bar[2].hoge[0]"));
    assertEquals("test3", result.get("foo.bar[2].hoge[1]"));
    assertEquals("test4", result.get("foo.bar[2].hoge[2]"));
    assertEquals("test0", PropertyUtils.getProperty(dynaBean, "foo.bar[0].hoge[0]"));
    assertEquals("test0", BeanUtils.getProperty(dynaBean, "foo.bar[0].hoge[0]"));
}

From source file:org.apache.struts.validator.BeanValidatorForm.java

/**
 * Construct a new <code>BeanValidatorForm</code> with the specified
 * bean./*  w w w  .  jav  a2  s  .c  om*/
 */
public BeanValidatorForm(Object bean) {
    if (bean instanceof DynaBean) {
        dynaBean = (DynaBean) bean;
    } else {
        dynaBean = new WrapDynaBean(bean);
    }
}

From source file:org.ejbca.ui.cli.FieldEditor.java

/** Lists methods in a class the has "setXyz", and prints them as "Xyz".
 * Ignores (does not list) type, version, latestVersion, upgrade and class
 * //from w  w  w .  j  av  a2s  .c  o m
 * @param obj the Object where to look for setMethods
 */
public void listSetMethods(final Object obj) {
    DynaBean wrapper = new WrapDynaBean(obj);
    DynaProperty[] props = wrapper.getDynaClass().getDynaProperties();
    for (DynaProperty dynaProperty : props) {
        if (!excluded.contains(dynaProperty.getName())) {
            logger.info(dynaProperty.getName() + ", " + dynaProperty.getType());
        }
    }
}