Search in sources :

Example 1 with InvalidPropertyException

use of org.springframework.beans.InvalidPropertyException in project grails-core by grails.

the class ConstrainedPropertyBuilder method createNode.

@SuppressWarnings("rawtypes")
@Override
protected Object createNode(Object name, Map attributes) {
    try {
        String property = (String) name;
        ConstrainedProperty cp;
        if (constrainedProperties.containsKey(property)) {
            cp = (ConstrainedProperty) constrainedProperties.get(property);
        } else {
            Class<?> propertyType = classPropertyFetcher.getPropertyType(property, true);
            if (propertyType == null) {
                throw new MissingMethodException(property, targetClass, new Object[] { attributes }, true);
            }
            cp = new ConstrainedProperty(targetClass, property, propertyType);
            cp.setOrder(order++);
            constrainedProperties.put(property, cp);
        }
        if (cp.getPropertyType() == null) {
            if (!IMPORT_FROM_CONSTRAINT.equals(name)) {
                GrailsUtil.warn("Property [" + cp.getPropertyName() + "] not found in domain class " + targetClass.getName() + "; cannot apply constraints: " + attributes);
            }
            return cp;
        }
        for (Object o : attributes.keySet()) {
            String constraintName = (String) o;
            final Object value = attributes.get(constraintName);
            if (SHARED_CONSTRAINT.equals(constraintName)) {
                if (value != null) {
                    sharedConstraints.put(property, value.toString());
                }
                continue;
            }
            if (cp.supportsContraint(constraintName)) {
                cp.applyConstraint(constraintName, value);
            } else {
                if (ConstrainedProperty.hasRegisteredConstraint(constraintName)) {
                    // constraint is registered but doesn't support this property's type
                    GrailsUtil.warn("Property [" + cp.getPropertyName() + "] of domain class " + targetClass.getName() + " has type [" + cp.getPropertyType().getName() + "] and doesn't support constraint [" + constraintName + "]. This constraint will not be checked during validation.");
                } else {
                    // in the case where the constraint is not supported we still retain meta data
                    // about the constraint in case its needed for other things
                    cp.addMetaConstraint(constraintName, value);
                }
            }
        }
        return cp;
    } catch (InvalidPropertyException ipe) {
        throw new MissingMethodException((String) name, targetClass, new Object[] { attributes });
    }
}
Also used : MissingMethodException(groovy.lang.MissingMethodException) InvalidPropertyException(org.springframework.beans.InvalidPropertyException) ConstrainedProperty(grails.validation.ConstrainedProperty)

Example 2 with InvalidPropertyException

use of org.springframework.beans.InvalidPropertyException in project com.revolsys.open by revolsys.

the class ScriptExecutorRunnable method getBeanExceptionCause.

private static Throwable getBeanExceptionCause(final BeanCreationException e) {
    Throwable cause = e.getCause();
    while (cause instanceof BeanCreationException || cause instanceof MethodInvocationException || cause instanceof PropertyAccessException || cause instanceof PropertyBatchUpdateException || cause instanceof InvalidPropertyException) {
        Throwable newCause;
        if (cause instanceof PropertyBatchUpdateException) {
            final PropertyBatchUpdateException batchEx = (PropertyBatchUpdateException) cause;
            newCause = batchEx.getPropertyAccessExceptions()[0];
        } else {
            newCause = cause.getCause();
        }
        if (newCause != null) {
            cause = newCause;
        } else {
            return cause;
        }
    }
    return cause;
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) PropertyBatchUpdateException(org.springframework.beans.PropertyBatchUpdateException) InvalidPropertyException(org.springframework.beans.InvalidPropertyException) PropertyAccessException(org.springframework.beans.PropertyAccessException) MethodInvocationException(org.springframework.beans.MethodInvocationException)

Example 3 with InvalidPropertyException

use of org.springframework.beans.InvalidPropertyException in project com.revolsys.open by revolsys.

the class ScriptTool method getBeanExceptionCause.

private static Throwable getBeanExceptionCause(final BeanCreationException e) {
    Throwable cause = e.getCause();
    if (cause == null) {
        return e;
    }
    while (cause instanceof BeanCreationException || cause instanceof MethodInvocationException || cause instanceof PropertyAccessException || cause instanceof PropertyBatchUpdateException || cause instanceof InvalidPropertyException) {
        Throwable newCause;
        if (cause instanceof PropertyBatchUpdateException) {
            final PropertyBatchUpdateException batchEx = (PropertyBatchUpdateException) cause;
            newCause = batchEx.getPropertyAccessExceptions()[0];
        } else {
            newCause = cause.getCause();
        }
        if (newCause != null) {
            cause = newCause;
        } else {
            return cause;
        }
    }
    return cause;
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) PropertyBatchUpdateException(org.springframework.beans.PropertyBatchUpdateException) InvalidPropertyException(org.springframework.beans.InvalidPropertyException) PropertyAccessException(org.springframework.beans.PropertyAccessException) MethodInvocationException(org.springframework.beans.MethodInvocationException)

Example 4 with InvalidPropertyException

use of org.springframework.beans.InvalidPropertyException in project opennms by OpenNMS.

the class MockForeignSourceRepositoryTest method testBeanWrapperAccess.

/**
 * This test ensures that the Spring Bean accessor classes work properly
 * since our REST implementation uses bean access to update the values.
 */
