Search in sources :

Example 1 with ConversionException

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);
}
Also used : DynaProperty(org.apache.commons.beanutils.DynaProperty) ConversionException(org.apache.commons.beanutils.ConversionException)

Example 2 with ConversionException

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());
    }
}
Also used : ConversionException(org.apache.commons.beanutils.ConversionException) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)

Example 3 with ConversionException

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);
}
Also used : DynaProperty(org.apache.commons.beanutils.DynaProperty) ConversionException(org.apache.commons.beanutils.ConversionException)

Example 4 with ConversionException

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);
    }
}
Also used : ConversionException(org.apache.commons.beanutils.ConversionException) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 5 with ConversionException

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);
    }
}
Also used : ConversionException(org.apache.commons.beanutils.ConversionException) DefaultContext(com.puppycrawl.tools.checkstyle.DefaultContext) Test(org.junit.jupiter.api.Test)

Aggregations

ConversionException (org.apache.commons.beanutils.ConversionException)7 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)2 DynaProperty (org.apache.commons.beanutils.DynaProperty)2 DefaultContext (com.puppycrawl.tools.checkstyle.DefaultContext)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Matcher (java.util.regex.Matcher)1 PatternSyntaxException (java.util.regex.PatternSyntaxException)1 KeyValPair (org.apache.apex.malhar.lib.util.KeyValPair)1 DateConverter (org.apache.commons.beanutils.converters.DateConverter)1 DateTimeConverter (org.apache.commons.beanutils.converters.DateTimeConverter)1 Test (org.junit.jupiter.api.Test)1