Search in sources :

Example 16 with ProcessorException

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

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

Example 18 with ProcessorException

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

the class FilterAnnotationValidator method validate.

public void validate() throws ProcessorException {
    // get elements annotated with EventBus annotation
    Set<? extends Element> elementsWithFilterAnnotation = this.roundEnvironment.getElementsAnnotatedWith(Filters.class);
    // at least there should only one Application annotation!
    if (elementsWithFilterAnnotation.size() > 1) {
        throw new ProcessorException("Mvp4g2Processor: There should be at least only one interface, that is annotated with @Filter");
    }
    // annotated element has to be a interface
    for (Element element : elementsWithFilterAnnotation) {
        if (element instanceof TypeElement) {
            TypeElement typeElement = (TypeElement) element;
            if (!typeElement.getKind().isInterface()) {
                throw new ProcessorException("Mvp4g2Processor: @Filter can only be used with an interface");
            }
            // @Filter 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("Mvp4g2Processor: @Filter can only be used on interfaces that extends IsEventBus");
            }
            // test, that all filters implement IsEventFilter!
            List<String> eventFilterAsStringList = this.getEventFiltersAsList(eventBusTypeElement);
            for (String eventFilterClassname : eventFilterAsStringList) {
                TypeElement filterElement = this.processingEnvironment.getElementUtils().getTypeElement(eventFilterClassname);
                if (!this.processorUtils.extendsClassOrInterface(this.processingEnvironment.getTypeUtils(), filterElement.asType(), this.processingEnvironment.getElementUtils().getTypeElement(IsEventFilter.class.getCanonicalName()).asType())) {
                    throw new ProcessorException("Mvp4g2Processor: @Filter - the filters attribute needs class that implements IsEventFilter");
                }
            }
        } else {
            throw new ProcessorException("Mvp4g2Processor: @Filter 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)

Example 19 with ProcessorException

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

the class HistoryGenerator method generate.

public void generate(HistoryMetaModel metaModel) throws ProcessorException {
    for (String historyConverterClassName : metaModel.getHistoryConverterClassNames()) {
        HistoryMetaModel.HistoryData historyData = metaModel.getHistoryData(historyConverterClassName);
        TypeSpec.Builder typeSpec = TypeSpec.classBuilder(this.processorUtils.createHistoryMetaDataClassName(historyData.getHistoryConverter().getClassName())).superclass(ClassName.get(HistoryMetaData.class)).addModifiers(Modifier.PUBLIC, Modifier.FINAL);
        // constructor ...
        MethodSpec constructor = MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addStatement("super($S, $T.$L)", historyConverterClassName, ClassName.get(History.HistoryConverterType.class), historyData.getHistoryConverterType()).build();
        typeSpec.addMethod(constructor);
        JavaFile javaFile = JavaFile.builder(historyData.getHistoryConverter().getPackage(), typeSpec.build()).build();
        try {
            javaFile.writeTo(this.processingEnvironment.getFiler());
        } catch (IOException e) {
            throw new ProcessorException("Unable to write generated file: >>" + this.processorUtils.createHistoryMetaDataClassName(historyData.getHistoryConverter().getClassName()) + "<< -> exception: " + e.getMessage());
        }
    }
}
Also used : HistoryMetaModel(com.github.mvp4g.mvp4g2.processor.model.HistoryMetaModel) ProcessorException(com.github.mvp4g.mvp4g2.processor.ProcessorException) MethodSpec(com.squareup.javapoet.MethodSpec) JavaFile(com.squareup.javapoet.JavaFile) IOException(java.io.IOException) History(com.github.mvp4g.mvp4g2.core.history.annotation.History) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 20 with ProcessorException

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

the class PresenterGenerator method generate.

public void generate(PresenterMetaModel metaModel) throws ProcessorException {
    ClassName presenterrMetaDataClassName = ClassName.get(PresenterMetaData.class);
    ClassName presenterMetaDataKindClassName = ClassName.get(HandlerMetaData.Kind.class);
    ClassName presenterViewCreationMethodClassName = ClassName.get(Presenter.VIEW_CREATION_METHOD.class);
    for (String presenter : metaModel.getPresenterKeys()) {
        // check if element is existing (to avoid generating code for deleted items)
        if (this.processorUtils.doesExist(new ClassNameModel(presenter))) {
            PresenterMetaModel.PresenterData data = metaModel.getPresenterData(presenter);
            String className = this.processorUtils.createFullClassName(data.getPresenter().getPackage(), data.getPresenter().getSimpleName() + ProcessorConstants.META_DATA);
            TypeSpec.Builder typeSpec = TypeSpec.classBuilder(this.processorUtils.setFirstCharacterToUpperCase(className)).superclass(ParameterizedTypeName.get(presenterrMetaDataClassName, data.getPresenter().getTypeName(), data.getViewInterface().getTypeName())).addModifiers(Modifier.PUBLIC, Modifier.FINAL);
            // constructor ...
            MethodSpec.Builder constructor = MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addStatement("super($S, $T.PRESENTER, $N, $T.$L)", data.getPresenter().getClassName(), presenterMetaDataKindClassName, data.getIsMultiple(), presenterViewCreationMethodClassName, data.getViewCreationMethod());
            if (!metaModel.getPresenterData(presenter).isMultiple()) {
                constructor.addStatement("super.presenter = new $T()", data.getPresenter().getTypeName());
                if (Presenter.VIEW_CREATION_METHOD.FRAMEWORK.toString().equals(data.getViewCreationMethod())) {
                    constructor.addStatement("super.view = ($T) new $T()", data.getViewInterface().getTypeName(), data.getViewClass().getTypeName());
                } else {
                    constructor.addStatement("super.view = presenter.createView()");
                }
            }
            typeSpec.addMethod(constructor.build());
            JavaFile javaFile = JavaFile.builder(data.getPresenter().getPackage(), typeSpec.build()).build();
            try {
                javaFile.writeTo(this.processingEnvironment.getFiler());
            } catch (IOException e) {
                throw new ProcessorException("Unable to write generated file: >>" + data.getPresenter().getSimpleName() + ProcessorConstants.META_DATA + "<< -> exception: " + e.getMessage());
            }
        }
    }
}
Also used : HandlerMetaData(com.github.mvp4g.mvp4g2.core.internal.ui.HandlerMetaData) ProcessorException(com.github.mvp4g.mvp4g2.processor.ProcessorException) MethodSpec(com.squareup.javapoet.MethodSpec) IOException(java.io.IOException) Presenter(com.github.mvp4g.mvp4g2.core.ui.annotation.Presenter) PresenterMetaModel(com.github.mvp4g.mvp4g2.processor.model.PresenterMetaModel) ClassName(com.squareup.javapoet.ClassName) JavaFile(com.squareup.javapoet.JavaFile) ClassNameModel(com.github.mvp4g.mvp4g2.processor.model.intern.ClassNameModel) TypeSpec(com.squareup.javapoet.TypeSpec)

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