Search in sources :

Example 6 with ProcessorException

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

the class EventBusGenerator method generate.

public void generate(EventBusMetaModel eventBusMetaMetaModel, HandlerMetaModel handlerMetaModel, PresenterMetaModel presenterMetaModel, HistoryMetaModel historyMetaModel) throws ProcessorException {
    // check if element is existing (to avoid generating code for deleted items)
    if (!this.processorUtils.doesExist(eventBusMetaMetaModel.getEventBus())) {
        return;
    }
    ClassName abstractEventBusClassName = ClassName.get(AbstractEventBus.class);
    TypeSpec.Builder typeSpec = TypeSpec.classBuilder(eventBusMetaMetaModel.getEventBus().getSimpleName() + EventBusGenerator.IMPL_NAME).superclass(ParameterizedTypeName.get(abstractEventBusClassName, eventBusMetaMetaModel.getEventBus().getTypeName())).addModifiers(Modifier.PUBLIC, Modifier.FINAL).addSuperinterface(eventBusMetaMetaModel.getEventBus().getTypeName());
    MethodSpec constructor = MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addStatement("super($S)", eventBusMetaMetaModel.getShell().getClassName()).build();
    typeSpec.addMethod(constructor);
    DebugGenerator.builder().eventBusMetaModel(eventBusMetaMetaModel).typeSpec(typeSpec).build().generate();
    FilterGenerator.builder().eventBusMetaModel(eventBusMetaMetaModel).typeSpec(typeSpec).build().generate();
    EventLoadMetaDataGenerator.builder().processingEnvironment(this.processingEnvironment).eventBusMetaModel(eventBusMetaMetaModel).typeSpec(typeSpec).build().generate();
    EventMetaDataGenerator.builder().processingEnvironment(this.processingEnvironment).eventBusMetaModel(eventBusMetaMetaModel).build().generate();
    EventHandlingMethodGenerator.builder().processorUtils(this.processorUtils).typeSpec(typeSpec).eventBusMetaModel(eventBusMetaMetaModel).historyMetaModel(historyMetaModel).presenterMetaModel(presenterMetaModel).handlerMetaModel(handlerMetaModel).build().generate();
    AddPresenterGenerator.builder().typeSpec(typeSpec).processingEnvironment(this.processingEnvironment).presenterMetaModel(presenterMetaModel).build().generate();
    HandlerAndPresenterRegristrationGenerator.builder().processingEnvironment(this.processingEnvironment).typeSpec(typeSpec).eventBusMetaModel(eventBusMetaMetaModel).presenterMetaModel(presenterMetaModel).handlerMetaModel(handlerMetaModel).build().generate();
    JavaFile javaFile = JavaFile.builder(eventBusMetaMetaModel.getEventBus().getPackage(), typeSpec.build()).build();
    // System.out.println(javaFile.toString());
    try {
        javaFile.writeTo(this.processingEnvironment.getFiler());
    } catch (IOException e) {
        throw new ProcessorException("Unable to write generated file: >>" + eventBusMetaMetaModel.getEventBus().getSimpleName() + EventBusGenerator.IMPL_NAME + "<< -> exception: " + e.getMessage());
    }
}
Also used : ProcessorException(com.github.mvp4g.mvp4g2.processor.ProcessorException) MethodSpec(com.squareup.javapoet.MethodSpec) ClassName(com.squareup.javapoet.ClassName) JavaFile(com.squareup.javapoet.JavaFile) IOException(java.io.IOException) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 7 with ProcessorException

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

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

the class DebugAnnotationValidator method validate.

public void validate() throws ProcessorException {
    // get elements annotated with Debug annotation
    Set<? extends Element> elementsWithDebugAnnotation = this.roundEnvironment.getElementsAnnotatedWith(Debug.class);
    // at least there should only one Application annotation!
    if (elementsWithDebugAnnotation.size() > 1) {
        throw new ProcessorException("There should be at least only one interface, that is annotated with @Debug");
    }
    for (Element element : elementsWithDebugAnnotation) {
        if (element instanceof TypeElement) {
            TypeElement typeElement = (TypeElement) element;
            // @Debug can only be used on a interface
            if (!typeElement.getKind().isInterface()) {
                throw new ProcessorException("@Debug can only be used with an interface");
            }
            // @Debug 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("@Debug can only be used on interfaces that extends IsEventBus");
            }
            // the loggerinside the annotation must extends IsMvp4g2Logger!
            TypeElement loggerElement = this.getLogger(typeElement.getAnnotation(Debug.class));
            if (!this.processorUtils.extendsClassOrInterface(this.processingEnvironment.getTypeUtils(), loggerElement.asType(), this.processingEnvironment.getElementUtils().getTypeElement(IsMvp4g2Logger.class.getCanonicalName()).asType())) {
                throw new ProcessorException("@Debug - the logger attribute needs class that extends IsMvp4g2Logger");
            }
        } else {
            throw new ProcessorException("@Debug 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) Debug(com.github.mvp4g.mvp4g2.core.eventbus.annotation.Debug)

Example 9 with ProcessorException

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

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

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