Search in sources :

Example 1 with InjectionPoint

use of io.micronaut.inject.InjectionPoint in project micronaut-core by micronaut-projects.

the class DefaultValidator method validateBeanArgument.

@Override
public <T> void validateBeanArgument(@NonNull BeanResolutionContext resolutionContext, @NonNull InjectionPoint injectionPoint, @NonNull Argument<T> argument, int index, @Nullable T value) throws BeanInstantiationException {
    final AnnotationMetadata annotationMetadata = argument.getAnnotationMetadata();
    final boolean hasValid = annotationMetadata.hasStereotype(Valid.class);
    final boolean hasConstraint = annotationMetadata.hasStereotype(Constraint.class);
    final Class<T> parameterType = argument.getType();
    final Class rootClass = injectionPoint.getDeclaringBean().getBeanType();
    DefaultConstraintValidatorContext context = new DefaultConstraintValidatorContext(value);
    Set overallViolations = new HashSet<>(5);
    if (hasConstraint) {
        final Path.Node parentNode = context.addConstructorNode(rootClass.getName(), injectionPoint.getDeclaringBean().getConstructor().getArguments());
        ValueExtractor<Object> valueExtractor = (ValueExtractor<Object>) valueExtractorRegistry.findValueExtractor(parameterType).orElse(null);
        if (valueExtractor != null) {
            valueExtractor.extractValues(value, new ValueExtractor.ValueReceiver() {

                @Override
                public void value(String nodeName, Object object1) {
                }

                @Override
                public void iterableValue(String nodeName, Object iterableValue) {
                    if (iterableValue != null && context.validatedObjects.contains(iterableValue)) {
                        return;
                    }
                    cascadeToIterableValue(context, rootClass, null, value, parentNode, argument, iterableValue, overallViolations, null, null, true);
                }

                @Override
                public void indexedValue(String nodeName, int i, Object iterableValue) {
                    if (iterableValue != null && context.validatedObjects.contains(iterableValue)) {
                        return;
                    }
                    cascadeToIterableValue(context, rootClass, null, value, parentNode, argument, iterableValue, overallViolations, i, null, true);
                }

                @Override
                public void keyedValue(String nodeName, Object key, Object keyedValue) {
                    if (keyedValue != null && context.validatedObjects.contains(keyedValue)) {
                        return;
                    }
                    cascadeToIterableValue(context, rootClass, null, value, parentNode, argument, keyedValue, overallViolations, null, key, false);
                }
            });
        } else {
            validateParameterInternal(rootClass, null, ArrayUtils.EMPTY_OBJECT_ARRAY, context, overallViolations, argument.getName(), parameterType, index, annotationMetadata, value);
        }
        context.removeLast();
    } else if (hasValid && value != null) {
        final BeanIntrospection<Object> beanIntrospection = getBeanIntrospection(value, parameterType);
        if (beanIntrospection != null) {
            try {
                context.addParameterNode(argument.getName(), index);
                cascadeToOneIntrospection(context, null, value, beanIntrospection, overallViolations);
            } finally {
                context.removeLast();
            }
        }
    }
    failOnError(resolutionContext, overallViolations, rootClass);
}
Also used : Path(javax.validation.Path) ValueExtractor(javax.validation.valueextraction.ValueExtractor) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) InjectionPoint(io.micronaut.inject.InjectionPoint) Constraint(javax.validation.Constraint) BeanIntrospection(io.micronaut.core.beans.BeanIntrospection)

Aggregations

AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)1 BeanIntrospection (io.micronaut.core.beans.BeanIntrospection)1 InjectionPoint (io.micronaut.inject.InjectionPoint)1 Constraint (javax.validation.Constraint)1 Path (javax.validation.Path)1 ValueExtractor (javax.validation.valueextraction.ValueExtractor)1