Search in sources :

Example 11 with TypeSpec

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

the class WireGenerateSourcesMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    // Add the directory into which generated sources are placed as a compiled source root.
    project.addCompileSourceRoot(generatedSourceDirectory);
    try {
        List<String> directories = protoPaths != null && protoPaths.length > 0 ? Arrays.asList(protoPaths) : Collections.singletonList(protoSourceDirectory);
        List<String> protoFilesList = Arrays.asList(protoFiles);
        Schema schema = loadSchema(directories, protoFilesList);
        Profile profile = loadProfile(schema);
        IdentifierSet identifierSet = identifierSet();
        if (!identifierSet.isEmpty()) {
            schema = retainRoots(identifierSet, schema);
        }
        JavaGenerator javaGenerator = JavaGenerator.get(schema).withAndroid(emitAndroid).withCompact(emitCompact).withProfile(profile);
        for (ProtoFile protoFile : schema.protoFiles()) {
            if (!protoFilesList.isEmpty() && !protoFilesList.contains(protoFile.location().path())) {
                // Don't emit anything for files not explicitly compiled.
                continue;
            }
            for (Type type : protoFile.types()) {
                Stopwatch stopwatch = Stopwatch.createStarted();
                TypeSpec typeSpec = javaGenerator.generateType(type);
                ClassName javaTypeName = javaGenerator.generatedTypeName(type);
                writeJavaFile(javaTypeName, typeSpec, type.location().withPathOnly());
                getLog().info(String.format("Generated %s in %s", javaTypeName, stopwatch));
            }
        }
    } catch (Exception e) {
        throw new MojoExecutionException("Wire Plugin: Failure compiling proto sources.", e);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Schema(com.squareup.wire.schema.Schema) ProtoFile(com.squareup.wire.schema.ProtoFile) Stopwatch(com.google.common.base.Stopwatch) JavaGenerator(com.squareup.wire.java.JavaGenerator) IdentifierSet(com.squareup.wire.schema.IdentifierSet) Profile(com.squareup.wire.java.Profile) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Type(com.squareup.wire.schema.Type) ClassName(com.squareup.javapoet.ClassName) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 12 with TypeSpec

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

the class ServiceGenerator method api.

public TypeSpec api(Service service) {
    ClassName apiName = (ClassName) javaGenerator.typeName(service.type());
    TypeSpec.Builder typeBuilder = TypeSpec.interfaceBuilder(apiName.simpleName());
    typeBuilder.addModifiers(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(rpc.name());
        rpcBuilder.addModifiers(PUBLIC, ABSTRACT);
        rpcBuilder.returns(responseJavaType);
        rpcBuilder.addParameter(requestJavaType, "request");
        if (!rpc.documentation().isEmpty()) {
            rpcBuilder.addJavadoc("$L\n", rpc.documentation());
        }
        typeBuilder.addMethod(rpcBuilder.build());
    }
    return typeBuilder.build();
}
Also used : ProtoType(com.squareup.wire.schema.ProtoType) TypeName(com.squareup.javapoet.TypeName) Rpc(com.squareup.wire.schema.Rpc) MethodSpec(com.squareup.javapoet.MethodSpec) ClassName(com.squareup.javapoet.ClassName) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 13 with TypeSpec

use of com.squareup.javapoet.TypeSpec in project bazel by bazelbuild.

the class JavaCodeGeneratorHelper method parallelDepsMainClassHelper.

/**
   * Writes {@code count-1} class files to the directory {@code projectPath/com/example/deps(index)}
   * and one main class.
   */
static void parallelDepsMainClassHelper(int count, Path projectPath) throws IOException {
    MethodSpec.Builder callDepsBuilder = MethodSpec.methodBuilder("main").addModifiers(Modifier.PUBLIC, Modifier.STATIC).addParameter(String[].class, "args").returns(void.class);
    for (int i = 1; i < count; ++i) {
        ClassName callingClass = ClassName.get("com.example.deps" + i, "Deps" + i);
        callDepsBuilder.addStatement("$T.PrintSth()", callingClass);
    }
    MethodSpec callDeps = callDepsBuilder.build();
    TypeSpec klass = TypeSpec.classBuilder("Main").addModifiers(Modifier.PUBLIC, Modifier.FINAL).addMethod(callDeps).build();
    writeClassToDir(klass, "com.example.generated", projectPath);
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) ClassName(com.squareup.javapoet.ClassName) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 14 with TypeSpec

use of com.squareup.javapoet.TypeSpec in project bazel by bazelbuild.

the class JavaCodeGeneratorHelper method writeMainClassToDir.

static void writeMainClassToDir(String packageName, Path projectPath) throws IOException {
    TypeSpec main = genMainClass();
    writeClassToDir(main, packageName, projectPath);
}
Also used : TypeSpec(com.squareup.javapoet.TypeSpec)

Example 15 with TypeSpec

use of com.squareup.javapoet.TypeSpec in project tiger by google.

the class NewInjectorGenerator method getInjectorTypeSpecBuilder.

/**
   * Get {@link TypeSpec} for packaged injector specified by className.
   */
private TypeSpec.Builder getInjectorTypeSpecBuilder(ClassName injectorClassName) {
    if (!packagedInjectorBuilders.containsKey(injectorClassName)) {
        // injector for peer packaged injectors.
        for (ComponentInfo component : orderedComponents) {
            TypeSpec.Builder typeSpecBuilder = createInjectorTypeSpec(component, injectorClassName);
            packagedInjectorBuilders.put(getInjectorNameOfScope(injectorClassName, component.getScope()), typeSpecBuilder);
        }
    }
    return packagedInjectorBuilders.get(injectorClassName);
}
Also used : Builder(com.squareup.javapoet.TypeSpec.Builder) 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