@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"); }
private static void putAll(Map<ProtoType, ClassName> wireToJava, String javaPackage, ClassName enclosingClassName, List<Type> types) { for (Type type : types) { ClassName className = enclosingClassName != null ? enclosingClassName.nestedClass(type.type().simpleName()) : ClassName.get(javaPackage, type.type().simpleName()); wireToJava.put(type.type(), className); putAll(wireToJava, javaPackage, className, type.nestedTypes()); } }
static ImmutableList<Type> fromElements(String packageName, ImmutableList<TypeElement> elements) { ImmutableList.Builder<Type> types = new ImmutableList.Builder<>(); for (TypeElement element : elements) { ProtoType protoType = ProtoType.get(packageName, element.name()); types.add(Type.get(packageName, protoType, element)); } return types.build(); }
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); } } }
Linker linker = withContext(protoFile); for (Type type : protoFile.types()) { type.link(linker); protoFile.linkOptions(linker); for (Type type : protoFile.types()) { type.linkOptions(linker); Linker linker = withContext(protoFile); for (Type type : protoFile.types()) { type.validate(linker);
@Override Type retainAll(Schema schema, MarkSet markSet) { ImmutableList.Builder<Type> retainedNestedTypesBuilder = ImmutableList.builder(); for (Type nestedType : nestedTypes) { Type retainedNestedType = nestedType.retainAll(schema, markSet); if (retainedNestedType != null) { retainedNestedTypesBuilder.add(retainedNestedType); } } ImmutableList<Type> retainedNestedTypes = retainedNestedTypesBuilder.build(); if (retainedNestedTypes.isEmpty()) { return null; } return new EnclosingType(location, type, documentation, retainedNestedTypes); }
@Override void linkOptions(Linker linker) { for (Type nestedType : nestedTypes) { nestedType.linkOptions(linker); } }
@Override void link(Linker linker) { for (Type nestedType : nestedTypes) { nestedType.link(linker); } }
static ProtoFile get(ProtoFileElement protoFileElement) { String packageName = protoFileElement.packageName(); ImmutableList<Type> types = Type.fromElements(packageName, protoFileElement.types()); ImmutableList<Service> services = Service.fromElements(packageName, protoFileElement.services()); ImmutableList<Extend> wireExtends = Extend.fromElements(packageName, protoFileElement.extendDeclarations()); Options options = new Options(Options.FILE_OPTIONS, protoFileElement.options()); return new ProtoFile(protoFileElement.location(), protoFileElement.imports(), protoFileElement.publicImports(), packageName, types, services, wireExtends, options, protoFileElement.syntax()); }
static ImmutableList<TypeElement> toElements(ImmutableList<Type> types) { ImmutableList.Builder<TypeElement> elements = new ImmutableList.Builder<>(); for (Type type : types) { elements.add(Type.toElement(type)); } return elements.build(); } }
private String resolveContext() { for (int i = contextStack.size() - 1; i >= 0; i--) { Object context = contextStack.get(i); if (context instanceof Type) { return ((Type) context).type().toString(); } else if (context instanceof ProtoFile) { String packageName = ((ProtoFile) context).packageName(); return packageName != null ? packageName : ""; } else if (context instanceof Field && ((Field) context).isExtension()) { String packageName = ((Field) context).packageName(); return packageName != null ? packageName : ""; } } throw new IllegalStateException(); }
Linker linker = withContext(protoFile); for (Type type : protoFile.types()) { type.link(linker); protoFile.linkOptions(linker); for (Type type : protoFile.types()) { type.linkOptions(linker); Linker linker = withContext(protoFile); for (Type type : protoFile.types()) { type.validate(linker);
/** Returns a new proto file that omits types and services not in {@code identifiers}. */ ProtoFile retainAll(Schema schema, MarkSet markSet) { ImmutableList.Builder<Type> retainedTypes = ImmutableList.builder(); for (Type type : types) { Type retainedType = type.retainAll(schema, markSet); if (retainedType != null) { retainedTypes.add(retainedType); } } ImmutableList.Builder<Service> retainedServices = ImmutableList.builder(); for (Service service : services) { Service retainedService = service.retainAll(schema, markSet); if (retainedService != null) { retainedServices.add(retainedService); } } ProtoFile result = new ProtoFile(location, imports, publicImports, packageName, retainedTypes.build(), retainedServices.build(), extendList, options.retainAll(schema, markSet), syntax); result.javaPackage = javaPackage; return result; }
void linkOptions(Linker linker) { linker = linker.withContext(this); for (Type type : nestedTypes) { type.linkOptions(linker); } for (Field field : declaredFields) { field.linkOptions(linker); } for (Field field : extensionFields) { field.linkOptions(linker); } for (OneOf oneOf : oneOfs) { oneOf.linkOptions(linker); } options.link(linker); }
void link(Linker linker) { linker = linker.withContext(this); for (Field field : declaredFields) { field.link(linker); } for (Field field : extensionFields) { field.link(linker); } for (OneOf oneOf : oneOfs) { oneOf.link(linker); } for (Type type : nestedTypes) { type.link(linker); } }
static ProtoFile get(ProtoFileElement protoFileElement) { String packageName = protoFileElement.packageName(); ImmutableList<Type> types = Type.fromElements(packageName, protoFileElement.types()); ImmutableList<Service> services = Service.fromElements(packageName, protoFileElement.services()); ImmutableList<Extend> wireExtends = Extend.fromElements(packageName, protoFileElement.extendDeclarations()); Options options = new Options(Options.FILE_OPTIONS, protoFileElement.options()); return new ProtoFile(protoFileElement.location(), protoFileElement.imports(), protoFileElement.publicImports(), packageName, types, services, wireExtends, options, protoFileElement.syntax()); }
static ImmutableList<TypeElement> toElements(ImmutableList<Type> types) { ImmutableList.Builder<TypeElement> elements = new ImmutableList.Builder<>(); for (Type type : types) { elements.add(Type.toElement(type)); } return elements.build(); } }