Search in sources :

Example 1 with PresenterMetaModel

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

the class PresenterAnnotationScanner method scan.

public PresenterMetaModel scan(RoundEnvironment roundEnvironment) throws ProcessorException {
    // Validator
    PresenterAnnotationValidator validator = PresenterAnnotationValidator.builder().processingEnvironment(processingEnvironment).build();
    // read all already created model
    PresenterMetaModel model = this.restore();
    // iterate over Presenter
    for (Element element : roundEnvironment.getElementsAnnotatedWith(Presenter.class)) {
        TypeElement typeElement = (TypeElement) element;
        // validate
        validator.validate(typeElement, this.getViewClassTypeElement(typeElement.getAnnotation(Presenter.class)), this.getViewInterfaceTypeElement(typeElement.getAnnotation(Presenter.class)));
        // update model
        model.add(((TypeElement) element).getQualifiedName().toString(), typeElement.getAnnotation(Presenter.class).multiple() ? "true" : "false", this.processorUtils.extendsClassOrInterface(this.processingEnvironment.getTypeUtils(), typeElement.asType(), this.processingEnvironment.getElementUtils().getTypeElement(IsShell.class.getCanonicalName()).asType()) ? "true" : "false", this.getViewClassTypeElement(element.getAnnotation(Presenter.class)).getQualifiedName().toString(), this.getViewInterfaceTypeElement(element.getAnnotation(Presenter.class)).getQualifiedName().toString(), typeElement.getAnnotation(Presenter.class).viewCreator().toString(), this.processorUtils.createHandledEventArray(typeElement));
    }
    // let's store the updated model
    this.processorUtils.store(model, this.createRelativeFileName());
    return model;
}
Also used : IsShell(com.github.mvp4g.mvp4g2.core.ui.IsShell) PresenterMetaModel(com.github.mvp4g.mvp4g2.processor.model.PresenterMetaModel) TypeElement(javax.lang.model.element.TypeElement) Presenter(com.github.mvp4g.mvp4g2.core.ui.annotation.Presenter) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) PresenterAnnotationValidator(com.github.mvp4g.mvp4g2.processor.scanner.validation.PresenterAnnotationValidator)

Example 2 with PresenterMetaModel

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

the class PresenterAnnotationScanner method restore.

private PresenterMetaModel restore() {
    Properties props = new Properties();
    try {
        FileObject resource = this.processingEnvironment.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", this.createRelativeFileName());
        props.load(resource.openInputStream());
        return new PresenterMetaModel(props);
    } catch (IOException e) {
    // every thing is ok -> no operation
    // this.processorUtils.createNoteMessage("no resource found for : >>" + this.createRelativeFileName() + "<<");
    }
    return new PresenterMetaModel();
}
Also used : PresenterMetaModel(com.github.mvp4g.mvp4g2.processor.model.PresenterMetaModel) FileObject(javax.tools.FileObject) IOException(java.io.IOException) Properties(java.util.Properties)

Example 3 with PresenterMetaModel

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

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

Example 5 with PresenterMetaModel

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

the class PropertiesTest method testPresenteryMetaModel.

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

Aggregations

PresenterMetaModel (com.github.mvp4g.mvp4g2.processor.model.PresenterMetaModel)4 IOException (java.io.IOException)4 Presenter (com.github.mvp4g.mvp4g2.core.ui.annotation.Presenter)2 ProcessorException (com.github.mvp4g.mvp4g2.processor.ProcessorException)2 ClassName (com.squareup.javapoet.ClassName)2 JavaFile (com.squareup.javapoet.JavaFile)2 MethodSpec (com.squareup.javapoet.MethodSpec)2 TypeSpec (com.squareup.javapoet.TypeSpec)2 Properties (java.util.Properties)2 HandlerMetaData (com.github.mvp4g.mvp4g2.core.internal.ui.HandlerMetaData)1 IsShell (com.github.mvp4g.mvp4g2.core.ui.IsShell)1 ClassNameModel (com.github.mvp4g.mvp4g2.processor.model.intern.ClassNameModel)1 PresenterAnnotationValidator (com.github.mvp4g.mvp4g2.processor.scanner.validation.PresenterAnnotationValidator)1 Element (javax.lang.model.element.Element)1 TypeElement (javax.lang.model.element.TypeElement)1 FileObject (javax.tools.FileObject)1 Test (org.junit.Test)1