Search in sources :

Example 1 with ApplicationMetaModel

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

the class ApplicationAnnotationScanner method scan.

public ApplicationMetaModel scan(RoundEnvironment roundEnvironment) throws ProcessorException {
    // First we try to read an already created resource ...
    ApplicationMetaModel model = this.restore();
    // Check if we have an element annotated with @Application
    if (!roundEnvironment.getElementsAnnotatedWith(Application.class).isEmpty()) {
        // check, whether we have o do something ...
        ApplicationAnnotationValidator validator = ApplicationAnnotationValidator.builder().roundEnvironment(roundEnvironment).processingEnvironment(this.processingEnvironment).build();
        validator.validate();
        // should only be one, so we can search for the first! ...
        Optional<? extends Element> optionalElement = this.roundEnvironment.getElementsAnnotatedWith(Application.class).stream().findFirst();
        if (optionalElement.isPresent()) {
            Element applicationAnnotationElement = optionalElement.get();
            validator.validate(applicationAnnotationElement);
            Application applicationAnnotation = applicationAnnotationElement.getAnnotation(Application.class);
            if (!isNull(applicationAnnotation)) {
                TypeElement eventBusTypeElement = this.getEventBusTypeElement(applicationAnnotation);
                TypeElement applicationLoaderTypeElement = this.getApplicationLoaderTypeElement(applicationAnnotation);
                model = new ApplicationMetaModel(applicationAnnotationElement.toString(), isNull(eventBusTypeElement) ? "" : eventBusTypeElement.toString(), isNull(applicationLoaderTypeElement) ? "" : applicationLoaderTypeElement.toString(), String.valueOf(applicationAnnotation.historyOnStart()));
                // let's store the updated model
                this.processorUtils.store(model, this.createRelativeFileName());
            }
        }
    }
    return model;
}
Also used : ApplicationMetaModel(com.github.mvp4g.mvp4g2.processor.model.ApplicationMetaModel) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) Application(com.github.mvp4g.mvp4g2.core.application.annotation.Application) ApplicationAnnotationValidator(com.github.mvp4g.mvp4g2.processor.scanner.validation.ApplicationAnnotationValidator)

Example 2 with ApplicationMetaModel

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

the class ApplicationAnnotationScanner method restore.

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

Example 3 with ApplicationMetaModel

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

the class ApplicationGenerator method generate.

public void generate(ApplicationMetaModel metaModel) throws ProcessorException {
    // check if element is existing (to avoid generating code for deleted items)
    if (!this.processorUtils.doesExist(metaModel.getApplication())) {
        return;
    }
    // generate code
    TypeSpec.Builder typeSpec = TypeSpec.classBuilder(metaModel.getApplication().getSimpleName() + ApplicationGenerator.IMPL_NAME).superclass(ParameterizedTypeName.get(ClassName.get(AbstractApplication.class), metaModel.getEventBus().getTypeName())).addModifiers(Modifier.PUBLIC, Modifier.FINAL).addSuperinterface(metaModel.getApplication().getTypeName());
    // constructor ...
    MethodSpec constructor = MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addStatement("super()").addStatement("super.eventBus = new $N.$N()", metaModel.getEventBus().getPackage(), metaModel.getEventBus().getSimpleName() + ApplicationGenerator.IMPL_NAME).addStatement("super.historyOnStart = $L", metaModel.getHistoryOnStart()).build();
    typeSpec.addMethod(constructor);
    // method "getApplicaitonLoader"
    MethodSpec getApplicaitonLaoderMethod = MethodSpec.methodBuilder("getApplicationLoader").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).returns(IsApplicationLoader.class).addStatement("return new $T()", metaModel.getLoader().getTypeName()).build();
    typeSpec.addMethod(getApplicaitonLaoderMethod);
    JavaFile javaFile = JavaFile.builder(metaModel.getEventBus().getPackage(), typeSpec.build()).build();
    try {
        javaFile.writeTo(this.processingEnvironment.getFiler());
    } catch (IOException e) {
        throw new ProcessorException("Unable to write generated file: >>" + metaModel.getEventBus().getSimpleName() + ApplicationGenerator.IMPL_NAME + "<< -> 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) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 4 with ApplicationMetaModel

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

the class PropertiesTest method testApplicationMetaModel.

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

Aggregations

ApplicationMetaModel (com.github.mvp4g.mvp4g2.processor.model.ApplicationMetaModel)3 IOException (java.io.IOException)3 Properties (java.util.Properties)2 Application (com.github.mvp4g.mvp4g2.core.application.annotation.Application)1 ProcessorException (com.github.mvp4g.mvp4g2.processor.ProcessorException)1 ApplicationAnnotationValidator (com.github.mvp4g.mvp4g2.processor.scanner.validation.ApplicationAnnotationValidator)1 JavaFile (com.squareup.javapoet.JavaFile)1 MethodSpec (com.squareup.javapoet.MethodSpec)1 TypeSpec (com.squareup.javapoet.TypeSpec)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