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();
}
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");
}
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);
}
}
}
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();
}
Aggregations