private List<ServiceContext> findServices(List<FileDescriptorProto> protos, ProtoTypeMap typeMap) { List<ServiceContext> contexts = new ArrayList<>(); protos.forEach(fileProto -> { List<Location> locations = fileProto.getSourceCodeInfo().getLocationList(); locations.stream() .filter(location -> location.getPathCount() == 2 && location.getPath(0) == FileDescriptorProto.SERVICE_FIELD_NUMBER) .forEach(location -> { int serviceNumber = location.getPath(1); ServiceContext serviceContext = buildServiceContext(fileProto.getService(serviceNumber), typeMap, locations, serviceNumber); serviceContext.javaDoc = getJavaDoc(getComments(location), getServiceJavaDocPrefix()); serviceContext.protoName = fileProto.getName(); serviceContext.packageName = extractPackageName(fileProto); contexts.add(serviceContext); }); }); return contexts; }
private ServiceContext buildServiceContext(ServiceDescriptorProto serviceProto, ProtoTypeMap typeMap, List<Location> locations, int serviceNumber) { ServiceContext serviceContext = new ServiceContext(); serviceContext.fileName = getClassPrefix() + serviceProto.getName() + "Grpc.java"; serviceContext.className = getClassPrefix() + serviceProto.getName() + "Grpc"; serviceContext.serviceName = serviceProto.getName(); serviceContext.deprecated = serviceProto.getOptions() != null && serviceProto.getOptions().getDeprecated(); locations.stream() .filter(location -> location.getPathCount() == METHOD_NUMBER_OF_PATHS && location.getPath(0) == FileDescriptorProto.SERVICE_FIELD_NUMBER && location.getPath(1) == serviceNumber && location.getPath(2) == ServiceDescriptorProto.METHOD_FIELD_NUMBER) .forEach(location -> { int methodNumber = location.getPath(METHOD_NUMBER_OF_PATHS - 1); MethodContext methodContext = buildMethodContext(serviceProto.getMethod(methodNumber), typeMap); methodContext.methodNumber = methodNumber; methodContext.javaDoc = getJavaDoc(getComments(location), getMethodJavaDocPrefix()); serviceContext.methods.add(methodContext); }); return serviceContext; }
public void generateFile(String protoPath) { try { if (pojoTypes == null) { pojoTypes = Maps.newHashMap(); } } finally { if (!new File(protoPath).exists()) { logger.warn("protoPath:" + protoPath + " not exist, it may be in the third party jars, so it can't be generate"); return; } FileDescriptorSet fileDescriptorSet = commondProtoc.invoke(protoPath); for (FileDescriptorProto fdp : fileDescriptorSet.getFileList()) { Pair<String, String> packageClassName = this.packageClassName(fdp.getOptions()); if (packageClassName == null) { continue; } ProtocolStringList dependencyList = fdp.getDependencyList(); for (Iterator<String> it = dependencyList.iterator(); it.hasNext();) { String dependencyPath = discoveryRoot + "/" + it.next(); generateFile(dependencyPath); } doPrint(fdp, packageClassName.getLeft(), packageClassName.getRight()); } } }
/** Creates a resolver which searches the supplied {@link FileDescriptorSet}. */ public static ServiceResolver fromFileDescriptorSet(FileDescriptorSet descriptorSet) { ImmutableMap<String, FileDescriptorProto> descriptorProtoIndex = computeDescriptorProtoIndex(descriptorSet); Map<String, FileDescriptor> descriptorCache = new HashMap<>(); ImmutableList.Builder<FileDescriptor> result = ImmutableList.builder(); for (FileDescriptorProto descriptorProto : descriptorSet.getFileList()) { try { result.add(descriptorFromProto(descriptorProto, descriptorProtoIndex, descriptorCache)); } catch (DescriptorValidationException e) { logger.warn("Skipped descriptor " + descriptorProto.getName() + " due to error", e); continue; } } return new ServiceResolver(result.build()); }
/** * Recursively constructs file descriptors for all dependencies of the supplied proto and returns * a {@link FileDescriptor} for the supplied proto itself. For maximal efficiency, reuse the * descriptorCache argument across calls. */ private static FileDescriptor descriptorFromProto( FileDescriptorProto descriptorProto, ImmutableMap<String, FileDescriptorProto> descriptorProtoIndex, Map<String, FileDescriptor> descriptorCache) throws DescriptorValidationException { // First, check the cache. String descritorName = descriptorProto.getName(); if (descriptorCache.containsKey(descritorName)) { return descriptorCache.get(descritorName); } // Then, fetch all the required dependencies recursively. ImmutableList.Builder<FileDescriptor> dependencies = ImmutableList.builder(); for (String dependencyName : descriptorProto.getDependencyList()) { if (!descriptorProtoIndex.containsKey(dependencyName)) { throw new IllegalArgumentException("Could not find dependency: " + dependencyName); } FileDescriptorProto dependencyProto = descriptorProtoIndex.get(dependencyName); dependencies.add(descriptorFromProto(dependencyProto, descriptorProtoIndex, descriptorCache)); } // Finally, construct the actual descriptor. FileDescriptor[] empty = new FileDescriptor[0]; return FileDescriptor.buildFrom(descriptorProto, dependencies.build().toArray(empty)); }
private void processDependencies(FileDescriptorProto fileDescriptor) { logger.debug("Processing deps of descriptor: " + fileDescriptor.getName()); fileDescriptor.getDependencyList().forEach(dep -> { if (!resolvedDescriptors.containsKey(dep) && !requestedDescriptors.contains(dep)) { requestedDescriptors.add(dep); ++outstandingRequests; requestStream.onNext(requestForDescriptor(dep)); } }); --outstandingRequests; if (outstandingRequests == 0) { logger.debug("Retrieved service definition for [{}] by reflection", serviceName); resultFuture.set(FileDescriptorSet.newBuilder() .addAllFile(resolvedDescriptors.values()) .build()); requestStream.onCompleted(); } }
private void loadService() { LOG.info("Load service definition is starting..."); InputStream in = null; FileDescriptorSet descriptorSet; try { in = ClassHelper.getClassLoader().getResourceAsStream(GrpcConstants.PROTO_DESC_FILENAME); descriptorSet = FileDescriptorSet.parseFrom(in); for (FileDescriptorProto fdp : descriptorSet.getFileList()) { FileDescriptor fd = FileDescriptor.buildFrom(fdp, new FileDescriptor[] {}, true); for (com.google.protobuf.Descriptors.ServiceDescriptor service : fd.getServices()) { addServiceDenifition(service.getName(), fd.getOptions().getJavaPackage() + '.' + service.getFullName()); } } LOG.info("Load service denifition is finished, total {} service are found.", services.size()); } catch (Exception ex) { LOG.error("Load service denifition error happened.", ex); throw new RuntimeException(ex); } finally { IOUtils.closeInputStream(in); } }
private FileDescriptorProto generateFile(String name, FileContents contents) { FileDescriptorProto.Builder fileBuilder = FileDescriptorProto.newBuilder(); fileBuilder.setName(name); if (!Strings.isNullOrEmpty(contents.packageName)) { fileBuilder.setPackage(contents.packageName); } for (Api api : contents.apis) { fileBuilder.addService(generateApi(api)); } for (Type type : contents.types.values()) { fileBuilder.addMessageType(generateType(type, contents)); } for (Enum e : contents.enums) { fileBuilder.addEnumType(generateEnum(e)); } if (imports.containsKey(name)) { for (String imported : imports.get(name)) { fileBuilder.addDependency(imported); } } return fileBuilder.build(); }
private void add(FileDescriptorProto file) { currentFile = file; fullNameSegments.push(file.getPackage()); locationMap = buildLocationMap(file); pathSegments.push(FileDescriptorProto.EXTENSION_FIELD_NUMBER); add(file.getExtensionList()); pathSegments.pop(); pathSegments.push(FileDescriptorProto.MESSAGE_TYPE_FIELD_NUMBER); for (int i = 0; i < file.getMessageTypeCount(); i++) { pathSegments.push(i); add(file.getMessageType(i)); pathSegments.pop(); } pathSegments.pop(); fullNameSegments.pop(); }
/** * Construct a {@code FileDescriptor}. * * @param proto The protocol message form of the FileDescriptor. * @param dependencies {@code FileDescriptor}s corresponding to all of * the file's dependencies. * @param allowUnknownDependencies If true, non-exist dependenncies will be * ignored and undefined message types will be replaced with a * placeholder type. * @throws DescriptorValidationException {@code proto} is not a valid * descriptor. This can occur for a number of reasons, e.g. * because a field has an undefined type or because two messages * were defined with the same name. */ public static FileDescriptor buildFrom( final FileDescriptorProto proto, final FileDescriptor[] dependencies, final boolean allowUnknownDependencies) throws DescriptorValidationException { // Building descriptors involves two steps: translating and linking. // In the translation step (implemented by FileDescriptor's // constructor), we build an object tree mirroring the // FileDescriptorProto's tree and put all of the descriptors into the // DescriptorPool's lookup tables. In the linking step, we look up all // type references in the DescriptorPool, so that, for example, a // FieldDescriptor for an embedded message contains a pointer directly // to the Descriptor for that message's type. We also detect undefined // types in the linking step. final DescriptorPool pool = new DescriptorPool( dependencies, allowUnknownDependencies); final FileDescriptor result = new FileDescriptor( proto, dependencies, pool, allowUnknownDependencies); result.crossLink(); return result; }
/** * Create a placeholder FileDescriptor for a message Descriptor. */ FileDescriptor(String packageName, Descriptor message) throws DescriptorValidationException { this.pool = new DescriptorPool(new FileDescriptor[0], true); this.proto = FileDescriptorProto.newBuilder() .setName(message.getFullName() + ".placeholder.proto") .setPackage(packageName).addMessageType(message.toProto()).build(); this.dependencies = new FileDescriptor[0]; this.publicDependencies = new FileDescriptor[0]; messageTypes = new Descriptor[] {message}; enumTypes = new EnumDescriptor[0]; services = new ServiceDescriptor[0]; extensions = new FieldDescriptor[0]; pool.addPackage(packageName, this); pool.addSymbol(message); }
/** * Replace our {@link FileDescriptorProto} with the given one, which is * identical except that it might contain extensions that weren't present * in the original. This method is needed for bootstrapping when a file * defines custom options. The options may be defined in the file itself, * so we can't actually parse them until we've constructed the descriptors, * but to construct the descriptors we have to have parsed the descriptor * protos. So, we have to parse the descriptor protos a second time after * constructing the descriptors. */ private void setProto(final FileDescriptorProto proto) { this.proto = proto; for (int i = 0; i < messageTypes.length; i++) { messageTypes[i].setProto(proto.getMessageType(i)); } for (int i = 0; i < enumTypes.length; i++) { enumTypes[i].setProto(proto.getEnumType(i)); } for (int i = 0; i < services.length; i++) { services[i].setProto(proto.getService(i)); } for (int i = 0; i < extensions.length; i++) { extensions[i].setProto(proto.getExtension(i)); } }
public void decompile(FileDescriptorProto fileDescriptor) throws IOException { if (fileDescriptor.hasPackage()) { indentedFormat("package %s;", fileDescriptor.getPackage()); absolutePackage = "." + fileDescriptor.getPackage() + "."; } for (String dependency : fileDescriptor.getDependencyList()) { indentedFormat("import \"%s\";", dependency); } if (fileDescriptor.hasOptions()) { decompileOptions(fileDescriptor.getOptions()); } decompileMembers(fileDescriptor.getEnumTypeList(), fileDescriptor.getMessageTypeList(), Collections.<FieldDescriptorProto>emptyList(), Collections.<DescriptorProto.ExtensionRange>emptyList(), fileDescriptor.getExtensionList()); for (ServiceDescriptorProto serviceDescriptor : fileDescriptor.getServiceList()) { decompile(serviceDescriptor); } newline(); flush(); }
public void testDependencyOrder() throws Exception { FileDescriptorProto fooProto = FileDescriptorProto.newBuilder() .setName("foo.proto").build(); FileDescriptorProto barProto = FileDescriptorProto.newBuilder() .setName("bar.proto") .addDependency("foo.proto") .build(); FileDescriptorProto bazProto = FileDescriptorProto.newBuilder() .setName("baz.proto") .addDependency("foo.proto") .addDependency("bar.proto") .addPublicDependency(0) .addPublicDependency(1) .build(); FileDescriptor fooFile = Descriptors.FileDescriptor.buildFrom(fooProto, new FileDescriptor[0]); FileDescriptor barFile = Descriptors.FileDescriptor.buildFrom(barProto, new FileDescriptor[] {fooFile}); // Items in the FileDescriptor array can be in any order. Descriptors.FileDescriptor.buildFrom(bazProto, new FileDescriptor[] {fooFile, barFile}); Descriptors.FileDescriptor.buildFrom(bazProto, new FileDescriptor[] {barFile, fooFile}); }
public void testInvalidPublicDependency() throws Exception { FileDescriptorProto fooProto = FileDescriptorProto.newBuilder() .setName("foo.proto").build(); FileDescriptorProto barProto = FileDescriptorProto.newBuilder() .setName("boo.proto") .addDependency("foo.proto") .addPublicDependency(1) // Error, should be 0. .build(); FileDescriptor fooFile = Descriptors.FileDescriptor.buildFrom(fooProto, new FileDescriptor[0]); try { Descriptors.FileDescriptor.buildFrom(barProto, new FileDescriptor[] {fooFile}); fail("DescriptorValidationException expected"); } catch (DescriptorValidationException e) { assertTrue( e.getMessage().indexOf("Invalid public dependency index.") != -1); } }
public void testPackedEnumField() throws Exception { FileDescriptorProto fileDescriptorProto = FileDescriptorProto.newBuilder() .setName("foo.proto") .addEnumType(EnumDescriptorProto.newBuilder() .setName("Enum") .addValue(EnumValueDescriptorProto.newBuilder() .setName("FOO") .setNumber(1) .build()) .build()) .addMessageType(DescriptorProto.newBuilder() .setName("Message") .addField(FieldDescriptorProto.newBuilder() .setName("foo") .setTypeName("Enum") .setNumber(1) .setLabel(FieldDescriptorProto.Label.LABEL_REPEATED) .setOptions(DescriptorProtos.FieldOptions.newBuilder() .setPacked(true) .build()) .build()) .build()) .build(); Descriptors.FileDescriptor.buildFrom( fileDescriptorProto, new FileDescriptor[0]); }
private static Optional<String> getFullName(FileDescriptorProto descriptor, List<Integer> path) { String fullNameSoFar = descriptor.getPackage(); switch (path.get(0)) { case FileDescriptorProto.MESSAGE_TYPE_FIELD_NUMBER: DescriptorProto message = descriptor.getMessageType(path.get(1)); return appendMessageToFullName(message, path, fullNameSoFar); case FileDescriptorProto.ENUM_TYPE_FIELD_NUMBER: EnumDescriptorProto enumDescriptor = descriptor.getEnumType(path.get(1)); return Optional.of(appendEnumToFullName(enumDescriptor, path, fullNameSoFar)); case FileDescriptorProto.SERVICE_FIELD_NUMBER: ServiceDescriptorProto serviceDescriptor = descriptor.getService(path.get(1)); fullNameSoFar = appendNameComponent(fullNameSoFar, serviceDescriptor.getName()); if (path.size() > 2) { fullNameSoFar = appendFieldComponent( fullNameSoFar, serviceDescriptor.getMethod(path.get(3)).getName()); } return Optional.of(fullNameSoFar); default: return Optional.empty(); } }
/** * Construct a {@code FileDescriptor}. * * @param proto The protocol message form of the FileDescriptor. * @param dependencies {@code FileDescriptor}s corresponding to all of * the file's dependencies. * @param allowUnknownDependencies If true, non-exist dependenncies will * be ignored and undefined message types will be replaced with a * placeholder type. * * @throws DescriptorValidationException {@code proto} is not a valid * descriptor. This can occur for a number of reasons, e.g. because a * field has an undefined type or because two messages were defined with * the same name. */ private static FileDescriptor buildFrom ( final FileDescriptorProto proto, final FileDescriptor[] dependencies, final boolean allowUnknownDependencies) throws DescriptorValidationException { // Building descriptors involves two steps: translating and linking. // In the translation step (implemented by FileDescriptor's // constructor), we build an object tree mirroring the // FileDescriptorProto's tree and put all of the descriptors into the // DescriptorPool's lookup tables. In the linking step, we look up all // type references in the DescriptorPool, so that, for example, a // FieldDescriptor for an embedded message contains a pointer directly // to the Descriptor for that message's type. We also detect undefined // types in the linking step. final DescriptorPool pool = new DescriptorPool ( dependencies, allowUnknownDependencies); final FileDescriptor result = new FileDescriptor ( proto, dependencies, pool, allowUnknownDependencies); result.crossLink (); return result; }
/** * Create a placeholder FileDescriptor for a message Descriptor. */ FileDescriptor (String packageName, Descriptor message) throws DescriptorValidationException { this.pool = new DescriptorPool (new FileDescriptor[0], true); this.proto = FileDescriptorProto.newBuilder () .setName (message.getFullName () + ".placeholder.proto") .setPackage (packageName).addMessageType (message.toProto ()).build (); this.dependencies = new FileDescriptor[0]; this.publicDependencies = new FileDescriptor[0]; messageTypes = new Descriptor[] {message}; enumTypes = new EnumDescriptor[0]; services = new ServiceDescriptor[0]; extensions = new FieldDescriptor[0]; pool.addPackage (packageName, this); pool.addSymbol (message); }
/** * Replace our {@link FileDescriptorProto} with the given one, which is * identical except that it might contain extensions that weren't * present in the original. This method is needed for bootstrapping when * a file defines custom options. The options may be defined in the file * itself, so we can't actually parse them until we've constructed the * descriptors, but to construct the descriptors we have to have parsed * the descriptor protos. So, we have to parse the descriptor protos a * second time after constructing the descriptors. */ private void setProto (final FileDescriptorProto proto) { this.proto = proto; for (int i = 0; i < messageTypes.length; i++) { messageTypes[i].setProto (proto.getMessageType (i)); } for (int i = 0; i < enumTypes.length; i++) { enumTypes[i].setProto (proto.getEnumType (i)); } for (int i = 0; i < services.length; i++) { services[i].setProto (proto.getService (i)); } for (int i = 0; i < extensions.length; i++) { extensions[i].setProto (proto.getExtension (i)); } }
@Override public void exitPublicImport(final PublicImportContext ctx) { super.exitPublicImport(ctx); // locationBuilder.addLocation() // .setAllSpan(ctx.Public()) // .addPath(FileDescriptorProto.PUBLIC_DEPENDENCY_FIELD_NUMBER) // .addPath(fileBuilder.getPublicDependencyCount() - 1) // // .addLocation() // .comments(ctx) // .setAllSpan(ctx.importPath()) // .addPath(FileDescriptorProto.DEPENDENCY_FIELD_NUMBER) // .addPath(fileBuilder.getDependencyCount() - 1); locationBuilder.addLocationForPrimitive(FileDescriptorProto.PUBLIC_DEPENDENCY_FIELD_NUMBER) .setAllSpan((TerminalNode) ctx.getChild(1)) .addLocationForPrimitive(FileDescriptorProto.DEPENDENCY_FIELD_NUMBER).comments(ctx) .setAllSpan(ctx.importPath()); }
private FileDescriptorProto makeCanonicalProto(final FileDescriptor fileDescriptor) { final FileDescriptorProto.Builder protoBuilder = FileDescriptorProto.newBuilder(fileDescriptor.toProto()); for (final FieldDescriptorProto.Builder field : protoBuilder.getExtensionBuilderList()) { makeCanonicalField(field, fileDescriptor.findExtensionByName(field.getName())); } for (final DescriptorProto.Builder message : protoBuilder.getMessageTypeBuilderList()) { makeCanonicalMessage(message, fileDescriptor.findMessageTypeByName(message.getName())); } // for (EnumDescriptorProto.Builder enumProto : // protoBuilder.getEnumTypeBuilderList()) { // makeCanonicalEnum(enumProto, // fileDescriptor.findEnumTypeByName(enumProto.getName())); // } for (final ServiceDescriptorProto.Builder serviceProto : protoBuilder.getServiceBuilderList()) { makeCanonicalService(serviceProto, fileDescriptor.findServiceByName(serviceProto.getName())); } // TODO: incorporate options' tree walking into canonicalization to eliminate double walking return OptionResolver.newBuilder().setCustomOptionsAsExtensions(false) .resolveAllOptionsFor(fileDescriptor, protoBuilder).build(); }
private ParsedContext(final FileDescriptorProto.Builder proto, final List<FieldDescriptorProto.Builder> unresolved, final ContextLookup lookup) { this.proto = proto; if ((unresolved == null) || unresolved.isEmpty()) { this.unresolved = Collections.emptyList(); } else { if (lookup == null) { throw new NullPointerException(); } this.unresolved = new ArrayList<Map.Entry<FieldDescriptorProto.Builder, FieldContext>>(); for (final FieldDescriptorProto.Builder field : unresolved) { final FieldContext context = (FieldContext) lookup.getContext(field, false); if (context == null) { throw new IllegalStateException("field context must not be null"); } this.unresolved.add(new SimpleEntry<FieldDescriptorProto.Builder, FieldContext>(field, context)); } } }
private void buildAllOptions(final FileDescriptorProto.Builder proto) { if (!buildOptions(proto.getOptionsBuilder())) { proto.clearOptions(); } for (final FieldDescriptorProto.Builder extensionProto : proto.getExtensionBuilderList()) { if (!buildOptions(extensionProto.getOptionsBuilder())) { extensionProto.clearOptions(); } } for (final EnumDescriptorProto.Builder enumProto : proto.getEnumTypeBuilderList()) { buildAllOptions(enumProto); } for (final ServiceDescriptorProto.Builder serviceProto : proto.getServiceBuilderList()) { buildAllOptions(serviceProto); } for (final DescriptorProto.Builder messageProto : proto.getMessageTypeBuilderList()) { buildAllOptions(messageProto); } }
private FileDescriptor getDeepCanonicalFileDescriptor(final FileDescriptor file, final boolean forceRebuild) throws DescriptorValidationException { if (!forceRebuild && isDeeplyCanonical(file)) { return file; } final FileDescriptor[] dependencies = new FileDescriptor[file.getDependencies().size()]; int i = 0; for (final FileDescriptor dependency : file.getDependencies()) { dependencies[i++] = getDeepCanonicalFileDescriptor(dependency, forceRebuild); } final FileDescriptorProto proto = isCanonical(file) ? file.toProto() : makeCanonicalProto(file); return buildFileDescriptorWithReserializedProto(proto, dependencies); }
private FileDescriptorProto makeCanonicalProto(final FileDescriptor file) { final FileDescriptorProto.Builder protoBuilder = FileDescriptorProto.newBuilder(file.toProto()); for (final FieldDescriptorProto.Builder field : protoBuilder.getExtensionBuilderList()) { makeCanonicalField(field, file.findExtensionByName(field.getName())); } for (final DescriptorProto.Builder message : protoBuilder.getMessageTypeBuilderList()) { makeCanonicalMessage(message, file.findMessageTypeByName(message.getName())); } // for (EnumDescriptorProto.Builder enumProto : // protoBuilder.getEnumTypeBuilderList()) { // makeCanonicalEnum(enumProto, // file.findEnumTypeByName(enumProto.getName())); // } for (final ServiceDescriptorProto.Builder serviceProto : protoBuilder.getServiceBuilderList()) { makeCanonicalService(serviceProto, file.findServiceByName(serviceProto.getName())); } return OptionResolver.newBuilder().setCustomOptionsAsExtensions(reparseCustomOptions) .resolveAllOptionsFor(file, protoBuilder).build(); }
@SuppressWarnings("null") public static List<FileDescriptorProto> getFileDescriptorProtos(final Pattern protoNamePattern, final boolean includeSourceInfo, final FieldTypeRefsMode fieldTypeRefsMode, final String fileDescriptorSetPath, final Class<?> baseResourceClass) { final List<FileDescriptorProto> fdProtos = getFileDescriptorProtos(protoNamePattern, fileDescriptorSetPath, baseResourceClass); if (!includeSourceInfo && (fieldTypeRefsMode != FieldTypeRefsMode.AS_IS)) { for (final ListIterator<FileDescriptorProto> iterator = fdProtos.listIterator(); iterator .hasNext();) { final FileDescriptorProto fd = iterator.next(); iterator.set(makeProtoRefsRelative(fd, fieldTypeRefsMode).build()); } } return fdProtos; }
@SuppressWarnings("null") public static FileDescriptorProto.Builder makeProtoRefsRelative(final FileDescriptorProto proto, final FieldTypeRefsMode fieldTypeRefsMode) { if (fieldTypeRefsMode == FieldTypeRefsMode.AS_IS) { return FileDescriptorProto.newBuilder(proto); } final FileDescriptorProto.Builder protoBuilder = FileDescriptorProto.newBuilder(proto); final String packagePath = "." + proto.getPackage(); for (final FieldDescriptorProto.Builder field : protoBuilder.getExtensionBuilderList()) { makeFieldRefsRelative(packagePath, field, fieldTypeRefsMode, packagePath); } for (final DescriptorProto.Builder message : protoBuilder.getMessageTypeBuilderList()) { makeMessageRefsRelative(packagePath, message, fieldTypeRefsMode, packagePath); } return protoBuilder; }
@Ignore @Test public void viewFileDescriptors() throws Exception { final FileDescriptorProto child1 = getProtocFileDescriptorProto("nonTreeChild1.proto", false, FieldTypeRefsMode.AS_IS); final FileDescriptorProto parent1 = getProtocFileDescriptorProto("nonTreeParent1.proto", false, FieldTypeRefsMode.AS_IS); final FileDescriptorProto parent2 = getProtocFileDescriptorProto("nonTreeParent2.proto", false, FieldTypeRefsMode.AS_IS); final FileDescriptor fileParent2 = FileDescriptor.buildFrom(parent2, new FileDescriptor[0]); final FileDescriptor fileChild1 = FileDescriptor.buildFrom(child1, new FileDescriptor[] {fileParent2}); final FileDescriptor fileParent1 = FileDescriptor.buildFrom(parent1, new FileDescriptor[] {fileChild1}); log.info(fileParent1.toProto().toString()); log.info(fileChild1.toProto().toString()); log.info(fileParent2.toProto().toString()); log.info("messages: {}", fileParent1.getMessageTypes()); log.info("messages: {}", fileParent1.getEnumTypes()); assertThat(fileParent1.findMessageTypeByName("Child1"), nullValue()); assertThat(fileParent1.findEnumTypeByName("Parent2"), not(nullValue())); }
@Test public void testUnresolvedNamesPostBuildFileProto() throws Exception { final FileDescriptorProto child1 = getProtocFileDescriptorProto("nonTreeChild1.proto", false, FieldTypeRefsMode.AS_IS); final FileDescriptorProto child2 = getProtocFileDescriptorProto("nonTreeChild2.proto", false, FieldTypeRefsMode.AS_IS); final FileDescriptorProto originalParent1 = getProtocFileDescriptorProto("nonTreeParent1.proto", false, FieldTypeRefsMode.AS_IS); final FileDescriptorProto parent2 = getProtocFileDescriptorProto("nonTreeParent2.proto", false, FieldTypeRefsMode.AS_IS); final Builder parentBuilder = FileDescriptorProto.newBuilder(originalParent1); parentBuilder.getMessageTypeBuilder(0).getFieldBuilder(2).clearType().setTypeName("Child2"); final FileDescriptorProto parent1 = parentBuilder.build(); final FileDescriptor fileChild2 = FileDescriptor.buildFrom(child2, new FileDescriptor[0]); final FileDescriptor fileParent2 = FileDescriptor.buildFrom(parent2, new FileDescriptor[] {fileChild2}); final FileDescriptor fileChild1 = FileDescriptor.buildFrom(child1, new FileDescriptor[] {fileParent2}); final FileDescriptor fileParent1 = FileDescriptor.buildFrom(parent1, new FileDescriptor[] {fileChild1}); log.info(fileParent1.toProto().toString()); }
public void testUnknownFieldsDenied() throws Exception { FileDescriptorProto fooProto = FileDescriptorProto.newBuilder() .setName("foo.proto") .addMessageType(DescriptorProto.newBuilder() .setName("Foo") .addField(FieldDescriptorProto.newBuilder() .setLabel(FieldDescriptorProto.Label.LABEL_OPTIONAL) .setTypeName("Bar") .setName("bar") .setNumber(1))) .build(); try { Descriptors.FileDescriptor.buildFrom(fooProto, new FileDescriptor[0]); fail("DescriptorValidationException expected"); } catch (DescriptorValidationException e) { assertTrue(e.getMessage().indexOf("Bar") != -1); assertTrue(e.getMessage().indexOf("is not defined") != -1); } }
@Test public final void testParsed() throws Exception { try { walker.walk(protoParser, tree); final FileDescriptorProto.Builder protoBuilder = (FileDescriptorProto.Builder) protoParser.getParsed().getProto(); final FileDescriptor fileDescriptor = FileDescriptor.buildFrom(protoBuilder.build(), new FileDescriptor[0]); fail(String.format("expected exception type %s with message containg '%s', but got none!", exception, exceptionExtra)); } catch (final Exception e) { log.debug("expected exception with message '{}'", e.getMessage()); assertThat(e, instanceOf(exception)); assertThat(e.getMessage(), containsString(exceptionExtra)); } }
@Test public void testTextFormatWithDescriptor() throws Exception { //TestUtil.getAllSet(); String allSet = TextFormat.printToString(TestUtil.getAllSet()); final DescriptorProto expectedAllSetProto = TestAllTypes.getDescriptor().toProto(); String allSetProto = TextFormat.printToString(expectedAllSetProto); log.debug("the message: {}", allSet); log.debug("the proto: {}", allSetProto); DynamicMessage.Builder builder = DynamicMessage.newBuilder(DescriptorProto.getDescriptor()); TextFormat.merge(allSetProto, builder); Message actualAllSetProto = builder.build(); assertThat(actualAllSetProto).isEqualTo(expectedAllSetProto); FieldDescriptor field = FileDescriptorProto.getDescriptor() .findFieldByNumber(FileDescriptorProto.MESSAGE_TYPE_FIELD_NUMBER); FileDescriptorProto fileProto = FileDescriptorProto.newBuilder().setName("my file1") .addRepeatedField(field, actualAllSetProto) .build(); FileDescriptor fileDescriptor = FileDescriptor.buildFrom(fileProto, new FileDescriptor[0]); Descriptor actualAllTypesDescriptor = fileDescriptor.findMessageTypeByName( TestAllTypes.getDescriptor().getFullName()); assertThat(actualAllTypesDescriptor, equalTo(TestAllTypes.getDescriptor())); }
private FileDescriptorProto.Builder addDeepMessageTypeToFile( FileDescriptorProto.Builder builder, Message descriptorProto) { //TODO delete this method!!! final FieldDescriptor field = FileDescriptorProto.getDescriptor() .findFieldByNumber(FileDescriptorProto.MESSAGE_TYPE_FIELD_NUMBER); final FieldDescriptor nestedTypeField = DescriptorProto.getDescriptor().findFieldByNumber( DescriptorProto.NESTED_TYPE_FIELD_NUMBER); builder.addRepeatedField(field, descriptorProto); for (Message nestedMessageType : (List<Message>) descriptorProto.getField(nestedTypeField)) { // builder.addRepeatedField(field, nestedMessageType); addDeepMessageTypeToFile(builder, nestedMessageType); } return builder; }
private FileDescriptorSet.Builder addDescriptorToFileSet(FileDescriptorSet.Builder builder, Descriptor descriptor, Set<FileDescriptorProto> fileProtoSet) { List<? extends FileDescriptorProtoOrBuilder> fileList = builder.getFileOrBuilderList(); final FileDescriptor file = descriptor.getFile(); FileDescriptorProto proto = file.toProto(); if (fileList.contains(proto)) { return builder; } builder.addFile(proto); for (FileDescriptor dependency : file.getDependencies()) { proto = dependency.toProto(); if (!fileList.contains(proto)) { builder.addFile(proto); } } return builder; }
private void assertReserializationInvariant(final FileDescriptor fileWithUnknownFieldsProto, final ExtensionRegistry registry) throws InvalidProtocolBufferException { final FileDescriptorProto expectedProtoWithUnknownFields = fileWithUnknownFieldsProto.toProto(); final FileDescriptorProto actualProtoWithExtensions = FileDescriptorProto.parseFrom(expectedProtoWithUnknownFields.toByteString(), registry); final FileDescriptorProto actualProtoWithUnknownFields = FileDescriptorProto.parseFrom(actualProtoWithExtensions.toByteString()); softly .assertThat(actualProtoWithUnknownFields.toString()) .as("check reserialization invariant textual equality for %s", fileWithUnknownFieldsProto.getName()) .isEqualTo(expectedProtoWithUnknownFields.toString()); softly.assertThat(actualProtoWithUnknownFields) .as("check reserialization invariant for %s", fileWithUnknownFieldsProto.getName()) .isEqualTo(expectedProtoWithUnknownFields); }