Search in sources :

Example 71 with Mvp4g2Processor

use of com.github.mvp4g.mvp4g2.processor.Mvp4g2Processor in project mvp4g2 by mvp4g.

the class PresenterTest method testPresenterWithViewCreationMethodPresenter04.

// IsViewCreator interface used without generic viewCreator = Presenter.VIEW_CREATION_METHOD.PRESENTER
@Test
public void testPresenterWithViewCreationMethodPresenter04() {
    Compilation compilation = javac().withProcessors(new Mvp4g2Processor()).compile(new ArrayList<JavaFileObject>() {

        {
            add(JavaFileObjects.forResource("com/github/mvp4g/mvp4g2/processor/eventhandler/presenterWithViewCreationMethodPresenter04/EventBusPresenterWithViewCreationMethodPresenter04.java"));
            add(JavaFileObjects.forResource("com/github/mvp4g/mvp4g2/processor/eventhandler/presenterWithViewCreationMethodPresenter04/MockShellPresenter04.java"));
            add(JavaFileObjects.forResource("com/github/mvp4g/mvp4g2/processor/eventhandler/presenterWithViewCreationMethodPresenter04/IMockShellView04.java"));
            add(JavaFileObjects.forResource("com/github/mvp4g/mvp4g2/processor/eventhandler/presenterWithViewCreationMethodPresenter04/MockShellView04.java"));
            add(JavaFileObjects.forResource("com/github/mvp4g/mvp4g2/processor/eventhandler/presenterWithViewCreationMethodPresenter04/MockPresenter04.java"));
            add(JavaFileObjects.forResource("com/github/mvp4g/mvp4g2/processor/eventhandler/presenterWithViewCreationMethodPresenter04/IMockView04.java"));
            add(JavaFileObjects.forResource("com/github/mvp4g/mvp4g2/processor/eventhandler/presenterWithViewCreationMethodPresenter04/MockView04.java"));
        }
    });
    CompilationSubject.assertThat(compilation).failed();
    CompilationSubject.assertThat(compilation).hadErrorContaining("the IsViewCreator interface can only be used in case of viewCreator = Presenter.VIEW_CREATION_METHOD.PRESENTER");
}
Also used : JavaFileObject(javax.tools.JavaFileObject) Compilation(com.google.testing.compile.Compilation) Mvp4g2Processor(com.github.mvp4g.mvp4g2.processor.Mvp4g2Processor) Test(org.junit.Test)

Example 72 with Mvp4g2Processor

use of com.github.mvp4g.mvp4g2.processor.Mvp4g2Processor in project mvp4g2 by mvp4g.

the class HistoryTest method testHistoryAnnoationOnAInterface.

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

        {
            add(JavaFileObjects.forResource("com/github/mvp4g/mvp4g2/processor/history/historyAnnotationOnAInterface/HistoryAnnoationOnAInterface.java"));
        }
    });
    CompilationSubject.assertThat(compilation).failed();
    CompilationSubject.assertThat(compilation).hadErrorContaining("@History can only be used with a class!");
}
Also used : JavaFileObject(javax.tools.JavaFileObject) Compilation(com.google.testing.compile.Compilation) Mvp4g2Processor(com.github.mvp4g.mvp4g2.processor.Mvp4g2Processor) Test(org.junit.Test)

Example 73 with Mvp4g2Processor

use of com.github.mvp4g.mvp4g2.processor.Mvp4g2Processor in project mvp4g2 by mvp4g.

the class ImplementationTest method testOnlyEventBusAndHandlerData.

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

        {
            add(JavaFileObjects.forResource("com/github/mvp4g/mvp4g2/processor/implementation/testOnlyEventBusData/MockEventBus.java"));
            add(JavaFileObjects.forResource("com/github/mvp4g/mvp4g2/processor/implementation/testOnlyEventBusData/MockShellPresenter.java"));
        }
    });
    CompilationSubject.assertThat(compilation).succeeded();
}
Also used : JavaFileObject(javax.tools.JavaFileObject) Compilation(com.google.testing.compile.Compilation) Mvp4g2Processor(com.github.mvp4g.mvp4g2.processor.Mvp4g2Processor) Test(org.junit.Test)

Example 74 with Mvp4g2Processor

use of com.github.mvp4g.mvp4g2.processor.Mvp4g2Processor in project mvp4g2 by mvp4g.

the class EventAnnotationValidator method validate.

