提交 10d5926b 编写于 作者: J Juergen Hoeller

Direct use of Bean Definition 1.1 ExecutableValidator

Issue: SPR-13481
上级 f065589a
...@@ -23,6 +23,7 @@ import javax.validation.ConstraintViolationException; ...@@ -23,6 +23,7 @@ import javax.validation.ConstraintViolationException;
import javax.validation.Validation; import javax.validation.Validation;
import javax.validation.Validator; import javax.validation.Validator;
import javax.validation.ValidatorFactory; import javax.validation.ValidatorFactory;
import javax.validation.executable.ExecutableValidator;
import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation; import org.aopalliance.intercept.MethodInvocation;
...@@ -30,7 +31,6 @@ import org.aopalliance.intercept.MethodInvocation; ...@@ -30,7 +31,6 @@ import org.aopalliance.intercept.MethodInvocation;
import org.springframework.core.BridgeMethodResolver; import org.springframework.core.BridgeMethodResolver;
import org.springframework.core.annotation.AnnotationUtils; import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
/** /**
...@@ -58,27 +58,6 @@ import org.springframework.validation.annotation.Validated; ...@@ -58,27 +58,6 @@ import org.springframework.validation.annotation.Validated;
*/ */
public class MethodValidationInterceptor implements MethodInterceptor { 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; private final Validator validator;
...@@ -112,21 +91,21 @@ public class MethodValidationInterceptor implements MethodInterceptor { ...@@ -112,21 +91,21 @@ public class MethodValidationInterceptor implements MethodInterceptor {
Class<?>[] groups = determineValidationGroups(invocation); Class<?>[] groups = determineValidationGroups(invocation);
// Standard Bean Validation 1.1 API // Standard Bean Validation 1.1 API
Object execVal = ReflectionUtils.invokeMethod(forExecutablesMethod, this.validator); ExecutableValidator execVal = this.validator.forExecutables();
Method methodToValidate = invocation.getMethod(); Method methodToValidate = invocation.getMethod();
Set<ConstraintViolation<?>> result; Set<ConstraintViolation<Object>> result;
try { try {
result = (Set<ConstraintViolation<?>>) ReflectionUtils.invokeMethod(validateParametersMethod, result = execVal.validateParameters(
execVal, invocation.getThis(), methodToValidate, invocation.getArguments(), groups); invocation.getThis(), methodToValidate, invocation.getArguments(), groups);
} }
catch (IllegalArgumentException ex) { catch (IllegalArgumentException ex) {
// Probably a generic type mismatch between interface and impl as reported in SPR-12237 / HV-1011 // 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... // Let's try to find the bridged method on the implementation class...
methodToValidate = BridgeMethodResolver.findBridgedMethod( methodToValidate = BridgeMethodResolver.findBridgedMethod(
ClassUtils.getMostSpecificMethod(invocation.getMethod(), invocation.getThis().getClass())); ClassUtils.getMostSpecificMethod(invocation.getMethod(), invocation.getThis().getClass()));
result = (Set<ConstraintViolation<?>>) ReflectionUtils.invokeMethod(validateParametersMethod, result = execVal.validateParameters(
execVal, invocation.getThis(), methodToValidate, invocation.getArguments(), groups); invocation.getThis(), methodToValidate, invocation.getArguments(), groups);
} }
if (!result.isEmpty()) { if (!result.isEmpty()) {
throw new ConstraintViolationException(result); throw new ConstraintViolationException(result);
...@@ -134,8 +113,7 @@ public class MethodValidationInterceptor implements MethodInterceptor { ...@@ -134,8 +113,7 @@ public class MethodValidationInterceptor implements MethodInterceptor {
Object returnValue = invocation.proceed(); Object returnValue = invocation.proceed();
result = (Set<ConstraintViolation<?>>) ReflectionUtils.invokeMethod(validateReturnValueMethod, result = execVal.validateReturnValue(invocation.getThis(), methodToValidate, returnValue, groups);
execVal, invocation.getThis(), methodToValidate, returnValue, groups);
if (!result.isEmpty()) { if (!result.isEmpty()) {
throw new ConstraintViolationException(result); throw new ConstraintViolationException(result);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册