Search in sources :

Example 36 with CodeBlock

use of com.squareup.javapoet.CodeBlock in project wire by square.

the class JavaGenerator method identityValue.

private CodeBlock identityValue(Field field) {
    switch(field.getEncodeMode()) {
        case MAP:
            return CodeBlock.of("$T.emptyMap()", Collections.class);
        case REPEATED:
        case PACKED:
            return CodeBlock.of("$T.emptyList()", Collections.class);
        case NULL_IF_ABSENT:
            return CodeBlock.of("null");
        case OMIT_IDENTITY:
            ProtoType protoType = field.getType();
            Type type = schema.getType(protoType);
            if (protoType.equals(ProtoType.STRUCT_NULL)) {
                return CodeBlock.of("null");
            } else if (field.isOneOf()) {
                return CodeBlock.of("null");
            } else if (protoType.isScalar()) {
                CodeBlock value = PROTOTYPE_TO_IDENTITY_VALUES.get(protoType);
                if (value == null) {
                    throw new IllegalArgumentException("Unexpected scalar proto type: " + protoType);
                }
                return value;
            } else if (type instanceof MessageType) {
                return CodeBlock.of("null");
            } else if (type instanceof EnumType) {
                return identity((EnumType) type);
            }
        case REQUIRED:
        default:
            throw new IllegalArgumentException("No identity value for field: " + field + "(" + field.getEncodeMode() + ")");
    }
}
Also used : ProtoType(com.squareup.wire.schema.ProtoType) JvmLanguages.annotationTargetType(com.squareup.wire.schema.internal.JvmLanguages.annotationTargetType) Type(com.squareup.wire.schema.Type) ProtoType(com.squareup.wire.schema.ProtoType) MessageType(com.squareup.wire.schema.MessageType) ElementType(java.lang.annotation.ElementType) EnclosingType(com.squareup.wire.schema.EnclosingType) EnumType(com.squareup.wire.schema.EnumType) EnumType(com.squareup.wire.schema.EnumType) CodeBlock(com.squareup.javapoet.CodeBlock) MessageType(com.squareup.wire.schema.MessageType)

Example 37 with CodeBlock

use of com.squareup.javapoet.CodeBlock in project wire by square.

the class JavaGenerator method messageAdapterEncodedSize.

private MethodSpec messageAdapterEncodedSize(NameAllocator nameAllocator, MessageType type, TypeName javaType, boolean useBuilder) {
    MethodSpec.Builder result = MethodSpec.methodBuilder("encodedSize").addAnnotation(Override.class).addModifiers(PUBLIC).returns(int.class).addParameter(javaType, "value");
    String resultName = nameAllocator.clone().newName("result");
    result.addStatement("int $L = 0", resultName);
    for (Field field : type.getFieldsAndOneOfFields()) {
        int fieldTag = field.getTag();
        String fieldName = nameAllocator.get(field);
        CodeBlock adapter = adapterFor(field, nameAllocator);
        boolean omitIdentity = field.getEncodeMode().equals(Field.EncodeMode.OMIT_IDENTITY);
        if (omitIdentity) {
            result.beginControlFlow("if (!$T.equals(value.$L, $L))", ClassName.get(Objects.class), fieldName, identityValue(field));
        }
        result.addCode("$L += ", resultName).addCode("$L.encodedSizeWithTag($L, ", adapter, fieldTag).addCode((useBuilder ? "value.$L" : "$L(value)"), fieldName).addCode(");\n");
        if (omitIdentity) {
            result.endControlFlow();
        }
    }
    if (useBuilder) {
        result.addStatement("$L += value.unknownFields().size()", resultName);
    }
    result.addStatement("return $L", resultName);
    return result.build();
}
Also used : WireField(com.squareup.wire.WireField) Field(com.squareup.wire.schema.Field) MethodSpec(com.squareup.javapoet.MethodSpec) CodeBlock(com.squareup.javapoet.CodeBlock) Objects(java.util.Objects) ByteString(okio.ByteString) JvmLanguages.builtInAdapterString(com.squareup.wire.schema.internal.JvmLanguages.builtInAdapterString)

Example 38 with CodeBlock

use of com.squareup.javapoet.CodeBlock in project wire by square.

the class JavaGenerator method decodeAndAssign.

private CodeBlock decodeAndAssign(Field field, NameAllocator nameAllocator, boolean useBuilder) {
    String fieldName = nameAllocator.get(field);
    CodeBlock decode = CodeBlock.of("$L.decode(reader)", singleAdapterFor(field, nameAllocator));
    if (field.isRepeated()) {
        return useBuilder ? field.getType().equals(ProtoType.STRUCT_NULL) ? CodeBlock.of("builder.$L.add(($T) $L)", fieldName, Void.class, decode) : CodeBlock.of("builder.$L.add($L)", fieldName, decode) : CodeBlock.of("$L.add($L)", fieldName, decode);
    } else if (field.getType().isMap()) {
        return useBuilder ? CodeBlock.of("builder.$L.putAll($L)", fieldName, decode) : CodeBlock.of("$L.putAll($L)", fieldName, decode);
    } else {
        return useBuilder ? field.getType().equals(ProtoType.STRUCT_NULL) ? CodeBlock.of("builder.$L(($T) $L)", fieldName, Void.class, decode) : CodeBlock.of("builder.$L($L)", fieldName, decode) : CodeBlock.of("$L = $L", fieldName, decode);
    }
}
Also used : CodeBlock(com.squareup.javapoet.CodeBlock) ByteString(okio.ByteString) JvmLanguages.builtInAdapterString(com.squareup.wire.schema.internal.JvmLanguages.builtInAdapterString)

