use of com.squareup.javapoet.ParameterSpec in project kripton by xcesco.
the class SelectPaginatedResultHelper method buildSpecializedPagedResultClass.
/**
* Build paginated result class handler.
*
* @param classBuilder
* the class builder
* @param method
* the method
* @return name of generated class
*/
public static String buildSpecializedPagedResultClass(TypeSpec.Builder classBuilder, SQLiteModelMethod method) {
// TypeName entityTypeName =
// TypeUtility.typeName(method.getParent().getEntityClassName());
TypeName entityTypeName = TypeUtility.typeName(method.getEntity().getName());
String pagedResultName = "PaginatedResult" + (pagedResultCounter++);
TypeSpec.Builder typeBuilder = TypeSpec.classBuilder(pagedResultName).addModifiers(Modifier.PUBLIC).superclass(TypeUtility.parameterizedTypeName(TypeUtility.className(PagedResultImpl.class), entityTypeName));
// add fields and define constructor
MethodSpec.Builder setupBuilder = MethodSpec.constructorBuilder();
MethodSpec.Builder executeBuilder = MethodSpec.methodBuilder("execute").addModifiers(Modifier.PUBLIC).returns(TypeUtility.parameterizedTypeName(TypeUtility.className(List.class), entityTypeName));
executeBuilder.addComment("Executor builder - BEGIN");
// method for execution with pageRequest
// @formatter:off
ClassName dataSourceClassName = BindDataSourceBuilder.generateDataSourceName(method.getParent().getParent());
MethodSpec.Builder executeWithPageRequestBuilder = MethodSpec.methodBuilder("execute").addModifiers(Modifier.PUBLIC).addParameter(PageRequest.class, "pageRequest").returns(TypeUtility.parameterizedTypeName(TypeUtility.className(List.class), entityTypeName));
executeWithPageRequestBuilder.addComment("Executor with pageRequet - BEGIN");
executeWithPageRequestBuilder.addCode("return $T.getInstance().executeBatch(daoFactory -> daoFactory.get$L().$L" + SelectPaginatedResultHelper.WITH_PAGE_REQUEST_PREFIX + "(", dataSourceClassName, method.getParentName(), method.getName());
ClassName daoFactoryClassName = BindDaoFactoryBuilder.generateDaoFactoryClassName(method.getParent().getParent());
MethodSpec.Builder executeWithDaoFactortBuilder = MethodSpec.methodBuilder("execute").addParameter(daoFactoryClassName, "daoFactory").addModifiers(Modifier.PUBLIC).returns(TypeUtility.parameterizedTypeName(TypeUtility.className(List.class), entityTypeName));
executeWithDaoFactortBuilder.addCode("return daoFactory.get$L().$L(", method.getParentName(), method.getName());
if (!method.isPagedLiveData()) {
executeBuilder.addCode("list=$T.this.$L(", TypeUtility.typeName(method.getParent().getElement(), BindDaoBuilder.SUFFIX), method.getName());
}
// we have always a first parameter
String separator = "";
ParameterSpec parameterSpec;
for (Pair<String, TypeName> item : method.getParameters()) {
if (method.hasDynamicPageSizeConditions() && method.dynamicPageSizeName.equals(item.value0)) {
setupBuilder.addStatement("this.pageSize=$L", item.value0);
} else {
// field
typeBuilder.addField(item.value1, item.value0);
setupBuilder.addStatement("this.$L=$L", item.value0, item.value0);
}
// construtor
parameterSpec = ParameterSpec.builder(item.value1, item.value0).build();
setupBuilder.addParameter(parameterSpec);
// execute
if (method.dynamicPageSizeName != null && method.dynamicPageSizeName.equals(item.value0)) {
if (!method.isPagedLiveData()) {
executeBuilder.addCode(separator + "this.pageSize");
}
executeWithDaoFactortBuilder.addCode(separator + "this.pageSize");
executeWithPageRequestBuilder.addCode(separator + "this.pageSize");
} else {
if (!method.isPagedLiveData()) {
executeBuilder.addCode(separator + item.value0);
}
executeWithDaoFactortBuilder.addCode(separator + item.value0);
executeWithPageRequestBuilder.addCode(separator + item.value0);
}
separator = ", ";
}
if (method.isPagedLiveData()) {
// if method is not paged live data, add complete execute method
executeBuilder.addComment("paged result is used in live data, so this method must be empty");
executeBuilder.addStatement("return null");
} else {
executeBuilder.addCode(separator + "this);\n");
executeBuilder.addStatement("return list");
}
executeBuilder.addComment("Executor builder - END");
typeBuilder.addMethod(executeBuilder.build());
if (!method.hasDynamicPageSizeConditions()) {
ModelAnnotation annotation = method.getAnnotation(BindSqlSelect.class);
int pageSize = annotation.getAttributeAsInt(AnnotationAttributeType.PAGE_SIZE);
// in case pageSize is not specified (only for liveData)
if (pageSize == 0) {
pageSize = 20;
}
setupBuilder.addStatement("this.pageSize=$L", pageSize);
}
typeBuilder.addMethod(setupBuilder.build());
executeWithDaoFactortBuilder.addCode(separator + "this);\n");
executeWithPageRequestBuilder.addCode(separator + "pageRequest));\n");
executeWithPageRequestBuilder.addComment("Executor with pageRequet - END");
// add methods to type builder
typeBuilder.addMethod(executeWithDaoFactortBuilder.build());
typeBuilder.addMethod(executeWithPageRequestBuilder.build());
classBuilder.addType(typeBuilder.build());
return pagedResultName;
}
use of com.squareup.javapoet.ParameterSpec in project kripton by xcesco.
the class SqlInsertBuilder method generate.
/**
* Generate.
*
* @param classBuilder
* the class builder
* @param method
* the method
*/
public static void generate(TypeSpec.Builder classBuilder, SQLiteModelMethod method) {
InsertType insertResultType = detectInsertType(method);
// if true, field must be associate to ben attributes
TypeName returnType = method.getReturnClass();
AssertKripton.failWithInvalidMethodSignException(insertResultType == null, method);
// generate method code
MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(method.getName()).addAnnotation(Override.class).addModifiers(Modifier.PUBLIC);
ParameterSpec parameterSpec;
for (Pair<String, TypeName> item : method.getParameters()) {
parameterSpec = ParameterSpec.builder(item.value1, item.value0).build();
methodBuilder.addParameter(parameterSpec);
}
methodBuilder.returns(returnType);
// fail if we use jql to INSERT_BEAN with operation of INSERT-FOR-SELECT
// AssertKripton.failWithInvalidMethodSignException(insertResultType ==
// InsertType.INSERT_BEAN && method.jql.containsSelectOperation, method,
// "INSERT-FROM-SELECT SQL can not be used with method sign");
// generate inner code
methodBuilder.addComment("Specialized Insert - $L - BEGIN", insertResultType.getClass().getSimpleName());
insertResultType.generate(classBuilder, methodBuilder, method, returnType);
methodBuilder.addComment("Specialized Insert - $L - END", insertResultType.getClass().getSimpleName());
MethodSpec methodSpec = methodBuilder.build();
classBuilder.addMethod(methodSpec);
if (method.contentProviderEntryPathEnabled) {
// we need to generate insert for content provider to
generateInsertForContentProvider(classBuilder, method, insertResultType);
}
}
use of com.squareup.javapoet.ParameterSpec in project kripton by xcesco.
the class SqlInsertBuilder method generateInsertForContentProvider.
/**
* <p>
* Generate insert used in content provider class.
* </p>
*
* @param classBuilder
* the class builder
* @param method
* the method
* @param insertResultType
* the insert result type
*/
private static void generateInsertForContentProvider(TypeSpec.Builder classBuilder, final SQLiteModelMethod method, InsertType insertResultType) {
final SQLiteDaoDefinition daoDefinition = method.getParent();
final SQLiteEntity entity = method.getEntity();
final Set<String> columns = new LinkedHashSet<>();
MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(method.contentProviderMethodName);
if (!method.getParent().hasSamePackageOfSchema()) {
methodBuilder.addModifiers(Modifier.PUBLIC);
}
ParameterSpec parameterSpec;
parameterSpec = ParameterSpec.builder(Uri.class, "uri").build();
methodBuilder.addParameter(parameterSpec);
parameterSpec = ParameterSpec.builder(ContentValues.class, "contentValues").build();
methodBuilder.addParameter(parameterSpec);
methodBuilder.returns(Long.TYPE);
SqlBuilderHelper.generateLogForContentProviderBeginning(method, methodBuilder);
// just detect which columns are admitted
JQLChecker.getInstance().replace(method, method.jql, new JQLReplacerListenerImpl(method) {
@Override
public String onColumnName(String columnName) {
SQLProperty tempProperty = entity.get(columnName);
AssertKripton.assertTrueOrUnknownPropertyInJQLException(tempProperty != null, method, columnName);
columns.add(tempProperty.columnName);
return tempProperty.columnName;
}
@Override
public String onColumnFullyQualifiedName(String tableName, String columnName) {
AssertKripton.fail("Inconsistent state");
return null;
}
});
// generate columnCheckSet
SqlBuilderHelper.generateColumnCheckSet(classBuilder, method, columns);
// retrieve content values
methodBuilder.addStatement("$T _contentValues=contentValuesForContentProvider(contentValues)", KriptonContentValues.class);
// generate column check
SqlBuilderHelper.forEachColumnInContentValue(methodBuilder, method, "_contentValues.values().keySet()", true, null);
methodBuilder.addCode("\n");
String rxIdGetter = "result";
// every controls was done in constructor of SQLiteModelMethod
for (ContentUriPlaceHolder variable : method.contentProviderUriVariables) {
SQLProperty entityProperty = entity.get(variable.value);
if (entityProperty != null) {
methodBuilder.addCode("// Add parameter $L at path segment $L\n", variable.value, variable.pathSegmentIndex);
TypeName entityPropertyType = entityProperty.getPropertyType().getTypeName();
if (TypeUtility.isString(entityPropertyType)) {
methodBuilder.addStatement("contentValues.put($S, uri.getPathSegments().get($L))", entityProperty.columnName, variable.pathSegmentIndex);
} else {
methodBuilder.addStatement("contentValues.put($S, Long.valueOf(uri.getPathSegments().get($L)))", entityProperty.columnName, variable.pathSegmentIndex);
}
}
if (entityProperty.isPrimaryKey()) {
rxIdGetter = PropertyUtility.getter(entityProperty);
}
}
// generate log for inser operation
SqlBuilderHelper.generateLogForContentValuesContentProvider(method, methodBuilder);
ConflictAlgorithmType conflictAlgorithmType = InsertBeanHelper.getConflictAlgorithmType(method);
String conflictString2 = "";
// we need to use everytime after support
// if (conflictAlgorithmType != ConflictAlgorithmType.NONE) {
conflictString2 = ", " + conflictAlgorithmType.getConflictAlgorithm();
methodBuilder.addCode("// conflict algorithm $L\n", method.jql.conflictAlgorithmType);
// }
methodBuilder.addComment("insert operation");
methodBuilder.addStatement("long result = getDatabase().insert($S$L, _contentValues.values())", entity.getTableName(), conflictString2);
if (method.getParent().getParent().generateRx) {
SQLProperty primaryKey = entity.getPrimaryKey();
if (primaryKey.columnType == ColumnType.PRIMARY_KEY) {
// long autogenerated
rxIdGetter = "result";
} else {
if (primaryKey.isType(String.class)) {
rxIdGetter = String.format("contentValues.getAsString(\"%s\")", primaryKey.columnName);
} else {
rxIdGetter = String.format("contentValues.getAsLong(\"%s\")", primaryKey.columnName);
}
}
GenericSQLHelper.generateSubjectNext(entity, methodBuilder, SubjectType.INSERT, rxIdGetter);
}
// support for livedata
if (daoDefinition.hasLiveData()) {
methodBuilder.addComment("support for livedata");
methodBuilder.addStatement(BindDaoBuilder.METHOD_NAME_REGISTRY_EVENT + "(result>0?1:0)");
}
methodBuilder.addStatement("return result");
// javadoc
// we add at last javadoc, because need info is built at last.
SqlBuilderHelper.generateJavaDocForContentProvider(method, methodBuilder);
methodBuilder.addJavadoc("@param uri $S\n", method.contentProviderUriTemplate.replace("*", "[*]"));
methodBuilder.addJavadoc("@param contentValues content values\n");
methodBuilder.addJavadoc("@return new row's id\n");
classBuilder.addMethod(methodBuilder.build());
}
use of com.squareup.javapoet.ParameterSpec in project arez by arez.
the class ComponentGenerator method buildInverseUnsetMethod.
@Nonnull
private static MethodSpec buildInverseUnsetMethod(@Nonnull final InverseDescriptor inverse) throws ProcessorException {
final ObservableDescriptor observable = inverse.getObservable();
final String methodName = Multiplicity.ONE == inverse.getMultiplicity() ? getInverseUnsetMethodName(observable.getName()) : getInverseZUnsetMethodName(observable.getName());
final MethodSpec.Builder builder = MethodSpec.methodBuilder(methodName);
if (ElementsUtil.areTypesInDifferentPackage(inverse.getTargetType(), inverse.getComponent().getElement())) {
builder.addModifiers(Modifier.PUBLIC);
}
final String otherName = inverse.getOtherName();
final ParameterSpec parameter = ParameterSpec.builder(TypeName.get(inverse.getTargetType().asType()), otherName, Modifier.FINAL).addAnnotation(GeneratorUtil.NONNULL_CLASSNAME).build();
builder.addParameter(parameter);
generateNotDisposedInvariant(builder, methodName);
builder.addStatement("this.$N.preReportChanged()", observable.getFieldName());
final CodeBlock.Builder block = CodeBlock.builder();
block.beginControlFlow("if ( this.$N == $N )", observable.getDataFieldName(), otherName);
for (final ExecutableElement hook : inverse.getPreInverseRemoveHooks()) {
block.addStatement("$N( $N )", hook.getSimpleName(), otherName);
}
block.addStatement("this.$N = null", observable.getDataFieldName());
block.addStatement("this.$N.reportChanged()", observable.getFieldName());
block.endControlFlow();
builder.addCode(block.build());
return builder.build();
}
use of com.squareup.javapoet.ParameterSpec in project AndroidQuick by ddnosh.
the class AnnotatedClass method generateFile.
JavaFile generateFile() {
// 方法参数类型
ParameterizedTypeName parameterizedTypeName = ParameterizedTypeName.get(ClassName.get(Map.class), ClassName.get(String.class), ClassName.get(TagInfo.class));
ParameterSpec params = ParameterSpec.builder(parameterizedTypeName, "params").build();
MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("load").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).addParameter(params);
for (BindTagField item : mFields) {
String key = item.getTypeName().toString();
TagInfo.Type type = item.getType();
String[] value = item.getTag();
String description = item.getDescription();
// 添加方法内容
methodBuilder.addStatement("params.put($S,$T.build($T.$L,$S,$S,$S))", key, ClassName.get(TagInfo.class), ClassName.get(TagInfo.Type.class), type, key, Arrays.toString(value), description);
}
// 生成类
TypeSpec finderClass = TypeSpec.classBuilder("TagService").addSuperinterface(ClassName.get("com.androidwind.annotation.core", "ILoad")).addModifiers(Modifier.PUBLIC).addMethod(methodBuilder.build()).build();
return JavaFile.builder("com.androidwind.annotation", finderClass).build();
}
Aggregations