|
| 1 | +/* |
| 2 | + * Copyright 2002-2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.validation.beanvalidation; |
| 18 | + |
| 19 | +import java.lang.reflect.Method; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Set; |
| 22 | + |
| 23 | +import jakarta.validation.ConstraintViolation; |
| 24 | +import jakarta.validation.ConstraintViolationException; |
| 25 | + |
| 26 | +/** |
| 27 | + * Extension of {@link ConstraintViolationException} that exposes an additional |
| 28 | + * list of {@link ParameterValidationResult} with violations adapted to |
| 29 | + * {@link org.springframework.context.MessageSourceResolvable} and grouped by |
| 30 | + * method parameter. |
| 31 | + * |
| 32 | + * <p>For {@link jakarta.validation.Valid @Valid}-annotated, Object method |
| 33 | + * parameters or return types with cascaded violations, the {@link ParameterErrors} |
| 34 | + * subclass of {@link ParameterValidationResult} implements |
| 35 | + * {@link org.springframework.validation.Errors} and exposes |
| 36 | + * {@link org.springframework.validation.FieldError field errors}. |
| 37 | + * |
| 38 | + * @author Rossen Stoyanchev |
| 39 | + * @since 6.1 |
| 40 | + * @see ParameterValidationResult |
| 41 | + * @see ParameterErrors |
| 42 | + * @see MethodValidationAdapter |
| 43 | + */ |
| 44 | +@SuppressWarnings("serial") |
| 45 | +public class MethodValidationException extends ConstraintViolationException { |
| 46 | + |
| 47 | + private final Object target; |
| 48 | + |
| 49 | + private final Method method; |
| 50 | + |
| 51 | + private final List<ParameterValidationResult> allValidationResults; |
| 52 | + |
| 53 | + |
| 54 | + public MethodValidationException( |
| 55 | + Object target, Method method, |
| 56 | + List<ParameterValidationResult> validationResults, |
| 57 | + Set<? extends ConstraintViolation<?>> violations) { |
| 58 | + |
| 59 | + super(violations); |
| 60 | + this.target = target; |
| 61 | + this.method = method; |
| 62 | + this.allValidationResults = validationResults; |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + /** |
| 67 | + * Return the target of the method invocation to which validation was applied. |
| 68 | + */ |
| 69 | + public Object getTarget() { |
| 70 | + return this.target; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Return the method to which validation was applied. |
| 75 | + */ |
| 76 | + public Method getMethod() { |
| 77 | + return this.method; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Return all validation results. This includes method parameters with |
| 82 | + * constraints declared on them, as well as |
| 83 | + * {@link jakarta.validation.Valid @Valid} method parameters with |
| 84 | + * cascaded constraints. |
| 85 | + * @see #getValueResults() |
| 86 | + * @see #getBeanResults() |
| 87 | + */ |
| 88 | + public List<ParameterValidationResult> getAllValidationResults() { |
| 89 | + return this.allValidationResults; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Return only validation results for method parameters with constraints |
| 94 | + * declared directly on them. This excludes |
| 95 | + * {@link jakarta.validation.Valid @Valid} method parameters with cascaded |
| 96 | + * constraints. |
| 97 | + * @see #getAllValidationResults() |
| 98 | + */ |
| 99 | + public List<ParameterValidationResult> getValueResults() { |
| 100 | + return this.allValidationResults.stream() |
| 101 | + .filter(result -> !(result instanceof ParameterErrors)) |
| 102 | + .toList(); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Return only validation results for {@link jakarta.validation.Valid @Valid} |
| 107 | + * method parameters with cascaded constraints. This excludes method |
| 108 | + * parameters with constraints declared directly on them. |
| 109 | + * @see #getAllValidationResults() |
| 110 | + */ |
| 111 | + public List<ParameterErrors> getBeanResults() { |
| 112 | + return this.allValidationResults.stream() |
| 113 | + .filter(result -> result instanceof ParameterErrors) |
| 114 | + .map(result -> (ParameterErrors) result) |
| 115 | + .toList(); |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments