Skip to content

Commit 8fb5c98

Browse files
author
pith
committed
Methods refactoring
1 parent 0729a5b commit 8fb5c98

File tree

6 files changed

+51
-106
lines changed

6 files changed

+51
-106
lines changed

src/main/java/org/seedstack/validation/internal/ValidationErrorCode.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,8 @@
77
*/
88
package org.seedstack.validation.internal;
99

10-
1110
import org.seedstack.seed.ErrorCode;
1211

13-
/**
14-
* Enumerates all validation error codes.
15-
*
16-
17-
18-
*/
1912
enum ValidationErrorCode implements ErrorCode {
2013
VALIDATION_ISSUE,
2114
DYNAMIC_VALIDATION_IS_NOT_SUPPORTED

src/main/java/org/seedstack/validation/internal/ValidationExceptionMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* License, v. 2.0. If a copy of the MPL was not distributed with this
66
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
77
*/
8-
package org.seedstack.validation.internal;
8+
package org.seedstack.validation.internal;
99

1010
import org.seedstack.validation.ValidationException;
1111

src/main/java/org/seedstack/validation/internal/ValidationMethodInterceptor.java

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,11 @@
77
*/
88
package org.seedstack.validation.internal;
99

10-
1110
import org.aopalliance.intercept.MethodInterceptor;
1211
import org.aopalliance.intercept.MethodInvocation;
13-
import org.slf4j.Logger;
14-
import org.slf4j.LoggerFactory;
15-
1612
import org.seedstack.validation.ValidationService;
1713

18-
import static org.seedstack.seed.core.utils.SeedReflectionUtils.cleanProxy;
19-
20-
2114
class ValidationMethodInterceptor implements MethodInterceptor {
22-
private static final Logger LOGGER = LoggerFactory.getLogger(ValidationMethodInterceptor.class);
2315
private final ValidationService validationService;
2416

2517
ValidationMethodInterceptor(ValidationService validationService) {
@@ -28,13 +20,7 @@ class ValidationMethodInterceptor implements MethodInterceptor {
2820

2921
@Override
3022
public Object invoke(MethodInvocation invocation) throws Throwable {
31-
32-
try {
33-
LOGGER.debug("Validation of {}", cleanProxy(invocation.getClass()));
34-
return this.validationService.dynamicallyHandleAndProceed(invocation);
35-
} finally {
36-
LOGGER.debug("End of validation of {}", cleanProxy(invocation.getClass()));
37-
}
23+
return this.validationService.dynamicallyHandleAndProceed(invocation);
3824
}
3925

4026
}

src/main/java/org/seedstack/validation/internal/ValidationModule.java

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,23 @@
77
*/
88
package org.seedstack.validation.internal;
99

10-
import java.lang.reflect.Method;
11-
12-
import javax.validation.Validator;
13-
import javax.validation.ValidatorFactory;
14-
import javax.validation.executable.ExecutableValidator;
15-
16-
import org.seedstack.seed.core.internal.CorePlugin;
17-
import org.seedstack.validation.spi.ValidationConcern;
18-
import org.seedstack.validation.ValidationService;
19-
import org.slf4j.Logger;
20-
import org.slf4j.LoggerFactory;
21-
2210
import com.google.inject.AbstractModule;
2311
import com.google.inject.Binding;
2412
import com.google.inject.Provides;
2513
import com.google.inject.matcher.AbstractMatcher;
2614
import com.google.inject.matcher.Matcher;
2715
import com.google.inject.matcher.Matchers;
2816
import com.google.inject.spi.ProvisionListener;
17+
import org.seedstack.seed.core.internal.CorePlugin;
18+
import org.seedstack.validation.ValidationService;
19+
import org.seedstack.validation.spi.ValidationConcern;
20+
import org.slf4j.Logger;
21+
import org.slf4j.LoggerFactory;
22+
23+
import javax.validation.Validator;
24+
import javax.validation.ValidatorFactory;
25+
import javax.validation.executable.ExecutableValidator;
26+
import java.lang.reflect.Method;
2927

3028
@ValidationConcern
3129
class ValidationModule extends AbstractModule {
@@ -45,30 +43,25 @@ class ValidationModule extends AbstractModule {
4543
@Override
4644
protected void configure() {
4745
this.validator = factory.getValidator();
46+
enableValidationOnInjectionPoints();
47+
configureDynamicValidation();
48+
}
4849

49-
// ===================================
50-
// Configuration for static validation
51-
// ===================================
52-
final ProvisionListener provisionListener = new ProvisionListener() {
50+
private void enableValidationOnInjectionPoints() {
51+
bindListener(staticMatcher(validationService), new ProvisionListener() {
5352

5453
@Override
5554
public <A> void onProvision(ProvisionInvocation<A> provision) {
5655
A injectee = provision.provision();
57-
if (LOGGER.isDebugEnabled()) {
58-
LOGGER.debug("Starting validation of {}", injectee);
59-
}
6056
validationService.staticallyHandle(injectee);
61-
6257
}
63-
};
64-
bindListener(staticMatcher(validationService), provisionListener);
58+
});
59+
}
6560

66-
// ====================================
67-
// Configuration for dynamic validation
68-
// ====================================
61+
private void configureDynamicValidation() {
6962
try {
7063
this.executableValidator = validator.forExecutables();
71-
} catch(Throwable t) {
64+
} catch (Throwable t) {
7265
LOGGER.info("Unable to create the dynamic validator, support for dynamic validation disabled");
7366
LOGGER.debug(CorePlugin.DETAILS_MESSAGE, t);
7467
}

src/main/java/org/seedstack/validation/internal/ValidationPlugin.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,6 @@ public Object nativeUnitModule() {
3737
return validationModule;
3838
}
3939

40-
/**
41-
* Get the validation service.
42-
*
43-
* @return the validation service.
44-
*/
45-
public ValidationService getValidationService() {
46-
return validationService;
47-
}
48-
4940
@Override
5041
public void stop() {
5142
if (factory != null) {

src/main/java/org/seedstack/validation/internal/ValidationServiceInternal.java

Lines changed: 30 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@
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
*/
3835
class 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

Comments
 (0)