|
18 | 18 |
|
19 | 19 | import java.lang.reflect.Method;
|
20 | 20 | import java.util.ArrayList;
|
21 |
| -import java.util.Collections; |
22 | 21 | import java.util.Comparator;
|
23 | 22 | import java.util.Iterator;
|
24 | 23 | import java.util.LinkedHashMap;
|
|
50 | 49 | import org.springframework.core.ParameterNameDiscoverer;
|
51 | 50 | import org.springframework.core.annotation.AnnotationUtils;
|
52 | 51 | import org.springframework.lang.Nullable;
|
53 |
| -import org.springframework.util.Assert; |
54 | 52 | import org.springframework.util.ClassUtils;
|
55 | 53 | import org.springframework.util.function.SingletonSupplier;
|
56 | 54 | import org.springframework.validation.BeanPropertyBindingResult;
|
|
59 | 57 | import org.springframework.validation.Errors;
|
60 | 58 | import org.springframework.validation.MessageCodesResolver;
|
61 | 59 | import org.springframework.validation.annotation.Validated;
|
| 60 | +import org.springframework.validation.method.MethodValidationResult; |
| 61 | +import org.springframework.validation.method.MethodValidator; |
| 62 | +import org.springframework.validation.method.ParameterErrors; |
| 63 | +import org.springframework.validation.method.ParameterValidationResult; |
62 | 64 |
|
63 | 65 | /**
|
64 | 66 | * {@link MethodValidator} that uses a Bean Validation
|
|
70 | 72 | */
|
71 | 73 | public class MethodValidationAdapter implements MethodValidator {
|
72 | 74 |
|
| 75 | + private static final MethodValidationResult emptyValidationResult = MethodValidationResult.emptyResult(); |
| 76 | + |
73 | 77 | private static final ObjectNameResolver defaultObjectNameResolver = new DefaultObjectNameResolver();
|
74 | 78 |
|
75 | 79 | private static final Comparator<ParameterValidationResult> resultComparator = new ResultComparator();
|
76 | 80 |
|
77 |
| - private static final MethodValidationResult emptyResult = new EmptyMethodValidationResult(); |
78 |
| - |
79 | 81 |
|
80 | 82 | private final Supplier<Validator> validator;
|
81 | 83 |
|
@@ -219,7 +221,7 @@ public final MethodValidationResult validateArguments(
|
219 | 221 | invokeValidatorForArguments(target, method, arguments, groups);
|
220 | 222 |
|
221 | 223 | if (violations.isEmpty()) {
|
222 |
| - return emptyResult; |
| 224 | + return emptyValidationResult; |
223 | 225 | }
|
224 | 226 |
|
225 | 227 | return adaptViolations(target, method, violations,
|
@@ -257,7 +259,7 @@ public final MethodValidationResult validateReturnValue(
|
257 | 259 | invokeValidatorForReturnValue(target, method, returnValue, groups);
|
258 | 260 |
|
259 | 261 | if (violations.isEmpty()) {
|
260 |
| - return emptyResult; |
| 262 | + return emptyValidationResult; |
261 | 263 | }
|
262 | 264 |
|
263 | 265 | return adaptViolations(target, method, violations,
|
@@ -320,7 +322,7 @@ else if (node.getKind().equals(ElementKind.RETURN_VALUE)) {
|
320 | 322 | cascadedViolations.forEach((node, builder) -> validatonResultList.add(builder.build()));
|
321 | 323 | validatonResultList.sort(resultComparator);
|
322 | 324 |
|
323 |
| - return new DefaultMethodValidationResult(target, method, validatonResultList); |
| 325 | + return MethodValidationResult.create(target, method, validatonResultList); |
324 | 326 | }
|
325 | 327 |
|
326 | 328 | private MethodParameter initMethodParameter(Method method, int index) {
|
@@ -534,91 +536,4 @@ private <E> int compareKeys(ParameterErrors errors1, ParameterErrors errors2) {
|
534 | 536 | }
|
535 | 537 | }
|
536 | 538 |
|
537 |
| - |
538 |
| - /** |
539 |
| - * Default {@link MethodValidationResult} implementation with non-zero errors. |
540 |
| - */ |
541 |
| - private static class DefaultMethodValidationResult implements MethodValidationResult { |
542 |
| - |
543 |
| - private final Object target; |
544 |
| - |
545 |
| - private final Method method; |
546 |
| - |
547 |
| - private final List<ParameterValidationResult> allValidationResults; |
548 |
| - |
549 |
| - private final boolean forReturnValue; |
550 |
| - |
551 |
| - |
552 |
| - DefaultMethodValidationResult(Object target, Method method, List<ParameterValidationResult> results) { |
553 |
| - Assert.notEmpty(results, "'results' is required and must not be empty"); |
554 |
| - Assert.notNull(target, "'target' is required"); |
555 |
| - Assert.notNull(method, "Method is required"); |
556 |
| - this.target = target; |
557 |
| - this.method = method; |
558 |
| - this.allValidationResults = results; |
559 |
| - this.forReturnValue = (results.get(0).getMethodParameter().getParameterIndex() == -1); |
560 |
| - } |
561 |
| - |
562 |
| - |
563 |
| - @Override |
564 |
| - public Object getTarget() { |
565 |
| - return this.target; |
566 |
| - } |
567 |
| - |
568 |
| - @Override |
569 |
| - public Method getMethod() { |
570 |
| - return this.method; |
571 |
| - } |
572 |
| - |
573 |
| - @Override |
574 |
| - public boolean isForReturnValue() { |
575 |
| - return this.forReturnValue; |
576 |
| - } |
577 |
| - |
578 |
| - @Override |
579 |
| - public List<ParameterValidationResult> getAllValidationResults() { |
580 |
| - return this.allValidationResults; |
581 |
| - } |
582 |
| - |
583 |
| - |
584 |
| - @Override |
585 |
| - public String toString() { |
586 |
| - return getAllErrors().size() + " validation errors " + |
587 |
| - "for " + (isForReturnValue() ? "return value" : "arguments") + " of " + |
588 |
| - this.method.toGenericString(); |
589 |
| - } |
590 |
| - } |
591 |
| - |
592 |
| - |
593 |
| - /** |
594 |
| - * {@link MethodValidationResult} for when there are no errors. |
595 |
| - */ |
596 |
| - private static class EmptyMethodValidationResult implements MethodValidationResult { |
597 |
| - |
598 |
| - @Override |
599 |
| - public Object getTarget() { |
600 |
| - throw new UnsupportedOperationException(); |
601 |
| - } |
602 |
| - |
603 |
| - @Override |
604 |
| - public Method getMethod() { |
605 |
| - throw new UnsupportedOperationException(); |
606 |
| - } |
607 |
| - |
608 |
| - @Override |
609 |
| - public boolean isForReturnValue() { |
610 |
| - throw new UnsupportedOperationException(); |
611 |
| - } |
612 |
| - |
613 |
| - @Override |
614 |
| - public List<ParameterValidationResult> getAllValidationResults() { |
615 |
| - return Collections.emptyList(); |
616 |
| - } |
617 |
| - |
618 |
| - @Override |
619 |
| - public String toString() { |
620 |
| - return "0 validation errors"; |
621 |
| - } |
622 |
| - } |
623 |
| - |
624 | 539 | }
|
0 commit comments