|
25 | 25 | import java.util.Set;
|
26 | 26 | import java.util.TreeMap;
|
27 | 27 | import javax.validation.ConstraintViolation;
|
| 28 | +import javax.validation.ValidationException; |
| 29 | +import javax.validation.Validator; |
28 | 30 | import javax.validation.metadata.BeanDescriptor;
|
29 | 31 | import javax.validation.metadata.ConstraintDescriptor;
|
30 | 32 |
|
|
39 | 41 | import org.springframework.validation.SmartValidator;
|
40 | 42 |
|
41 | 43 | /**
|
42 |
| - * Adapter that takes a JSR-303 {@code javax.validator.Validator} |
43 |
| - * and exposes it as a Spring {@link org.springframework.validation.Validator} |
| 44 | + * Adapter that takes a JSR-303 {@code javax.validator.Validator} and |
| 45 | + * exposes it as a Spring {@link org.springframework.validation.Validator} |
44 | 46 | * while also exposing the original JSR-303 Validator interface itself.
|
45 | 47 | *
|
46 | 48 | * <p>Can be used as a programmatic wrapper. Also serves as base class for
|
47 | 49 | * {@link CustomValidatorBean} and {@link LocalValidatorFactoryBean}.
|
48 | 50 | *
|
| 51 | + * <p>Note that Bean Validation 1.1's {@code #forExecutables} method isn't supported |
| 52 | + * on this adapter: We do not expect that method to be called by application code; |
| 53 | + * consider {@link MethodValidationInterceptor} instead. If you really need programmatic |
| 54 | + * {@code #forExecutables} access, call {@code #unwrap(Validator.class) which will |
| 55 | + * provide the native {@link Validator} object with {@code #forExecutables} support. |
| 56 | + * |
49 | 57 | * @author Juergen Hoeller
|
50 | 58 | * @since 3.0
|
51 | 59 | */
|
@@ -297,7 +305,16 @@ public BeanDescriptor getConstraintsForClass(Class<?> clazz) {
|
297 | 305 | @SuppressWarnings("unchecked")
|
298 | 306 | public <T> T unwrap(Class<T> type) {
|
299 | 307 | Assert.state(this.targetValidator != null, "No target Validator set");
|
300 |
| - return (type != null ? this.targetValidator.unwrap(type) : (T) this.targetValidator); |
| 308 | + try { |
| 309 | + return (type != null ? this.targetValidator.unwrap(type) : (T) this.targetValidator); |
| 310 | + } |
| 311 | + catch (ValidationException ex) { |
| 312 | + // ignore if just being asked for plain Validator |
| 313 | + if (Validator.class == type) { |
| 314 | + return (T) this.targetValidator; |
| 315 | + } |
| 316 | + throw ex; |
| 317 | + } |
301 | 318 | }
|
302 | 319 |
|
303 | 320 |
|
|
0 commit comments