Direct use of Bean Definition 1.1 ExecutableValidator
Issue: SPR-13481
This commit is contained in:
parent
f065589a37
commit
10d5926b1a
|
|
@ -23,6 +23,7 @@ import javax.validation.ConstraintViolationException;
|
|||
import javax.validation.Validation;
|
||||
import javax.validation.Validator;
|
||||
import javax.validation.ValidatorFactory;
|
||||
import javax.validation.executable.ExecutableValidator;
|
||||
|
||||
import org.aopalliance.intercept.MethodInterceptor;
|
||||
import org.aopalliance.intercept.MethodInvocation;
|
||||
|
|
@ -30,7 +31,6 @@ import org.aopalliance.intercept.MethodInvocation;
|
|||
import org.springframework.core.BridgeMethodResolver;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
/**
|
||||
|
|
@ -58,27 +58,6 @@ import org.springframework.validation.annotation.Validated;
|
|||
*/
|
||||
public class MethodValidationInterceptor implements MethodInterceptor {
|
||||
|
||||
private static Method forExecutablesMethod;
|
||||
|
||||
private static Method validateParametersMethod;
|
||||
|
||||
private static Method validateReturnValueMethod;
|
||||
|
||||
static {
|
||||
try {
|
||||
forExecutablesMethod = Validator.class.getMethod("forExecutables");
|
||||
Class<?> executableValidatorClass = forExecutablesMethod.getReturnType();
|
||||
validateParametersMethod = executableValidatorClass.getMethod(
|
||||
"validateParameters", Object.class, Method.class, Object[].class, Class[].class);
|
||||
validateReturnValueMethod = executableValidatorClass.getMethod(
|
||||
"validateReturnValue", Object.class, Method.class, Object.class, Class[].class);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
// Bean Validation 1.1 ExecutableValidator API not available
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private final Validator validator;
|
||||
|
||||
|
||||
|
|
@ -112,21 +91,21 @@ public class MethodValidationInterceptor implements MethodInterceptor {
|
|||
Class<?>[] groups = determineValidationGroups(invocation);
|
||||
|
||||
// Standard Bean Validation 1.1 API
|
||||
Object execVal = ReflectionUtils.invokeMethod(forExecutablesMethod, this.validator);
|
||||
ExecutableValidator execVal = this.validator.forExecutables();
|
||||
Method methodToValidate = invocation.getMethod();
|
||||
Set<ConstraintViolation<?>> result;
|
||||
Set<ConstraintViolation<Object>> result;
|
||||
|
||||
try {
|
||||
result = (Set<ConstraintViolation<?>>) ReflectionUtils.invokeMethod(validateParametersMethod,
|
||||
execVal, invocation.getThis(), methodToValidate, invocation.getArguments(), groups);
|
||||
result = execVal.validateParameters(
|
||||
invocation.getThis(), methodToValidate, invocation.getArguments(), groups);
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
// Probably a generic type mismatch between interface and impl as reported in SPR-12237 / HV-1011
|
||||
// Let's try to find the bridged method on the implementation class...
|
||||
methodToValidate = BridgeMethodResolver.findBridgedMethod(
|
||||
ClassUtils.getMostSpecificMethod(invocation.getMethod(), invocation.getThis().getClass()));
|
||||
result = (Set<ConstraintViolation<?>>) ReflectionUtils.invokeMethod(validateParametersMethod,
|
||||
execVal, invocation.getThis(), methodToValidate, invocation.getArguments(), groups);
|
||||
result = execVal.validateParameters(
|
||||
invocation.getThis(), methodToValidate, invocation.getArguments(), groups);
|
||||
}
|
||||
if (!result.isEmpty()) {
|
||||
throw new ConstraintViolationException(result);
|
||||
|
|
@ -134,8 +113,7 @@ public class MethodValidationInterceptor implements MethodInterceptor {
|
|||
|
||||
Object returnValue = invocation.proceed();
|
||||
|
||||
result = (Set<ConstraintViolation<?>>) ReflectionUtils.invokeMethod(validateReturnValueMethod,
|
||||
execVal, invocation.getThis(), methodToValidate, returnValue, groups);
|
||||
result = execVal.validateReturnValue(invocation.getThis(), methodToValidate, returnValue, groups);
|
||||
if (!result.isEmpty()) {
|
||||
throw new ConstraintViolationException(result);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue