public static Location get(String path) { return get("", path); }
@Override public String toString() { StringBuilder result = new StringBuilder(); if (!base().isEmpty()) { result.append(base()).append(File.separator); } result.append(path()); if (line() != -1) { result.append(" at ").append(line()); if (column() != -1) { result.append(':').append(column()); } } return result.toString(); } }
public Location location() { return location.at(line + 1, pos - lineStart + 1); }
/** Returns the proto file at {@code path}, or null if this schema has no such file. */ public ProtoFile protoFile(String path) { for (ProtoFile protoFile : protoFiles) { if (protoFile.location().path().equals(path)) { return protoFile; } } return null; }
if (!protoFilesList.isEmpty() && !protoFilesList.contains(protoFile.location().path())) { continue; // Don't emit anything for files not explicitly compiled. 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));
fieldBuilder.addJavadoc("Extension source: $L\n", field.location().withPathOnly());
/** Returns a copy of this location including only its path. */ public Location withPathOnly() { return new AutoValue_Location("", path(), -1, -1); }
/** * Computes all possible {@code .wire} profile files for the {@code .proto} at {@code location} * and adds them to {@code result}. */ void pathsToAttempt(Multimap<Path, String> sink, Location location) { Path base = fileSystem.getPath(location.base()); String path = location.path(); while (!path.isEmpty()) { String parent = path.substring(0, path.lastIndexOf('/', path.length() - 2) + 1); String profilePath = parent + name + ".wire"; sink.put(base, profilePath); path = parent; } }
ClassName javaTypeName = javaGenerator.generatedTypeName(type); TypeSpec typeSpec = javaGenerator.generateType(protoFile, type); writeJavaFile(javaTypeName, typeSpec, type.location().withPathOnly()); TypeSpec typeSpec = javaGenerator.generateService(protoFile, service); ClassName typeName = (ClassName) javaGenerator.typeName(service.type()); writeJavaFile(typeName, typeSpec, service.location().withPathOnly());
@Test public void reserved() { String proto = "" + "message Foo {\n" + " reserved 10, 12 to 14, 'foo';" + "}"; TypeElement message = MessageElement.builder(location.at(1, 1)) .name("Foo") .reserveds(ImmutableList.of(ReservedElement.create(location.at(2, 3), "", ImmutableList.<Object>of(10, Range.closed(12, 14), "foo")))) .build(); ProtoFileElement expected = ProtoFileElement.builder(location) .types(ImmutableList.of(message)) .build(); assertThat(ProtoParser.parse(location, proto)).isEqualTo(expected); }
@Test public void locateInZipFile() throws IOException { Files.createDirectories(fileSystem.getPath("/source")); Path zip = fileSystem.getPath("/source/protos.zip"); ZipOutputStream zipOutputStream = new ZipOutputStream(Files.newOutputStream(zip)); zipOutputStream.putNextEntry(new ZipEntry("a/b/message.proto")); zipOutputStream.write("message Message {}".getBytes(UTF_8)); zipOutputStream.close(); Schema schema = new SchemaLoader() .addSource(zip) .addProto("a/b/message.proto") .load(); Type message = schema.getType("Message"); assertThat(message).isNotNull(); assertThat(message.location().base()).isEqualTo("/source/protos.zip"); assertThat(message.location().path()).isEqualTo("a/b/message.proto"); }
/** * Returns Google's protobuf descriptor, which defines standard options like default, deprecated, * and java_package. If the user has provided their own version of the descriptor proto, that is * preferred. */ private ProtoFile loadDescriptorProto() throws IOException { InputStream resourceAsStream = SchemaLoader.class.getResourceAsStream("/" + DESCRIPTOR_PROTO); try (BufferedSource buffer = Okio.buffer(Okio.source(resourceAsStream))) { String data = buffer.readUtf8(); Location location = Location.get("", DESCRIPTOR_PROTO); ProtoFileElement element = ProtoParser.parse(location, data); return ProtoFile.get(element); } }
fieldBuilder.addJavadoc("Extension source: $L\n", field.location().withPathOnly());
@Override public String toString() { StringBuilder result = new StringBuilder(); if (!base().isEmpty()) { result.append(base()).append(File.separator); } result.append(path()); if (line() != -1) { result.append(" at ").append(line()); if (column() != -1) { result.append(':').append(column()); } } return result.toString(); } }