Search in sources :

Example 1 with EventMetaModel

use of com.github.mvp4g.mvp4g2.processor.model.EventMetaModel in project mvp4g2 by mvp4g.

the class EventMetaDataGenerator method generate.

public void generate() throws ProcessorException {
    for (EventMetaModel eventMetaModel : this.eventBusMetaModel.getEventMetaModels()) {
        TypeSpec.Builder typeSpec = TypeSpec.classBuilder(this.createEventMetaDataClassName(this.eventBusMetaModel.getEventBus().getClassName(), eventMetaModel.getEventName())).superclass(ParameterizedTypeName.get(ClassName.get(EventMetaData.class), this.eventBusMetaModel.getEventBus().getTypeName())).addModifiers(Modifier.PUBLIC, Modifier.FINAL);
        // constructor ...
        MethodSpec.Builder constructor = MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addCode("super($S, $S, ", eventMetaModel.getEventInternalName(), eventMetaModel.getEventName());
        if (Event.DEFAULT_HISTORY_NAME.equals(eventMetaModel.getHistoryEventName())) {
            constructor.addCode("null, ");
        } else {
            constructor.addCode("$S, ", eventMetaModel.getHistoryEventName());
        }
        if (Event.NoHistoryConverter.class.getCanonicalName().equals(eventMetaModel.getHistoryConverter().getClassName())) {
            constructor.addCode("null, null, ");
        } else {
            constructor.addCode("new $L(), new $T(), ", eventMetaModel.getHistoryConverter().getPackage() + "." + this.processorUtils.createHistoryMetaDataClassName(eventMetaModel.getHistoryConverter().getClassName()), eventMetaModel.getHistoryConverter().getTypeName());
        }
        constructor.addCode("$L, $L);\n", eventMetaModel.getPassive(), eventMetaModel.getNavigationEvent());
        eventMetaModel.getBindings().forEach(s -> constructor.addStatement("super.addBindHandler($S)", s.getClassName()));
        eventMetaModel.getHandlers().forEach(s -> constructor.addStatement("super.addHandler($S)", s.getClassName()));
        eventMetaModel.getActivateHandlers().forEach(s -> constructor.addStatement("super.addActivateHandler($S)", s.getClassName()));
        eventMetaModel.getDeactivateHandlers().forEach(s -> constructor.addStatement("super.addDeactivateHandler($S)", s.getClassName()));
        typeSpec.addMethod(constructor.build());
        JavaFile javaFile = JavaFile.builder(eventBusMetaModel.getEventBus().getPackage(), typeSpec.build()).build();
        try {
            javaFile.writeTo(this.processingEnvironment.getFiler());
        } catch (IOException e) {
            throw new ProcessorException("Unable to write generated file: >>" + eventBusMetaModel.getEventBus().getPackage() + "." + this.createEventMetaDataClassName(this.eventBusMetaModel.getEventBus().getClassName(), eventMetaModel.getEventName()) + "<< -> exception: " + e.getMessage());
        }
    }
}
Also used : ProcessorException(com.github.mvp4g.mvp4g2.processor.ProcessorException) MethodSpec(com.squareup.javapoet.MethodSpec) JavaFile(com.squareup.javapoet.JavaFile) IOException(java.io.IOException) EventMetaModel(com.github.mvp4g.mvp4g2.processor.model.EventMetaModel) EventMetaData(com.github.mvp4g.mvp4g2.core.internal.eventbus.EventMetaData) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 2 with EventMetaModel

use of com.github.mvp4g.mvp4g2.processor.model.EventMetaModel 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 3 with EventMetaModel

use of com.github.mvp4g.mvp4g2.processor.model.EventMetaModel in project mvp4g2 by mvp4g.

the class PropertiesTest method testEventMetaModel.