Example 39 with CodeBlock

use of com.squareup.javapoet.CodeBlock in project wire by square.

the class JavaGenerator method builderNoArgsConstructor.

// Example:
// 
// public Builder() {
// names = newMutableList();
// }
// 
private MethodSpec builderNoArgsConstructor(NameAllocator nameAllocator, MessageType type) {
    MethodSpec.Builder result = MethodSpec.constructorBuilder().addModifiers(PUBLIC);
    for (Field field : type.getFieldsAndOneOfFields()) {
        String fieldName = nameAllocator.get(field);
        CodeBlock initialValue = initialValue(field);
        if (initialValue != null) {
            result.addStatement("$L = $L", fieldName, initialValue);
        }
    }
    return result.build();
}
Also used : WireField(com.squareup.wire.WireField) Field(com.squareup.wire.schema.Field) MethodSpec(com.squareup.javapoet.MethodSpec) CodeBlock(com.squareup.javapoet.CodeBlock) ByteString(okio.ByteString) JvmLanguages.builtInAdapterString(com.squareup.wire.schema.internal.JvmLanguages.builtInAdapterString)

Example 40 with CodeBlock

use of com.squareup.javapoet.CodeBlock in project wire by square.

the class JavaGenerator method messageAdapterEncode.

private MethodSpec messageAdapterEncode(NameAllocator nameAllocator, MessageType type, TypeName javaType, boolean useBuilder, boolean reverse) {
    MethodSpec.Builder result = MethodSpec.methodBuilder("encode").addAnnotation(Override.class).addModifiers(PUBLIC).addParameter(reverse ? ReverseProtoWriter.class : ProtoWriter.class, "writer").addParameter(javaType, "value").addException(IOException.class);
    List<CodeBlock> encodeCalls = new ArrayList<>();
    for (Field field : type.getFieldsAndOneOfFields()) {
        int fieldTag = field.getTag();
        CodeBlock adapter = adapterFor(field, nameAllocator);
        String fieldName = nameAllocator.get(field);
        CodeBlock.Builder encodeCall = CodeBlock.builder();
        if (field.getEncodeMode().equals(Field.EncodeMode.OMIT_IDENTITY)) {
            encodeCall.add("if (!$T.equals(value.$L, $L)) ", ClassName.get(Objects.class), fieldName, identityValue(field));
        }
        encodeCall.add("$L.encodeWithTag(writer, $L, ", adapter, fieldTag).add((useBuilder ? "value.$L" : "$L(value)"), fieldName).add(");\n");
        encodeCalls.add(encodeCall.build());
    }
    if (useBuilder) {
        encodeCalls.add(CodeBlock.builder().addStatement("writer.writeBytes(value.unknownFields())").build());
    }
    if (reverse) {
        Collections.reverse(encodeCalls);
    }
    for (CodeBlock encodeCall : encodeCalls) {
        result.addCode(encodeCall);
    }
    return result.build();
}
Also used : WireField(com.squareup.wire.WireField) Field(com.squareup.wire.schema.Field) MethodSpec(com.squareup.javapoet.MethodSpec) CodeBlock(com.squareup.javapoet.CodeBlock) ArrayList(java.util.ArrayList) Objects(java.util.Objects) ProtoWriter(com.squareup.wire.ProtoWriter) ReverseProtoWriter(com.squareup.wire.ReverseProtoWriter) ByteString(okio.ByteString) JvmLanguages.builtInAdapterString(com.squareup.wire.schema.internal.JvmLanguages.builtInAdapterString) ReverseProtoWriter(com.squareup.wire.ReverseProtoWriter)

Aggregations

CodeBlock (com.squareup.javapoet.CodeBlock)42 MethodSpec (com.squareup.javapoet.MethodSpec)19 TypeMirror (javax.lang.model.type.TypeMirror)13 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)11 TypeName (com.squareup.javapoet.TypeName)10 CompilationAbstractTest (com.google.auto.value.extension.serializable.serializer.utils.CompilationAbstractTest)8 ClassName (com.squareup.javapoet.ClassName)8 WireField (com.squareup.wire.WireField)8 Field (com.squareup.wire.schema.Field)8 ByteString (okio.ByteString)8 Test (org.junit.Test)8 JvmLanguages.builtInAdapterString (com.squareup.wire.schema.internal.JvmLanguages.builtInAdapterString)7 ArrayList (java.util.ArrayList)7 Serializer (com.google.auto.value.extension.serializable.serializer.interfaces.Serializer)6 ParameterSpec (com.squareup.javapoet.ParameterSpec)6 AnnotationSpec (com.squareup.javapoet.AnnotationSpec)4 TypeSpec (com.squareup.javapoet.TypeSpec)3 Element (javax.lang.model.element.Element)3 Predicate (com.google.common.base.Predicate)2 FieldSpec (com.squareup.javapoet.FieldSpec)2