Search in sources :

Example 16 with EventBus

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

the class EventAnnotationScanner method scan.

public EventBusMetaModel scan(RoundEnvironment roundEnvironment) throws ProcessorException {
    EventAnnotationValidator eventAnnotationValidator = EventAnnotationValidator.builder().roundEnvironment(roundEnvironment).processingEnvironment(processingEnvironment).eventBusMetaModel(this.eventBusMetaModel).eventBusTypeElement(eventBusTypeElement).build();
    // validate event bus
    eventAnnotationValidator.validate();
    // handle events
    for (Element element : roundEnvironment.getElementsAnnotatedWith(Event.class)) {
        // do validation
        EventAnnotationValidator.builder().roundEnvironment(roundEnvironment).processingEnvironment(processingEnvironment).eventBusMetaModel(this.eventBusMetaModel).eventBusTypeElement(eventBusTypeElement).build().validate(element);
        ExecutableElement executableElement = (ExecutableElement) element;
        // restore meta data (if there is one)
        // First we try to read an already created resource ...
        EventMetaModel model = this.restore(element);
        // get event annoation
        Event eventAnnotation = element.getAnnotation(Event.class);
        // scan for data
        // internal event name
        model.setEventInternalName(this.processorUtils.createInternalEventName(executableElement));
        // event name
        model.setEventName(executableElement.getSimpleName().toString());
        // history name of hte event
        model.setHistoryEventName(eventAnnotation.historyName());
        // navigation event
        model.setNavigationEvent(Boolean.toString(eventAnnotation.navigationEvent()));
        // passive
        model.setPassive(Boolean.toString(eventAnnotation.passive()));
        // handlers
        model.setHandlers(this.getElementsFromAnnotationAsList(executableElement, "handlers"));
        // List of binding handlers (full class names as String)
        model.setBindings(this.getElementsFromAnnotationAsList(executableElement, "bind"));
        // List of activate handlers (full class names as String)
        model.setActivateHandlers(this.getElementsFromAnnotationAsList(executableElement, "activate"));
        // List of deactivate handlers (full class names as String)
        model.setDeactivateHandlers(this.getElementsFromAnnotationAsList(executableElement, "deactivate"));
        // history converter
        model.setHistoryConverter(Objects.requireNonNull(this.getHistoryConverterTypeElement(eventAnnotation)).getQualifiedName().toString());
        // start event?
        model.setStartEvent(isNull(executableElement.getAnnotation(Start.class)) ? "false" : "true");
        // initHistory event?
        model.setInitHistory(isNull(executableElement.getAnnotation(InitHistory.class)) ? "false" : "true");
        // start event?
        model.setNotFoundHistory(isNull(executableElement.getAnnotation(NotFoundHistory.class)) ? "false" : "true");
        // parameters
        executableElement.getParameters().forEach(v -> model.addParameter(v.getSimpleName().toString(), v.asType().toString()));
        // let's store the updated model
        this.processorUtils.store(model, this.createRelativeFileName(element));
        // store the event meta data model in the eventbus meta data model
        this.eventBusMetaModel.add(model);
    }
    return this.eventBusMetaModel;
}
Also used : InitHistory(com.github.mvp4g.mvp4g2.core.history.annotation.InitHistory) NotFoundHistory(com.github.mvp4g.mvp4g2.core.history.annotation.NotFoundHistory) EventAnnotationValidator(com.github.mvp4g.mvp4g2.processor.scanner.validation.EventAnnotationValidator) Start(com.github.mvp4g.mvp4g2.core.eventbus.annotation.Start) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) ExecutableElement(javax.lang.model.element.ExecutableElement) Event(com.github.mvp4g.mvp4g2.core.eventbus.annotation.Event) EventMetaModel(com.github.mvp4g.mvp4g2.processor.model.EventMetaModel)

Example 17 with EventBus

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

the class EventBusAnnotationScanner method scan.

