Search in sources :

Example 91 with ParameterizedTypeName

use of com.squareup.javapoet.ParameterizedTypeName in project kripton by xcesco.

the class SetPrefsTransformation method generateReadProperty.

@Override
public void generateReadProperty(Builder methodBuilder, String preferenceName, TypeName beanClass, String beanName, PrefsProperty property, boolean readAll) {
    boolean isStringSet = isStringSet(property);
    String tempPre = "";
    String tempPost = "";
    String tempPreDefaultValue = "";
    String tempPostDefaultValue = "";
    if (readAll) {
        methodBuilder.beginControlFlow("");
    }
    if (property.hasTypeAdapter()) {
        // this comment is needed to include $T for PrefsTypeAdapterUtils
        methodBuilder.addComment("Use $T to convert objects", PrefsTypeAdapterUtils.class);
        tempPre = String.format("%s.getAdapter(%s.class).toJava(", PrefsTypeAdapterUtils.class.getSimpleName(), TypeUtility.className(property.typeAdapter.adapterClazz).simpleName());
        tempPost = ")";
        tempPreDefaultValue = String.format("%s.getAdapter(%s.class).toData(", PrefsTypeAdapterUtils.class.getSimpleName(), TypeUtility.className(property.typeAdapter.adapterClazz).simpleName());
        tempPostDefaultValue = ")";
    }
    if (isStringSet) {
        methodBuilder.addStatement("$T<String> temp=$L.getStringSet($S, " + tempPreDefaultValue + "defaultBean." + getter(beanClass, property) + tempPostDefaultValue + ")", Set.class, preferenceName, property.getPreferenceKey());
        if (readAll) {
            methodBuilder.addCode("$L." + setter(beanClass, property) + (!property.isPublicField() ? "(" : "=") + "", beanName);
        } else {
            methodBuilder.addCode("return ");
        }
        methodBuilder.addCode(tempPre);
        ParameterizedTypeName typeName;
        if (property.hasTypeAdapter()) {
            typeName = (ParameterizedTypeName) TypeUtility.typeName(property.typeAdapter.dataType);
        } else {
            typeName = (ParameterizedTypeName) TypeUtility.typeName(property.getElement().asType());
        }
        if (TypeUtility.isEquals(typeName.rawType, Set.class.getCanonicalName())) {
            methodBuilder.addCode("temp");
        } else {
            methodBuilder.addCode("new $T<String>(temp)", typeName.rawType);
        }
        methodBuilder.addCode(tempPost);
        if (readAll) {
            methodBuilder.addCode((!property.isPublicField() ? ")" : ""));
        }
        methodBuilder.addCode(";\n");
    } else {
        methodBuilder.addStatement("String temp=$L.getString($S, null)", preferenceName, property.getPreferenceKey());
        if (readAll) {
            methodBuilder.addCode("$L." + setter(beanClass, property) + (!property.isPublicField() ? "(" : "=") + "", beanName);
        } else {
            methodBuilder.addCode("return ");
        }
        methodBuilder.addCode("$T.hasText(temp) ? ", StringUtils.class);
        methodBuilder.addCode("parse$L(temp)", formatter.convert(property.getName()));
        methodBuilder.addCode(": null");
        if (readAll) {
            methodBuilder.addCode((!property.isPublicField() ? ")" : ""));
        }
        methodBuilder.addCode(";\n");
    }
    if (readAll) {
        methodBuilder.endControlFlow();
    }
}
Also used : Set(java.util.Set) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 92 with ParameterizedTypeName

use of com.squareup.javapoet.ParameterizedTypeName in project AndroidLife by CaMnter.

the class FieldCollectionViewBinding method render.

/**
 * 为 @BindViews 生成代码块
 *
 * @return CodeBlock
 */
CodeBlock render() {
    CodeBlock.Builder builder = CodeBlock.builder().add("target.$L = $T.$L(", name, UTILS, kind.factoryName);
    for (int i = 0; i < ids.size(); i++) {
        if (i > 0) {
            builder.add(", ");
        }
        builder.add("\n");
        boolean requiresCast = requiresCast(type);
        if (!requiresCast && !required) {
            builder.add("source.findViewById($L)", ids.get(i).code);
        } else {
            builder.add("$T.find", UTILS);
            builder.add(required ? "RequiredView" : "OptionalView");
            if (requiresCast) {
                builder.add("AsType");
            }
            builder.add("(source, $L, \"field '$L'\"", ids.get(i).code, name);
            if (requiresCast) {
                TypeName rawType = type;
                if (rawType instanceof ParameterizedTypeName) {
                    rawType = ((ParameterizedTypeName) rawType).rawType;
                }
                builder.add(", $T.class", rawType);
            }
            builder.add(")");
        }
    }
    return builder.add(")").build();
}
Also used : TypeName(com.squareup.javapoet.TypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) CodeBlock(com.squareup.javapoet.CodeBlock) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 93 with ParameterizedTypeName

