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);
}
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());
}
Aggregations