Skip to content

Commit 0fef380

Browse files
committed
Add MethodValidator
See gh-29825
1 parent cc8361c commit 0fef380

File tree

2 files changed

+148
-0
lines changed

2 files changed

+148
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
* Contract to apply method validation without directly using
25+
* {@link MethodValidationAdapter}. For use in components where Jakarta Bean
26+
* Validation is an optional dependency and may or may not be present on the
27+
* classpath. If that's not a concern, use {@code MethodValidationAdapter}
28+
* directly.
29+
*
30+
* @author Rossen Stoyanchev
31+
* @since 6.1
32+
* @see DefaultMethodValidator
33+
*/
34+
public interface MethodValidator {
35+
36+
/**
37+
* Use this method determine the validation groups to pass into
38+
* {@link #validateArguments(Object, Method, Object[], Class[])} and
39+
* {@link #validateReturnValue(Object, Method, Object, Class[])}.
40+
* @param target the target Object
41+
* @param method the target method
42+
* @return the applicable validation groups as a {@code Class} array
43+
* @see MethodValidationAdapter#determineValidationGroups(Object, Method)
44+
*/
45+
Class<?>[] determineValidationGroups(Object target, Method method);
46+
47+
/**
48+
* Validate the given method arguments and return the result of validation.
49+
* @param target the target Object
50+
* @param method the target method
51+
* @param arguments candidate arguments for a method invocation
52+
* @param groups groups for validation determined via
53+
* {@link #determineValidationGroups(Object, Method)}
54+
* @throws MethodValidationException should be raised in case of validation
55+
* errors unless the implementation handles those errors otherwise (e.g.
56+
* by injecting {@code BindingResult} into the method).
57+
*/
58+
void validateArguments(Object target, Method method, Object[] arguments, Class<?>[] groups);
59+
60+
/**
61+
* Validate the given return value and return the result of validation.
62+
* @param target the target Object
63+
* @param method the target method
64+
* @param returnValue value returned from invoking the target method
65+
* @param groups groups for validation determined via
66+
* {@link #determineValidationGroups(Object, Method)}
67+
* @throws MethodValidationException in case of validation errors
68+
*/
69+
void validateReturnValue(Object target, Method method, @Nullable Object returnValue, Class<?>[] groups);
70+
71+
}

0 commit comments

Comments
 (0)