Search in sources :

Example 6 with Start

use of com.github.mvp4g.mvp4g2.core.eventbus.annotation.Start in project mvp4g2 by mvp4g.

the class EventBusAnnotationValidator method validate.

public void validate(Element element) throws ProcessorException {
    // get elements annotated with EventBus annotation
    Set<? extends Element> elementsWithEventBusAnnotation = this.roundEnvironment.getElementsAnnotatedWith(EventBus.class);
    // annotated element has to be a interface
    if (element instanceof TypeElement) {
        TypeElement typeElement = (TypeElement) element;
        // Eventbus must be an interface!
        if (!typeElement.getKind().isInterface()) {
            throw new ProcessorException("Mvp4g2Processor: @Eventbus can only be used with an interface");
        }
        // check, that the typeElement extends IsEventBus
        if (!this.processorUtils.extendsClassOrInterface(this.processingEnvironment.getTypeUtils(), typeElement.asType(), this.processingEnvironment.getElementUtils().getTypeElement(IsEventBus.class.getCanonicalName()).asType())) {
            throw new ProcessorException(typeElement.getSimpleName().toString() + ": @Eventbus must extend IsEventBus.class!");
        }
        // @Start validation
        // check, if there are more than @Start annotated method
        List<Element> elementsAnnotatedWithStart = this.processorUtils.getMethodFromTypeElementAnnotatedWith(this.processingEnvironment, typeElement, Start.class);
        if (elementsAnnotatedWithStart.size() > 1) {
            throw new ProcessorException("Mvp4g2Processor: @Start-annotation can only be used a single time in a eventbus interface");
        } else {
            // check, that the @Start annotation is only used on zero-argument methods!
            if (elementsAnnotatedWithStart.size() == 1) {
                ExecutableElement executableElement = (ExecutableElement) elementsAnnotatedWithStart.get(0);
                if (executableElement.getParameters().size() > 0) {
                    throw new ProcessorException("Mvp4g2Processor: @Start-annotation can only be used on zero argument methods");
                }
            }
        }
    } else {
        throw new ProcessorException("Mvp4g2Processor: @Eventbus can only be used on a type (interface)");
    }
}
Also used : ProcessorException(com.github.mvp4g.mvp4g2.processor.ProcessorException) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement)

Aggregations

Mvp4g2Processor (com.github.mvp4g.mvp4g2.processor.Mvp4g2Processor)4 Compilation (com.google.testing.compile.Compilation)4 JavaFileObject (javax.tools.JavaFileObject)4 Test (org.junit.Test)4 Element (javax.lang.model.element.Element)2 ExecutableElement (javax.lang.model.element.ExecutableElement)2 TypeElement (javax.lang.model.element.TypeElement)2 Event (com.github.mvp4g.mvp4g2.core.eventbus.annotation.Event)1 Start (com.github.mvp4g.mvp4g2.core.eventbus.annotation.Start)1 InitHistory (com.github.mvp4g.mvp4g2.core.history.annotation.InitHistory)1 NotFoundHistory (com.github.mvp4g.mvp4g2.core.history.annotation.NotFoundHistory)1 ProcessorException (com.github.mvp4g.mvp4g2.processor.ProcessorException)1 EventMetaModel (com.github.mvp4g.mvp4g2.processor.model.EventMetaModel)1 EventAnnotationValidator (com.github.mvp4g.mvp4g2.processor.scanner.validation.EventAnnotationValidator)1