use of com.squareup.javapoet.ParameterizedTypeName in project CoreData by FangCloud-Android.
the class BindCursorMethod method bind.
private void bind(MethodSpec.Builder builder) {
// final int cursorIndexOfId = cursor.getColumnIndexOrThrow("id");
// final int cursorIndexOfName = cursor.getColumnIndexOrThrow("name");
// final int cursorIndexOfTags = cursor.getColumnIndexOrThrow("tags");
// final int cursorIndexOfAuthor = cursor.getColumnIndexOrThrow("author_id");
// final int cursorIndexOfContent = cursor.getColumnIndexOrThrow("desc_content");
// final int cursorIndexOfEmail = cursor.getColumnIndexOrThrow("desc_email");
// List<Book> bookList = new ArrayList<>();
// Map<Integer, Book> authorIdWithBookMap = new HashMap<>();
// while (cursor.moveToNext()) {
// Book book = new Book();
// book.id = cursor.getLong(cursorIndexOfId);
// book.name = cursor.getString(cursorIndexOfName);
// book.tags = __TagListConverter.convertToValue(cursor.getString(cursorIndexOfTags));
// int authorId = cursor.getInt(cursorIndexOfAuthor);
// book.desc = new Desc();
// book.desc.content = cursor.getString(cursorIndexOfContent);
// book.desc.email = cursor.getString(cursorIndexOfEmail);
// bookList.add(book);
// authorIdWithBookMap.put(authorId, book);
// }
// List<Author> authorList = __authorCoreDao.queryByKeys(authorIdWithBookMap.keySet().toArray(new Integer[]{}));
// for (Author author : authorList) {
// Book book = authorIdWithBookMap.get(author.getId());
// if (book != null) {
// book.author = author;
// }
// }
// return bookList;
TypeName typeNameEntity = ClassName.get(entityDetail.getEntityElement().asType());
ParameterizedTypeName typeListEntity = ParameterizedTypeName.get(ClassName.get(ArrayList.class), typeNameEntity);
List<Element> elementsForDb = entityDetail.getDbElements();
List<Property> properties = entityDetail.getProperties(processingEnv);
List<Element> relationElements = entityDetail.getRelationElements();
for (Property property : properties) {
builder.addStatement("int $N = cursor.getColumnIndexOrThrow($S)", "cursorIndexOf" + property.name, property.name);
}
// 创建自己的列表
builder.addStatement("$T list = new $T()", typeListEntity, typeListEntity);
// 创建多个关联数据的hashMap,主键类型为key
for (Element relationElement : relationElements) {
TypeElement typeRelation = (TypeElement) processingEnv.getTypeUtils().asElement(relationElement.asType());
EntityDetail relationEntityDetail = EntityDetail.parse(processingEnv, typeRelation);
Element primaryKeyElement = relationEntityDetail.getPrimaryKey();
if (primaryKeyElement != null) {
TypeMirror typeMirror = primaryKeyElement.asType();
ParameterizedTypeName hashMapType = ParameterizedTypeName.get(ClassName.get(HashMap.class), ClassName.get(typeMirror).box(), typeNameEntity);
builder.addStatement("$T __$NMap = new $T()", hashMapType, Utils.getColumnName(relationElement), hashMapType);
}
}
builder.addCode("while (cursor.moveToNext()) {\n ");
// 创建一个对象
String itemName = "entity";
builder.addStatement("$T $N = new $T()", typeNameEntity, itemName, typeNameEntity);
for (Element element : elementsForDb) {
bindCursorToField(builder, element, itemName);
}
builder.addStatement("list.add(entity)");
// 将对象放入相应关联对象对应的map
builder.addCode("}\n");
// 循环关联对象,赋值给主对象
for (Element relationElement : relationElements) {
ClassName classNameRelation = ClassName.bestGuess(relationElement.asType().toString());
TypeElement typeRelationElement = (TypeElement) processingEnv.getTypeUtils().asElement(relationElement.asType());
EntityDetail relationEntityDetail = EntityDetail.parse(processingEnv, typeRelationElement);
Element primaryKeyElement = relationEntityDetail.getPrimaryKey();
if (primaryKeyElement != null) {
// List<Author> authorList = __authorCoreDao.queryByKeys(authorIdWithBookMap.keySet().toArray(new Integer[]{}));
TypeName primaryTypeName = ClassName.get(primaryKeyElement.asType());
ParameterizedTypeName listRelationType = ParameterizedTypeName.get(ClassName.get(List.class), classNameRelation);
String listName = String.format("__%sList", typeRelationElement.getSimpleName());
String mapName = String.format("__%sMap", Utils.getColumnName(relationElement));
builder.addStatement("$T $N = $N.queryByKeys($N.keySet().toArray(new $T[]{}))", listRelationType, listName, Utils.relationDaoName(classNameRelation), mapName, primaryTypeName.box());
builder.addCode("for($T item : $N){\n", classNameRelation, listName);
builder.addStatement(" $T entity = $N.get($N)", typeNameEntity, mapName, Utils.methodGet(primaryKeyElement, "item"));
builder.addCode(" if(entity != null){\n");
builder.addStatement(Utils.methodSetFormat(relationElement, "entity"), "item");
builder.addCode(" }\n");
builder.addCode("}\n");
}
}
builder.addStatement("return list");
}
use of com.squareup.javapoet.ParameterizedTypeName in project CoreData by FangCloud-Android.
the class ReplaceInternalMethod method bind.
private void bind(MethodSpec.Builder builder) {
Types typeUtils = processingEnv.getTypeUtils();
TypeName typeEntity = ClassName.get(entityDetail.getEntityElement().asType());
ParameterizedTypeName typeListEntity = ParameterizedTypeName.get(ClassName.get(ArrayList.class), typeEntity);
builder.addStatement("$T $N = new $T()", typeListEntity, entityDetail.getEntityElement().getSimpleName() + "List", typeListEntity);
List<Element> relationElements = entityDetail.getRelationElements();
for (Element relationElement : relationElements) {
Element typeRelationElement = typeUtils.asElement(relationElement.asType());
TypeName typeRelation = ClassName.get(relationElement.asType());
ParameterizedTypeName typeListRelation = ParameterizedTypeName.get(ClassName.get(ArrayList.class), typeRelation);
builder.addStatement("$T $N = new $T()", typeListRelation, typeRelationElement.getSimpleName() + "List", typeListRelation);
}
builder.addCode("for ($T item : collection) {\n ", typeEntity);
builder.addStatement("$N.add($N)", entityDetail.getEntityElement().getSimpleName() + "List", "item");
for (Element relationElement : relationElements) {
Element typeRelationElement = typeUtils.asElement(relationElement.asType());
TypeName typeRelation = ClassName.get(relationElement.asType());
relationElement.getSimpleName();
String methodGet = Utils.methodGet(relationElement, "item");
builder.addCode("if($N != null){\n ", methodGet);
builder.addStatement("$N.add($N)", typeRelationElement.getSimpleName() + "List", methodGet);
builder.addCode("}\n");
}
builder.addCode("}\n");
builder.addStatement("executeInsert($N, db)", entityDetail.getEntityElement().getSimpleName() + "List");
for (Element relationElement : relationElements) {
Element typeRelationElement = typeUtils.asElement(relationElement.asType());
ClassName classNameRelation = ClassName.bestGuess(relationElement.asType().toString());
String relationDaoName = Utils.relationDaoName(classNameRelation);
builder.addStatement("$N.replace($N, db)", relationDaoName, typeRelationElement.getSimpleName() + "List");
}
}
use of com.squareup.javapoet.ParameterizedTypeName 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.ParameterizedTypeName in project web3sdk by FISCO-BCOS.
the class SolidityFunctionWrapper method buildTupleResultContainer.
private void buildTupleResultContainer(MethodSpec.Builder methodBuilder, ParameterizedTypeName tupleType, List<TypeName> outputParameterTypes) throws ClassNotFoundException {
List<TypeName> typeArguments = tupleType.typeArguments;
CodeBlock.Builder tupleConstructor = CodeBlock.builder();
tupleConstructor.addStatement("$T results = executeCallMultipleValueReturn(function)", ParameterizedTypeName.get(List.class, Type.class)).add("return new $T(", tupleType).add("$>$>");
String resultStringSimple = "\n($T) results.get($L)";
if (useNativeJavaTypes) {
resultStringSimple += ".getValue()";
}
String resultStringNativeList = "\nconvertToNative(($T) results.get($L).getValue())";
int size = typeArguments.size();
ClassName classList = ClassName.get(List.class);
for (int i = 0; i < size; i++) {
TypeName param = outputParameterTypes.get(i);
TypeName convertTo = typeArguments.get(i);
String resultString = resultStringSimple;
// elements of arrays to native java types too
if (useNativeJavaTypes && param instanceof ParameterizedTypeName) {
ParameterizedTypeName oldContainer = (ParameterizedTypeName) param;
ParameterizedTypeName newContainer = (ParameterizedTypeName) convertTo;
if (newContainer.rawType.compareTo(classList) == 0 && newContainer.typeArguments.size() == 1) {
convertTo = ParameterizedTypeName.get(classList, oldContainer.typeArguments.get(0));
resultString = resultStringNativeList;
}
}
tupleConstructor.add(resultString, convertTo, i);
tupleConstructor.add(i < size - 1 ? ", " : ");\n");
}
tupleConstructor.add("$<$<");
TypeSpec callableType = TypeSpec.anonymousClassBuilder("").addSuperinterface(ParameterizedTypeName.get(ClassName.get(Callable.class), tupleType)).addMethod(MethodSpec.methodBuilder("call").addAnnotation(Override.class).addModifiers(Modifier.PUBLIC).addException(Exception.class).returns(tupleType).addCode(tupleConstructor.build()).build()).build();
methodBuilder.addStatement("return new $T(\n$L)", buildRemoteCall(tupleType), callableType);
}
use of com.squareup.javapoet.ParameterizedTypeName in project web3sdk by FISCO-BCOS.
the class SolidityFunctionWrapper method getNativeType.
static TypeName getNativeType(ParameterizedTypeName parameterizedTypeName) {
List<TypeName> typeNames = parameterizedTypeName.typeArguments;
List<TypeName> nativeTypeNames = new ArrayList<>(typeNames.size());
for (TypeName enclosedTypeName : typeNames) {
nativeTypeNames.add(getNativeType(enclosedTypeName));
}
return ParameterizedTypeName.get(ClassName.get(List.class), nativeTypeNames.toArray(new TypeName[nativeTypeNames.size()]));
}
Aggregations