3030
3131
3232/**
33- * Internal validation service. It handles static validation, and by contract validation thanks to Validator and ExecutableValidator.
34- *
35- 36- 33+ * Handles static validation, and "by contract" validation thanks to Validator and ExecutableValidator.
3734 */
3835class ValidationServiceInternal implements ValidationService {
3936 private static final Logger LOGGER = LoggerFactory .getLogger (ValidationServiceInternal .class );
@@ -46,7 +43,6 @@ class ValidationServiceInternal implements ValidationService {
4643
4744 @ Override
4845 public <T > void staticallyHandle (T candidate ) {
49-
5046 Set <ConstraintViolation <T >> constraintViolations = validator .validate (candidate );
5147
5248 if (!constraintViolations .isEmpty ()) {
@@ -69,12 +65,12 @@ public <T> void staticallyHandle(T candidate) {
6965 exceptionMessage .append (rootBeanClass .getName ()).append ("\n " );
7066 first = false ;
7167 }
72- exceptionMessage .append ("\t " ).append (violation .getPropertyPath ()).append (" - " ).append (violation .getMessage ()).append (", but " ).append (violation .getInvalidValue ()).append (" was found.\n " );
68+ exceptionMessage .append ("\t " ).append (violation .getPropertyPath ()).append (" - " ).append (violation .getMessage ())
69+ .append (", but " ).append (violation .getInvalidValue ()).append (" was found.\n " );
7370
7471 ++i ;
7572 }
7673
77-
7874 newException .put ("message" , exceptionMessage );
7975 newException .put (JAVAX_VALIDATION_CONSTRAINT_VIOLATIONS , constraintViolations );
8076 throw newException ;
@@ -89,7 +85,7 @@ public Object dynamicallyHandleAndProceed(MethodInvocation invocation) throws Th
8985 throw SeedException .createNew (ValidationErrorCode .DYNAMIC_VALIDATION_IS_NOT_SUPPORTED );
9086 }
9187
92- // TODO : ajout des groupes
88+ // TODO : add groups
9389 Object this1 = invocation .getThis ();
9490 Method method = invocation .getMethod ();
9591 Object [] arguments = invocation .getArguments ();
@@ -132,66 +128,52 @@ private void handleConstraintViolations(Set<ConstraintViolation<Object>> constra
132128
133129 @ Override
134130 public boolean candidateForStaticValidation (Class <?> candidate ) {
135- boolean isCandidate = false ;
136-
137- // look for fields
138131 for (Field field : candidate .getDeclaredFields ()) {
139132 for (Annotation annotation : field .getAnnotations ()) {
140- if (SeedReflectionUtils .hasAnnotationDeep (annotation .annotationType (), Constraint .class ) || Valid .class .equals (annotation .annotationType ())) {
141- // check if the annotation type is itself annotated with
142- // Constraint making it a constraint
143- isCandidate = true ;
144- break ;
133+ if (hasConstraintOrValidAnnotation (annotation )) {
134+ return true ;
145135 }
146136 }
147- if (isCandidate ) {
148- break ;
149- }
150137 }
138+ return false ;
139+ }
151140
152- return isCandidate ;
141+ private boolean hasConstraintOrValidAnnotation (Annotation annotation ) {
142+ return SeedReflectionUtils .hasAnnotationDeep (annotation .annotationType (), Constraint .class ) || Valid .class .equals (annotation .annotationType ());
153143 }
154144
155145 @ Override
156- public boolean candidateForDynamicValidation (Method candidate ) {
157- boolean isCandidate = false ;
158- // Check Parameters
159- for (Annotation [] annotationsForOneParameter : candidate .getParameterAnnotations ()) {
160- for (Annotation annotation : annotationsForOneParameter ) {
161- if (SeedReflectionUtils .hasAnnotationDeep (annotation .annotationType (), Constraint .class ) || Valid .class .equals (annotation .annotationType ())) {
162- // check if the annotation type is itself annotated with Constraint making it a constraint
163- isCandidate = true ;
164- break ;
165- }
166- }
167- if (isCandidate ) {
168- break ;
146+ public boolean candidateForDynamicValidation (Class <?> candidate ) {
147+ for (Method method : candidate .getDeclaredMethods ()) {
148+ if (candidateForDynamicValidation (method )) {
149+ return true ;
169150 }
170151 }
152+ return false ;
153+ }
154+
155+ @ Override
156+ public boolean candidateForDynamicValidation (Method candidate ) {
157+ return shouldValidateParameters (candidate ) || shouldValidateReturnType (candidate );
158+ }
171159
172- // Check returned type
173- if (! isCandidate ) {
174- for (Annotation annotation : candidate . getAnnotations () ) {
175- if (SeedReflectionUtils . hasAnnotationDeep (annotation . annotationType (), Constraint . class ) || Valid . class . equals ( annotation . annotationType () )) {
176- isCandidate = true ;
160+ private boolean shouldValidateParameters ( Method candidate ) {
161+ for ( Annotation [] annotationsForOneParameter : candidate . getParameterAnnotations () ) {
162+ for (Annotation annotation : annotationsForOneParameter ) {
163+ if (hasConstraintOrValidAnnotation (annotation )) {
164+ return true ;
177165 }
178166 }
179167 }
180-
181- return isCandidate ;
182-
168+ return false ;
183169 }
184170
185- @ Override
186- public boolean candidateForDynamicValidation (Class <?> candidate ) {
187- // look for methods
188-
189- for (Method method : candidate .getDeclaredMethods ()) {
190- if (candidateForDynamicValidation (method )) {
171+ private boolean shouldValidateReturnType (Method candidate ) {
172+ for (Annotation annotation : candidate .getAnnotations ()) {
173+ if (hasConstraintOrValidAnnotation (annotation )) {
191174 return true ;
192175 }
193176 }
194-
195177 return false ;
196178 }
197179
0 commit comments