Search in sources :

Example 1 with DynaProperty

use of org.apache.commons.beanutils.DynaProperty in project sonarqube by SonarSource.

the class DynaActionForm method set.

/**
     * <p>Set the value of a simple property with the specified name.</p>
     *
     * @param name  Name of the property whose value is to be set
     * @param value Value to which this property is to be set
     * @throws ConversionException      if the specified value cannot be
     *                                  converted to the type required for
     *                                  this property
     * @throws IllegalArgumentException if there is no property of the
     *                                  specified name
     * @throws NullPointerException     if the type specified for the property
     *                                  is invalid
     * @throws NullPointerException     if an attempt is made to set a
     *                                  primitive property to null
     */
public void set(String name, Object value) {
    DynaProperty descriptor = getDynaProperty(name);
    if (descriptor.getType() == null) {
        throw new NullPointerException("The type for property " + name + " is invalid");
    }
    if (value == null) {
        if (descriptor.getType().isPrimitive()) {
            throw new NullPointerException("Primitive value for '" + name + "'");
        }
    } else if (!isDynaAssignable(descriptor.getType(), value.getClass())) {
        throw new ConversionException("Cannot assign value of type '" + value.getClass().getName() + "' to property '" + name + "' of type '" + descriptor.getType().getName() + "'");
    }
    dynaValues.put(name, value);
}
Also used : DynaProperty(org.apache.commons.beanutils.DynaProperty) ConversionException(org.apache.commons.beanutils.ConversionException)

Example 2 with DynaProperty

use of org.apache.commons.beanutils.DynaProperty in project sonarqube by SonarSource.

the class DynaActionFormConfig method testGetDescriptorArguments.

// --------------------------------------- Tests from BasicDynaBeanTestCase
/**
     * Corner cases on getDynaProperty invalid arguments.
     */
public void testGetDescriptorArguments() {
    DynaProperty descriptor = dynaForm.getDynaClass().getDynaProperty("unknown");
    assertNull("Unknown property descriptor should be null", descriptor);
    try {
        dynaForm.getDynaClass().getDynaProperty(null);
        fail("Should throw IllegalArgumentException");
    } catch (IllegalArgumentException e) {
        // Expected response
        ;
    }
}
Also used : DynaProperty(org.apache.commons.beanutils.DynaProperty)

Example 3 with DynaProperty

use of org.apache.commons.beanutils.DynaProperty in project sonarqube by SonarSource.

the class TestDynaActionFormClass method testClassCreate.

// --------------------------------------------- Create DynaActionFormClass
// Test basic DynaActionFormClass name and properties
public void testClassCreate() {
    assertEquals("name", "dynaForm", dynaClass.getName());
    for (int i = 0; i < dynaProperties.length; i++) {
        DynaProperty prop = dynaClass.getDynaProperty(dynaProperties[i].getName());
        assertNotNull("Found property " + dynaProperties[i].getName());
        assertEquals("Class for property " + dynaProperties[i].getName(), dynaProperties[i].getTypeClass(), prop.getType());
    }
}
Also used : DynaProperty(org.apache.commons.beanutils.DynaProperty)

Example 4 with DynaProperty

use of org.apache.commons.beanutils.DynaProperty in project sonarqube by SonarSource.

the class DynaActionForm method toString.

// --------------------------------------------------------- Public Methods
/**
     * <p>Render a String representation of this object.</p>
     *
     * @return A string representation of this object.
     */
public String toString() {
    StringBuffer sb = new StringBuffer("DynaActionForm[dynaClass=");
    DynaClass dynaClass = getDynaClass();
    if (dynaClass == null) {
        return sb.append("null]").toString();
    }
    sb.append(dynaClass.getName());
    DynaProperty[] props = dynaClass.getDynaProperties();
    if (props == null) {
        props = new DynaProperty[0];
    }
    for (int i = 0; i < props.length; i++) {
        sb.append(',');
        sb.append(props[i].getName());
        sb.append('=');
        Object value = get(props[i].getName());
        if (value == null) {
            sb.append("<NULL>");
        } else if (value.getClass().isArray()) {
            int n = Array.getLength(value);
            sb.append("{");
            for (int j = 0; j < n; j++) {
                if (j > 0) {
                    sb.append(',');
                }
                sb.append(Array.get(value, j));
            }
            sb.append("}");
        } else if (value instanceof List) {
            int n = ((List) value).size();
            sb.append("{");
            for (int j = 0; j < n; j++) {
                if (j > 0) {
                    sb.append(',');
                }
                sb.append(((List) value).get(j));
            }
            sb.append("}");
        } else if (value instanceof Map) {
            int n = 0;
            Iterator keys = ((Map) value).keySet().iterator();
            sb.append("{");
            while (keys.hasNext()) {
                if (n > 0) {
                    sb.append(',');
                }
                n++;
                Object key = keys.next();
                sb.append(key);
                sb.append('=');
                sb.append(((Map) value).get(key));
            }
            sb.append("}");
        } else {
            sb.append(value);
        }
    }
    sb.append("]");
    return (sb.toString());
}
Also used : DynaProperty(org.apache.commons.beanutils.DynaProperty) DynaClass(org.apache.commons.beanutils.DynaClass) Iterator(java.util.Iterator) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with DynaProperty

use of org.apache.commons.beanutils.DynaProperty in project sonarqube by SonarSource.

the class DynaActionFormClass method introspect.

/**
     * <p>Introspect our form bean configuration to identify the supported
     * properties.</p>
     *
     * @param config The FormBeanConfig instance describing the properties of
     *               the bean to be created
     * @throws IllegalArgumentException if the bean implementation class
     *                                  specified in the configuration is not
     *                                  DynaActionForm (or a subclass of
     *                                  DynaActionForm)
     */
protected void introspect(FormBeanConfig config) {
    this.config = config;
    // Validate the ActionFormBean implementation class
    try {
        beanClass = RequestUtils.applicationClass(config.getType());
    } catch (Throwable t) {
        throw new IllegalArgumentException("Cannot instantiate ActionFormBean class '" + config.getType() + "': " + t);
    }
    if (!DynaActionForm.class.isAssignableFrom(beanClass)) {
        throw new IllegalArgumentException("Class '" + config.getType() + "' is not a subclass of " + "'org.apache.struts.action.DynaActionForm'");
    }
    // Set the name we will know ourselves by from the form bean name
    this.name = config.getName();
    // Look up the property descriptors for this bean class
    FormPropertyConfig[] descriptors = config.findFormPropertyConfigs();
    if (descriptors == null) {
        descriptors = new FormPropertyConfig[0];
    }
    // Create corresponding dynamic property definitions
    properties = new DynaProperty[descriptors.length];
    for (int i = 0; i < descriptors.length; i++) {
        properties[i] = new DynaProperty(descriptors[i].getName(), descriptors[i].getTypeClass());
        propertiesMap.put(properties[i].getName(), properties[i]);
    }
}
Also used : DynaProperty(org.apache.commons.beanutils.DynaProperty) FormPropertyConfig(org.apache.struts.config.FormPropertyConfig)

Aggregations

DynaProperty (org.apache.commons.beanutils.DynaProperty)6 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 ConversionException (org.apache.commons.beanutils.ConversionException)1 DynaClass (org.apache.commons.beanutils.DynaClass)1 FormPropertyConfig (org.apache.struts.config.FormPropertyConfig)1