Search in sources :

Example 11 with Event

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

the class PresenterTest method testHandlerWithWrongImplementation02.

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

        {
            add(JavaFileObjects.forResource("com/github/mvp4g/mvp4g2/processor/eventhandler/presenterWithWrongImplementation02/EventBusHandlerWithNotImplementedEvent.java"));
            add(JavaFileObjects.forResource("com/github/mvp4g/mvp4g2/processor/eventhandler/presenterWithWrongImplementation02/MockShellPresenter01.java"));
            add(JavaFileObjects.forResource("com/github/mvp4g/mvp4g2/processor/eventhandler/presenterWithWrongImplementation02/IMockShellView01.java"));
            add(JavaFileObjects.forResource("com/github/mvp4g/mvp4g2/processor/eventhandler/presenterWithWrongImplementation02/MockShellView01.java"));
        }
    });
    CompilationSubject.assertThat(compilation).failed();
    CompilationSubject.assertThat(compilation).hadErrorContaining("event >>doSomething(java.lang.String)<< is never handled by a presenter or handler");
}
Also used : JavaFileObject(javax.tools.JavaFileObject) Compilation(com.google.testing.compile.Compilation) Mvp4g2Processor(com.github.mvp4g.mvp4g2.processor.Mvp4g2Processor) Test(org.junit.Test)

Example 12 with Event

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

the class ModelValidator method hasEventHandlingMethodImplemented.

private void hasEventHandlingMethodImplemented(EventMetaModel eventMetaModel, String eventInternalName, ClassNameModel classNameModel) throws ProcessorException {
    TypeElement typeElement = this.processorUtils.getElements().getTypeElement(classNameModel.getClassName());
    if (typeElement != null) {
        // improvement: get all ExecutabelElement of type,
        // convert to String and add to list and use this list
        // for the compare!
        Map<String, ExecutableElement> nameOfExecutableElements = new HashMap<>();
        this.processorUtils.getElements().getAllMembers(typeElement).stream().filter(element -> element instanceof ExecutableElement).map(element -> (ExecutableElement) element).forEach(executableElement -> nameOfExecutableElements.put(executableElement.toString(), executableElement));
        // method to look for
        String methodNameToLookFor = this.createEventHandlungMethodName(eventInternalName);
        // try to find in Map
        ExecutableElement handlingElement = nameOfExecutableElements.get(methodNameToLookFor);
        if (handlingElement != null) {
            if (!"void".equals(handlingElement.getReturnType().toString())) {
                throw new ProcessorException("Mvp4g2Processor: EventElement: >>" + eventInternalName.split(",")[0] + "<< must return 'void'");
            }
            return;
        }
        // otherwiese we throw an excpetion!
        if (eventMetaModel.getBindings().size() > 0) {
            this.processorUtils.createNoteMessage("Mvp4g2Processor: event >>" + eventInternalName.split(",")[0] + "<< is only used for binding");
        } else {
            this.processorUtils.createErrorMessage("Mvp4g2Processor: presenter >>" + classNameModel.getClassName() + "<< -> event >>" + createEventHandlungMethodName(eventInternalName) + "<< is not handled by presenter/handler and has no bindings");
        }
    }
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) EventBusMetaModel(com.github.mvp4g.mvp4g2.processor.model.EventBusMetaModel) HandlerMetaModel(com.github.mvp4g.mvp4g2.processor.model.HandlerMetaModel) Collection(java.util.Collection) ExecutableElement(javax.lang.model.element.ExecutableElement) ClassNameModel(com.github.mvp4g.mvp4g2.processor.model.intern.ClassNameModel) HashMap(java.util.HashMap) TypeElement(javax.lang.model.element.TypeElement) ProcessorUtils(com.github.mvp4g.mvp4g2.processor.ProcessorUtils) ProcessorException(com.github.mvp4g.mvp4g2.processor.ProcessorException) PresenterMetaModel(com.github.mvp4g.mvp4g2.processor.model.PresenterMetaModel) List(java.util.List) EventMetaModel(com.github.mvp4g.mvp4g2.processor.model.EventMetaModel) Map(java.util.Map) Objects.isNull(java.util.Objects.isNull) ProcessorException(com.github.mvp4g.mvp4g2.processor.ProcessorException) HashMap(java.util.HashMap) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement)

Example 13 with Event

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

the class PresenterAnnotationValidator method validate.

