18
18
19
19
import java .lang .reflect .Method ;
20
20
import java .util .ArrayList ;
21
+ import java .util .Collections ;
21
22
import java .util .Comparator ;
22
23
import java .util .Iterator ;
23
24
import java .util .LinkedHashMap ;
@@ -73,6 +74,8 @@ public class MethodValidationAdapter {
73
74
74
75
private static final Comparator <ParameterValidationResult > RESULT_COMPARATOR = new ResultComparator ();
75
76
77
+ private static final MethodValidationResult EMPTY_RESULT = new EmptyMethodValidationResult ();
78
+
76
79
77
80
private final Supplier <Validator > validator ;
78
81
@@ -186,15 +189,19 @@ public static Class<?>[] determineValidationGroups(Object target, Method method)
186
189
}
187
190
188
191
/**
189
- * Validate the given method arguments and raise {@link ConstraintViolation}
190
- * in case of any errors.
192
+ * Validate the given method arguments and return the result of validation.
191
193
* @param target the target Object
192
194
* @param method the target method
193
195
* @param arguments candidate arguments for a method invocation
194
196
* @param groups groups for validation determined via
195
197
* {@link #determineValidationGroups(Object, Method)}
198
+ * @return a result with {@link ConstraintViolation violations} and
199
+ * {@link ParameterValidationResult validationResults}, both possibly empty
200
+ * in case there are no violations
196
201
*/
197
- public void validateMethodArguments (Object target , Method method , Object [] arguments , Class <?>[] groups ) {
202
+ public MethodValidationResult validateMethodArguments (
203
+ Object target , Method method , Object [] arguments , Class <?>[] groups ) {
204
+
198
205
ExecutableValidator execVal = this .validator .get ().forExecutables ();
199
206
Set <ConstraintViolation <Object >> result ;
200
207
try {
@@ -207,28 +214,26 @@ public void validateMethodArguments(Object target, Method method, Object[] argum
207
214
Method bridgedMethod = BridgeMethodResolver .findBridgedMethod (mostSpecificMethod );
208
215
result = execVal .validateParameters (target , bridgedMethod , arguments , groups );
209
216
}
210
- if (!result .isEmpty ()) {
211
- throw createException (target , method , result , i -> arguments [i ]);
212
- }
217
+ return (result .isEmpty () ? EMPTY_RESULT : createException (target , method , result , i -> arguments [i ]));
213
218
}
214
219
215
220
/**
216
- * Validate the given return value and raise {@link ConstraintViolation}
217
- * in case of any errors.
221
+ * Validate the given return value and return the result of validation.
218
222
* @param target the target Object
219
223
* @param method the target method
220
224
* @param returnValue value returned from invoking the target method
221
225
* @param groups groups for validation determined via
222
226
* {@link #determineValidationGroups(Object, Method)}
227
+ * @return a result with {@link ConstraintViolation violations} and
228
+ * {@link ParameterValidationResult validationResults}, both possibly empty
229
+ * in case there are no violations
223
230
*/
224
- public void validateMethodReturnValue (
231
+ public MethodValidationResult validateMethodReturnValue (
225
232
Object target , Method method , @ Nullable Object returnValue , Class <?>[] groups ) {
226
233
227
234
ExecutableValidator execVal = this .validator .get ().forExecutables ();
228
235
Set <ConstraintViolation <Object >> result = execVal .validateReturnValue (target , method , returnValue , groups );
229
- if (!result .isEmpty ()) {
230
- throw createException (target , method , result , i -> returnValue );
231
- }
236
+ return (result .isEmpty () ? EMPTY_RESULT : createException (target , method , result , i -> returnValue ));
232
237
}
233
238
234
239
private MethodValidationException createException (
@@ -275,7 +280,7 @@ else if (node.getKind().equals(ElementKind.RETURN_VALUE)) {
275
280
cascadedViolations .forEach ((node , builder ) -> validatonResultList .add (builder .build ()));
276
281
validatonResultList .sort (RESULT_COMPARATOR );
277
282
278
- return new MethodValidationException (target , method , validatonResultList , violations );
283
+ return new MethodValidationException (target , method , violations , validatonResultList );
279
284
}
280
285
281
286
/**
@@ -470,4 +475,36 @@ private <E> int compareKeys(ParameterErrors errors1, ParameterErrors errors2) {
470
475
}
471
476
}
472
477
478
+
479
+ /**
480
+ * {@link MethodValidationResult} to use when there are no violations.
481
+ */
482
+ private static final class EmptyMethodValidationResult implements MethodValidationResult {
483
+
484
+ @ Override
485
+ public Set <ConstraintViolation <?>> getConstraintViolations () {
486
+ return Collections .emptySet ();
487
+ }
488
+
489
+ @ Override
490
+ public List <ParameterValidationResult > getAllValidationResults () {
491
+ return Collections .emptyList ();
492
+ }
493
+
494
+ @ Override
495
+ public List <ParameterValidationResult > getValueResults () {
496
+ return Collections .emptyList ();
497
+ }
498
+
499
+ @ Override
500
+ public List <ParameterErrors > getBeanResults () {
501
+ return Collections .emptyList ();
502
+ }
503
+
504
+ @ Override
505
+ public void throwIfViolationsPresent () {
506
+ }
507
+
508
+ }
509
+
473
510
}
0 commit comments