Search in sources :

Example 1 with OneOf

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

the class JavaGenerator method messageFieldsAndUnknownFieldsConstructor.

// Example:
//
// public SimpleMessage(int optional_int32, long optional_int64, ByteString unknownFields) {
//   super(ADAPTER, unknownFields);
//   this.optional_int32 = optional_int32;
//   this.optional_int64 = optional_int64;
// }
//
private MethodSpec messageFieldsAndUnknownFieldsConstructor(NameAllocator nameAllocator, MessageType type) {
    NameAllocator localNameAllocator = nameAllocator.clone();
    String adapterName = localNameAllocator.get("ADAPTER");
    String unknownFieldsName = localNameAllocator.newName("unknownFields");
    MethodSpec.Builder result = MethodSpec.constructorBuilder().addModifiers(PUBLIC).addStatement("super($N, $N)", adapterName, unknownFieldsName);
    for (OneOf oneOf : type.oneOfs()) {
        if (oneOf.fields().size() < 2)
            continue;
        CodeBlock.Builder fieldNamesBuilder = CodeBlock.builder();
        boolean first = true;
        for (Field field : oneOf.fields()) {
            if (!first)
                fieldNamesBuilder.add(", ");
            fieldNamesBuilder.add("$N", localNameAllocator.get(field));
            first = false;
        }
        CodeBlock fieldNames = fieldNamesBuilder.build();
        result.beginControlFlow("if ($T.countNonNull($L) > 1)", Internal.class, fieldNames);
        result.addStatement("throw new IllegalArgumentException($S)", "at most one of " + fieldNames + " may be non-null");
        result.endControlFlow();
    }
    for (Field field : type.fieldsAndOneOfFields()) {
        TypeName javaType = fieldType(field);
        String fieldName = localNameAllocator.get(field);
        ParameterSpec.Builder param = ParameterSpec.builder(javaType, fieldName);
        if (emitAndroid && field.isOptional()) {
            param.addAnnotation(NULLABLE);
        }
        result.addParameter(param.build());
        if (field.isRepeated() || field.type().isMap()) {
            result.addStatement("this.$1L = $2T.immutableCopyOf($1S, $1L)", fieldName, Internal.class);
        } else {
            result.addStatement("this.$1L = $1L", fieldName);
        }
    }
    result.addParameter(BYTE_STRING, unknownFieldsName);
    return result.build();
}
Also used : NameAllocator(com.squareup.javapoet.NameAllocator) OneOf(com.squareup.wire.schema.OneOf) WireField(com.squareup.wire.WireField) Field(com.squareup.wire.schema.Field) TypeName(com.squareup.javapoet.TypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) MethodSpec(com.squareup.javapoet.MethodSpec) ParameterSpec(com.squareup.javapoet.ParameterSpec) CodeBlock(com.squareup.javapoet.CodeBlock) ByteString(okio.ByteString)

Example 2 with OneOf

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

the class JavaGenerator method wireFieldAnnotation.

