use of com.squareup.javapoet.ParameterizedTypeName in project kripton by xcesco.
the class SelectScalarListHelper method generateSpecializedPart.
/*
* (non-Javadoc)
*
* @see com.abubusoft.kripton.processor.sqlite.SQLiteSelectBuilder.
* SelectCodeGenerator#generate(com.squareup.javapoet.MethodSpec.Builder)
*/
@Override
public void generateSpecializedPart(SQLiteModelMethod method, TypeSpec.Builder classBuilder, MethodSpec.Builder methodBuilder, Set<JQLProjection> fieldList, boolean mapFields) {
// ASSERT: returnType is a supported type
// no column or too many columns
AssertKripton.assertTrueOrInvalidMethodSignException(fieldList.size() == 1, method, "only one field can be defined as result for this method");
ParameterizedTypeName returnListName = (ParameterizedTypeName) method.getReturnClass();
;
TypeName collectionClass;
ClassName listClazzName = returnListName.rawType;
TypeName elementName = returnListName.typeArguments.get(0);
collectionClass = SelectBeanListHelper.defineCollection(listClazzName);
methodBuilder.addCode("\n");
if (TypeUtility.isTypeEquals(collectionClass, TypeUtility.typeName(ArrayList.class))) {
methodBuilder.addStatement("$T<$T> resultList=new $T<$T>(_cursor.getCount())", collectionClass, elementName, collectionClass, elementName);
} else {
methodBuilder.addStatement("$T<$T> resultList=new $T<$T>()", collectionClass, elementName, collectionClass, elementName);
}
methodBuilder.addCode("\n");
SQLTransform t = SQLTransformer.lookup(returnListName);
// @formatter:off
methodBuilder.addCode("\n");
methodBuilder.beginControlFlow("if (_cursor.moveToFirst())");
methodBuilder.addCode("\n");
methodBuilder.beginControlFlow("do\n");
methodBuilder.beginControlFlow("if (!_cursor.isNull(0))");
methodBuilder.addCode("resultList.add(");
t.generateReadValueFromCursor(methodBuilder, method.getParent(), elementName, "_cursor", "0");
methodBuilder.addCode(");\n");
methodBuilder.nextControlFlow("else");
methodBuilder.addCode("resultList.add(null);\n");
methodBuilder.endControlFlow();
methodBuilder.endControlFlow("while (_cursor.moveToNext())");
methodBuilder.endControlFlow();
methodBuilder.addCode("return resultList;\n");
methodBuilder.endControlFlow();
// @formatter:on
}
use of com.squareup.javapoet.ParameterizedTypeName in project kripton by xcesco.
the class BindAsyncTaskBuilder method generate.
/**
* Generate async task for database operations
*
* @throws IOException
*/
public static void generate(Elements elementUtils, Filer filer, SQLiteDatabaseSchema schema) throws IOException {
String className = schema.getName();
String dataSourceName = PREFIX + className;
className = className.replaceAll(BindDataSourceBuilder.SUFFIX, SUFFIX);
className = PREFIX + className;
PackageElement pkg = elementUtils.getPackageOf(schema.getElement());
String packageName = pkg.isUnnamed() ? "" : pkg.getQualifiedName().toString();
AnnotationProcessorUtilis.infoOnGeneratedClasses(BindDataSource.class, packageName, className);
// @formatter:off
builder = TypeSpec.classBuilder(className).addModifiers(Modifier.PUBLIC).addModifiers(Modifier.ABSTRACT).addTypeVariable(TypeVariableName.get("I")).addTypeVariable(TypeVariableName.get("U")).addTypeVariable(TypeVariableName.get("R"));
// @formatter:on
// javadoc
builder.addJavadoc("\n<p>\nSpecialized async task to make async database operation on activity\n</p>\n");
builder.addJavadoc("\n<p>\nUnlike standard async task, for an instance of this class can be used many time.\n</p>\n");
builder.addJavadoc("\n<p>\nWhen method <code>execute</code> is invoked, an inner async task is created.\n</p>\n\n");
JavadocUtility.generateJavadocGeneratedBy(builder);
builder.addJavadoc("@param I input param\n");
builder.addJavadoc("@param U update param\n");
builder.addJavadoc("@param R result param\n\n");
builder.addJavadoc("@see $T\n", className(className.replaceAll(SUFFIX, BindDaoFactoryBuilder.SUFFIX)));
builder.addJavadoc("@see $T\n", className(dataSourceName));
builder.addJavadoc("@see $T\n", BindAsyncTaskType.class);
// build constructors
builder.addMethod(MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addStatement("this($T.READ)", BindAsyncTaskType.class).addJavadoc("<p>\nWith this constructor, a read only database connection will be used\n</p>\n").build());
builder.addMethod(MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addJavadoc("<p>\nWith this constructor it is possible to specify which type of database use in async task\n</p>\n\n").addJavadoc("@param mode allows to specify if and how open a data source connection\n").addParameter(BindAsyncTaskType.class, "mode").addCode("this.mode = mode;").build());
// define fields
{
FieldSpec.Builder fieldSpec = FieldSpec.builder(BindAsyncTaskType.class, "mode", Modifier.PROTECTED);
fieldSpec.addJavadoc("Allows to specify how async task interacts with data source.\n\n");
builder.addField(fieldSpec.build());
}
{
ParameterizedTypeName parameterizedTypeName = ParameterizedTypeName.get(TypeUtility.className(AsyncTask.class), TypeUtility.typeName("I"), TypeUtility.typeName("U"), TypeUtility.typeName("R"));
FieldSpec.Builder fieldSpec = FieldSpec.builder(parameterizedTypeName, "asyncTask", Modifier.PROTECTED);
fieldSpec.addJavadoc("Async task wrapped by this class\n\n");
builder.addField(fieldSpec.build());
}
// build methods
builder.addMethod(MethodSpec.methodBuilder("onPreExecute").addModifiers(Modifier.PUBLIC).addJavadoc("Use this method for operations on UI-thread before start execution\n").build());
builder.addMethod(MethodSpec.methodBuilder("onExecute").returns(TypeUtility.typeName("R")).addParameter(TypeUtility.typeName(dataSourceName), "dataSource").addJavadoc("Method used to encapsulate operations on datasource\n\n@param dataSource\n\tuse it to retrieve DAO\n@return\n\tresult of operation (list, bean, etc) and execute transactions.\n").addModifiers(Modifier.PUBLIC).addModifiers(Modifier.ABSTRACT).addException(Throwable.class).build());
builder.addMethod(MethodSpec.methodBuilder("onFinish").addParameter(TypeUtility.typeName("R"), "result").addModifiers(Modifier.PUBLIC).addModifiers(Modifier.ABSTRACT).addJavadoc("Use this method for operations on UI-thread after execution\n").build());
builder.addMethod(MethodSpec.methodBuilder("onProgressUpdate").addModifiers(Modifier.PUBLIC).addParameter(ParameterSpec.builder(ArrayTypeName.of(TypeUtility.typeName("U")), "update").build()).varargs().addJavadoc("Override this method to KRIPTON_DEBUG operation progress on UI-Thread\n").build());
builder.addMethod(MethodSpec.methodBuilder("onError").addParameter(Throwable.class, "exception").addModifiers(Modifier.PUBLIC).addJavadoc("This method is invoked when <code>onExecute</code> method generate an exception.\n@param exception exception generated\n").addStatement("$T.error(exception.getMessage())", Logger.class).addStatement("exception.printStackTrace()").build());
// method execute1
{
MethodSpec.Builder executeBuilder = MethodSpec.methodBuilder("execute").addParameter(ParameterSpec.builder(ArrayTypeName.of(TypeUtility.className("I")), "params").addAnnotation(AnnotationSpec.builder(SuppressWarnings.class).addMember("value", "$S", "unchecked").build()).build()).varargs(true).addModifiers(Modifier.PUBLIC).addJavadoc("Method to start operations.\n\n@param executor used executor\n@param data input\n");
executeBuilder.addStatement("executeOnExecutor($T.SERIAL_EXECUTOR, params)", android.os.AsyncTask.class);
builder.addMethod(executeBuilder.build());
}
// method execute2
{
MethodSpec.Builder executeBuilder = MethodSpec.methodBuilder("executeOnExecutor").addParameter(Executor.class, "executor").addParameter(ParameterSpec.builder(ArrayTypeName.of(TypeUtility.className("I")), "params").addAnnotation(AnnotationSpec.builder(SuppressWarnings.class).addMember("value", "$S", "unchecked").build()).build()).varargs(true).addModifiers(Modifier.PUBLIC).addJavadoc("Method to start operations.\n\n@param executor used executor\n@param data input\n");
// @formatter:off
TypeSpec.Builder anonymous = TypeSpec.anonymousClassBuilder("").addSuperinterface(ParameterizedTypeName.get(TypeUtility.className(AsyncTask.class), TypeUtility.className("I"), TypeUtility.className("U"), TypeUtility.className("R")));
anonymous.addMethod(MethodSpec.methodBuilder("onPreExecute").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).addStatement("$L.this.onPreExecute()", className).build());
anonymous.addMethod(MethodSpec.methodBuilder("doInBackground").addModifiers(Modifier.PUBLIC).returns(TypeUtility.typeName("R")).addAnnotation(Override.class).addParameter(ParameterSpec.builder(ArrayTypeName.of(TypeUtility.className("I")), "params").addAnnotation(AnnotationSpec.builder(SuppressWarnings.class).addMember("value", "$S", "unchecked").build()).build()).varargs(true).addStatement("$L dataSource=$L.instance()", dataSourceName, dataSourceName).addStatement("R result=null").addStatement("if (mode==$T.READ) dataSource.openReadOnlyDatabase(); else if (mode==$T.READ_WRITE) dataSource.openWritableDatabase()", BindAsyncTaskType.class, BindAsyncTaskType.class).beginControlFlow("try").addStatement("result=onExecute(dataSource)").nextControlFlow("catch(Throwable e)").addStatement("onError(e)").nextControlFlow("finally").beginControlFlow("if (dataSource.isOpen())").addStatement("dataSource.close()").endControlFlow().endControlFlow().addStatement("return result").build());
anonymous.addMethod(MethodSpec.methodBuilder("onProgressUpdate").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).addParameter(ParameterSpec.builder(ArrayTypeName.of(TypeUtility.className("U")), "values").addAnnotation(AnnotationSpec.builder(SuppressWarnings.class).addMember("value", "$S", "unchecked").build()).build()).varargs(true).addStatement("$L.this.onProgressUpdate(values)", className).build());
anonymous.addMethod(MethodSpec.methodBuilder("onPostExecute").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).addParameter(ParameterSpec.builder(TypeUtility.className("R"), "result").build()).addStatement("$L.this.onFinish(result)", className).build());
// @formatter:on
// @formatter:off
executeBuilder.addStatement("asyncTask=$L", anonymous.build());
executeBuilder.addStatement("asyncTask.executeOnExecutor(executor, params)", anonymous.build());
builder.addMethod(executeBuilder.build());
anonymous.addMethod(MethodSpec.methodBuilder("cancel").addModifiers(Modifier.PUBLIC).addModifiers(Modifier.FINAL).addParameter(Boolean.TYPE, "mayInterruptIfRunning").beginControlFlow("if (asyncTask!=null, args)").addStatement("return asyncTask.cancel(mayInterruptIfRunning)").endControlFlow().addStatement("return false").build());
}
// build SimpleAsyncTask
builder.addType(TypeSpec.classBuilder("Simple").addJavadoc("Simple implementation of async task. It uses read only database.\n\n").addJavadoc("@see $T\n", TypeUtility.typeName(className.replaceAll(SUFFIX, BindDaoFactoryBuilder.SUFFIX))).addJavadoc("@see $T\n", TypeUtility.typeName(dataSourceName)).addModifiers(Modifier.PUBLIC).addModifiers(Modifier.STATIC).addModifiers(Modifier.ABSTRACT).addTypeVariable(TypeVariableName.get("R")).superclass(ParameterizedTypeName.get(TypeUtility.className(className), TypeUtility.className("Void"), TypeUtility.className("Void"), TypeUtility.className("R"))).addMethod(MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addJavadoc("Create an simple async task allowing user to decide which kind of operation can be done on datasource").addParameter(BindAsyncTaskType.class, "mode").addStatement("super(mode)").build()).addMethod(MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addJavadoc("Create an simple async task for data source read only operation").addStatement("super($T.READ)", BindAsyncTaskType.class).build()).build());
// @formatter:on
TypeSpec typeSpec = builder.build();
JavaWriterHelper.writeJava2File(filer, packageName, typeSpec);
}
use of com.squareup.javapoet.ParameterizedTypeName in project kripton by xcesco.
the class BindCursorBuilder method generateExecuteMethod.
private MethodSpec.Builder generateExecuteMethod(String packageName, SQLiteEntity entity) {
ParameterizedTypeName parameterizedReturnTypeName = ParameterizedTypeName.get(className(ArrayList.class), className(packageName, entity.getSimpleName()));
// @formatter:off
MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("execute").addJavadoc("<p>Execute the cursor and read all rows from database.</p>\n\n").addJavadoc("@return list of beans\n").addModifiers(Modifier.PUBLIC).returns(parameterizedReturnTypeName);
// @formatter:on
TypeName entityClass = typeName(entity.getElement());
methodBuilder.addCode("\n");
methodBuilder.addCode("$T resultList=new $T(cursor.getCount());\n", parameterizedReturnTypeName, parameterizedReturnTypeName);
methodBuilder.addCode("$T resultBean=new $T();\n", entityClass, entityClass);
methodBuilder.addCode("\n");
methodBuilder.beginControlFlow("if (cursor.moveToFirst())");
methodBuilder.beginControlFlow("do\n");
methodBuilder.addCode("resultBean=new $T();\n\n", entityClass);
// generate mapping
int i = 0;
for (ModelProperty item : entity.getCollection()) {
methodBuilder.addCode("if (index$L>=0 && !cursor.isNull(index$L)) { ", i, i);
SQLTransformer.cursor2Java(methodBuilder, entityClass, item, "resultBean", "cursor", "index" + i + "");
methodBuilder.addCode(";");
methodBuilder.addCode("}\n");
i++;
}
methodBuilder.addCode("\n");
methodBuilder.addCode("resultList.add(resultBean);\n");
methodBuilder.endControlFlow("while (cursor.moveToNext())");
methodBuilder.endControlFlow();
methodBuilder.addCode("cursor.close();\n");
methodBuilder.addCode("\n");
methodBuilder.addCode("return resultList;\n");
return methodBuilder;
}
use of com.squareup.javapoet.ParameterizedTypeName in project kripton by xcesco.
the class SQLiteModelMethod method isLiveData.
/**
* @throws ClassNotFoundException
*/
public static boolean isLiveData(SQLiteModelMethod methodDefinition) {
boolean result = false;
TypeName returnTypeName = methodDefinition.getReturnClass();
if (returnTypeName instanceof ParameterizedTypeName) {
ParameterizedTypeName returnParameterizedTypeName = (ParameterizedTypeName) returnTypeName;
ClassName returnParameterizedClassName = returnParameterizedTypeName.rawType;
Class<?> wrapperClazz;
try {
wrapperClazz = Class.forName(returnParameterizedClassName.toString());
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw (new KriptonRuntimeException(e));
}
if ("android.arch.lifecycle.LiveData".equals(wrapperClazz.getName())) {
result = true;
}
}
return result;
}
use of com.squareup.javapoet.ParameterizedTypeName in project kripton by xcesco.
the class SetPrefsTransformation method isStringSet.
/**
* @param property
* @return
*/
public static boolean isStringSet(PrefsProperty property) {
boolean isStringSet = false;
TypeName typeName;
if (property.hasTypeAdapter()) {
typeName = TypeUtility.typeName(property.typeAdapter.dataType);
} else {
typeName = TypeUtility.typeName(property.getElement().asType());
}
if (typeName instanceof ParameterizedTypeName && TypeUtility.isSet(typeName)) {
ParameterizedTypeName parameterizedTypeName = (ParameterizedTypeName) typeName;
if (TypeUtility.isEquals(parameterizedTypeName.typeArguments.get(0), String.class.getCanonicalName())) {
isStringSet = true;
}
}
return isStringSet;
}
Aggregations