All Downloads are FREE. Search and download functionalities are using the official Maven repository.

present.rpc.JavaGenerator Maven / Gradle / Ivy

The newest version!
package present.rpc;

import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.TypeName;
import com.squareup.javapoet.TypeSpec;
import com.squareup.wire.schema.Location;
import com.squareup.wire.schema.ProtoFile;
import com.squareup.wire.schema.ProtoType;
import com.squareup.wire.schema.Rpc;
import com.squareup.wire.schema.Schema;
import com.squareup.wire.schema.Service;
import java.io.File;
import java.io.IOException;
import javax.lang.model.element.Modifier;

import static com.google.common.base.CaseFormat.LOWER_CAMEL;
import static com.google.common.base.CaseFormat.UPPER_CAMEL;

class JavaGenerator {

  private final Log log;
  private final Schema schema;
  private final String outputDirectory;

  public JavaGenerator(Log log, Schema schema, String outputDirectory) {
    this.log = log;
    this.schema = schema;
    this.outputDirectory = outputDirectory;
  }

  public void execute() throws IOException {
    new File(outputDirectory).mkdirs();
    com.squareup.wire.java.JavaGenerator javaGenerator = com.squareup.wire.java.JavaGenerator.get(schema);
    for (ProtoFile protoFile : schema.protoFiles()) {
      for (Service service : protoFile.services()) {
        ClassName javaTypeName = (ClassName) javaGenerator.typeName(service.type());
        TypeSpec typeSpec = createJavaInterface(javaGenerator, service);
        writeJavaFile(javaTypeName, typeSpec, service.location());
      }
    }
  }

  private void writeJavaFile(ClassName javaTypeName, TypeSpec typeSpec, Location location)
      throws IOException {
    JavaFile.Builder builder = JavaFile.builder(javaTypeName.packageName(), typeSpec)
        .addFileComment("Code generated by $L, do not edit.", JavaGenerator.class.getName());
    if (location != null) {
      builder.addFileComment("\nSource file: $L", location.path());
    }
    JavaFile javaFile = builder.build();
    try {
      javaFile.writeTo(new File(outputDirectory));
    } catch (IOException e) {
      throw new IOException("Failed to write " + javaFile.packageName + "."
          + javaFile.typeSpec.name + " to " + outputDirectory, e);
    }
    log.info("Generated %s.", javaTypeName);
  }

  private TypeSpec createJavaInterface(com.squareup.wire.java.JavaGenerator javaGenerator, Service service) {
    ClassName interfaceName = (ClassName) javaGenerator.typeName(service.type());

    TypeSpec.Builder typeBuilder = TypeSpec.interfaceBuilder(interfaceName.simpleName());
    typeBuilder.addModifiers(Modifier.PUBLIC);

    if (!service.documentation().isEmpty()) {
      typeBuilder.addJavadoc("$L\n", service.documentation());
    }

    for (Rpc rpc : service.rpcs()) {
      ProtoType requestType = rpc.requestType();
      TypeName requestJavaType = javaGenerator.typeName(requestType);
      ProtoType responseType = rpc.responseType();
      TypeName responseJavaType = javaGenerator.typeName(responseType);

      MethodSpec.Builder rpcBuilder = MethodSpec.methodBuilder(upperToLowerCamel(rpc.name()));
      rpcBuilder.addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT);
      rpcBuilder.returns(responseJavaType);
      rpcBuilder.addException(TypeName.get(IOException.class));

      rpcBuilder.addParameter(requestJavaType, "request");

      if (!rpc.documentation().isEmpty()) {
        rpcBuilder.addJavadoc("$L\n", rpc.documentation());
      }

      typeBuilder.addMethod(rpcBuilder.build());
    }

    return typeBuilder.build();
  }

  private String upperToLowerCamel(String string) {
    return UPPER_CAMEL.to(LOWER_CAMEL, string);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy