Search in sources :

Example 11 with ProcessorException

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

the class HandlerGenerator method generate.

public void generate(HandlerMetaModel metaModel) throws ProcessorException {
    ClassName eventHandlerMetaDataClassName = ClassName.get(HandlerMetaData.class);
    ClassName eventHandlerMetaDataKindClassName = ClassName.get(HandlerMetaData.Kind.class);
    for (String eventHandler : metaModel.getHandlerKeys()) {
        // check if element is existing (to avoid generating code for deleted items)
        if (this.processorUtils.doesExist(new ClassNameModel(eventHandler))) {
            HandlerMetaModel.HandlerData data = metaModel.getHandlerData(eventHandler);
            String className = this.processorUtils.createFullClassName(data.getHandler().getPackage(), data.getHandler().getSimpleName() + ProcessorConstants.META_DATA);
            TypeSpec.Builder typeSpec = TypeSpec.classBuilder(this.processorUtils.setFirstCharacterToUpperCase(className)).superclass(ParameterizedTypeName.get(eventHandlerMetaDataClassName, data.getHandler().getTypeName())).addModifiers(Modifier.PUBLIC, Modifier.FINAL);
            // constructor ...
            MethodSpec.Builder constructor = MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addStatement("super($S, $T.EVENT_HANDLER, new $T())", data.getHandler().getClassName(), eventHandlerMetaDataKindClassName, data.getHandler().getTypeName());
            typeSpec.addMethod(constructor.build());
            JavaFile javaFile = JavaFile.builder(data.getHandler().getPackage(), typeSpec.build()).build();
            try {
                javaFile.writeTo(this.processingEnvironment.getFiler());
            } catch (IOException e) {
                throw new ProcessorException("Unable to write generated file: >>" + data.getHandler().getSimpleName() + ProcessorConstants.META_DATA + "<< -> exception: " + e.getMessage());
            }
        }
    }
}
Also used : HandlerMetaData(com.github.mvp4g.mvp4g2.core.internal.ui.HandlerMetaData) HandlerMetaModel(com.github.mvp4g.mvp4g2.processor.model.HandlerMetaModel) ProcessorException(com.github.mvp4g.mvp4g2.processor.ProcessorException) MethodSpec(com.squareup.javapoet.MethodSpec) IOException(java.io.IOException) ClassName(com.squareup.javapoet.ClassName) JavaFile(com.squareup.javapoet.JavaFile) ClassNameModel(com.github.mvp4g.mvp4g2.processor.model.intern.ClassNameModel) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 12 with ProcessorException

use of com.github.mvp4g.mvp4g2.processor.ProcessorException 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 13 with ProcessorException

use of com.github.mvp4g.mvp4g2.processor.ProcessorException 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 14 with ProcessorException

use of com.github.mvp4g.mvp4g2.processor.ProcessorException 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 15 with ProcessorException

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

the class HandlerAnnotationScanner method scan.

public HandlerMetaModel scan(RoundEnvironment roundEnvironment) throws ProcessorException {
    // Validator
    HandlerAnnotationValidator validator = HandlerAnnotationValidator.builder().roundEnvironment(roundEnvironment).processingEnvironment(processingEnvironment).build();
    // read all already created model
    HandlerMetaModel model = this.restore();
    for (Element element : roundEnvironment.getElementsAnnotatedWith(Handler.class)) {
        TypeElement typeElement = (TypeElement) element;
        // validate the element. In case of error throw exception!
        validator.validate(typeElement);
        // update model
        model.add(((TypeElement) element).getQualifiedName().toString(), this.processorUtils.createHandledEventArray(typeElement));
    }
    // let's store the updated model
    this.processorUtils.store(model, this.createRelativeFileName());
    return model;
}
Also used : HandlerMetaModel(com.github.mvp4g.mvp4g2.processor.model.HandlerMetaModel) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) HandlerAnnotationValidator(com.github.mvp4g.mvp4g2.processor.scanner.validation.HandlerAnnotationValidator)

Aggregations

ProcessorException (com.github.mvp4g.mvp4g2.processor.ProcessorException)13 TypeElement (javax.lang.model.element.TypeElement)12 Element (javax.lang.model.element.Element)10 JavaFile (com.squareup.javapoet.JavaFile)6 MethodSpec (com.squareup.javapoet.MethodSpec)6 TypeSpec (com.squareup.javapoet.TypeSpec)6 IOException (java.io.IOException)6 ClassNameModel (com.github.mvp4g.mvp4g2.processor.model.intern.ClassNameModel)4 ExecutableElement (javax.lang.model.element.ExecutableElement)4 Presenter (com.github.mvp4g.mvp4g2.core.ui.annotation.Presenter)3 EventMetaModel (com.github.mvp4g.mvp4g2.processor.model.EventMetaModel)3 HandlerMetaModel (com.github.mvp4g.mvp4g2.processor.model.HandlerMetaModel)3 PresenterMetaModel (com.github.mvp4g.mvp4g2.processor.model.PresenterMetaModel)3 ClassName (com.squareup.javapoet.ClassName)3 Debug (com.github.mvp4g.mvp4g2.core.eventbus.annotation.Debug)2 Event (com.github.mvp4g.mvp4g2.core.eventbus.annotation.Event)2 History (com.github.mvp4g.mvp4g2.core.history.annotation.History)2 InitHistory (com.github.mvp4g.mvp4g2.core.history.annotation.InitHistory)2 NotFoundHistory (com.github.mvp4g.mvp4g2.core.history.annotation.NotFoundHistory)2 HandlerMetaData (com.github.mvp4g.mvp4g2.core.internal.ui.HandlerMetaData)2