// Example:
// 
// @WireField(
// tag = 1,
// type = INT32
// )
// 
private AnnotationSpec wireFieldAnnotation(NameAllocator nameAllocator, Field field, MessageType message) {
    AnnotationSpec.Builder result = AnnotationSpec.builder(WireField.class);
    NameAllocator localNameAllocator = nameAllocator.clone();
    int tag = field.getTag();
    result.addMember("tag", String.valueOf(tag));
    if (field.getType().isMap()) {
        result.addMember("keyAdapter", "$S", adapterString(field.getType().getKeyType()));
        result.addMember("adapter", "$S", adapterString(field.getType().getValueType()));
    } else {
        result.addMember("adapter", "$S", adapterString(field.getType()));
    }
    WireField.Label wireFieldLabel;
    // noinspection ConstantConditions
    switch(field.getEncodeMode()) {
        case REQUIRED:
            wireFieldLabel = WireField.Label.REQUIRED;
            break;
        case OMIT_IDENTITY:
            wireFieldLabel = WireField.Label.OMIT_IDENTITY;
            break;
        case REPEATED:
            wireFieldLabel = WireField.Label.REPEATED;
            break;
        case PACKED:
            wireFieldLabel = WireField.Label.PACKED;
            break;
        case MAP:
        case NULL_IF_ABSENT:
        default:
            wireFieldLabel = null;
    }
    if (wireFieldLabel != null) {
        result.addMember("label", "$T.$L", WireField.Label.class, wireFieldLabel);
    }
    if (field.isRedacted()) {
        result.addMember("redacted", "true");
    }
    String generatedName = localNameAllocator.get(field);
    if (!generatedName.equals(field.getName())) {
        result.addMember("declaredName", "$S", field.getName());
    }
    if (!field.getJsonName().equals(field.getName())) {
        result.addMember("jsonName", "$S", field.getJsonName());
    }
    if (field.isOneOf()) {
        String oneofName = null;
        for (OneOf oneOf : message.getOneOfs()) {
            if (oneOf.getFields().contains(field)) {
                oneofName = oneOf.getName();
                break;
            }
        }
        if (oneofName == null) {
            throw new IllegalArgumentException("No oneof found for field: " + field.getQualifiedName());
        }
        result.addMember("oneofName", "$S", oneofName);
    }
    return result.build();
}
Also used : NameAllocator(com.squareup.javapoet.NameAllocator) OneOf(com.squareup.wire.schema.OneOf) ByteString(okio.ByteString) JvmLanguages.builtInAdapterString(com.squareup.wire.schema.internal.JvmLanguages.builtInAdapterString) AnnotationSpec(com.squareup.javapoet.AnnotationSpec) WireField(com.squareup.wire.WireField)

Example 3 with OneOf

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

the class JavaGenerator method builder.

private TypeSpec builder(NameAllocator nameAllocator, MessageType type, ClassName javaType, ClassName builderType) {
    TypeSpec.Builder result = TypeSpec.classBuilder("Builder").addModifiers(PUBLIC, STATIC, FINAL);
    result.superclass(builderOf(javaType, builderType));
    for (Field field : type.getFieldsAndOneOfFields()) {
        String fieldName = nameAllocator.get(field);
        result.addField(fieldType(field), fieldName, PUBLIC);
    }
    result.addMethod(builderNoArgsConstructor(nameAllocator, type));
    for (Field field : type.fields()) {
        result.addMethod(setter(nameAllocator, builderType, null, field));
    }
    for (OneOf oneOf : type.getOneOfs()) {
        for (Field field : oneOf.getFields()) {
            result.addMethod(setter(nameAllocator, builderType, oneOf, field));
        }
    }
    result.addMethod(builderBuild(nameAllocator, type, javaType));
    return result.build();
}
Also used : WireField(com.squareup.wire.WireField) Field(com.squareup.wire.schema.Field) OneOf(com.squareup.wire.schema.OneOf) ByteString(okio.ByteString) JvmLanguages.builtInAdapterString(com.squareup.wire.schema.internal.JvmLanguages.builtInAdapterString) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 4 with OneOf

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

the class JavaGenerator method messageConstructor.

