use of com.squareup.wire.java.JavaGenerator 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.wire.java.JavaGenerator 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.wire.java.JavaGenerator in project wire by square.
the class WireCompiler method compile.
void compile() throws IOException {
SchemaLoader schemaLoader = new SchemaLoader();
for (String protoPath : protoPaths) {
schemaLoader.addSource(fs.getPath(protoPath));
}
for (String sourceFileName : sourceFileNames) {
schemaLoader.addProto(sourceFileName);
}
Schema schema = schemaLoader.load();
String profileName = emitAndroid ? "android" : "java";
Profile profile = new ProfileLoader(profileName).schema(schema).load();
if (!identifierSet.isEmpty()) {
log.info("Analyzing dependencies of root types.");
schema = schema.prune(identifierSet);
for (String rule : identifierSet.unusedIncludes()) {
log.info("Unused include: " + rule);
}
for (String rule : identifierSet.unusedExcludes()) {
log.info("Unused exclude: " + rule);
}
}
JavaGenerator javaGenerator = JavaGenerator.get(schema).withProfile(profile).withAndroid(emitAndroid).withCompact(emitCompact);
ConcurrentLinkedQueue<Type> types = new ConcurrentLinkedQueue<>();
for (ProtoFile protoFile : schema.protoFiles()) {
// Check if we're skipping files not explicitly named.
if (!sourceFileNames.isEmpty() && !sourceFileNames.contains(protoFile.location().path())) {
if (namedFilesOnly || protoFile.location().path().equals(DESCRIPTOR_PROTO))
continue;
}
types.addAll(protoFile.types());
}
ExecutorService executor = Executors.newCachedThreadPool();
List<Future<Void>> futures = new ArrayList<>(MAX_WRITE_CONCURRENCY);
for (int i = 0; i < MAX_WRITE_CONCURRENCY; ++i) {
futures.add(i, executor.submit(new JavaFileWriter(javaGenerator, types)));
}
executor.shutdown();
try {
for (Future<Void> future : futures) {
future.get();
}
} catch (ExecutionException e) {
throw new IOException(e.getMessage(), e);
} catch (InterruptedException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
use of com.squareup.wire.java.JavaGenerator 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.wire.java.JavaGenerator 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