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