public void validate(Element element, TypeElement viewClassTypeElement, TypeElement viewInterfaceTypeElement) throws ProcessorException {
    if (element instanceof TypeElement) {
        TypeElement typeElement = (TypeElement) element;
        // check, that the presenter annotion is only used with classes
        if (!typeElement.getKind().isClass()) {
            throw new ProcessorException("Mvp4g2Processor:" + typeElement.getSimpleName().toString() + ": @Presenter can only be used with a class!");
        }
        // check, that the viewClass is a class
        if (!viewClassTypeElement.getKind().isClass()) {
            throw new ProcessorException("Mvp4g2Processor:" + typeElement.getSimpleName().toString() + ": the viewClass-attribute of a @Presenter must be a class!");
        }
        // chekc if the vioewInterface is a interface
        if (!viewInterfaceTypeElement.getKind().isInterface()) {
            throw new ProcessorException("Mvp4g2Processor:" + typeElement.getSimpleName().toString() + ": the viewInterface-attribute of a @Presenter must be a interface!");
        }
        // check, if viewClass is implementing viewInterface
        if (!this.processorUtils.implementsInterface(this.processingEnvironment, viewClassTypeElement, viewInterfaceTypeElement.asType())) {
            throw new ProcessorException("Mvp4g2Processor:" + typeElement.getSimpleName().toString() + ": the viewClass-attribute of a @Presenter must implement the viewInterface!");
        }
        // check, that the typeElement extends AbstractHandler
        if (!this.processorUtils.extendsClassOrInterface(this.processingEnvironment.getTypeUtils(), typeElement.asType(), this.processingEnvironment.getElementUtils().getTypeElement(AbstractPresenter.class.getCanonicalName()).asType())) {
            throw new ProcessorException(typeElement.getSimpleName().toString() + ": @Presenter must extend AbstractPresenter.class!");
        }
        // check if annotated class is abstract
        if (typeElement.getModifiers().contains(Modifier.ABSTRACT)) {
            throw new ProcessorException("Mvp4g2Processor:" + typeElement.getSimpleName().toString() + ": @Presenter can not be ABSTRACT");
        }
        // check if class attribute is not abstradt
        if (viewClassTypeElement.getModifiers().contains(Modifier.ABSTRACT)) {
            throw new ProcessorException(typeElement.getSimpleName().toString() + ": class-attribute of @Presenter can not be ABSTRACT");
        }
        // check if a shell presenter does not implememt the multiple feature
        TypeMirror isShellMirror = this.processorUtils.getFlattenedSupertype(this.processingEnvironment.getTypeUtils(), typeElement.asType(), this.processingEnvironment.getElementUtils().getTypeElement(IsShell.class.getCanonicalName()).asType());
        if (isShellMirror != null) {
            if (typeElement.getAnnotation(Presenter.class).multiple()) {
                throw new ProcessorException(typeElement.getSimpleName().toString() + ": IsShell interface can not be used on a presenter which is defiend as multiple = true");
            }
        }
        Presenter presenterAnnotation = typeElement.getAnnotation(Presenter.class);
        if (Presenter.VIEW_CREATION_METHOD.PRESENTER.equals(presenterAnnotation.viewCreator())) {
            // IsViewCreator interface!
            if (!this.processorUtils.extendsClassOrInterface(this.processingEnvironment.getTypeUtils(), typeElement.asType(), this.processingEnvironment.getElementUtils().getTypeElement(IsViewCreator.class.getCanonicalName()).asType())) {
                throw new ProcessorException(typeElement.getSimpleName().toString() + ": @Presenter must implement the IsViewCreator interface");
            }
            // check, if the viewCreator has a generic parameter
            if (!this.processorUtils.supertypeHasGeneric(this.processingEnvironment.getTypeUtils(), typeElement.asType(), this.processingEnvironment.getElementUtils().getTypeElement(IsViewCreator.class.getCanonicalName()).asType())) {
                throw new ProcessorException(typeElement.getSimpleName().toString() + ": IsViewCreator interface needs " + "a generic parameter (add: >>" + viewInterfaceTypeElement.toString() + "<< as generic to IsViewCreator)");
            } else {
                TypeMirror isViewCreatorTypeMirror = this.processorUtils.getFlattenedSupertype(this.processingEnvironment.getTypeUtils(), typeElement.asType(), this.processingEnvironment.getElementUtils().getTypeElement(IsViewCreator.class.getCanonicalName()).asType());
                ClassNameModel classNameModel = new ClassNameModel(viewInterfaceTypeElement.toString());
                if (!isViewCreatorTypeMirror.toString().contains(classNameModel.getSimpleName())) {
                    throw new ProcessorException(typeElement.getSimpleName().toString() + ": IsViewCreator interface only allows the generic parameter -> " + viewInterfaceTypeElement.toString());
                }
            }
        } else if (Presenter.VIEW_CREATION_METHOD.FRAMEWORK.equals(presenterAnnotation.viewCreator())) {
            // check, if a presenter implements IsViewCreator, that the viewCreation method is set to PRESENTER!
            if (this.processorUtils.extendsClassOrInterface(this.processingEnvironment.getTypeUtils(), typeElement.asType(), this.processingEnvironment.getElementUtils().getTypeElement(IsViewCreator.class.getCanonicalName()).asType())) {
                throw new ProcessorException(typeElement.getSimpleName().toString() + ": the IsViewCreator interface can only be used in case of viewCreator = Presenter.VIEW_CREATION_METHOD.PRESENTER");
            }
        }
    } else {
        throw new ProcessorException("Mvp4g2Processor: @Presenter can only be used on a type (class)");
    }
// TODo El Hoss: check, that @Event is only used inside a EventBus
}
Also used : IsShell(com.github.mvp4g.mvp4g2.core.ui.IsShell) ProcessorException(com.github.mvp4g.mvp4g2.processor.ProcessorException) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) AbstractPresenter(com.github.mvp4g.mvp4g2.core.ui.AbstractPresenter) Presenter(com.github.mvp4g.mvp4g2.core.ui.annotation.Presenter) IsViewCreator(com.github.mvp4g.mvp4g2.core.ui.IsViewCreator) ClassNameModel(com.github.mvp4g.mvp4g2.processor.model.intern.ClassNameModel)

