Search in sources :

Example 1 with MessageType

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

the class JavaGeneratorTest method map.

@Test
public void map() throws Exception {
    Schema schema = new RepoBuilder().add("message.proto", "" + "message Message {\n" + "  map<string, CdnResource> templates = 1;\n" + "  message CdnResource {\n" + "  }\n" + "}\n").schema();
    MessageType message = (MessageType) schema.getType("Message");
    JavaGenerator javaGenerator = JavaGenerator.get(schema);
    TypeSpec typeSpec = javaGenerator.generateType(message);
    assertThat(JavaFile.builder("", typeSpec).build().toString()).contains("" + "  @WireField(\n" + "      tag = 1,\n" + "      keyAdapter = \"com.squareup.wire.ProtoAdapter#STRING\",\n" + "      adapter = \"Message$CdnResource#ADAPTER\"\n" + "  )\n" + "  public final Map<String, CdnResource> templates;\n");
}
Also used : RepoBuilder(com.squareup.wire.schema.RepoBuilder) Schema(com.squareup.wire.schema.Schema) MessageType(com.squareup.wire.schema.MessageType) TypeSpec(com.squareup.javapoet.TypeSpec) Test(org.junit.Test)

Example 2 with MessageType

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

the class JavaGenerator method generateMessage.

/** @deprecated Use {@link #generateType(Type)} */
@Deprecated
public TypeSpec generateMessage(MessageType type) {
    NameAllocator nameAllocator = nameAllocators.getUnchecked(type);
    ClassName javaType = (ClassName) typeName(type.type());
    ClassName builderJavaType = javaType.nestedClass("Builder");
    TypeSpec.Builder builder = TypeSpec.classBuilder(javaType.simpleName());
    builder.addModifiers(PUBLIC, FINAL);
    if (javaType.enclosingClassName() != null) {
        builder.addModifiers(STATIC);
    }
    if (!type.documentation().isEmpty()) {
        builder.addJavadoc("$L\n", sanitizeJavadoc(type.documentation()));
    }
    ClassName messageType = emitAndroid ? ANDROID_MESSAGE : MESSAGE;
    builder.superclass(messageOf(messageType, javaType, builderJavaType));
    String adapterName = nameAllocator.get("ADAPTER");
    String protoAdapterName = "ProtoAdapter_" + javaType.simpleName();
    String protoAdapterClassName = nameAllocator.newName(protoAdapterName);
    ClassName adapterJavaType = javaType.nestedClass(protoAdapterClassName);
    builder.addField(messageAdapterField(adapterName, javaType, adapterJavaType));
    if (emitAndroid) {
        TypeName creatorType = creatorOf(javaType);
        String creatorName = nameAllocator.get("CREATOR");
        builder.addField(FieldSpec.builder(creatorType, creatorName, PUBLIC, STATIC, FINAL).initializer("$T.newCreator($L)", ANDROID_MESSAGE, adapterName).build());
    }
    builder.addField(FieldSpec.builder(TypeName.LONG, nameAllocator.get("serialVersionUID")).addModifiers(PRIVATE, STATIC, FINAL).initializer("$LL", 0L).build());
    FieldSpec messageOptions = optionsField(MESSAGE_OPTIONS, nameAllocator.get("MESSAGE_OPTIONS"), type.options());
    if (messageOptions != null) {
        builder.addField(messageOptions);
    }
    for (Field field : type.fieldsAndOneOfFields()) {
        String fieldName = nameAllocator.get(field);
        String optionsFieldName = "FIELD_OPTIONS_" + fieldName.toUpperCase(Locale.US);
        FieldSpec fieldOptions = optionsField(FIELD_OPTIONS, optionsFieldName, field.options());
        if (fieldOptions != null) {
            builder.addField(fieldOptions);
        }
    }
    for (Field field : type.fieldsAndOneOfFields()) {
        TypeName fieldJavaType = fieldType(field);
        if ((field.type().isScalar() || isEnum(field.type())) && !field.isRepeated() && !field.isPacked()) {
            builder.addField(defaultField(nameAllocator, field, fieldJavaType));
        }
        String fieldName = nameAllocator.get(field);
        FieldSpec.Builder fieldBuilder = FieldSpec.builder(fieldJavaType, fieldName, PUBLIC, FINAL);
        fieldBuilder.addAnnotation(wireFieldAnnotation(field));
        if (!field.documentation().isEmpty()) {
            fieldBuilder.addJavadoc("$L\n", sanitizeJavadoc(field.documentation()));
        }
        if (field.isExtension()) {
            fieldBuilder.addJavadoc("Extension source: $L\n", field.location().withPathOnly());
        }
        if (field.isDeprecated()) {
            fieldBuilder.addAnnotation(Deprecated.class);
        }
        if (emitAndroid && field.isOptional()) {
            fieldBuilder.addAnnotation(NULLABLE);
        }
        builder.addField(fieldBuilder.build());
    }
    builder.addMethod(messageFieldsConstructor(nameAllocator, type));
    builder.addMethod(messageFieldsAndUnknownFieldsConstructor(nameAllocator, type));
    builder.addMethod(newBuilder(nameAllocator, type));
    builder.addMethod(messageEquals(nameAllocator, type));
    builder.addMethod(messageHashCode(nameAllocator, type));
    if (!emitCompact) {
        builder.addMethod(messageToString(nameAllocator, type));
    }
    builder.addType(builder(nameAllocator, type, javaType, builderJavaType));
    for (Type nestedType : type.nestedTypes()) {
        builder.addType(generateType(nestedType));
    }
    if (!emitCompact) {
        // Add the ProtoAdapter implementation at the very bottom since it's ugly serialization code.
        builder.addType(messageAdapter(nameAllocator, type, javaType, adapterJavaType, builderJavaType));
    }
    return builder.build();
}
Also used : NameAllocator(com.squareup.javapoet.NameAllocator) WireField(com.squareup.wire.WireField) Field(com.squareup.wire.schema.Field) TypeName(com.squareup.javapoet.TypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) Type(com.squareup.wire.schema.Type) ProtoType(com.squareup.wire.schema.ProtoType) MessageType(com.squareup.wire.schema.MessageType) EnclosingType(com.squareup.wire.schema.EnclosingType) EnumType(com.squareup.wire.schema.EnumType) ClassName(com.squareup.javapoet.ClassName) ByteString(okio.ByteString) FieldSpec(com.squareup.javapoet.FieldSpec) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 3 with MessageType

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