@Test
public void testEventMetaModel() {
    Properties properties = new Properties();
    try {
        properties.load(ClassLoader.getSystemResourceAsStream("META-INF" + File.separator + "mvp4g2" + File.separator + "gotoList_pPp_java_lang_String_pPp_java_lang_String.properties"));
    } catch (IOException e) {
        assert false : "IOException reading gotoList_pPp_java_lang_String_pPp_java_lang_String.properties";
    }
    EventMetaModel metaModel = new EventMetaModel(properties);
    Properties newProperties = metaModel.createPropertes();
    assertEquals(properties, newProperties);
}
Also used : IOException(java.io.IOException) Properties(java.util.Properties) EventMetaModel(com.github.mvp4g.mvp4g2.processor.model.EventMetaModel) Test(org.junit.Test)

Example 4 with EventMetaModel

use of com.github.mvp4g.mvp4g2.processor.model.EventMetaModel in project mvp4g2 by mvp4g.

the class PropertiesTest method testEventBusMetaModel.

@Test
public void testEventBusMetaModel() {
    Properties properties = new Properties();
    try {
        properties.load(ClassLoader.getSystemResourceAsStream("META-INF" + File.separator + "mvp4g2" + File.separator + "eventBus.properties"));
    } catch (IOException e) {
        assert false : "IOException reading eventBus.properties";
    }
    EventBusMetaModel metaModel = new EventBusMetaModel(properties);
    for (String eventInternalName : properties.getProperty("events").split(",")) {
        try {
            Properties eventProperties = new Properties();
            eventProperties.load(ClassLoader.getSystemResourceAsStream("META-INF" + File.separator + "mvp4g2" + File.separator + eventInternalName + ".properties"));
            metaModel.add(new EventMetaModel(eventProperties));
        } catch (IOException e) {
            assert false : "IOException reading " + eventInternalName + ".properties";
        }
    }
    Properties newProperties = metaModel.createPropertes();
    assertEquals(properties, newProperties);
}
Also used : EventBusMetaModel(com.github.mvp4g.mvp4g2.processor.model.EventBusMetaModel) IOException(java.io.IOException) Properties(java.util.Properties) EventMetaModel(com.github.mvp4g.mvp4g2.processor.model.EventMetaModel) Test(org.junit.Test)

Example 5 with EventMetaModel

use of com.github.mvp4g.mvp4g2.processor.model.EventMetaModel in project mvp4g2 by mvp4g.

the class EventHandlingMethodGenerator method generateEventHandlingMethod.

