Search in sources :

Example 1 with ValidationException

use of org.androidannotations.internal.exception.ValidationException in project androidannotations by androidannotations.

the class ModelValidator method validate.

public AnnotationElements validate(AnnotationElements extractedModel, AnnotationElementsHolder validatingHolder) throws ValidationException {
    LOGGER.info("Validating elements");
    List<ElementValidation> failedValidations = new ArrayList<>();
    for (AnnotationHandler annotationHandler : environment.getHandlers()) {
        if (!annotationHandler.isEnabled()) {
            continue;
        }
        String validatorSimpleName = annotationHandler.getClass().getSimpleName();
        String annotationName = annotationHandler.getTarget();
        Set<? extends Element> annotatedElements = extractedModel.getRootAnnotatedElements(annotationName);
        Set<Element> validatedAnnotatedElements = new LinkedHashSet<>();
        validatingHolder.putRootAnnotatedElements(annotationName, validatedAnnotatedElements);
        if (!annotatedElements.isEmpty()) {
            LOGGER.debug("Validating with {}: {}", validatorSimpleName, annotatedElements);
        }
        for (Element annotatedElement : annotatedElements) {
            ElementValidation elementValidation = annotationHandler.validate(annotatedElement);
            AnnotationMirror annotationMirror = elementValidation.getAnnotationMirror();
            for (ElementValidation.Error error : elementValidation.getErrors()) {
                LOGGER.error(error.getMessage(), error.getElement(), annotationMirror);
            }
            for (String warning : elementValidation.getWarnings()) {
                LOGGER.warn(warning, elementValidation.getElement(), elementValidation.getAnnotationMirror());
            }
            if (elementValidation.isValid()) {
                validatedAnnotatedElements.add(annotatedElement);
            } else {
                failedValidations.add(elementValidation);
                LOGGER.warn("Element {} invalidated by {}", annotatedElement, annotatedElement, validatorSimpleName);
            }
        }
    }
    if (!failedValidations.isEmpty()) {
        throw new ValidationException(failedValidations);
    }
    return validatingHolder;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) AnnotationMirror(javax.lang.model.element.AnnotationMirror) ValidationException(org.androidannotations.internal.exception.ValidationException) ElementValidation(org.androidannotations.ElementValidation) Element(javax.lang.model.element.Element) ArrayList(java.util.ArrayList) AnnotationHandler(org.androidannotations.handler.AnnotationHandler)

Aggregations

ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)1 AnnotationMirror (javax.lang.model.element.AnnotationMirror)1 Element (javax.lang.model.element.Element)1 ElementValidation (org.androidannotations.ElementValidation)1 AnnotationHandler (org.androidannotations.handler.AnnotationHandler)1 ValidationException (org.androidannotations.internal.exception.ValidationException)1