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; }
private ServiceDescriptor(final ServiceDescriptorProto proto, final FileDescriptor file, final int index) throws DescriptorValidationException { this.index = index; this.proto = proto; fullName = computeFullName(file, null, proto.getName()); this.file = file; methods = new MethodDescriptor[proto.getMethodCount()]; for (int i = 0; i < proto.getMethodCount(); i++) { methods[i] = new MethodDescriptor( proto.getMethod(i), file, this, i); } file.pool.addSymbol(this); }
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(); }
protected void decompile(ServiceDescriptorProto serviceDescriptor) throws IOException { indentedFormat("service %s {", serviceDescriptor.getName()); indent++; if (serviceDescriptor.hasOptions()) { decompileOptions(serviceDescriptor.getOptions()); } for (MethodDescriptorProto methodDescriptor : serviceDescriptor.getMethodList()) { indentedFormat("rpc %s (%s) returns (%s)", methodDescriptor.getName(), methodDescriptor.getInputType(), methodDescriptor.getOutputType()); if (methodDescriptor.hasOptions()) { write("{ "); indent++; decompileOptions(methodDescriptor.getOptions()); indent--; indentedFormat("}"); } else { write(";"); } } indent--; indentedFormat("}"); }
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(); } }
private ServiceDescriptor (final ServiceDescriptorProto proto, final FileDescriptor file, final int index) throws DescriptorValidationException { this.index = index; this.proto = proto; fullName = computeFullName (file, null, proto.getName ()); this.file = file; methods = new MethodDescriptor[proto.getMethodCount ()]; for (int i = 0; i < proto.getMethodCount (); i++) { methods[i] = new MethodDescriptor ( proto.getMethod (i), file, this, i); } file.pool.addSymbol (this); }
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 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 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(); }
/** Checks whether any generated classes conflict with the given name. */ private static boolean hasConflictingClassName(FileDescriptorProto file, String name) { for (EnumDescriptorProto enumDesc : file.getEnumTypeList()) { if (name.equals(enumDesc.getName())) { return true; } } for (ServiceDescriptorProto serviceDesc : file.getServiceList()) { if (name.equals(serviceDesc.getName())) { return true; } } for (DescriptorProto messageDesc : file.getMessageTypeList()) { if (hasConflictingClassName(messageDesc, name)) { return true; } } return false; }
public void handle(FileDescriptorProto protoFile) throws IOException { String javaPackage = inferJavaPackage(protoFile); boolean multipleFiles = protoFile.getOptions().getJavaMultipleFiles(); String outerClassName = null; if (!multipleFiles) { if (protoFile.getOptions().hasJavaOuterClassname()) { outerClassName = protoFile.getOptions().getJavaOuterClassname(); } else { outerClassName = inferOuterClassName(protoFile); } } ProtoServiceHandler serviceHandler = new ProtoServiceHandler(javaPackage, types, multipleFiles, outerClassName, protoFile.getPackage(), output); for (ServiceDescriptorProto service : protoFile.getServiceList()) { serviceHandler.handle(service); } }
private void doPrint(FileDescriptorProto fdp, String javaPackage, String outerClassName) { List<DescriptorProto> messageDescList = Lists.newArrayList(fdp.getMessageTypeList()); List<ServiceDescriptorProto> serviceDescList = Lists.newArrayList(fdp.getServiceList()); List<EnumDescriptorProto> enumDescList = Lists.newArrayList(fdp.getEnumTypeList()); messageDescList.stream().filter(temp -> temp.getEnumTypeList() != null) .forEach(temp -> enumDescList.addAll(temp.getEnumTypeList())); printEnum(enumDescList, javaPackage, outerClassName); printMessage(messageDescList, javaPackage, outerClassName); printService(serviceDescList, javaPackage); }
private void printService(List<ServiceDescriptorProto> serviceDescList, String javaPackage) { for (ServiceDescriptorProto serviceDesc : serviceDescList) { PrintServiceFile serviceFile = new PrintServiceFile(generatePath, javaPackage, serviceDesc.getName()); try { serviceFile.setServiceMethods(serviceDesc.getMethodList()); serviceFile.setPojoTypeCache(pojoTypes); } finally { serviceFile.print(); } } }
private ServiceDescriptorProto generateApi(Api api) { ServiceDescriptorProto.Builder builder = ServiceDescriptorProto.newBuilder(); builder.setName(getSimpleName(api.getName())); for (Method method : api.getMethodsList()) { builder.addMethod(generateMethod(method)); } if (!api.getOptionsList().isEmpty()) { builder.setOptions(generateServiceOptions(api)); } return builder.build(); }
private Interface(ProtoFile parent, ServiceDescriptorProto proto, String path) { super(parent, proto.getName(), path); this.proto = proto; // Build methods. ImmutableList.Builder<Method> methodsBuilder = ImmutableList.builder(); List<MethodDescriptorProto> methodProtos = proto.getMethodList(); for (int i = 0; i < methodProtos.size(); i++) { String childPath = buildPath(path, ServiceDescriptorProto.METHOD_FIELD_NUMBER, i); methodsBuilder.add(Method.create(this, methodProtos.get(i), childPath)); } methods = methodsBuilder.build(); }
@Accepts protected void accept(ServiceDescriptorProto.Builder service) { pushParent(BuilderVisitorNodeInfo.create(service, currentFile)); visitRepeated(ServiceDescriptorProto.METHOD_FIELD_NUMBER); visit(service.getOptionsBuilder()); popExpectedParent(service); }
/** See {@link FileDescriptor#setProto}. */ private void setProto(final ServiceDescriptorProto proto) { this.proto = proto; for (int i = 0; i < methods.length; i++) { methods[i].setProto(proto.getMethod(i)); } }
/** * See {@link FileDescriptor#setProto}. */ private void setProto (final ServiceDescriptorProto proto) { this.proto = proto; for (int i = 0; i < methods.length; i++) { methods[i].setProto (proto.getMethod (i)); } }
private void makeCanonicalService(final ServiceDescriptorProto.Builder service, final ServiceDescriptor serviceDescriptor) { for (final MethodDescriptorProto.Builder method : service.getMethodBuilderList()) { final MethodDescriptor methodDescriptor = serviceDescriptor.findMethodByName(method.getName()); method.setInputType(ensureLeadingDot(methodDescriptor.getInputType().getFullName())); method.setOutputType(ensureLeadingDot(methodDescriptor.getOutputType().getFullName())); } }
@Override public void exitService(final ServiceContext ctx) { final ServiceDescriptorProto.Builder serviceBuilder = ServiceDescriptorProto.Builder.class.cast(scopes.getProtoBuilder()); serviceBuilder.setName(ctx.identifier().getText()); scopes.popScope(); }
private void buildAllOptions(final ServiceDescriptorProto.Builder proto) { if (!buildOptions(proto.getOptionsBuilder())) { proto.clearOptions(); } for (final MethodDescriptorProto.Builder methodProto : proto.getMethodBuilderList()) { if (!buildOptions(methodProto.getOptionsBuilder())) { methodProto.clearOptions(); } } }
private boolean isCanonical(final FileDescriptor file) { final FileDescriptorProto proto = file.toProto(); if (proto.hasOptions() && proto.getOptions().getUninterpretedOptionCount() > 0) { return false; } for (final FieldDescriptorProto field : proto.getExtensionList()) { if (!isFieldCanonical(field)) { return false; } } for (final ServiceDescriptorProto serviceProto : proto.getServiceList()) { if (!isCanonical(serviceProto)) { return false; } } for (final EnumDescriptorProto enumProto : proto.getEnumTypeList()) { if (!isCanonical(enumProto)) { return false; } } for (final DescriptorProto message : proto.getMessageTypeList()) { if (!isMessageRefsCanonical(message)) { return false; } } return true; }
private boolean isCanonical(final ServiceDescriptorProto serviceProto) { if (serviceProto.hasOptions() && serviceProto.getOptions().getUninterpretedOptionCount() > 0) { return false; } for (final MethodDescriptorProto methodProto : serviceProto.getMethodList()) { if (methodProto.hasOptions() && methodProto.getOptions().getUninterpretedOptionCount() > 0) { return false; } } return true; }
private FileDescriptorProto createFileDescriptorProto(String fileName, String packageName, UnknownFieldSet unknownFields) { FileDescriptorProto.Builder fileBuilder = FileDescriptorProto.newBuilder(); return fileBuilder .setName(fileName) .setPackage(packageName) .setUnknownFields(unknownFields) .addAllDependency(Collections.<String>emptyList()) .addAllEnumType(Collections.<EnumDescriptorProto>emptyList()) .addAllExtension(Collections.<FieldDescriptorProto>emptyList()) .addAllMessageType(Collections.<DescriptorProto>emptyList()) .addAllPublicDependency(Collections.<Integer>emptyList()) .addAllService(Collections.<ServiceDescriptorProto>emptyList()) .build(); }
public void handle(ServiceDescriptorProto service) throws IOException { ImmutableList.Builder<ServiceHandlerData.Method> methods = ImmutableList.builder(); for (MethodDescriptorProto method : service.getMethodList()) { ServiceHandlerData.Method methodData = new ServiceHandlerData.Method( method.getName(), CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, method.getName()), types.lookup(method.getInputType()).toString(), types.lookup(method.getOutputType()).toString()); methods.add(methodData); } String fullName = Joiner.on('.').skipNulls().join(protoPackage, service.getName()); ServiceHandlerData.Service serviceData = new ServiceHandlerData.Service( service.getName(), fullName, methods.build()); ServiceHandlerData data = new ServiceHandlerData(javaPackage, multipleFiles, serviceData); String template = Resources.toString(Resources.getResource(this.getClass(), "service_class.mvel"), Charsets.UTF_8); String serviceFile = (String) TemplateRuntime.eval(template, ImmutableMap.<String, Object>of("handler", data)); CodeGeneratorResponse.Builder response = CodeGeneratorResponse.newBuilder(); CodeGeneratorResponse.File.Builder file = CodeGeneratorResponse.File.newBuilder(); file.setContent(serviceFile); file.setName(javaPackage.replace('.', '/') + '/' + service.getName() + ".java"); if (!multipleFiles) { file.setName(javaPackage.replace('.', '/') + '/' + outerClassName + ".java"); file.setInsertionPoint("outer_class_scope"); } response.addFile(file); response.build().writeTo(output); }
public static List<Option> getOptions(ServiceDescriptorProto descriptor) { return getOptions(descriptor, true); }
public static List<Option> getOptions(ServiceDescriptorProto descriptor, boolean withDefaults) { return toCoreOptions(maybeCombineOptionsWithDefault(withDefaults, descriptor.getOptions().getAllFields(), DEFAULT_SERVICE_OPTIONS)); }
/** * Creates a interface backed up by the given proto. */ public static Interface create(ProtoFile parent, ServiceDescriptorProto proto, String path) { return new Interface(parent, proto, path); }
/** * Returns the underlying proto representation. */ public ServiceDescriptorProto getProto() { return proto; }
private void restify(MethodKind httpKind, String simpleName, String template) { Model model = Model.create(FileDescriptorSet.getDefaultInstance()); model.setServiceConfig( ConfigSource.newBuilder(Service.getDefaultInstance()) .setValue( Service.getDescriptor().findFieldByNumber(Service.CONFIG_VERSION_FIELD_NUMBER), null, UInt32Value.newBuilder().setValue(configVersion).build(), new SimpleLocation("from test")) .build()); HttpConfigAspect aspect = HttpConfigAspect.create(model); ProtoFile file = ProtoFile.create( model, FileDescriptorProto.getDefaultInstance(), true, ExtensionPool.EMPTY); Interface iface = Interface.create(file, ServiceDescriptorProto.getDefaultInstance(), ""); Method method = Method.create(iface, MethodDescriptorProto.newBuilder().setName(simpleName).build(), ""); RestMethod restMethod; ImmutableList<PathSegment> path = parse(model, template); if (!model.getDiagReporter().getDiagCollector().getDiags().isEmpty()) { restMethod = RestMethod.create(method, RestKind.CUSTOM, "*error*", "*error*", null); } else { HttpRule httpRule = HttpRule.getDefaultInstance(); HttpAttribute httpConfig = new HttpAttribute( httpRule, httpKind, MessageType.create(file, Empty.getDescriptor().toProto(), "", ExtensionPool.EMPTY), path, "", false, ImmutableList.<HttpAttribute>of(), false); RestAnalyzer analyzer = new RestAnalyzer(aspect); restMethod = analyzer.analyzeMethod(method, httpConfig); } PrintWriter pw = testOutput(); pw.print(httpKind.toString()); pw.print(" "); pw.print(simpleName); pw.print(" "); pw.print(template.isEmpty() ? "(empty)" : template); pw.println(); pw.println(Strings.repeat("=", 70)); pw.printf("Rest Kind: %s\n", restMethod.getRestKind()); pw.printf( "Version: %s\n", restMethod.getVersion().isEmpty() ? "(empty)" : restMethod.getVersion()); pw.printf( "Version with default: %s\n", restMethod.getVersionWithDefault().isEmpty() ? "(empty)" : restMethod.getVersionWithDefault()); pw.printf( "Simple collection: %s\n", restMethod.getRestCollectionName().isEmpty() ? "(empty)" : restMethod.getSimpleRestCollectionName()); pw.printf( "Versioned collection: %s\n", restMethod.getRestCollectionName().isEmpty() ? "(empty)" : restMethod.getRestCollectionName()); pw.printf("Base collection: %s\n", restMethod.getBaseRestCollectionName().isEmpty() ? "(empty)" : restMethod.getBaseRestCollectionName()); pw.printf("Custom Name: %s\n", restMethod.getRestKind() == RestKind.CUSTOM ? restMethod.getRestMethodName() : "(null)"); List<Diag> diags = model.getDiagReporter().getDiagCollector().getDiags(); if (diags.size() > 0) { pw.println("Diagnostics:"); for (Diag d : diags) { pw.printf(" %s\n", DiagUtils.getDiagToPrint(d, true)); } } pw.println(); }
/** Convert the descriptor to its protocol message representation. */ @Override public ServiceDescriptorProto toProto() { return proto; }
/** * Convert the descriptor to its protocol message representation. */ public ServiceDescriptorProto toProto () { return proto; }
public ServiceDescriptorProto.Builder addService() { final ServiceDescriptorProto.Builder builder = currentScope.addService(); pushScope(new ServiceScope(builder, currentScope)); return builder; }
protected ServiceDescriptorProto.Builder addService() { throw new RuntimeException(NOT_APPLICABLE_IN_CURRENT_SCOPE); }
@Override protected ServiceDescriptorProto.Builder addService() { return protoBuilder.addServiceBuilder(); }
private ServiceScope(final ServiceDescriptorProto.Builder protoBuilder, final Scope<?> parent) { super(protoBuilder, parent); }
public static boolean compareProto(final ServiceDescriptorProto me, final @Nullable Object other) { return compareMessage(me, other, new ProtoFilter()); }