package io.micronaut.validation.validator;
import io.micronaut.core.annotation.Internal;
import io.micronaut.core.io.service.ServiceDefinition;
import io.micronaut.core.io.service.SoftServiceLoader;
import io.micronaut.core.reflect.GenericTypeUtils;
import io.micronaut.core.util.ArrayUtils;
import io.micronaut.inject.annotation.AnnotatedElementValidator;
import io.micronaut.inject.qualifiers.TypeArgumentQualifier;
import io.micronaut.validation.validator.constraints.ConstraintValidator;
import io.micronaut.validation.validator.constraints.DefaultConstraintValidators;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
@Internal
public class DefaultAnnotatedElementValidator extends DefaultValidator implements AnnotatedElementValidator {
public DefaultAnnotatedElementValidator() {
super(new DefaultValidatorConfiguration()
.setConstraintValidatorRegistry(new LocalConstraintValidators()));
}
private static class LocalConstraintValidators extends DefaultConstraintValidators {
private Map<ValidatorKey, ConstraintValidator> validatorMap;
@Override
protected <A extends Annotation, T> Optional<ConstraintValidator> findLocalConstraintValidator(@NonNull Class<A> constraintType, @NonNull Class<T> targetType) {
return findConstraintValidatorFromServiceLoader(constraintType, targetType);
}
private <A extends Annotation, T> Optional<ConstraintValidator> findConstraintValidatorFromServiceLoader(Class<A> constraintType, Class<T> targetType) {
if (validatorMap == null) {
validatorMap = initializeValidatorMap();
}
return validatorMap.entrySet().stream()
.filter(entry -> {
final ValidatorKey key = entry.getKey();
final Class[] left = {constraintType, targetType};
return TypeArgumentQualifier.areTypesCompatible(
left,
Arrays.asList(key.getConstraintType(), key.getTargetType())
);
})
.findFirst().map(Map.Entry::getValue);
}
private Map<ValidatorKey, ConstraintValidator> initializeValidatorMap() {
validatorMap = new HashMap<>();
final SoftServiceLoader<ConstraintValidator> constraintValidators = SoftServiceLoader.load(ConstraintValidator.class);
for (ServiceDefinition<ConstraintValidator> constraintValidator : constraintValidators) {
if (constraintValidator.isPresent()) {
try {
final ConstraintValidator validator = constraintValidator.load();
final Class[] typeArgs = GenericTypeUtils.resolveInterfaceTypeArguments(validator.getClass(), ConstraintValidator.class);
if (ArrayUtils.isNotEmpty(typeArgs) && typeArgs.length == 2) {
validatorMap.put(
new ValidatorKey(typeArgs[0], typeArgs[1]),
validator
);
}
} catch (Exception e) {
System.err.println("WARNING: Could not validator [" + constraintValidator.getName() + "]: " + e.getMessage());
}
}
}
return validatorMap;
}
}
}