Search in sources :

Example 41 with TypeSpec

use of com.squareup.javapoet.TypeSpec 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.fieldsAndOneOfFields()) {
        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.oneOfs()) {
        for (Field field : oneOf.fields()) {
            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) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 42 with TypeSpec

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

the class ServiceGeneratorTest method service.

@Test
public void service() throws IOException {
    Schema schema = schema(ImmutableMap.of("sample.proto", "" + "syntax = \"proto2\";\n" + "package squareup.wire.sample;\n" + "\n" + "message SampleMessage {\n" + "  repeated string array = 1;\n" + "}\n" + "\n" + "message SampleRequest {\n" + "  optional string name = 1;\n" + "  optional SampleMessage sample_message = 2;\n" + "}\n" + "\n" + "message SampleResponse {\n" + "  optional int32 age = 1;\n" + "}\n" + "\n" + "// This is it. A really fantastic service interface.\n" + "service SampleApi {\n" + "  // Call this RPC. You'll be glad you did!\n" + "  rpc FirstRpc (SampleRequest) returns (SampleResponse);\n" + "  rpc OtherOne (SampleRequest) returns (SampleResponse);\n" + "}\n"));
    Service service = schema.getService("squareup.wire.sample.SampleApi");
    JavaGenerator javaGenerator = JavaGenerator.get(schema);
    ServiceGenerator generator = new ServiceGenerator(javaGenerator);
    TypeSpec typeSpec = generator.api(service);
    assertThat(toString(typeSpec)).isEqualTo("" + "package squareup.wire.sample;\n" + "\n" + "/**\n" + " * This is it. A really fantastic service interface.\n" + " */\n" + "public interface SampleApi {\n" + "  /**\n" + "   * Call this RPC. You'll be glad you did!\n" + "   */\n" + "  SampleResponse FirstRpc(SampleRequest request);\n" + "\n" + "  SampleResponse OtherOne(SampleRequest request);\n" + "}\n");
}
Also used : Schema(com.squareup.wire.schema.Schema) Service(com.squareup.wire.schema.Service) JavaGenerator(com.squareup.wire.java.JavaGenerator) TypeSpec(com.squareup.javapoet.TypeSpec) Test(org.junit.Test)

Example 43 with TypeSpec

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

the class CodegenSample method execute.

public void execute() throws IOException {
    Schema schema = loadSchema();
    if (!identifierSet.isEmpty()) {
        schema = retainRoots(schema);
    }
    JavaGenerator javaGenerator = JavaGenerator.get(schema);
    ServiceGenerator serviceGenerator = new ServiceGenerator(javaGenerator);
    for (ProtoFile protoFile : schema.protoFiles()) {
        for (Type type : protoFile.types()) {
            Stopwatch stopwatch = Stopwatch.createStarted();
            TypeSpec typeSpec = javaGenerator.generateType(type);
            ClassName javaTypeName = (ClassName) javaGenerator.typeName(type.type());
            writeJavaFile(javaTypeName, typeSpec, type.location(), stopwatch);
        }
        for (Service service : protoFile.services()) {
            Stopwatch stopwatch = Stopwatch.createStarted();
            ClassName javaTypeName = (ClassName) javaGenerator.typeName(service.type());
            TypeSpec typeSpec = serviceGenerator.api(service);
            writeJavaFile(javaTypeName, typeSpec, service.location(), stopwatch);
        }
    }
}
Also used : Type(com.squareup.wire.schema.Type) Schema(com.squareup.wire.schema.Schema) ProtoFile(com.squareup.wire.schema.ProtoFile) Stopwatch(com.google.common.base.Stopwatch) ClassName(com.squareup.javapoet.ClassName) Service(com.squareup.wire.schema.Service) JavaGenerator(com.squareup.wire.java.JavaGenerator) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 44 with TypeSpec

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

the class RepoBuilder method generateCode.

public String generateCode(String typeName, String profile) throws IOException {
    Schema schema = schema();
    JavaGenerator javaGenerator = JavaGenerator.get(schema);
    if (profile != null) {
        javaGenerator = javaGenerator.withProfile(profile(profile));
    }
    Type type = schema.getType(typeName);
    TypeSpec typeSpec = javaGenerator.generateType(type);
    ClassName typeName1 = javaGenerator.generatedTypeName(type);
    return JavaFile.builder(typeName1.packageName(), typeSpec).build().toString();
}
Also used : ClassName(com.squareup.javapoet.ClassName) JavaGenerator(com.squareup.wire.java.JavaGenerator) TypeSpec(com.squareup.javapoet.TypeSpec)

Aggregations

TypeSpec (com.squareup.javapoet.TypeSpec)44 ClassName (com.squareup.javapoet.ClassName)26 MethodSpec (com.squareup.javapoet.MethodSpec)20 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)10 TypeName (com.squareup.javapoet.TypeName)8 NotNull (org.jetbrains.annotations.NotNull)8 FieldSpec (com.squareup.javapoet.FieldSpec)7 ArrayList (java.util.ArrayList)6 Type (com.squareup.wire.schema.Type)5 TypeElement (javax.lang.model.element.TypeElement)5 ByteString (okio.ByteString)5 WireField (com.squareup.wire.WireField)4 JavaGenerator (com.squareup.wire.java.JavaGenerator)4 Field (com.squareup.wire.schema.Field)4 MessageType (com.squareup.wire.schema.MessageType)4 ProtoType (com.squareup.wire.schema.ProtoType)4 Schema (com.squareup.wire.schema.Schema)4 CodeBlock (com.squareup.javapoet.CodeBlock)3 EnclosingType (com.squareup.wire.schema.EnclosingType)3 EnumType (com.squareup.wire.schema.EnumType)3