Example usage for org.apache.commons.beanutils MutableDynaClass add

List of usage examples for org.apache.commons.beanutils MutableDynaClass add

Introduction

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

Prototype

public void add(String name, Class type);

Source Link

Document

Add a new dynamic property with the specified data type, but with no restrictions on readability or writeability.

Usage

From source file:io.coala.experimental.dynabean.DynaBeanTest.java

@Test
public void testDynaBeans() throws Exception {
    // for usage, see
    // http://commons.apache.org/proper/commons-beanutils/javadocs/v1.9.2/apidocs/org/apache/commons/beanutils/package-summary.html#package_description

    final DynaBean dynaBean = new LazyDynaBean(); // Create LazyDynaBean
    final MutableDynaClass dynaClass = (MutableDynaClass) dynaBean.getDynaClass(); // get DynaClass

    dynaClass.add("amount", java.lang.Integer.class); // add property
    dynaClass.add("myMap", java.util.TreeMap.class); // add mapped property

    final DynaBean employee = dynaClass.newInstance();

    // TODO experiment with Jackson's AnnotationIntrospector to annotate
    // DynaBean#get(...) method with @JsonAnyGetter and #set(...) method
    // with @JsonAnySetter

    employee.set("address", new HashMap<String, String>());
    employee.set("activeEmployee", Boolean.FALSE);
    employee.set("firstName", "Fred");
    employee.set("lastName", "Flintstone");

    LOG.trace("Employee: " + JsonUtil.toPrettyJSON(employee));

    // set all <activeEmployee> attribute values to <true>
    final BeanPropertyValueChangeClosure closure = new BeanPropertyValueChangeClosure("activeEmployee",
            Boolean.TRUE);//from   w ww  .  j  a v a2 s .c om

    final Collection<?> employees = Collections.singleton(employee);
    LOG.trace("Mutated employees: " + JsonUtil.toPrettyJSON(employees));

    // update the Collection
    CollectionUtils.forAllDo(employees, closure);

    // filter for beans with <activeEmployee> set to <false>
    final BeanPropertyValueEqualsPredicate predicate = new BeanPropertyValueEqualsPredicate("lastName",
            "Flintstone");

    // filter the Collection
    CollectionUtils.filter(employees, predicate);
    LOG.trace("Filtered employees: " + JsonUtil.toPrettyJSON(employees));
}

From source file:org.apache.struts.config.FormBeanConfig.java

/**
 * <p>Create and return an <code>ActionForm</code> instance appropriate to
 * the information in this <code>FormBeanConfig</code>.</p>
 *
 * <p>Although this method is not formally deprecated yet, where possible,
 * the form which accepts an <code>ActionContext</code> as an argument is
 * preferred, to help sever direct dependencies on the Servlet API.  As
 * the ActionContext becomes more familiar in Struts, this method will
 * almost certainly be deprecated.</p>
 *
 * @param servlet The action servlet//from  ww  w.j  a  v  a  2s  .  c om
 * @return ActionForm instance
 * @throws IllegalAccessException if the Class or the appropriate
 *                                constructor is not accessible
 * @throws InstantiationException if this Class represents an abstract
 *                                class, an array class, a primitive type,
 *                                or void; or if instantiation fails for
 *                                some other reason
 */
public ActionForm createActionForm(ActionServlet servlet)
        throws IllegalAccessException, InstantiationException {
    Object obj = null;

    // Create a new form bean instance
    if (getDynamic()) {
        obj = getDynaActionFormClass().newInstance();
    } else {
        obj = formBeanClass().newInstance();
    }

    ActionForm form = null;

    if (obj instanceof ActionForm) {
        form = (ActionForm) obj;
    } else {
        form = new BeanValidatorForm(obj);
    }

    form.setServlet(servlet);

    if (form instanceof DynaBean && ((DynaBean) form).getDynaClass() instanceof MutableDynaClass) {
        DynaBean dynaBean = (DynaBean) form;
        MutableDynaClass dynaClass = (MutableDynaClass) dynaBean.getDynaClass();

        // Add properties
        dynaClass.setRestricted(false);

        FormPropertyConfig[] props = findFormPropertyConfigs();

        for (int i = 0; i < props.length; i++) {
            dynaClass.add(props[i].getName(), props[i].getTypeClass());
            dynaBean.set(props[i].getName(), props[i].initial());
        }

        dynaClass.setRestricted(isRestricted());
    }

    if (form instanceof BeanValidatorForm) {
        ((BeanValidatorForm) form).initialize(this);
    }

    return form;
}