*/
public Object invoke(MethodInvocation invocation) throws Throwable {
Validate validate = invocation.getMethod().getAnnotation(Validate.class);
Validator validator = this.validatorProvider.get();
MethodValidator methodValidator = validator.unwrap(MethodValidator.class);
Set<ConstraintViolation<?>> constraintViolations = new HashSet<ConstraintViolation<?>>();
Class<?> clazz = invocation.getMethod().getDeclaringClass();
Method method = invocation.getMethod();
Object[] arguments = invocation.getArguments();
Class<?>[] groups = validate.groups();
constraintViolations.addAll(methodValidator.validateParameters(clazz,
method,
arguments,
groups));
if (!constraintViolations.isEmpty()) {
throw getException(new ConstraintViolationException("Validation error when calling method '"
+ method
+ "' with arguments "
+ Arrays.deepToString(arguments), constraintViolations),
validate.rethrowExceptionsAs());
}
Object returnedValue = invocation.proceed();
if (validate.validateReturnedValue()) {
constraintViolations.addAll(methodValidator.validateReturnedValue(clazz, method, returnedValue, groups));
if (!constraintViolations.isEmpty()) {
throw getException(new ConstraintViolationException("Method '"
+ method
+ "' returned a not valid value "
+ returnedValue, constraintViolations), validate.rethrowExceptionsAs());