Search in sources :

Example 1 with ValidationRuntimeException

use of org.craftercms.commons.validation.ValidationRuntimeException in project commons by craftercms.

the class ValidateParamsAspect method doValidation.

@Before("@within(org.craftercms.commons.validation.annotations.param.ValidateParams) || " + "@annotation(org.craftercms.commons.validation.annotations.param.ValidateParams)")
public void doValidation(JoinPoint joinPoint) {
    Object[] args = joinPoint.getArgs();
    Method method = AopUtils.getActualMethod(joinPoint);
    Annotation[][] allParamAnnotations = method.getParameterAnnotations();
    ValidationResult result = new ValidationResult(errorMessageBundle);
    if (ArrayUtils.isNotEmpty(allParamAnnotations)) {
        for (int i = 0; i < args.length; i++) {
            Object param = args[i];
            Annotation[] paramAnnotations = allParamAnnotations[i];
            for (Annotation annotation : paramAnnotations) {
                validateParam(annotation, param, result);
            }
        }
    }
    if (result.hasErrors()) {
        String methodStr = method.toGenericString();
        result.setMessage(ValidationUtils.getErrorMessage(errorMessageBundle, INVALID_METHOD_PARAMS_ERROR_CODE, methodStr));
        throw new ValidationRuntimeException(result);
    }
}
Also used : Method(java.lang.reflect.Method) ValidationRuntimeException(org.craftercms.commons.validation.ValidationRuntimeException) ValidationResult(org.craftercms.commons.validation.ValidationResult) JoinPoint(org.aspectj.lang.JoinPoint) Annotation(java.lang.annotation.Annotation) Before(org.aspectj.lang.annotation.Before)

Aggregations

Annotation (java.lang.annotation.Annotation)1 Method (java.lang.reflect.Method)1 JoinPoint (org.aspectj.lang.JoinPoint)1 Before (org.aspectj.lang.annotation.Before)1 ValidationResult (org.craftercms.commons.validation.ValidationResult)1 ValidationRuntimeException (org.craftercms.commons.validation.ValidationRuntimeException)1