Search in sources :

Example 16 with Field

use of com.squareup.wire.schema.Field in project wire by square.

the class JavaGenerator method messageEquals.

// Example:
// 
// @Override
// public boolean equals(Object other) {
// if (other == this) return true;
// if (!(other instanceof SimpleMessage)) return false;
// SimpleMessage o = (SimpleMessage) other;
// return equals(unknownFields(), o.unknownFields())
// && equals(optional_int32, o.optional_int32);
// 
private MethodSpec messageEquals(NameAllocator nameAllocator, MessageType type) {
    NameAllocator localNameAllocator = nameAllocator.clone();
    String otherName = localNameAllocator.newName("other");
    String oName = localNameAllocator.newName("o");
    TypeName javaType = typeName(type.getType());
    MethodSpec.Builder result = MethodSpec.methodBuilder("equals").addAnnotation(Override.class).addModifiers(PUBLIC).returns(boolean.class).addParameter(Object.class, otherName);
    result.addStatement("if ($N == this) return true", otherName);
    result.addStatement("if (!($N instanceof $T)) return false", otherName, javaType);
    result.addStatement("$T $N = ($T) $N", javaType, oName, javaType, otherName);
    result.addCode("$[return unknownFields().equals($N.unknownFields())", oName);
    List<Field> fields = type.getFieldsAndOneOfFields();
    for (Field field : fields) {
        String fieldName = localNameAllocator.get(field);
        if (field.isRequired() || field.isRepeated() || field.getType().isMap()) {
            result.addCode("\n&& $1L.equals($2N.$1L)", fieldName, oName);
        } else {
            result.addCode("\n&& $1T.equals($2L, $3N.$2L)", Internal.class, fieldName, oName);
        }
    }
    result.addCode(";\n$]");
    return result.build();
}
Also used : NameAllocator(com.squareup.javapoet.NameAllocator) WireField(com.squareup.wire.WireField) Field(com.squareup.wire.schema.Field) TypeName(com.squareup.javapoet.TypeName) WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) MethodSpec(com.squareup.javapoet.MethodSpec) ByteString(okio.ByteString) JvmLanguages.builtInAdapterString(com.squareup.wire.schema.internal.JvmLanguages.builtInAdapterString)

Example 17 with Field

use of com.squareup.wire.schema.Field in project wire by square.

the class JavaGenerator method messageToString.

private MethodSpec messageToString(NameAllocator nameAllocator, MessageType type) {
    NameAllocator localNameAllocator = nameAllocator.clone();
    MethodSpec.Builder result = MethodSpec.methodBuilder("toString").addAnnotation(Override.class).addModifiers(PUBLIC).returns(String.class);
    String builderName = localNameAllocator.newName("builder");
    result.addStatement("$1T $2N = new $1T()", StringBuilder.class, builderName);
    for (Field field : type.getFieldsAndOneOfFields()) {
        String fieldName = nameAllocator.get(field);
        TypeName fieldType = fieldType(field);
        if (field.isRepeated() || field.getType().isMap()) {
            result.addCode("if (!$N.isEmpty()) ", fieldName);
        } else if (!field.isRequired() && !fieldType.isPrimitive()) {
            result.addCode("if ($N != null) ", fieldName);
        }
        if (field.isRedacted()) {
            result.addStatement("$N.append(\", $N=$L\")", builderName, field.getName(), DOUBLE_FULL_BLOCK);
        } else if (field.getType().equals(ProtoType.STRING)) {
            result.addStatement("$N.append(\", $N=\").append($T.sanitize($L))", builderName, field.getName(), Internal.class, fieldName);
        } else {
            result.addStatement("$N.append(\", $N=\").append($L)", builderName, field.getName(), fieldName);
        }
    }
    result.addStatement("return builder.replace(0, 2, \"$L{\").append('}').toString()", type.getType().getSimpleName());
    return result.build();
}
Also used : NameAllocator(com.squareup.javapoet.NameAllocator) WireField(com.squareup.wire.WireField) Field(com.squareup.wire.schema.Field) TypeName(com.squareup.javapoet.TypeName) WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) MethodSpec(com.squareup.javapoet.MethodSpec) Internal(com.squareup.wire.internal.Internal) ByteString(okio.ByteString) JvmLanguages.builtInAdapterString(com.squareup.wire.schema.internal.JvmLanguages.builtInAdapterString)

Example 18 with Field

use of com.squareup.wire.schema.Field in project wire by square.

the class JavaGenerator method messageFieldsConstructor.

// Example:
// 
// public SimpleMessage(int optional_int32, long optional_int64) {
// this(builder.optional_int32, builder.optional_int64, null);
// }
// 
private MethodSpec messageFieldsConstructor(NameAllocator nameAllocator, MessageType type) {
    MethodSpec.Builder result = MethodSpec.constructorBuilder();
    result.addModifiers(PUBLIC);
    result.addCode("this(");
    for (Field field : type.getFieldsAndOneOfFields()) {
        TypeName javaType = fieldType(field);
        String fieldName = nameAllocator.get(field);
        ParameterSpec.Builder param = ParameterSpec.builder(javaType, fieldName);
        if (emitAndroidAnnotations && field.getEncodeMode() == Field.EncodeMode.NULL_IF_ABSENT) {
            param.addAnnotation(NULLABLE);
        }
        result.addParameter(param.build());
        result.addCode("$L, ", fieldName);
    }
    result.addCode("$T.EMPTY);\n", BYTE_STRING);
    return result.build();
}
Also used : WireField(com.squareup.wire.WireField) Field(com.squareup.wire.schema.Field) TypeName(com.squareup.javapoet.TypeName) WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) MethodSpec(com.squareup.javapoet.MethodSpec) ParameterSpec(com.squareup.javapoet.ParameterSpec) ByteString(okio.ByteString) JvmLanguages.builtInAdapterString(com.squareup.wire.schema.internal.JvmLanguages.builtInAdapterString)

Example 19 with Field

use of com.squareup.wire.schema.Field 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 20 with Field

use of com.squareup.wire.schema.Field 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)

Aggregations

WireField (com.squareup.wire.WireField)23 Field (com.squareup.wire.schema.Field)23 ByteString (okio.ByteString)19 JvmLanguages.builtInAdapterString (com.squareup.wire.schema.internal.JvmLanguages.builtInAdapterString)17 MethodSpec (com.squareup.javapoet.MethodSpec)15 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)12 TypeName (com.squareup.javapoet.TypeName)12 CodeBlock (com.squareup.javapoet.CodeBlock)10 WildcardTypeName (com.squareup.javapoet.WildcardTypeName)10 NameAllocator (com.squareup.javapoet.NameAllocator)7 ClassName (com.squareup.javapoet.ClassName)4 Internal (com.squareup.wire.internal.Internal)4 ParameterSpec (com.squareup.javapoet.ParameterSpec)3 TypeSpec (com.squareup.javapoet.TypeSpec)3 OneOf (com.squareup.wire.schema.OneOf)3 ProtoMember (com.squareup.wire.schema.ProtoMember)3 ArrayList (java.util.ArrayList)3 LinkedHashMap (java.util.LinkedHashMap)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 ProtoFile (com.squareup.wire.schema.ProtoFile)2