Search in sources :

Example 1 with Filters

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

the class FilterTest method testFiltersAnnotationWithoutExtendsIsEventBus.

@Test
public void testFiltersAnnotationWithoutExtendsIsEventBus() {
    Compilation compilation = javac().withProcessors(new Mvp4g2Processor()).compile(new ArrayList<JavaFileObject>() {

        {
            add(JavaFileObjects.forResource("com/github/mvp4g/mvp4g2/processor/eventbus/filterAnnotationWithoutExtendsIsEventBus/FilterAnnotationWithoutExtendsIsEventBus.java"));
        }
    });
    CompilationSubject.assertThat(compilation).failed();
    CompilationSubject.assertThat(compilation).hadErrorContaining("@Filters can only be used with an interfaces annotated with @EventBus");
}
Also used : JavaFileObject(javax.tools.JavaFileObject) Compilation(com.google.testing.compile.Compilation) Mvp4g2Processor(com.github.mvp4g.mvp4g2.processor.Mvp4g2Processor) Test(org.junit.Test)

Example 2 with Filters

use of com.github.mvp4g.mvp4g2.core.eventbus.annotation.Filters 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 3 with Filters

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

the class FiltersAnnotationScanner method scan.

public EventBusMetaModel scan(RoundEnvironment roundEnvironment) throws ProcessorException {
    // do validation
    FilterAnnotationValidator.builder().roundEnvironment(roundEnvironment).processingEnvironment(processingEnvironment).eventBusTypeElement(this.eventBusTypeElement).build().validate();
    // handle filters-annotation
    Filters filtersAnnotation = eventBusTypeElement.getAnnotation(Filters.class);
    if (isNull(filtersAnnotation)) {
        this.eventBusMetaModel.setHasFiltersAnnotation("false");
    } else {
        this.eventBusMetaModel.setHasFiltersAnnotation("true");
        this.eventBusMetaModel.setFilters(this.getEventFiltersAsList(eventBusTypeElement));
    }
    return this.eventBusMetaModel;
}
Also used : Filters(com.github.mvp4g.mvp4g2.core.eventbus.annotation.Filters)

Example 4 with Filters

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

ProcessorException (com.github.mvp4g.mvp4g2.processor.ProcessorException)2 Element (javax.lang.model.element.Element)2 TypeElement (javax.lang.model.element.TypeElement)2 EventBus (com.github.mvp4g.mvp4g2.core.eventbus.annotation.EventBus)1 Filters (com.github.mvp4g.mvp4g2.core.eventbus.annotation.Filters)1 Mvp4g2Processor (com.github.mvp4g.mvp4g2.processor.Mvp4g2Processor)1 EventBusMetaModel (com.github.mvp4g.mvp4g2.processor.model.EventBusMetaModel)1 EventBusAnnotationValidator (com.github.mvp4g.mvp4g2.processor.scanner.validation.EventBusAnnotationValidator)1 Compilation (com.google.testing.compile.Compilation)1 JavaFileObject (javax.tools.JavaFileObject)1 Test (org.junit.Test)1