@Test
public void testBeanWrapperAccess() throws Exception {
    createRequisition();
    Requisition r = m_foreignSourceRepository.getRequisition(m_defaultForeignSourceName);
    BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(r);
    assertEquals("AC", wrapper.getPropertyValue("node[0].category[0].name"));
    assertEquals("UK", wrapper.getPropertyValue("node[0].category[1].name"));
    assertEquals("low", wrapper.getPropertyValue("node[0].category[2].name"));
    try {
        wrapper.getPropertyValue("node[1].category[0].name");
        fail("Did not catch expected InvalidPropertyException exception");
    } catch (InvalidPropertyException e) {
    // Expected failure
    }
    assertEquals(0, ((RequisitionCategory[]) wrapper.getPropertyValue("node[1].category")).length);
    wrapper.setPropertyValue("node[1].categories[0]", new RequisitionCategory("Hello world"));
    wrapper.setPropertyValue("node[1].categories[1]", new RequisitionCategory("Hello again"));
    assertEquals(2, ((RequisitionCategory[]) wrapper.getPropertyValue("node[1].category")).length);
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) RequisitionCategory(org.opennms.netmgt.provision.persist.requisition.RequisitionCategory) InvalidPropertyException(org.springframework.beans.InvalidPropertyException) Requisition(org.opennms.netmgt.provision.persist.requisition.Requisition) Test(org.junit.Test)

Example 5 with InvalidPropertyException

use of org.springframework.beans.InvalidPropertyException in project disconf by knightliao.

the class MyExceptionHandler method resolveException.

@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object o, Exception e) {
    LOG.warn(request.getRequestURI() + " ExceptionHandler FOUND. " + e.toString() + "\t" + e.getCause());
    // PathVariable 出错
    if (e instanceof TypeMismatchException) {
        return getParamErrors((TypeMismatchException) e);
    // Bean 参数无法映射错误
    } else if (e instanceof InvalidPropertyException) {
        return getParamErrors((InvalidPropertyException) e);
    // @Valid 出错
    } else if (e instanceof BindException) {
        return ParamValidateUtils.getParamErrors((BindException) e);
    // 业务校验处理
    } else if (e instanceof FieldException) {
        return getParamErrors((FieldException) e);
    } else if (e instanceof DocumentNotFoundException) {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        try {
            FileUtils.closeWriter(response.getWriter());
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        return null;
    // 用户没有请求方法的访问权限
    } else if (e instanceof AccessDeniedException) {
        LOG.warn("details: " + ((AccessDeniedException) e).getErrorMessage());
        return buildError("auth.access.denied", ErrorCode.ACCESS_NOAUTH_ERROR);
    } else if (e instanceof HttpRequestMethodNotSupportedException) {
        return buildError("syserror.httpmethod", ErrorCode.HttpRequestMethodNotSupportedException);
    } else if (e instanceof MissingServletRequestParameterException) {
        return buildError("syserror.param.miss", ErrorCode.MissingServletRequestParameterException);
    } else if (e instanceof GlobalExceptionAware) {
        LOG.error("details: ", e);
        GlobalExceptionAware g = (GlobalExceptionAware) e;
        return buildError(g.getErrorMessage(), g.getErrorCode());
    } else {
        LOG.warn("details: ", e);
        return buildError("syserror.inner", ErrorCode.GLOBAL_ERROR);
    }
}
Also used : AccessDeniedException(com.baidu.dsp.common.exception.AccessDeniedException) FieldException(com.baidu.dsp.common.exception.FieldException) DocumentNotFoundException(com.baidu.dsp.common.exception.DocumentNotFoundException) GlobalExceptionAware(com.baidu.dsp.common.exception.base.GlobalExceptionAware) TypeMismatchException(org.springframework.beans.TypeMismatchException) InvalidPropertyException(org.springframework.beans.InvalidPropertyException) BindException(org.springframework.validation.BindException) IOException(java.io.IOException) MissingServletRequestParameterException(org.springframework.web.bind.MissingServletRequestParameterException) HttpRequestMethodNotSupportedException(org.springframework.web.HttpRequestMethodNotSupportedException)

Aggregations

InvalidPropertyException (org.springframework.beans.InvalidPropertyException)5 MethodInvocationException (org.springframework.beans.MethodInvocationException)2 PropertyAccessException (org.springframework.beans.PropertyAccessException)2 PropertyBatchUpdateException (org.springframework.beans.PropertyBatchUpdateException)2 BeanCreationException (org.springframework.beans.factory.BeanCreationException)2 AccessDeniedException (com.baidu.dsp.common.exception.AccessDeniedException)1 DocumentNotFoundException (com.baidu.dsp.common.exception.DocumentNotFoundException)1 FieldException (com.baidu.dsp.common.exception.FieldException)1 GlobalExceptionAware (com.baidu.dsp.common.exception.base.GlobalExceptionAware)1 ConstrainedProperty (grails.validation.ConstrainedProperty)1 MissingMethodException (groovy.lang.MissingMethodException)1 IOException (java.io.IOException)1 Test (org.junit.Test)1 Requisition (org.opennms.netmgt.provision.persist.requisition.Requisition)1 RequisitionCategory (org.opennms.netmgt.provision.persist.requisition.RequisitionCategory)1 BeanWrapper (org.springframework.beans.BeanWrapper)1 TypeMismatchException (org.springframework.beans.TypeMismatchException)1 BindException (org.springframework.validation.BindException)1 HttpRequestMethodNotSupportedException (org.springframework.web.HttpRequestMethodNotSupportedException)1 MissingServletRequestParameterException (org.springframework.web.bind.MissingServletRequestParameterException)1