Skip to content

Commit 592ab0f

Browse files
committed
Add ~.validation.method package
Extract classes from ~.validation.beanvalidation without a direct dependency on beanvalidation. See gh-30644
1 parent 0da2241 commit 592ab0f

File tree

27 files changed

+220
-120
lines changed

27 files changed

+220
-120
lines changed

spring-context/src/main/java/org/springframework/validation/beanvalidation/MethodValidationAdapter.java

+9-94
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.lang.reflect.Method;
2020
import java.util.ArrayList;
21-
import java.util.Collections;
2221
import java.util.Comparator;
2322
import java.util.Iterator;
2423
import java.util.LinkedHashMap;
@@ -50,7 +49,6 @@
5049
import org.springframework.core.ParameterNameDiscoverer;
5150
import org.springframework.core.annotation.AnnotationUtils;
5251
import org.springframework.lang.Nullable;
53-
import org.springframework.util.Assert;
5452
import org.springframework.util.ClassUtils;
5553
import org.springframework.util.function.SingletonSupplier;
5654
import org.springframework.validation.BeanPropertyBindingResult;
@@ -59,6 +57,10 @@
5957
import org.springframework.validation.Errors;
6058
import org.springframework.validation.MessageCodesResolver;
6159
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;
6264

6365
/**
6466
* {@link MethodValidator} that uses a Bean Validation
@@ -70,12 +72,12 @@
7072
*/
7173
public class MethodValidationAdapter implements MethodValidator {
7274

75+
private static final MethodValidationResult emptyValidationResult = MethodValidationResult.emptyResult();
76+
7377
private static final ObjectNameResolver defaultObjectNameResolver = new DefaultObjectNameResolver();
7478

7579
private static final Comparator<ParameterValidationResult> resultComparator = new ResultComparator();
7680

77-
private static final MethodValidationResult emptyResult = new EmptyMethodValidationResult();
78-
7981

8082
private final Supplier<Validator> validator;
8183

@@ -219,7 +221,7 @@ public final MethodValidationResult validateArguments(
219221
invokeValidatorForArguments(target, method, arguments, groups);
220222

221223
if (violations.isEmpty()) {
222-
return emptyResult;
224+
return emptyValidationResult;
223225
}
224226

225227
return adaptViolations(target, method, violations,
@@ -257,7 +259,7 @@ public final MethodValidationResult validateReturnValue(
257259
invokeValidatorForReturnValue(target, method, returnValue, groups);
258260

259261
if (violations.isEmpty()) {
260-
return emptyResult;
262+
return emptyValidationResult;
261263
}
262264

263265
return adaptViolations(target, method, violations,
@@ -320,7 +322,7 @@ else if (node.getKind().equals(ElementKind.RETURN_VALUE)) {
320322
cascadedViolations.forEach((node, builder) -> validatonResultList.add(builder.build()));
321323
validatonResultList.sort(resultComparator);
322324

323-
return new DefaultMethodValidationResult(target, method, validatonResultList);
325+
return MethodValidationResult.create(target, method, validatonResultList);
324326
}
325327

326328
private MethodParameter initMethodParameter(Method method, int index) {
@@ -534,91 +536,4 @@ private <E> int compareKeys(ParameterErrors errors1, ParameterErrors errors2) {
534536
}
535537
}
536538

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-
624539
}

spring-context/src/main/java/org/springframework/validation/beanvalidation/MethodValidationInterceptor.java

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import org.springframework.util.Assert;
3535
import org.springframework.util.ClassUtils;
3636
import org.springframework.validation.annotation.Validated;
37+
import org.springframework.validation.method.MethodValidationException;
38+
import org.springframework.validation.method.MethodValidationResult;
3739

3840
/**
3941
* An AOP Alliance {@link MethodInterceptor} implementation that delegates to a

spring-context/src/main/java/org/springframework/validation/beanvalidation/MethodValidationPostProcessor.java

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import org.springframework.util.Assert;
3737
import org.springframework.util.function.SingletonSupplier;
3838
import org.springframework.validation.annotation.Validated;
39+
import org.springframework.validation.method.MethodValidationException;
40+
import org.springframework.validation.method.MethodValidationResult;
3941

4042
/**
4143
* A convenient {@link BeanPostProcessor} implementation that delegates to a
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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.method;
18+
19+
import java.lang.reflect.Method;
20+
import java.util.List;
21+
22+
import org.springframework.util.Assert;
23+
24+
/**
25+
* Default {@link MethodValidationResult} implementation as a simple container.
26+
*
27+
* @author Rossen Stoyanchev
28+
* @since 6.1
29+
*/
30+
final class DefaultMethodValidationResult implements MethodValidationResult {
31+
32+
private final Object target;
33+
34+
private final Method method;
35+
36+
private final List<ParameterValidationResult> allValidationResults;
37+
38+
private final boolean forReturnValue;
39+
40+
41+
DefaultMethodValidationResult(Object target, Method method, List<ParameterValidationResult> results) {
42+
Assert.notEmpty(results, "'results' is required and must not be empty");
43+
Assert.notNull(target, "'target' is required");
44+
Assert.notNull(method, "Method is required");
45+
this.target = target;
46+
this.method = method;
47+
this.allValidationResults = results;
48+
this.forReturnValue = (results.get(0).getMethodParameter().getParameterIndex() == -1);
49+
}
50+
51+
52+
@Override
53+
public Object getTarget() {
54+
return this.target;
55+
}
56+
57+
@Override
58+
public Method getMethod() {
59+
return this.method;
60+
}
61+
62+
@Override
63+
public boolean isForReturnValue() {
64+
return this.forReturnValue;
65+
}
66+
67+
@Override
68+
public List<ParameterValidationResult> getAllValidationResults() {
69+
return this.allValidationResults;
70+
}
71+
72+
73+
@Override
74+
public String toString() {
75+
return getAllErrors().size() + " validation errors " +
76+
"for " + (isForReturnValue() ? "return value" : "arguments") + " of " +
77+
this.method.toGenericString();
78+
}
79+
80+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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.method;
18+
19+
import java.lang.reflect.Method;
20+
import java.util.Collections;
21+
import java.util.List;
22+
23+
/**
24+
* {@link MethodValidationResult} with an empty list of results.
25+
*
26+
* @author Rossen Stoyanchev
27+
* @since 6.1
28+
*/
29+
final class EmptyMethodValidationResult implements MethodValidationResult {
30+
31+
@Override
32+
public Object getTarget() {
33+
throw new UnsupportedOperationException();
34+
}
35+
36+
@Override
37+
public Method getMethod() {
38+
throw new UnsupportedOperationException();
39+
}
40+
41+
@Override
42+
public boolean isForReturnValue() {
43+
throw new UnsupportedOperationException();
44+
}
45+
46+
@Override
47+
public List<ParameterValidationResult> getAllValidationResults() {
48+
return Collections.emptyList();
49+
}
50+
51+
@Override
52+
public String toString() {
53+
return "0 validation errors";
54+
}
55+
56+
}
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.validation.beanvalidation;
17+
package org.springframework.validation.method;
1818

1919
import java.lang.reflect.Method;
2020
import java.util.List;
+22-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.validation.beanvalidation;
17+
package org.springframework.validation.method;
1818

1919
import java.lang.reflect.Method;
2020
import java.util.List;
@@ -101,4 +101,25 @@ default List<ParameterErrors> getBeanResults() {
101101
.toList();
102102
}
103103

104+
105+
/**
106+
* Factory method to create a {@link MethodValidationResult} instance.
107+
* @param target the target Object
108+
* @param method the target method
109+
* @param results method validation results, expected to be non-empty
110+
* @return the created instance
111+
*/
112+
static MethodValidationResult create(Object target, Method method, List<ParameterValidationResult> results) {
113+
return new DefaultMethodValidationResult(target, method, results);
114+
}
115+
116+
/**
117+
* Factory method to create a {@link MethodValidationResult} instance with
118+
* 0 errors, suitable to use as a constant. Getters for a target object or
119+
* method are not supported.
120+
*/
121+
static MethodValidationResult emptyResult() {
122+
return new EmptyMethodValidationResult();
123+
}
124+
104125
}
+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.validation.beanvalidation;
17+
package org.springframework.validation.method;
1818

1919
import java.lang.reflect.Method;
2020

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.validation.beanvalidation;
17+
package org.springframework.validation.method;
1818

1919
import java.util.List;
2020

Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.validation.beanvalidation;
17+
package org.springframework.validation.method;
1818

1919
import java.util.Collection;
2020
import java.util.List;

0 commit comments

Comments
 (0)