@Interceptor
public class SampleMethodInterceptor {
@Inject
private Validator validator;
@AroundInvoke
public Object validateMethodInvocation(InvocationContext ctx) throws Exception {
Set<ConstraintViolation<Object>> violations = validator.forExecutables().validateParameters(
ctx.getTarget(),
ctx.getMethod(),
ctx.getParameters()
);
if ( !violations.isEmpty() ) {
throw new ConstraintViolationException(
buildMessage( ctx.getMethod(), ctx.getParameters(), violations ),
violations
);
}
Object result = ctx.proceed();
if ( method.getReturnType() == Void.TYPE ) {
violations = validator.forExecutables().validateReturnValue(
ctx.getTarget(),
ctx.getMethod(),
result
);
}
if ( !violations.isEmpty() ) {
throw new ConstraintViolationException(
buildMessage( ctx.getMethod(), ctx.getParameters(), violations ),
violations
);
}
return result;
}
}