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);
}
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
;
}
}
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());
}
}
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());
}
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]);
}
}
Aggregations