use of org.apache.commons.beanutils.ConversionException 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.ConversionException in project alfresco-remote-api by Alfresco.
the class MapBasedQueryWalker method getProperty.
/**
* Get the property value, converted to the requested type.
*
* @param propertyName name of the parameter
* @param type int
* @param returnType type of object to return
* @return the converted parameter value. Null, if the property has no
* value.
* @throws IllegalArgumentException when no conversion for the given
* returnType is available or if returnType is null.
* @throws InvalidArgumentException when conversion to the given type was
* not possible due to an error while converting
*/
@SuppressWarnings("unchecked")
public <T extends Object> T getProperty(String propertyName, int type, Class<T> returnType) {
if (returnType == null) {
throw new IllegalArgumentException("ReturnType cannot be null");
}
try {
Object result = null;
String stringValue = getProperty(propertyName, type);
if (stringValue != null) {
result = ConvertUtils.convert(stringValue, returnType);
if ((result instanceof String) && (!returnType.equals(String.class))) {
// If a string is returned, no converter has been found (for non-String return type)
throw new IllegalArgumentException("Unable to convert parameter to type: " + returnType.getName());
}
}
return (T) result;
} catch (ConversionException ce) {
// Conversion failed, wrap in Illegal
throw new InvalidArgumentException("Query property value for '" + propertyName + "' should be a valid " + returnType.getSimpleName());
}
}
use of org.apache.commons.beanutils.ConversionException in project sonar-java 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.ConversionException in project contribution by checkstyle.
the class AbstractUsageCheck method setIgnoreFormat.
/**
* Set the ignore format to the specified regular expression.
* @param aFormat a <code>String</code> value
* @throws ConversionException unable to parse aFormat
*/
public void setIgnoreFormat(String aFormat) throws ConversionException {
try {
mRegexp = Utils.getPattern(aFormat);
mIgnoreFormat = aFormat;
} catch (PatternSyntaxException e) {
throw new ConversionException("unable to parse " + aFormat, e);
}
}
use of org.apache.commons.beanutils.ConversionException in project checkstyle by checkstyle.
the class AutomaticBeanTest method testContextualizeConversionException.
@Test
public void testContextualizeConversionException() {
final TestBean testBean = new TestBean();
final DefaultContext context = new DefaultContext();
context.add("val", "some string");
try {
testBean.contextualize(context);
assertWithMessage("InvocationTargetException is expected").fail();
} catch (CheckstyleException ex) {
final String expected = "illegal value ";
assertWithMessage("Invalid exception cause, should be: ConversionException").that(ex).hasCauseThat().isInstanceOf(ConversionException.class);
assertWithMessage("Invalid exception message, should start with: " + expected).that(ex).hasMessageThat().startsWith(expected);
}
}
Aggregations