private void generateEventHandlingMethod(EventMetaModel eventMetaModel) {
    MethodSpec.Builder eventHandlingMethod = MethodSpec.methodBuilder(eventMetaModel.getEventName()).addAnnotation(Override.class).addModifiers(Modifier.PUBLIC, Modifier.FINAL);
    eventHandlingMethod.addStatement("int startLogDepth = $T.logDepth", ClassName.get(AbstractEventBus.class));
    eventHandlingMethod.beginControlFlow("try");
    // add parametners to method signature ...
    eventMetaModel.getParameterMetaDataList().stream().map(parameterMetaData -> ParameterSpec.builder(parameterMetaData.getType().getTypeName(), parameterMetaData.getName()).addModifiers(Modifier.FINAL).build()).forEach(eventHandlingMethod::addParameter);
    if (eventMetaModel.isNavigationEvent()) {
        eventHandlingMethod.addCode("super.logAskingForConfirmation(++$T.logDepth, $S", ClassName.get(AbstractEventBus.class), eventMetaModel.getEventName());
        eventMetaModel.getParameterMetaDataList().forEach(parameterMetaData -> eventHandlingMethod.addCode(", $N", parameterMetaData.getName()));
        eventHandlingMethod.addCode(");\n");
        MethodSpec.Builder executeMethod = MethodSpec.methodBuilder("execute").addAnnotation(Override.class).addModifiers(Modifier.PUBLIC);
        this.callEventExecMethod(executeMethod, eventMetaModel);
        TypeSpec confirmCommand = TypeSpec.anonymousClassBuilder(CodeBlock.builder().add("this").build()).addSuperinterface(NavigationEventCommand.class).addMethod(executeMethod.build()).build();
        eventHandlingMethod.addStatement("super.placeService.confirmEvent($L)", confirmCommand);
    } else {
        this.callEventExecMethod(eventHandlingMethod, eventMetaModel);
    }
    eventHandlingMethod.nextControlFlow("finally");
    eventHandlingMethod.addStatement("$T.logDepth = startLogDepth", ClassName.get(AbstractEventBus.class));
    eventHandlingMethod.endControlFlow();
    typeSpec.addMethod(eventHandlingMethod.build());
}
Also used : IntStream(java.util.stream.IntStream) HandlerMetaModel(com.github.mvp4g.mvp4g2.processor.model.HandlerMetaModel) History(com.github.mvp4g.mvp4g2.core.history.annotation.History) Modifier(javax.lang.model.element.Modifier) HandlerMetaData(com.github.mvp4g.mvp4g2.core.internal.ui.HandlerMetaData) ClassName(com.squareup.javapoet.ClassName) WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ProcessorUtils(com.github.mvp4g.mvp4g2.processor.ProcessorUtils) ArrayList(java.util.ArrayList) AbstractEventBus(com.github.mvp4g.mvp4g2.core.internal.eventbus.AbstractEventBus) Event(com.github.mvp4g.mvp4g2.core.eventbus.annotation.Event) CodeBlock(com.squareup.javapoet.CodeBlock) NavigationEventCommand(com.github.mvp4g.mvp4g2.core.history.NavigationEventCommand) EventBusMetaModel(com.github.mvp4g.mvp4g2.processor.model.EventBusMetaModel) ParameterSpec(com.squareup.javapoet.ParameterSpec) MethodSpec(com.squareup.javapoet.MethodSpec) HistoryMetaModel(com.github.mvp4g.mvp4g2.processor.model.HistoryMetaModel) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeSpec(com.squareup.javapoet.TypeSpec) PresenterMetaModel(com.github.mvp4g.mvp4g2.processor.model.PresenterMetaModel) List(java.util.List) PresenterMetaData(com.github.mvp4g.mvp4g2.core.internal.ui.PresenterMetaData) EventMetaModel(com.github.mvp4g.mvp4g2.processor.model.EventMetaModel) Optional(java.util.Optional) EventMetaData(com.github.mvp4g.mvp4g2.core.internal.eventbus.EventMetaData) MethodSpec(com.squareup.javapoet.MethodSpec) AbstractEventBus(com.github.mvp4g.mvp4g2.core.internal.eventbus.AbstractEventBus) TypeSpec(com.squareup.javapoet.TypeSpec)

Aggregations

EventMetaModel (com.github.mvp4g.mvp4g2.processor.model.EventMetaModel)8 IOException (java.io.IOException)5 EventBusMetaModel (com.github.mvp4g.mvp4g2.processor.model.EventBusMetaModel)4 Properties (java.util.Properties)4 Event (com.github.mvp4g.mvp4g2.core.eventbus.annotation.Event)2 EventMetaData (com.github.mvp4g.mvp4g2.core.internal.eventbus.EventMetaData)2 ProcessorException (com.github.mvp4g.mvp4g2.processor.ProcessorException)2 ProcessorUtils (com.github.mvp4g.mvp4g2.processor.ProcessorUtils)2 HandlerMetaModel (com.github.mvp4g.mvp4g2.processor.model.HandlerMetaModel)2 PresenterMetaModel (com.github.mvp4g.mvp4g2.processor.model.PresenterMetaModel)2 MethodSpec (com.squareup.javapoet.MethodSpec)2 TypeSpec (com.squareup.javapoet.TypeSpec)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 IntStream (java.util.stream.IntStream)2 ExecutableElement (javax.lang.model.element.ExecutableElement)2 TypeElement (javax.lang.model.element.TypeElement)2 FileObject (javax.tools.FileObject)2 Test (org.junit.Test)2 Start (com.github.mvp4g.mvp4g2.core.eventbus.annotation.Start)1