Example 14 with Event

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

the class AbstractEventBus method doFilterEvent.

/**
 * Performs the actual filtering by calling each associated event filter in turn. If any event
 * filter returns false, then the event will be canceled.
 *
 * @param eventName event's name
 * @param params    event parameters for this event
 */
@SuppressWarnings("unchecked")
private boolean doFilterEvent(String eventName, Object[] params) {
    int filterCount = eventFilters.size();
    @SuppressWarnings("rawtypes") IsEventFilter eventFilter;
    for (int i = 0; i < filterCount; i++) {
        eventFilter = eventFilters.get(i);
        if (!eventFilter.filterEvent(this, eventName, params)) {
            return false;
        }
    }
    return true;
}
Also used : IsEventFilter(com.github.mvp4g.mvp4g2.core.eventbus.IsEventFilter)

Example 15 with Event

use of com.github.mvp4g.mvp4g2.core.eventbus.annotation.Event in project mvp4g2-examples by mvp4g.

the class SearchView method createView.

public void createView() {
    container = div().add(div().style("width: 100%;").add(div().css("headline").textContent("Search Parameter (search for: 'S' or 'D')")).add(searchName = new TextField("Name")).add(searchCity = new TextField("City")).add(div().css("buttonBar").add(button().css("button").textContent("Search").on(click, event -> getPresenter().doClickSearchButton(searchName.getText(), searchCity.getText()))).add(button().css("button").textContent("Reset").on(click, event -> {
        searchName.setText("");
        searchCity.setText("");
    })).asElement())).asElement();
}
Also used : Element(elemental2.dom.Element) Elements.button(org.jboss.gwt.elemento.core.Elements.button) Elements.div(org.jboss.gwt.elemento.core.Elements.div) HTMLDivElement(elemental2.dom.HTMLDivElement) HTMLButtonElement(elemental2.dom.HTMLButtonElement) TextField(de.gishmo.gwt.example.mvp4g2.simpleapplication.client.widgets.TextField) EventType.click(org.jboss.gwt.elemento.core.EventType.click) LazyReverseView(com.github.mvp4g.mvp4g2.core.ui.LazyReverseView) TextField(de.gishmo.gwt.example.mvp4g2.simpleapplication.client.widgets.TextField)

Aggregations

Mvp4g2Processor (com.github.mvp4g.mvp4g2.processor.Mvp4g2Processor)25 Compilation (com.google.testing.compile.Compilation)25 JavaFileObject (javax.tools.JavaFileObject)25 Test (org.junit.Test)25 TypeElement (javax.lang.model.element.TypeElement)5 LazyReverseView (com.github.mvp4g.mvp4g2.core.ui.LazyReverseView)4 ProcessorException (com.github.mvp4g.mvp4g2.processor.ProcessorException)4 Element (elemental2.dom.Element)4 HTMLButtonElement (elemental2.dom.HTMLButtonElement)4 HTMLDivElement (elemental2.dom.HTMLDivElement)4 Elements.button (org.jboss.gwt.elemento.core.Elements.button)4 Elements.div (org.jboss.gwt.elemento.core.Elements.div)4 EventType.click (org.jboss.gwt.elemento.core.EventType.click)4 Element (javax.lang.model.element.Element)3 ExecutableElement (javax.lang.model.element.ExecutableElement)3 Event (com.github.mvp4g.mvp4g2.core.eventbus.annotation.Event)2 InitHistory (com.github.mvp4g.mvp4g2.core.history.annotation.InitHistory)2 NotFoundHistory (com.github.mvp4g.mvp4g2.core.history.annotation.NotFoundHistory)2 EventBusMetaModel (com.github.mvp4g.mvp4g2.processor.model.EventBusMetaModel)2 EventMetaModel (com.github.mvp4g.mvp4g2.processor.model.EventMetaModel)2