// Example:
// 
// public SimpleMessage(int optional_int32, long optional_int64, ByteString unknownFields) {
// super(ADAPTER, unknownFields);
// this.optional_int32 = optional_int32;
// this.optional_int64 = optional_int64;
// }
// 
// Alternate example, where the constructor takes in a builder, would be the case when there are
// too many fields:
// 
// public SimpleMessage(Builder builder, ByteString unknownFields) {
// super(ADAPTER, unknownFields);
// this.optional_int32 = builder.optional_int32;
// this.optional_int64 = builder.optional_int64;
// }
// 
private MethodSpec messageConstructor(NameAllocator nameAllocator, MessageType type, ClassName builderJavaType) {
    boolean constructorTakesAllFields = constructorTakesAllFields(type);
    NameAllocator localNameAllocator = nameAllocator.clone();
    String adapterName = localNameAllocator.get("ADAPTER");
    String unknownFieldsName = localNameAllocator.newName("unknownFields");
    String builderName = localNameAllocator.newName("builder");
    MethodSpec.Builder result = MethodSpec.constructorBuilder().addModifiers(PUBLIC).addStatement("super($N, $N)", adapterName, unknownFieldsName);
    for (OneOf oneOf : type.getOneOfs()) {
        if (oneOf.getFields().size() < 2)
            continue;
        CodeBlock.Builder fieldNamesBuilder = CodeBlock.builder();
        boolean first = true;
        for (Field field : oneOf.getFields()) {
            if (!first)
                fieldNamesBuilder.add(", ");
            if (constructorTakesAllFields) {
                fieldNamesBuilder.add("$N", localNameAllocator.get(field));
            } else {
                fieldNamesBuilder.add("$N.$N", builderName, localNameAllocator.get(field));
            }
            first = false;
        }
        CodeBlock fieldNames = fieldNamesBuilder.build();
        result.beginControlFlow("if ($T.countNonNull($L) > 1)", Internal.class, fieldNames);
        result.addStatement("throw new IllegalArgumentException($S)", "at most one of " + fieldNames + " may be non-null");
        result.endControlFlow();
    }
    for (Field field : type.getFieldsAndOneOfFields()) {
        TypeName javaType = fieldType(field);
        String fieldName = localNameAllocator.get(field);
        String fieldAccessName = constructorTakesAllFields ? fieldName : builderName + "." + fieldName;
        if (constructorTakesAllFields) {
            ParameterSpec.Builder param = ParameterSpec.builder(javaType, fieldName);
            if (emitAndroidAnnotations && field.getEncodeMode() == Field.EncodeMode.NULL_IF_ABSENT) {
                param.addAnnotation(NULLABLE);
            }
            result.addParameter(param.build());
        }
        if (field.getEncodeMode() == Field.EncodeMode.OMIT_IDENTITY) {
            // Other scalars use not-boxed types to guarantee a value.
            if (field.getType().isScalar() && (field.getType() == ProtoType.STRING || field.getType() == ProtoType.BYTES) || (isEnum(field.getType()) && !field.getType().equals(ProtoType.STRUCT_NULL))) {
                result.beginControlFlow("if ($L == null)", fieldAccessName);
                result.addStatement("throw new IllegalArgumentException($S)", fieldAccessName + " == null");
                result.endControlFlow();
            }
        }
        if (field.getType().isMap() && isStruct(field.getType().getValueType())) {
            result.addStatement("this.$1L = $2T.immutableCopyOfMapWithStructValues($1S, $3L)", fieldName, Internal.class, fieldAccessName);
        } else if (isStruct(field.getType())) {
            result.addStatement("this.$1L = $2T.immutableCopyOfStruct($1S, $3L)", fieldName, Internal.class, fieldAccessName);
        } else if (field.isRepeated() || field.getType().isMap()) {
            result.addStatement("this.$1L = $2T.immutableCopyOf($1S, $3L)", fieldName, Internal.class, fieldAccessName);
        } else {
            result.addStatement("this.$1L = $2L", fieldName, fieldAccessName);
        }
    }
    if (!constructorTakesAllFields) {
        result.addParameter(builderJavaType, builderName);
    }
    result.addParameter(BYTE_STRING, unknownFieldsName);
    return result.build();
}
Also used : NameAllocator(com.squareup.javapoet.NameAllocator) 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) Internal(com.squareup.wire.internal.Internal) CodeBlock(com.squareup.javapoet.CodeBlock) ByteString(okio.ByteString) JvmLanguages.builtInAdapterString(com.squareup.wire.schema.internal.JvmLanguages.builtInAdapterString) OneOf(com.squareup.wire.schema.OneOf) WireField(com.squareup.wire.WireField) Field(com.squareup.wire.schema.Field)

Aggregations

WireField (com.squareup.wire.WireField)4 OneOf (com.squareup.wire.schema.OneOf)4 ByteString (okio.ByteString)4 NameAllocator (com.squareup.javapoet.NameAllocator)3 Field (com.squareup.wire.schema.Field)3 JvmLanguages.builtInAdapterString (com.squareup.wire.schema.internal.JvmLanguages.builtInAdapterString)3 CodeBlock (com.squareup.javapoet.CodeBlock)2 MethodSpec (com.squareup.javapoet.MethodSpec)2 ParameterSpec (com.squareup.javapoet.ParameterSpec)2 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)2 TypeName (com.squareup.javapoet.TypeName)2 AnnotationSpec (com.squareup.javapoet.AnnotationSpec)1 TypeSpec (com.squareup.javapoet.TypeSpec)1 WildcardTypeName (com.squareup.javapoet.WildcardTypeName)1 Internal (com.squareup.wire.internal.Internal)1