use of com.squareup.javapoet.ParameterizedTypeName in project Rocket by mozilla-tw.

the class RequestBuilderGenerator method generateConstructors.

private List<MethodSpec> generateConstructors() {
    ParameterizedTypeName classOfTranscodeType = ParameterizedTypeName.get(ClassName.get(Class.class), transcodeTypeName);
    TypeName wildcardOfObject = WildcardTypeName.subtypeOf(Object.class);
    ParameterizedTypeName requestBuilderOfWildcardOfObject = ParameterizedTypeName.get(ClassName.get(requestBuilderType), wildcardOfObject);
    MethodSpec firstConstructor = MethodSpec.constructorBuilder().addParameter(classOfTranscodeType, "transcodeClass").addParameter(requestBuilderOfWildcardOfObject, "other").addStatement("super($N, $N)", "transcodeClass", "other").build();
    ClassName glide = ClassName.get("com.bumptech.glide", "Glide");
    ClassName requestManager = ClassName.get("com.bumptech.glide", "RequestManager");
    MethodSpec secondConstructor = MethodSpec.constructorBuilder().addParameter(glide, "glide").addParameter(requestManager, "requestManager").addParameter(classOfTranscodeType, "transcodeClass").addStatement("super($N, $N ,$N)", "glide", "requestManager", "transcodeClass").build();
    return ImmutableList.of(firstConstructor, secondConstructor);
}
Also used : WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) MethodSpec(com.squareup.javapoet.MethodSpec) ClassName(com.squareup.javapoet.ClassName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 94 with ParameterizedTypeName

use of com.squareup.javapoet.ParameterizedTypeName in project Rocket by mozilla-tw.

the class RequestManagerGenerator method generateAsMethod.

private MethodSpec generateAsMethod(String generatedCodePackageName, TypeSpec requestBuilder) {
    TypeVariableName resourceType = TypeVariableName.get("ResourceType");
    ParameterizedTypeName classOfResouceType = ParameterizedTypeName.get(ClassName.get(Class.class), resourceType);
    ClassName generatedRequestBuilderClassName = ClassName.get(generatedCodePackageName, requestBuilder.name);
    ParameterizedTypeName requestBuilderOfResourceType = ParameterizedTypeName.get(generatedRequestBuilderClassName, resourceType);
    return MethodSpec.methodBuilder("as").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).addTypeVariable(TypeVariableName.get("ResourceType")).addParameter(classOfResouceType, "resourceClass").returns(requestBuilderOfResourceType).addStatement("return new $T<>(glide, this, resourceClass)", this.generatedRequestBuilderClassName).build();
}
Also used : ClassName(com.squareup.javapoet.ClassName) TypeVariableName(com.squareup.javapoet.TypeVariableName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 95 with ParameterizedTypeName

use of com.squareup.javapoet.ParameterizedTypeName in project web3sdk by FISCO-BCOS.

the class SolidityFunctionWrapper method buildEventTransactionReceiptFunction.

static MethodSpec buildEventTransactionReceiptFunction(String responseClassName, String functionName, List<NamedTypeName> indexedParameters, List<NamedTypeName> nonIndexedParameters) throws ClassNotFoundException {
    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).addModifiers(Modifier.STATIC).addParameter(TransactionReceipt.class, "transactionReceipt").returns(parameterizedTypeName);
    buildVariableLengthEventConstructor(transactionMethodBuilder, functionName, indexedParameters, nonIndexedParameters);
    transactionMethodBuilder.addStatement("$T valueList = extractEventParameters(event, " + "transactionReceipt)", ParameterizedTypeName.get(List.class, EventValues.class)).addStatement("$1T responses = new $1T(valueList.size())", ParameterizedTypeName.get(ClassName.get(ArrayList.class), ClassName.get("", responseClassName))).beginControlFlow("for ($T eventValues : valueList)", EventValues.class).addStatement("$1T typedResponse = new $1T()", ClassName.get("", responseClassName)).addCode(buildTypedResponse("typedResponse", indexedParameters, nonIndexedParameters)).addStatement("responses.add(typedResponse)").endControlFlow();
    transactionMethodBuilder.addStatement("return responses");
    return transactionMethodBuilder.build();
}
Also used : EventValues(org.bcos.web3j.abi.EventValues) MethodSpec(com.squareup.javapoet.MethodSpec) TransactionReceipt(org.bcos.web3j.protocol.core.methods.response.TransactionReceipt) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

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