use of com.squareup.javapoet.MethodSpec in project CoreData by FangCloud-Android.
the class EntityProcessor method createEntityDao.
public void createEntityDao(TypeElement element) throws IOException {
EntityDetail entityDetail = EntityDetail.parse(processingEnv, element);
// 实体类的class
ClassName classEntity = ClassName.bestGuess(element.asType().toString());
if (entityDetail.getPrimaryKey() == null) {
throw new RuntimeException(classEntity.reflectionName() + " 没有主键");
}
List<Property> propertyList = entityDetail.getProperties(processingEnv);
// 1、找出tableName,PrimaryKeyName ok
// 2、找出所有的PropertyConverter,并生成局部变量,类似 __TagListConverter ok
// 3、找出所有关联对象 @Relation,并生成对应的dao 类似 __AuthorCoreDao ok
// 4、onCreate方法,初始化 关联对象对应的 dao ok
// 5、getInsertSql, 返回插入的sql语句 ok
// 6、getCreateTableSql,返回建表语句
// 7、getTableProperties,返回所有的表结构
// 8、绑定数据
// dao的java名字
String daoName = String.format("%sCoreDaoImpl", entityDetail.getEntityName());
TypeSpec.Builder daoTypeBuilder = TypeSpec.classBuilder(daoName).addModifiers(Modifier.PUBLIC, Modifier.FINAL).superclass(ParameterizedTypeName.get(classCoreDao, classEntity));
List<Element> convertElements = entityDetail.getConvertElements(processingEnv);
// static 代码块
CodeBlock convertStaticBlock = CreateConvertStatement.buildConvertStatic(convertElements);
if (convertStaticBlock != null) {
daoTypeBuilder.addStaticBlock(convertStaticBlock);
}
// 创建convert
List<FieldSpec> convertFieldSpecs = CreateConvertStatement.bindComvertFields(convertElements);
if (convertFieldSpecs != null) {
for (FieldSpec fieldSpec : convertFieldSpecs) {
daoTypeBuilder.addField(fieldSpec);
}
}
// onCreate方法
// 创建关联的dao
List<Element> relationElements = entityDetail.getRelationElements();
MethodSpec.Builder onCreateMethodBuilder = MethodSpec.methodBuilder("onCreate").addModifiers(Modifier.PROTECTED).returns(void.class).addParameter(classCoreData, "coreData").addStatement("super.onCreate($N)", "coreData");
for (Element relationElement : relationElements) {
TypeMirror typeMirror = relationElement.asType();
ClassName classRelation = ClassName.bestGuess(typeMirror.toString());
String daoFieldName = Utils.relationDaoName(classRelation);
FieldSpec fieldSpec = FieldSpec.builder(ParameterizedTypeName.get(classCoreDao, classRelation), daoFieldName, Modifier.PRIVATE).build();
daoTypeBuilder.addField(fieldSpec);
onCreateMethodBuilder.addStatement("$N = coreData.dao($T.class)", daoFieldName, classRelation);
}
MethodSpec onCreateMethod = onCreateMethodBuilder.build();
// getCreateTableSql 方法,用来获取建表语句
MethodSpec getCreateTableSqlMethod = MethodSpec.methodBuilder("getCreateTableSql").addModifiers(Modifier.PROTECTED).returns(String.class).addStatement("return $S", SqlBuilder.buildCreateSql(entityDetail.getTableName(), propertyList, true)).build();
// getInsertSql 方法,用来获取插入语句
MethodSpec getInsertSqlMethod = MethodSpec.methodBuilder("getInsertSql").addModifiers(Modifier.PROTECTED).returns(String.class).addStatement("return $S", Utils.getInsertSql(entityDetail.getTableName(), propertyList)).build();
// bindStatement 用来绑定数据
MethodSpec bindStatementMethod = new BindStatementMethod(processingEnv, entityDetail).build();
// replaceInternal 方法,用来处理关系型数据
MethodSpec replaceInternalMethod = new ReplaceInternalMethod(processingEnv, entityDetail).build();
// 创建 getTableName 方法,返回tableName
MethodSpec getTableNameMethod = MethodSpec.methodBuilder("getTableName").addModifiers(Modifier.PUBLIC).returns(String.class).addStatement("return $S", entityDetail.getTableName()).build();
// 创建 getPrimaryKeyName 方法,返回 主键的名字
MethodSpec getPrimaryKeyNameMethod = MethodSpec.methodBuilder("getPrimaryKeyName").addModifiers(Modifier.PUBLIC).returns(String.class).addStatement("return $S", Utils.getColumnName(entityDetail.getPrimaryKey())).build();
// 创建 bindCursor 方法,绑定游标数据到模型
MethodSpec bindCursorMethod = new BindCursorMethod(processingEnv, entityDetail).build();
// 创建 getTableProperties 方法,返回所有字段相关的 Property
ParameterizedTypeName listPropertyType = ParameterizedTypeName.get(ClassName.get(ArrayList.class), classCoreProperty);
MethodSpec.Builder getTablePropertiesBuilder = MethodSpec.methodBuilder("getTableProperties").addModifiers(Modifier.PUBLIC).returns(ParameterizedTypeName.get(ClassName.get(List.class), classCoreProperty));
getTablePropertiesBuilder.addStatement("$T list = new $T()", listPropertyType, listPropertyType);
for (Property property : propertyList) {
getTablePropertiesBuilder.addStatement("list.add(new $T($S, $T.class, $N))", classCoreProperty, property.name, Utils.getTypeNameByType(property.type), String.valueOf(property.primaryKey));
}
getTablePropertiesBuilder.addStatement("return list");
daoTypeBuilder.addMethod(onCreateMethod).addMethod(getTableNameMethod).addMethod(getPrimaryKeyNameMethod).addMethod(getTablePropertiesBuilder.build()).addMethod(getCreateTableSqlMethod).addMethod(getInsertSqlMethod).addMethod(bindStatementMethod).addMethod(replaceInternalMethod).addMethod(bindCursorMethod);
JavaFile javaFile = JavaFile.builder(entityDetail.getEntityPackageName(processingEnv), daoTypeBuilder.build()).build();
javaFile.writeTo(processingEnv.getFiler());
System.out.println(element.getSimpleName());
System.out.println(processingEnv.getElementUtils().getPackageOf(element).getQualifiedName());
}
use of com.squareup.javapoet.MethodSpec in project web3sdk by FISCO-BCOS.
the class SolidityFunctionWrapper method buildEventTransactionReceiptFunction.
MethodSpec buildEventTransactionReceiptFunction(String responseClassName, String functionName, List<NamedTypeName> indexedParameters, List<NamedTypeName> nonIndexedParameters) {
ParameterizedTypeName parameterizedTypeName = ParameterizedTypeName.get(ClassName.get(List.class), ClassName.get("", responseClassName));
String generatedFunctionName = "get" + Strings.capitaliseFirstLetter(functionName) + "Events";
MethodSpec.Builder transactionMethodBuilder = MethodSpec.methodBuilder(generatedFunctionName).addModifiers(Modifier.PUBLIC).addParameter(TransactionReceipt.class, "transactionReceipt").returns(parameterizedTypeName);
transactionMethodBuilder.addStatement("$T valueList = extractEventParametersWithLog(" + buildEventDefinitionName(functionName) + ", " + "transactionReceipt)", ParameterizedTypeName.get(List.class, Contract.EventValuesWithLog.class)).addStatement("$1T responses = new $1T(valueList.size())", ParameterizedTypeName.get(ClassName.get(ArrayList.class), ClassName.get("", responseClassName))).beginControlFlow("for ($T eventValues : valueList)", Contract.EventValuesWithLog.class).addStatement("$1T typedResponse = new $1T()", ClassName.get("", responseClassName)).addCode(buildTypedResponse("typedResponse", indexedParameters, nonIndexedParameters, false)).addStatement("responses.add(typedResponse)").endControlFlow();
transactionMethodBuilder.addStatement("return responses");
return transactionMethodBuilder.build();
}
use of com.squareup.javapoet.MethodSpec in project web3sdk by FISCO-BCOS.
the class SolidityFunctionWrapper method buildDeploy.
private MethodSpec buildDeploy(String className, AbiDefinition functionDefinition, Class authType, String authName, boolean withGasProvider) {
MethodSpec.Builder methodBuilder = getDeployMethodSpec(className, authType, authName, withGasProvider);
String inputParams = addParameters(methodBuilder, functionDefinition.getInputs());
if (!inputParams.isEmpty()) {
return buildDeployWithParams(methodBuilder, className, inputParams, authName, withGasProvider);
} else {
return buildDeployNoParams(methodBuilder, className, authName, withGasProvider);
}
}
use of com.squareup.javapoet.MethodSpec in project web3sdk by FISCO-BCOS.
the class SolidityFunctionWrapper method buildFunction.
private MethodSpec buildFunction(AbiDefinition functionDefinition) throws ClassNotFoundException {
String functionName = functionDefinition.getName();
if (!SourceVersion.isName(functionName)) {
functionName = "_" + functionName;
}
MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(functionName).addModifiers(Modifier.PUBLIC);
String inputParams = addParameters(methodBuilder, functionDefinition.getInputs());
List<TypeName> outputParameterTypes = buildTypeNames(functionDefinition.getOutputs());
if (functionDefinition.isConstant()) {
buildConstantFunction(functionDefinition, methodBuilder, outputParameterTypes, inputParams);
} else {
buildTransactionFunction(functionDefinition, methodBuilder, inputParams);
}
return methodBuilder.build();
}
use of com.squareup.javapoet.MethodSpec in project web3sdk by FISCO-BCOS.
the class SolidityFunctionWrapper method buildEventFunctions.
List<MethodSpec> buildEventFunctions(AbiDefinition functionDefinition, TypeSpec.Builder classBuilder) throws ClassNotFoundException {
String functionName = functionDefinition.getName();
List<AbiDefinition.NamedType> inputs = functionDefinition.getInputs();
String responseClassName = Strings.capitaliseFirstLetter(functionName) + "EventResponse";
List<NamedTypeName> parameters = new ArrayList<>();
List<NamedTypeName> indexedParameters = new ArrayList<>();
List<NamedTypeName> nonIndexedParameters = new ArrayList<>();
for (AbiDefinition.NamedType namedType : inputs) {
NamedTypeName parameter = new NamedTypeName(namedType.getName(), buildTypeName(namedType.getType()), namedType.isIndexed());
if (namedType.isIndexed()) {
indexedParameters.add(parameter);
} else {
nonIndexedParameters.add(parameter);
}
parameters.add(parameter);
}
classBuilder.addField(createEventDefinition(functionName, parameters));
classBuilder.addType(buildEventResponseObject(responseClassName, indexedParameters, nonIndexedParameters));
List<MethodSpec> methods = new ArrayList<>();
methods.add(buildEventTransactionReceiptFunction(responseClassName, functionName, indexedParameters, nonIndexedParameters));
methods.add(buildRegisterEventLogPushFunction(functionName));
methods.add(buildDefaultRegisterEventLogPushFunction(functionName));
return methods;
}
Aggregations