the class JavaGenerator method fieldName.

private String fieldName(ProtoType type, Field field) {
    MessageType messageType = (MessageType) schema.getType(type);
    NameAllocator names = nameAllocators.getUnchecked(messageType);
    return names.get(field);
}
Also used : NameAllocator(com.squareup.javapoet.NameAllocator) MessageType(com.squareup.wire.schema.MessageType)

Example 4 with MessageType

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

the class JavaGenerator method generateAbstractAdapter.

/** Returns an abstract adapter for {@code type}. */
public TypeSpec generateAbstractAdapter(MessageType type) {
    NameAllocator nameAllocator = nameAllocators.getUnchecked(type);
    ClassName adapterTypeName = abstractAdapterName(type.type());
    ClassName typeName = (ClassName) typeName(type.type());
    TypeSpec.Builder adapter = messageAdapter(nameAllocator, type, typeName, adapterTypeName, null).toBuilder();
    if (adapterTypeName.enclosingClassName() != null) {
        adapter.addModifiers(STATIC);
    }
    for (Type nestedType : type.nestedTypes()) {
        if (profile.getAdapter(nestedType.type()) == null) {
            throw new IllegalArgumentException("Missing custom proto adapter for " + nestedType.type().enclosingTypeOrPackage() + "." + nestedType.type().simpleName() + " when enclosing proto has custom proto adapter.");
        }
        if (nestedType instanceof MessageType) {
            adapter.addType(generateAbstractAdapter((MessageType) nestedType));
        }
    }
    return adapter.build();
}
Also used : NameAllocator(com.squareup.javapoet.NameAllocator) Type(com.squareup.wire.schema.Type) ProtoType(com.squareup.wire.schema.ProtoType) MessageType(com.squareup.wire.schema.MessageType) EnclosingType(com.squareup.wire.schema.EnclosingType) EnumType(com.squareup.wire.schema.EnumType) ClassName(com.squareup.javapoet.ClassName) MessageType(com.squareup.wire.schema.MessageType) TypeSpec(com.squareup.javapoet.TypeSpec)

Aggregations

MessageType (com.squareup.wire.schema.MessageType)4 NameAllocator (com.squareup.javapoet.NameAllocator)3 TypeSpec (com.squareup.javapoet.TypeSpec)3 ClassName (com.squareup.javapoet.ClassName)2 EnclosingType (com.squareup.wire.schema.EnclosingType)2 EnumType (com.squareup.wire.schema.EnumType)2 ProtoType (com.squareup.wire.schema.ProtoType)2 Type (com.squareup.wire.schema.Type)2 FieldSpec (com.squareup.javapoet.FieldSpec)1 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)1 TypeName (com.squareup.javapoet.TypeName)1 WireField (com.squareup.wire.WireField)1 Field (com.squareup.wire.schema.Field)1 RepoBuilder (com.squareup.wire.schema.RepoBuilder)1 Schema (com.squareup.wire.schema.Schema)1 ByteString (okio.ByteString)1 Test (org.junit.Test)1