public void validate() throws ProcessorException {
    // check if all historyNames are unique!
    List<String> historyNames = new ArrayList<>();
    for (Element element : this.roundEnvironment.getElementsAnnotatedWith(Event.class)) {
        Event eventAnnotation = element.getAnnotation(Event.class);
        // check, that the parent element extends IsEventBus
        if (!this.processorUtils.extendsClassOrInterface(this.processingEnvironment.getTypeUtils(), element.getEnclosingElement().asType(), this.processingEnvironment.getElementUtils().getTypeElement(IsEventBus.class.getCanonicalName()).asType())) {
            throw new ProcessorException("Mvp4g2Processor: @Event can only be used inside a event bus! >>" + element.getEnclosingElement().toString() + "<< does no implement IsEventBus");
        }
        if (!Event.DEFAULT_HISTORY_NAME.equals(eventAnnotation.historyName())) {
            if (historyNames.contains(eventAnnotation.historyName())) {
                throw new ProcessorException("Mvp4g2Processor: EventElement: >>" + element.getSimpleName().toString() + "<< using a already used historyName -> >>" + eventAnnotation.historyName() + "<<");
            } else {
                historyNames.add(eventAnnotation.historyName());
            }
        }
        ExecutableElement eventExecutableElement = (ExecutableElement) element;
        if (!"void".equals(eventExecutableElement.getReturnType().toString())) {
            throw new ProcessorException("Mvp4g2Processor: EventElement: >>" + element.getSimpleName().toString() + "<< must return 'void'");
        }
    }
    // check, if there are more than one InitHistory-annotation!
    if (this.roundEnvironment.getElementsAnnotatedWith(InitHistory.class).size() > 1) {
        throw new ProcessorException("Mvp4g2Processor: @InitHistory can only be set a single time inside a event bus");
    } else if (this.roundEnvironment.getElementsAnnotatedWith(InitHistory.class).size() == 1) {
        for (Element element : this.roundEnvironment.getElementsAnnotatedWith(InitHistory.class)) {
            ExecutableElement executableElement = (ExecutableElement) element;
            if (((ExecutableElement) element).getParameters().size() > 0) {
                throw new ProcessorException("Mvp4g2Processor: @InitHistory can only be used on a method with no arguments");
            }
        }
    }
    // check, if there are more than one NotFoundHistory-annotation!
    if (this.roundEnvironment.getElementsAnnotatedWith(NotFoundHistory.class).size() > 1) {
        throw new ProcessorException("Mvp4g2Processor: @NotFoundHistory can only be set a single time inside a event bus");
    } else if (this.roundEnvironment.getElementsAnnotatedWith(NotFoundHistory.class).size() == 1) {
        for (Element element : this.roundEnvironment.getElementsAnnotatedWith(NotFoundHistory.class)) {
            ExecutableElement executableElement = (ExecutableElement) element;
            if (((ExecutableElement) element).getParameters().size() > 0) {
                throw new ProcessorException("Mvp4g2Processor: @NotFoundHistory can only be used on a method with no arguments");
            }
        }
    }
}
Also used : ProcessorException(com.github.mvp4g.mvp4g2.processor.ProcessorException) InitHistory(com.github.mvp4g.mvp4g2.core.history.annotation.InitHistory) NotFoundHistory(com.github.mvp4g.mvp4g2.core.history.annotation.NotFoundHistory) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ArrayList(java.util.ArrayList) Event(com.github.mvp4g.mvp4g2.core.eventbus.annotation.Event)

Example 75 with Mvp4g2Processor

use of com.github.mvp4g.mvp4g2.processor.Mvp4g2Processor 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)72 Compilation (com.google.testing.compile.Compilation)72 Test (org.junit.Test)72 JavaFileObject (javax.tools.JavaFileObject)69 ProcessorException (com.github.mvp4g.mvp4g2.processor.ProcessorException)5 TypeElement (javax.lang.model.element.TypeElement)5 Element (javax.lang.model.element.Element)3 ExecutableElement (javax.lang.model.element.ExecutableElement)3 ClassNameModel (com.github.mvp4g.mvp4g2.processor.model.intern.ClassNameModel)2 Event (com.github.mvp4g.mvp4g2.core.eventbus.annotation.Event)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 ProcessorUtils (com.github.mvp4g.mvp4g2.processor.ProcessorUtils)1 EventBusMetaModel (com.github.mvp4g.mvp4g2.processor.model.EventBusMetaModel)1 EventMetaModel (com.github.mvp4g.mvp4g2.processor.model.EventMetaModel)1 HandlerMetaModel (com.github.mvp4g.mvp4g2.processor.model.HandlerMetaModel)1