|
| 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 | + |
| 21 | +import org.springframework.lang.Nullable; |
| 22 | + |
| 23 | +/** |
| 24 | + * Default implementation of {@link MethodValidator} that delegates to a |
| 25 | + * {@link MethodValidationAdapter}. Also, convenient as a base class that allows |
| 26 | + * handling of the validation result. |
| 27 | + * |
| 28 | + * @author Rossen Stoyanchev |
| 29 | + * @since 6.1 |
| 30 | + */ |
| 31 | +public class DefaultMethodValidator implements MethodValidator { |
| 32 | + |
| 33 | + private final MethodValidationAdapter adapter; |
| 34 | + |
| 35 | + |
| 36 | + public DefaultMethodValidator(MethodValidationAdapter adapter) { |
| 37 | + this.adapter = adapter; |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | + @Override |
| 42 | + public Class<?>[] determineValidationGroups(Object bean, Method method) { |
| 43 | + return MethodValidationAdapter.determineValidationGroups(bean, method); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public void validateArguments(Object target, Method method, Object[] arguments, Class<?>[] groups) { |
| 48 | + MethodValidationResult result = this.adapter.validateMethodArguments(target, method, arguments, groups); |
| 49 | + handleArgumentsResult(target, method, arguments, groups, result); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Subclasses can override this to handle the result of argument validation. |
| 54 | + * By default, {@link MethodValidationResult#throwIfViolationsPresent()} is called. |
| 55 | + */ |
| 56 | + protected void handleArgumentsResult( |
| 57 | + Object bean, Method method, Object[] arguments, Class<?>[] groups, MethodValidationResult result) { |
| 58 | + |
| 59 | + result.throwIfViolationsPresent(); |
| 60 | + } |
| 61 | + |
| 62 | + public void validateReturnValue(Object target, Method method, @Nullable Object returnValue, Class<?>[] groups) { |
| 63 | + MethodValidationResult result = this.adapter.validateMethodReturnValue(target, method, returnValue, groups); |
| 64 | + handleReturnValueResult(target, method, returnValue, groups, result); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Subclasses can override this to handle the result of return value validation. |
| 69 | + * By default, {@link MethodValidationResult#throwIfViolationsPresent()} is called. |
| 70 | + */ |
| 71 | + protected void handleReturnValueResult( |
| 72 | + Object bean, Method method, @Nullable Object returnValue, Class<?>[] groups, MethodValidationResult result) { |
| 73 | + |
| 74 | + result.throwIfViolationsPresent(); |
| 75 | + } |
| 76 | + |
| 77 | +} |
0 commit comments