Search in sources :

Example 1 with Builder

use of com.squareup.javapoet.FieldSpec.Builder in project kripton by xcesco.

the class BindDataSourceBuilder method buildDataSource.

public void buildDataSource(Elements elementUtils, Filer filer, SQLiteDatabaseSchema schema, String daoFactoryName) throws Exception {
    ClassName daoFactoryClazz = className(daoFactoryName);
    Converter<String, String> convert = CaseFormat.UPPER_CAMEL.converterTo(CaseFormat.LOWER_CAMEL);
    ClassName dataSourceClassName = generateDataSourceName(schema);
    AnnotationProcessorUtilis.infoOnGeneratedClasses(BindDataSource.class, dataSourceClassName);
    classBuilder = TypeSpec.classBuilder(dataSourceClassName.simpleName()).addModifiers(Modifier.PUBLIC).superclass(AbstractDataSource.class).addSuperinterface(daoFactoryClazz).addSuperinterface(TypeUtility.typeName(schema.getElement().asType()));
    classBuilder.addJavadoc("<p>\n");
    classBuilder.addJavadoc("Represents implementation of datasource $L.\n", schema.getName());
    classBuilder.addJavadoc("This class expose database interface through Dao attribute.\n", schema.getName());
    classBuilder.addJavadoc("</p>\n\n");
    JavadocUtility.generateJavadocGeneratedBy(classBuilder);
    classBuilder.addJavadoc("@see $T\n", className(schema.getName()));
    classBuilder.addJavadoc("@see $T\n", daoFactoryClazz);
    for (SQLiteDaoDefinition dao : schema.getCollection()) {
        TypeName daoImplName = BindDaoBuilder.daoTypeName(dao);
        classBuilder.addJavadoc("@see $T\n", dao.getElement());
        classBuilder.addJavadoc("@see $T\n", daoImplName);
        String entity = BindDataSourceSubProcessor.generateEntityName(dao, dao.getEntity());
        classBuilder.addJavadoc("@see $T\n", TypeUtility.typeName(entity));
    }
    // define static fields
    // instance
    classBuilder.addField(FieldSpec.builder(dataSourceClassName, "instance", Modifier.STATIC, Modifier.VOLATILE).addJavadoc("<p>datasource singleton</p>\n").build());
    classBuilder.addField(FieldSpec.builder(Object.class, "mutex", Modifier.STATIC, Modifier.FINAL, Modifier.PRIVATE).addJavadoc("<p>Mutex to manage multithread access to instance</p>\n").initializer("new Object()").build());
    for (SQLiteDaoDefinition dao : schema.getCollection()) {
        // TypeName daoInterfaceName =
        // BindDaoBuilder.daoInterfaceTypeName(dao);
        TypeName daoImplName = BindDaoBuilder.daoTypeName(dao);
        classBuilder.addField(FieldSpec.builder(daoImplName, convert.convert(dao.getName()), Modifier.PROTECTED).addJavadoc("<p>dao instance</p>\n").initializer("new $T(context)", daoImplName).build());
        // dao with connections
        {
            MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("get" + dao.getName()).addAnnotation(Override.class).addModifiers(Modifier.PUBLIC).returns(BindDaoBuilder.daoTypeName(dao));
            methodBuilder.addCode("return $L;\n", convert.convert(dao.getName()));
            classBuilder.addMethod(methodBuilder.build());
        }
    }
    if (schema.generateRx) {
        generateRx(dataSourceClassName, daoFactoryName);
        for (SQLiteDaoDefinition dao : schema.getCollection()) {
            // subject
            MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, dao.getEntitySimplyClassName() + "Subject")).addModifiers(Modifier.PUBLIC);
            methodBuilder.addStatement("return $L.subject()", CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, convert.convert(dao.getName()))).returns(ParameterizedTypeName.get(PublishSubject.class, SQLiteEvent.class));
            classBuilder.addMethod(methodBuilder.build());
        }
    }
    // interface
    generateMethodExecuteTransaction(daoFactoryName);
    generateMethodExecuteBatch(daoFactoryName);
    // generate instance
    generateInstanceOrBuild(schema, dataSourceClassName.simpleName(), true);
    // generate open
    generateOpen(dataSourceClassName.simpleName());
    // generate openReadOnly
    generateOpenReadOnly(dataSourceClassName.simpleName());
    // generate constructor
    generateConstructor(schema);
    // before use entities, order them with dependencies respect
    List<SQLiteEntity> orderedEntities = generateOrderedEntitiesList(schema);
    // onCreate
    boolean useForeignKey = generateOnCreate(schema, orderedEntities);
    // onUpgrade
    generateOnUpgrade(schema, orderedEntities);
    // onConfigure
    generateOnConfigure(useForeignKey);
    // generate
    generateDaoUids(classBuilder, schema);
    // 
    // generate prepared statement cleaner
    {
        MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("clearCompiledStatements").addModifiers(Modifier.PUBLIC).returns(Void.TYPE);
        for (SQLiteDaoDefinition dao : schema.getCollection()) {
            methodBuilder.addStatement("$T.clearCompiledStatements()", TypeUtility.className(dao.getElement().getQualifiedName().toString() + "Impl"));
        }
        classBuilder.addMethod(methodBuilder.build());
    }
    // generate single thread datasource
    generateDataSourceSingleThread(schema, dataSourceClassName.simpleName());
    // generate build
    generateInstanceOrBuild(schema, dataSourceClassName.simpleName(), false);
    {
        Builder f = FieldSpec.builder(ArrayTypeName.of(SQLiteTable.class), "TABLES", Modifier.FINAL, Modifier.STATIC).addJavadoc("List of tables compose datasource\n");
        com.squareup.javapoet.CodeBlock.Builder c = CodeBlock.builder();
        String s = "";
        c.add("{");
        for (SQLiteEntity entity : schema.getEntities()) {
            String tableName = BindTableGenerator.getTableClassName(entity.getName());
            c.add(s + "new $T()", TypeUtility.className(tableName));
            s = ", ";
        }
        for (GeneratedTypeElement entity : schema.generatedEntities) {
            String tableName = BindTableGenerator.getTableClassName(entity.getQualifiedName());
            c.add(s + "new $T()", TypeUtility.className(tableName));
            s = ", ";
        }
        c.add("}");
        f.initializer(c.build());
        classBuilder.addField(f.build());
        classBuilder.addMethod(MethodSpec.methodBuilder("tables").addJavadoc("List of tables compose datasource:\n").addModifiers(Modifier.PUBLIC, Modifier.STATIC).addStatement("return TABLES").returns(ArrayTypeName.of(SQLiteTable.class)).build());
    }
    TypeSpec typeSpec = classBuilder.build();
    JavaWriterHelper.writeJava2File(filer, dataSourceClassName.packageName(), typeSpec);
}
Also used : TypeName(com.squareup.javapoet.TypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) ArrayTypeName(com.squareup.javapoet.ArrayTypeName) MethodSpec(com.squareup.javapoet.MethodSpec) Builder(com.squareup.javapoet.FieldSpec.Builder) SQLiteTable(com.abubusoft.kripton.android.sqlite.SQLiteTable) AbstractDataSource(com.abubusoft.kripton.android.sqlite.AbstractDataSource) SQLiteDaoDefinition(com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition) SQLiteEvent(com.abubusoft.kripton.android.sqlite.SQLiteEvent) GeneratedTypeElement(com.abubusoft.kripton.processor.element.GeneratedTypeElement) PublishSubject(io.reactivex.subjects.PublishSubject) ClassName(com.squareup.javapoet.ClassName) SQLiteEntity(com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 2 with Builder

use of com.squareup.javapoet.FieldSpec.Builder in project kripton by xcesco.

the class BindTableGenerator method generateColumnsArray.

/**
 * generate columns array.
 *
 * @param entity
 */
private void generateColumnsArray(Finder<SQLProperty> entity) {
    // generate columns array
    Builder sp = FieldSpec.builder(ArrayTypeName.of(String.class), "COLUMNS", Modifier.STATIC, Modifier.PRIVATE, Modifier.FINAL);
    String s = "";
    StringBuilder buffer = new StringBuilder();
    for (SQLProperty property : entity.getCollection()) {
        buffer.append(s + "COLUMN_" + columnNameToUpperCaseConverter.convert(property.getName()));
        s = ", ";
    }
    classBuilder.addField(sp.addJavadoc("Columns array\n").initializer("{" + buffer.toString() + "}").build());
    classBuilder.addMethod(MethodSpec.methodBuilder("columns").addModifiers(Modifier.PUBLIC).addJavadoc("Columns array\n").addAnnotation(Override.class).returns(ArrayTypeName.of(String.class)).addStatement("return COLUMNS").build());
    classBuilder.addMethod(MethodSpec.methodBuilder("name").addModifiers(Modifier.PUBLIC).addJavadoc("table name\n").addAnnotation(Override.class).returns(TypeName.get(String.class)).addStatement("return TABLE_NAME").build());
}
Also used : Builder(com.squareup.javapoet.FieldSpec.Builder) SQLProperty(com.abubusoft.kripton.processor.sqlite.model.SQLProperty)

Aggregations

Builder (com.squareup.javapoet.FieldSpec.Builder)2 AbstractDataSource (com.abubusoft.kripton.android.sqlite.AbstractDataSource)1 SQLiteEvent (com.abubusoft.kripton.android.sqlite.SQLiteEvent)1 SQLiteTable (com.abubusoft.kripton.android.sqlite.SQLiteTable)1 GeneratedTypeElement (com.abubusoft.kripton.processor.element.GeneratedTypeElement)1 SQLProperty (com.abubusoft.kripton.processor.sqlite.model.SQLProperty)1 SQLiteDaoDefinition (com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition)1 SQLiteEntity (com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity)1 ArrayTypeName (com.squareup.javapoet.ArrayTypeName)1 ClassName (com.squareup.javapoet.ClassName)1 MethodSpec (com.squareup.javapoet.MethodSpec)1 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)1 TypeName (com.squareup.javapoet.TypeName)1 TypeSpec (com.squareup.javapoet.TypeSpec)1 PublishSubject (io.reactivex.subjects.PublishSubject)1