Search in sources :

Example 66 with ParameterizedTypeName

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");
}
Also used : ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) HashMap(java.util.HashMap) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) ArrayList(java.util.ArrayList) TypeMirror(javax.lang.model.type.TypeMirror) ClassName(com.squareup.javapoet.ClassName) ArrayList(java.util.ArrayList) List(java.util.List) EntityDetail(com.coredata.compiler.EntityDetail) Property(com.coredata.db.Property) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 67 with ParameterizedTypeName

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");
    }
}
Also used : Types(javax.lang.model.util.Types) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) ArrayList(java.util.ArrayList) ClassName(com.squareup.javapoet.ClassName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 68 with ParameterizedTypeName

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();
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) TransactionReceipt(org.fisco.bcos.web3j.protocol.core.methods.response.TransactionReceipt) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Utf8String(org.fisco.bcos.web3j.abi.datatypes.Utf8String) Contract(org.fisco.bcos.web3j.tx.Contract) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 69 with ParameterizedTypeName

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);
}
Also used : TypeName(com.squareup.javapoet.TypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) CodeBlock(com.squareup.javapoet.CodeBlock) Utf8String(org.fisco.bcos.web3j.abi.datatypes.Utf8String) IOException(java.io.IOException) Type(org.fisco.bcos.web3j.abi.datatypes.Type) NamedType(org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition.NamedType) EncryptType(org.fisco.bcos.web3j.crypto.EncryptType) ClassName(com.squareup.javapoet.ClassName) List(java.util.List) ArrayList(java.util.ArrayList) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 70 with ParameterizedTypeName

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()]));
}
Also used : TypeName(com.squareup.javapoet.TypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)135 TypeName (com.squareup.javapoet.TypeName)90 ClassName (com.squareup.javapoet.ClassName)53 MethodSpec (com.squareup.javapoet.MethodSpec)47 TypeSpec (com.squareup.javapoet.TypeSpec)29 WildcardTypeName (com.squareup.javapoet.WildcardTypeName)29 TypeElement (javax.lang.model.element.TypeElement)23 ArrayList (java.util.ArrayList)19 TypeMirror (javax.lang.model.type.TypeMirror)19 ArrayTypeName (com.squareup.javapoet.ArrayTypeName)15 HashSet (java.util.HashSet)14 Map (java.util.Map)13 List (java.util.List)12 Set (java.util.Set)12 FieldSpec (com.squareup.javapoet.FieldSpec)11 ParameterSpec (com.squareup.javapoet.ParameterSpec)11 TypeVariableName (com.squareup.javapoet.TypeVariableName)10 Element (javax.lang.model.element.Element)10 PackageElement (javax.lang.model.element.PackageElement)10 Builder (com.squareup.javapoet.TypeSpec.Builder)9