private Context extractServiceContext( ProtoTypeMap protoTypeMap, DescriptorProtos.ServiceDescriptorProto serviceProto) { Context ctx = new Context(); ctx.fileName = serviceProto.getName() + CLASS_SUFFIX + ".java"; ctx.className = serviceProto.getName() + CLASS_SUFFIX; ctx.serviceName = serviceProto.getName(); ctx.deprecated = serviceProto.getOptions() != null && serviceProto.getOptions().getDeprecated(); // Identify methods to generate a CompletableFuture-based client for. // Only unary methods are supported. serviceProto.getMethodList().stream() .filter(method -> !method.getClientStreaming() && !method.getServerStreaming()) .forEach(method -> { ContextMethod ctxMethod = new ContextMethod(); ctxMethod.methodName = lowerCaseFirst(method.getName()); ctxMethod.inputType = protoTypeMap.toJavaTypeName(method.getInputType()); ctxMethod.outputType = protoTypeMap.toJavaTypeName(method.getOutputType()); ctxMethod.deprecated = method.getOptions() != null && method.getOptions().getDeprecated(); ctx.methods.add(ctxMethod); }); return ctx; }
public ProtoLocation( DescriptorProtos.SourceCodeInfo.Location location, final ProtoElement element) { // Spit out "?:?" for line:column if there's no "span" set in the location. This can happen // when (for example) a proto transform tool synthesizes a field that doesn't appear in the // source *.proto files. if (location.getSpanCount() > 0) { this.displayString = String.format( "%s:%d:%d", element.getFile().getLocation().getDisplayString(), location.getSpan(0) + 1, location.getSpan(1) + 1); } else { this.displayString = String.format("%s:?:?", element.getFile().getLocation().getDisplayString()); } this.element = element; }
private void add(List<FieldDescriptorProto> extensions) { for (int i = 0; i < extensions.size(); i++) { pathSegments.push(i); FieldDescriptorProto extensionProto = extensions.get(i); String extendee = resolve(extensionProto.getExtendee()); Multimap<String, Extension> messageExtensions = builder.get(extendee); if (messageExtensions == null) { messageExtensions = ArrayListMultimap.create(); builder.put(extendee, messageExtensions); } String path = DOT_JOINER.join(pathSegments.descendingIterator()); DescriptorProtos.SourceCodeInfo.Location location = locationMap.get(path).get(0); // Since paths are only unique within a file, we need a synthetic path to make them unique, // given that paths are used to uniquely identify elements in a ProtoFile, and we're // stuffing elements from another file into it. path = currentFile.getName() + ":" + path; Location fileLocation = new SimpleLocation(String.format( "%s:%d:%d", currentFile.getName(), location.getSpan(0) + 1, location.getSpan(1) + 1)); Extension extension = new Extension(extensionProto, location, path, fileLocation); messageExtensions.put(getExtensionFieldName(extensionProto.getName()), extension); pathSegments.pop(); } }
private static DescriptorProtos.FileDescriptorProto getFileDescProtoForMsgType( String packageName, String messageType, DescriptorProtos.FileDescriptorSet set ) { DescriptorProtos.FileDescriptorProto file = null; for (DescriptorProtos.FileDescriptorProto fileDescriptorProto : set.getFileList()) { if (!packageMatch(fileDescriptorProto, packageName)) { continue; } file = containsMessageType(fileDescriptorProto, messageType); if (file != null) { break; } } return file; }
/** * Write a protobuf to a buffer 'numProtos' times, and then * read them back, making sure all data comes through correctly. */ private void doTest(int numProtos) throws IOException { Configuration conf = new Configuration(); DataOutputBuffer out = new DataOutputBuffer(); // Write numProtos protobufs to the buffer Message[] sent = new Message[numProtos]; for (int i = 0; i < numProtos; i++) { // Construct a test protocol buffer using one of the // protos that ships with the protobuf library Message testProto = DescriptorProtos.EnumValueDescriptorProto.newBuilder() .setName("test" + i).setNumber(i).build(); ObjectWritable.writeObject(out, testProto, DescriptorProtos.EnumValueDescriptorProto.class, conf); sent[i] = testProto; } // Read back the data DataInputBuffer in = new DataInputBuffer(); in.reset(out.getData(), out.getLength()); for (int i = 0; i < numProtos; i++) { Message received = (Message)ObjectWritable.readObject(in, conf); assertEquals(sent[i], received); } }
/** * Returns an instance of {@link ProtoTypeMap} based on the given FileDescriptorProto instances. * * @param fileDescriptorProtos the full collection of files descriptors from the code generator request */ public static ProtoTypeMap of(@Nonnull Collection<DescriptorProtos.FileDescriptorProto> fileDescriptorProtos) { Preconditions.checkNotNull(fileDescriptorProtos, "fileDescriptorProtos"); Preconditions.checkArgument(!fileDescriptorProtos.isEmpty(), "fileDescriptorProtos.isEmpty()"); final ImmutableMap.Builder<String, String> types = ImmutableMap.builder(); for (final DescriptorProtos.FileDescriptorProto fileDescriptor : fileDescriptorProtos) { final DescriptorProtos.FileOptions fileOptions = fileDescriptor.getOptions(); final String protoPackage = fileDescriptor.hasPackage() ? "." + fileDescriptor.getPackage() : ""; final String javaPackage = Strings.emptyToNull( fileOptions.hasJavaPackage() ? fileOptions.getJavaPackage() : fileDescriptor.getPackage()); final String enclosingClassName = fileOptions.getJavaMultipleFiles() ? null : getJavaOuterClassname(fileDescriptor, fileOptions); fileDescriptor.getEnumTypeList().forEach( e -> types.put( protoPackage + "." + e.getName(), toJavaTypeName(e.getName(), enclosingClassName, javaPackage))); fileDescriptor.getMessageTypeList().forEach( m -> types.put( protoPackage + "." + m.getName(), toJavaTypeName(m.getName(), enclosingClassName, javaPackage))); } return new ProtoTypeMap(types.build()); }
private static String getJavaOuterClassname( DescriptorProtos.FileDescriptorProto fileDescriptor, DescriptorProtos.FileOptions fileOptions) { if (fileOptions.hasJavaOuterClassname()) { return fileOptions.getJavaOuterClassname(); } // If the outer class name is not explicitly defined, then we take the proto filename, strip its extension, // and convert it from snake case to camel case. String filename = fileDescriptor.getName().substring(0, fileDescriptor.getName().length() - ".proto".length()); filename = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, filename); return appendOuterClassSuffix(filename, fileDescriptor); }
/** * In the event of a name conflict between the outer and inner type names, protoc adds an OuterClass suffix to the * outer type's name. */ private static String appendOuterClassSuffix(final String enclosingClassName, DescriptorProtos.FileDescriptorProto fd) { if (fd.getEnumTypeList().stream().anyMatch(enumProto -> enumProto.getName().equals(enclosingClassName)) || fd.getMessageTypeList().stream().anyMatch(messageProto -> messageProto.getName().equals(enclosingClassName))) { return enclosingClassName + "OuterClass"; } else { return enclosingClassName; } }
private Stream<Context> extractContext(ProtoTypeMap protoTypeMap, DescriptorProtos.FileDescriptorProto proto) { return proto.getServiceList().stream() .map(s -> extractServiceContext(protoTypeMap, s)) .map(ctx -> { ctx.packageName = extractPackageName(proto); return ctx; }) .map(ctx -> { ctx.protoName = proto.getName(); return ctx; }); }
private String extractPackageName(DescriptorProtos.FileDescriptorProto proto) { DescriptorProtos.FileOptions options = proto.getOptions(); if (options != null) { String javaPackage = options.getJavaPackage(); if (!Strings.isNullOrEmpty(javaPackage)) { return javaPackage; } } return Strings.nullToEmpty(proto.getPackage()); }
private Extension(FieldDescriptorProto proto, DescriptorProtos.SourceCodeInfo.Location location, String path, Location fileLocation) { this.proto = proto; this.location = location; this.path = path; this.fileLocation = fileLocation; }
private static ImmutableListMultimap<String, DescriptorProtos.SourceCodeInfo.Location> buildLocationMap(FileDescriptorProto file) { return Multimaps.<String, DescriptorProtos.SourceCodeInfo.Location>index( file.getSourceCodeInfo().getLocationList(), new Function<DescriptorProtos.SourceCodeInfo.Location, String>() { @Override public String apply(DescriptorProtos.SourceCodeInfo.Location location) { return DOT_JOINER.join(location.getPathList()); } }); }
private String getDocumentation(String path) { String comment = ""; DescriptorProtos.SourceCodeInfo.Location location = getSourceCodeLocation(path); if (location != null) { if (!Strings.isNullOrEmpty(location.getLeadingComments())) { comment = location.getLeadingComments(); } if (!Strings.isNullOrEmpty(location.getTrailingComments())){ comment += location.getTrailingComments(); } } return comment; }
private DescriptorProtos.SourceCodeInfo.Location getSourceCodeLocation(String path) { if (locationMap.containsKey(path)) { // We get the first location. return locationMap.get(path).get(0); } else { return null; } }
/** * Loads a Protobuf file descriptor set into an ubermap of file descriptors. * * @param set FileDescriptorSet * @param dependenciesMap FileDescriptor dependency map * @param fileDescriptorMap The populated map of FileDescriptors * @throws StageException */ public static void getAllFileDescriptors( DescriptorProtos.FileDescriptorSet set, Map<String, Set<Descriptors.FileDescriptor>> dependenciesMap, Map<String, Descriptors.FileDescriptor> fileDescriptorMap ) throws StageException { List<DescriptorProtos.FileDescriptorProto> fileList = set.getFileList(); try { for (DescriptorProtos.FileDescriptorProto fdp : fileList) { if (!fileDescriptorMap.containsKey(fdp.getName())) { Set<Descriptors.FileDescriptor> dependencies = dependenciesMap.get(fdp.getName()); if (dependencies == null) { dependencies = new LinkedHashSet<>(); dependenciesMap.put(fdp.getName(), dependencies); dependencies.addAll(getDependencies(dependenciesMap, fileDescriptorMap, fdp, set)); } Descriptors.FileDescriptor fileDescriptor = Descriptors.FileDescriptor.buildFrom( fdp, dependencies.toArray(new Descriptors.FileDescriptor[dependencies.size()]) ); fileDescriptorMap.put(fdp.getName(), fileDescriptor); } } } catch (Descriptors.DescriptorValidationException e) { throw new StageException(Errors.PROTOBUF_07, e.getDescription(), e); } }
/** * Generates a protobuf descriptor instance from a FileDescriptor set. * * @param set set of file descriptors * @param fileDescriptorMap map of message types to file descriptors * @param descriptorFile descriptor file for message to be decoded * @param qualifiedMessageType the name of the message to be decoded * @return protobuf descriptor instance * @throws StageException */ public static Descriptors.Descriptor getDescriptor( DescriptorProtos.FileDescriptorSet set, Map<String, Descriptors.FileDescriptor> fileDescriptorMap, String descriptorFile, String qualifiedMessageType ) throws StageException { // find the FileDescriptorProto which contains the message type // IF cannot find, then bail out String packageName = null; String messageType = qualifiedMessageType; int lastIndex = qualifiedMessageType.lastIndexOf('.'); if (lastIndex != -1) { packageName = qualifiedMessageType.substring(0, lastIndex); messageType = qualifiedMessageType.substring(lastIndex + 1); } DescriptorProtos.FileDescriptorProto file = getFileDescProtoForMsgType(packageName, messageType, set); if (file == null) { // could not find the message type from all the proto files contained in the descriptor file throw new StageException(Errors.PROTOBUF_00, qualifiedMessageType, descriptorFile); } // finally get the FileDescriptor for the message type Descriptors.FileDescriptor fileDescriptor = fileDescriptorMap.get(file.getName()); // create builder using the FileDescriptor // this can only find the top level message types return fileDescriptor.findMessageTypeByName(messageType); }
private static DescriptorProtos.FileDescriptorProto containsMessageType( DescriptorProtos.FileDescriptorProto fileDescriptorProto, String messageType ) { DescriptorProtos.FileDescriptorProto file = null; for (DescriptorProtos.DescriptorProto descriptorProto : getAllMessageTypesInDescriptorProto(fileDescriptorProto)) { if (messageType.equals(descriptorProto.getName())) { file = fileDescriptorProto; break; } } return file; }
private static List<DescriptorProtos.DescriptorProto> getAllMessageTypesInDescriptorProto( DescriptorProtos.FileDescriptorProto fileDescriptorProto ) { Queue<DescriptorProtos.DescriptorProto> queue = new LinkedList<>(); queue.addAll(fileDescriptorProto.getMessageTypeList()); List<DescriptorProtos.DescriptorProto> result = new ArrayList<>(); while (!queue.isEmpty()) { DescriptorProtos.DescriptorProto descriptorProto = queue.poll(); queue.addAll(descriptorProto.getNestedTypeList()); result.add(descriptorProto); } return result; }
@Before public void setUp() throws Exception { FileInputStream fin = new FileInputStream(Resources.getResource("Employee.desc").getPath()); set = DescriptorProtos.FileDescriptorSet.parseFrom(fin); ProtobufTypeUtil.getAllFileDescriptors(set, fileDescriptorDependentsMap, fileDescriptorMap); ProtobufTypeUtil.populateDefaultsAndExtensions(fileDescriptorMap, typeToExtensionMap, defaultValueMap); md = ProtobufTypeUtil.getDescriptor(set, fileDescriptorMap, "Employee.desc", "util.Employee"); extensionRegistry = ProtobufTestUtil.createExtensionRegistry(typeToExtensionMap); }
private FileDescriptorEx() { delegate = DescriptorProtos.getDescriptor(); proto = delegate.toProto(); dependencies = Collections.<FileDescriptorEx>emptyList(); deeplyCanonical = 1; hashCode = delegate.hashCode(); reparseCustomOptions = false; }
public DataVerifyPbEnum(DescriptorProtos.EnumDescriptorProto desc) { super(desc.getName()); for (DescriptorProtos.EnumValueDescriptorProto val_desc : desc.getValueList()) { all_names.put(val_desc.getName(), (long)val_desc.getNumber()); all_numbers.add((long)val_desc.getNumber()); } }
public DataVerifyPbMsg(DescriptorProtos.DescriptorProto desc) { super(desc.getName()); for (DescriptorProtos.FieldDescriptorProto fd : desc.getFieldList()) { all_names.put(fd.getName(), (long)fd.getNumber()); all_numbers.add((long)fd.getNumber()); } }
@JRubyMethod(name = "label=") public IRubyObject setLabel(ThreadContext context, IRubyObject value) { String labelName = value.asJavaString(); this.label = context.runtime.newSymbol(labelName.toLowerCase()); this.builder.setLabel( DescriptorProtos.FieldDescriptorProto.Label.valueOf("LABEL_" + labelName.toUpperCase())); return context.runtime.getNil(); }
@JRubyMethod public IRubyObject initialize(ThreadContext context) { this.symtab = new HashMap<IRubyObject, IRubyObject>(); this.cBuilder = (RubyClass) context.runtime.getClassFromPath("Google::Protobuf::Builder"); this.builder = DescriptorProtos.FileDescriptorProto.newBuilder(); return this; }
@JRubyMethod public IRubyObject initialize(ThreadContext context) { this.builder = DescriptorProtos.DescriptorProto.newBuilder(); this.fieldDefMap = new HashMap<String, RubyFieldDescriptor>(); this.oneofDefs = new HashMap<IRubyObject, RubyOneofDescriptor>(); return this; }
@Provides @Singleton SoyProtoTypeProvider provideSoyTypeProvider() throws IOException, Descriptors.DescriptorValidationException { return new SoyProtoTypeProvider.Builder() .addDescriptors( collectDescriptors( DescriptorProtos.getDescriptor(), Dossier.getDescriptor(), Expression.getDescriptor())) .build(); }
DescriptorProtos.EnumDescriptorProto exchangeProto( DescriptorProtos.EnumDescriptorProto arg);
public DescriptorProtos.SourceCodeInfo.Location getLocation() { return location; }