Search in sources :

Example 6 with ElementValidation

use of org.androidannotations.ElementValidation in project androidannotations by androidannotations.

the class ValidatorParameterHelperTest method inOrderFail.

@Test
public void inOrderFail() throws Exception {
    ExecutableElement executableElement = createMethod(AdapterView.class, long.class, boolean.class, Bundle.class);
    ElementValidation valid = new ElementValidation("", executableElement);
    //
    validator.inOrder().type(boolean.class.getName()).optional().type(long.class.getName()).optional().type(int.class.getName()).optional().validate(executableElement, valid);
    assertFalse(valid.isValid());
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) ElementValidation(org.androidannotations.ElementValidation) Test(org.junit.Test)

Example 7 with ElementValidation

use of org.androidannotations.ElementValidation in project androidannotations by androidannotations.

the class BaseAnnotationHandler method validate.

@Override
public ElementValidation validate(Element element) {
    ElementValidation validation = new ElementValidation(target, element);
    validate(element, validation);
    return validation;
}
Also used : ElementValidation(org.androidannotations.ElementValidation)

Example 8 with ElementValidation

use of org.androidannotations.ElementValidation in project androidannotations by androidannotations.

the class ValidatorParameterHelperTest method optionalStringParam.

@Test
public void optionalStringParam() throws Exception {
    ExecutableElement executableElement = createMethod(String.class);
    ElementValidation valid = new ElementValidation("", executableElement);
    validator.type(CanonicalNameConstants.STRING).optional().validate(executableElement, valid);
    assertTrue(valid.isValid());
    ExecutableElement withoutArgument = createMethod();
    ElementValidation valid2 = new ElementValidation("", executableElement);
    validator.type(CanonicalNameConstants.STRING).optional().validate(withoutArgument, valid2);
    assertTrue(valid2.isValid());
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) ElementValidation(org.androidannotations.ElementValidation) Test(org.junit.Test)

Example 9 with ElementValidation

use of org.androidannotations.ElementValidation 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)

Example 10 with ElementValidation

use of org.androidannotations.ElementValidation in project androidannotations by androidannotations.

the class ValidatorParameterHelperTest method anyOfTypes.

@Test
public void anyOfTypes() throws Exception {
    ExecutableElement executableElement = createMethod(Integer.class, Long.class);
    ElementValidation valid = new ElementValidation("", executableElement);
    validator.anyOfTypes(CanonicalNameConstants.INTEGER, CanonicalNameConstants.LONG).multiple().validate(executableElement, valid);
    assertTrue(valid.isValid());
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) ElementValidation(org.androidannotations.ElementValidation) Test(org.junit.Test)

Aggregations

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