public EventBusMetaModel scan(RoundEnvironment roundEnvironment) throws ProcessorException {
    // First we try to read an already created resource ...
    EventBusMetaModel model = this.restore();
    // Check if we have an element annotated with @Application
    if (!roundEnvironment.getElementsAnnotatedWith(EventBus.class).isEmpty()) {
        // create validator
        EventBusAnnotationValidator eventBusValidaitor = EventBusAnnotationValidator.builder().roundEnvironment(roundEnvironment).processingEnvironment(processingEnvironment).build();
        // check, whether we have o do something ...
        eventBusValidaitor.validate();
        // should only be one, so we can search for the first! ...
        Optional<? extends Element> optionalElement = this.roundEnvironment.getElementsAnnotatedWith(EventBus.class).stream().findFirst();
        if (optionalElement.isPresent()) {
            Element eventBusAnnotationElement = optionalElement.get();
            eventBusValidaitor.validate(eventBusAnnotationElement);
            EventBus eventBusAnnotation = eventBusAnnotationElement.getAnnotation(EventBus.class);
            if (!isNull(eventBusAnnotation)) {
                TypeElement shellTypeElement = this.getShellTypeElement(eventBusAnnotation);
                model = new EventBusMetaModel(eventBusAnnotationElement.toString(), isNull(shellTypeElement) ? "" : shellTypeElement.toString());
                // Debug-Annotation
                model = DebugAnnotationScanner.builder().processingEnvironment(processingEnvironment).eventBusTypeElement((TypeElement) eventBusAnnotationElement).eventBusMetaModel(model).build().scan(roundEnvironment);
                // Filters-Annotation
                model = FiltersAnnotationScanner.builder().processingEnvironment(processingEnvironment).eventBusTypeElement((TypeElement) eventBusAnnotationElement).eventBusMetaModel(model).build().scan(roundEnvironment);
                // handle Event-annotation
                model = EventAnnotationScanner.builder().processingEnvironment(processingEnvironment).eventBusTypeElement((TypeElement) eventBusAnnotationElement).eventBusMetaModel(model).build().scan(roundEnvironment);
                // let's store the updated model
                this.processorUtils.store(model, this.createRelativeFileName());
            }
        }
    } else {
        // @Debug annotation with no @EventBus annotation ... bad idea!
        Optional<? extends Element> optionalDebugElement = this.roundEnvironment.getElementsAnnotatedWith(Debug.class).stream().findFirst();
        if (optionalDebugElement.isPresent()) {
            EventBus eventBusAnnotation = optionalDebugElement.get().getAnnotation(EventBus.class);
            if (eventBusAnnotation == null) {
                throw new ProcessorException(((TypeElement) optionalDebugElement.get()).getQualifiedName().toString() + " -> @Debug can only be used with an interfaces annotated with @EventBus");
            }
        }
        // @Filters annotation with no @EventBus annotation ... bad idea!
        Optional<? extends Element> optionalFiltersElement = this.roundEnvironment.getElementsAnnotatedWith(Filters.class).stream().findFirst();
        if (optionalFiltersElement.isPresent()) {
            EventBus eventBusAnnotation = optionalFiltersElement.get().getAnnotation(EventBus.class);
            if (eventBusAnnotation == null) {
                throw new ProcessorException(((TypeElement) optionalFiltersElement.get()).getQualifiedName().toString() + " -> @Filters can only be used with an interfaces annotated with @EventBus");
            }
        }
    }
    return model;
}
Also used : ProcessorException(com.github.mvp4g.mvp4g2.processor.ProcessorException) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) EventBusMetaModel(com.github.mvp4g.mvp4g2.processor.model.EventBusMetaModel) EventBus(com.github.mvp4g.mvp4g2.core.eventbus.annotation.EventBus) EventBusAnnotationValidator(com.github.mvp4g.mvp4g2.processor.scanner.validation.EventBusAnnotationValidator)

Example 18 with EventBus

use of com.github.mvp4g.mvp4g2.core.eventbus.annotation.EventBus 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)

Example 19 with EventBus

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

the class FilterAnnotationValidator method validate.

public void validate() throws ProcessorException {
    // get elements annotated with EventBus annotation
    Set<? extends Element> elementsWithFilterAnnotation = this.roundEnvironment.getElementsAnnotatedWith(Filters.class);
    // at least there should only one Application annotation!
    if (elementsWithFilterAnnotation.size() > 1) {
        throw new ProcessorException("Mvp4g2Processor: There should be at least only one interface, that is annotated with @Filter");
    }
    // annotated element has to be a interface
    for (Element element : elementsWithFilterAnnotation) {
        if (element instanceof TypeElement) {
            TypeElement typeElement = (TypeElement) element;
            if (!typeElement.getKind().isInterface()) {
                throw new ProcessorException("Mvp4g2Processor: @Filter can only be used with an interface");
            }
            // @Filter can only be used on a interface that extends IsEventBus
            if (!this.processorUtils.extendsClassOrInterface(this.processingEnvironment.getTypeUtils(), typeElement.asType(), this.processingEnvironment.getElementUtils().getTypeElement(IsEventBus.class.getCanonicalName()).asType())) {
                throw new ProcessorException("Mvp4g2Processor: @Filter can only be used on interfaces that extends IsEventBus");
            }
            // test, that all filters implement IsEventFilter!
            List<String> eventFilterAsStringList = this.getEventFiltersAsList(eventBusTypeElement);
            for (String eventFilterClassname : eventFilterAsStringList) {
                TypeElement filterElement = this.processingEnvironment.getElementUtils().getTypeElement(eventFilterClassname);
                if (!this.processorUtils.extendsClassOrInterface(this.processingEnvironment.getTypeUtils(), filterElement.asType(), this.processingEnvironment.getElementUtils().getTypeElement(IsEventFilter.class.getCanonicalName()).asType())) {
                    throw new ProcessorException("Mvp4g2Processor: @Filter - the filters attribute needs class that implements IsEventFilter");
                }
            }
        } else {
            throw new ProcessorException("Mvp4g2Processor: @Filter can only be used on a type (interface)");
        }
    }
}
Also used : ProcessorException(com.github.mvp4g.mvp4g2.processor.ProcessorException) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement)

Aggregations

Mvp4g2Processor (com.github.mvp4g.mvp4g2.processor.Mvp4g2Processor)14 Compilation (com.google.testing.compile.Compilation)14 JavaFileObject (javax.tools.JavaFileObject)14 Test (org.junit.Test)14 TypeElement (javax.lang.model.element.TypeElement)5 ProcessorException (com.github.mvp4g.mvp4g2.processor.ProcessorException)4 Element (javax.lang.model.element.Element)4 ExecutableElement (javax.lang.model.element.ExecutableElement)2 Event (com.github.mvp4g.mvp4g2.core.eventbus.annotation.Event)1 EventBus (com.github.mvp4g.mvp4g2.core.eventbus.annotation.EventBus)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 AbstractPresenter (com.github.mvp4g.mvp4g2.core.ui.AbstractPresenter)1 IsShell (com.github.mvp4g.mvp4g2.core.ui.IsShell)1 IsViewCreator (com.github.mvp4g.mvp4g2.core.ui.IsViewCreator)1 Presenter (com.github.mvp4g.mvp4g2.core.ui.annotation.Presenter)1 EventBusMetaModel (com.github.mvp4g.mvp4g2.processor.model.EventBusMetaModel)1 EventMetaModel (com.github.mvp4g.mvp4g2.processor.model.EventMetaModel)1 ClassNameModel (com.github.mvp4g.mvp4g2.processor.model.intern.ClassNameModel)1