/** Lists the GRPC services - filtered by service name (contains) or method name (contains) */ public static void listServices( Output output, FileDescriptorSet fileDescriptorSet, String protoDiscoveryRoot, Optional<String> serviceFilter, Optional<String> methodFilter, Optional<Boolean> withMessage) { ServiceResolver serviceResolver = ServiceResolver.fromFileDescriptorSet(fileDescriptorSet); // Add white-space before the rendered output output.newLine(); for (ServiceDescriptor descriptor : serviceResolver.listServices()) { boolean matchingDescriptor = !serviceFilter.isPresent() || descriptor.getFullName().toLowerCase().contains(serviceFilter.get().toLowerCase()); if (matchingDescriptor) { listMethods(output, protoDiscoveryRoot, descriptor, methodFilter, withMessage); } } }
public void registerGpbMsgDesc(FileDescriptor fileDescriptor) { if (fileDescriptor == null) return; // service for (ServiceDescriptor service : fileDescriptor.getServices()) { for (MethodDescriptor method : service.getMethods()) { if (gpbMsgDescMap.containsKey(method.getName())) { LOG.error("[Gpb] the method [" + method.getName() + "] already registered."); } registerGpbMessage(method.getInputType()); methodInputTypeMap.put(method.getName(), method.getInputType().getName()); } } // message for (Descriptor descriptor : fileDescriptor.getMessageTypes()) { registerGpbMessage(descriptor); } }
private void processService(ServiceDescriptor service, FileDescriptor fd) { String serviceName = service.getFullName(); checkState( !fileDescriptorsBySymbol.containsKey(serviceName), "Service already defined: %s", serviceName); fileDescriptorsBySymbol.put(serviceName, fd); for (MethodDescriptor method : service.getMethods()) { String methodName = method.getFullName(); checkState( !fileDescriptorsBySymbol.containsKey(methodName), "Method already defined: %s", methodName); fileDescriptorsBySymbol.put(methodName, fd); } }
public void testServiceDescriptor() throws Exception { ServiceDescriptor service = TestService.getDescriptor(); assertEquals("TestService", service.getName()); assertEquals("protobuf_unittest.TestService", service.getFullName()); assertEquals(UnittestProto.getDescriptor(), service.getFile()); assertEquals(2, service.getMethods().size()); MethodDescriptor fooMethod = service.getMethods().get(0); assertEquals("Foo", fooMethod.getName()); assertEquals(UnittestProto.FooRequest.getDescriptor(), fooMethod.getInputType()); assertEquals(UnittestProto.FooResponse.getDescriptor(), fooMethod.getOutputType()); assertEquals(fooMethod, service.findMethodByName("Foo")); MethodDescriptor barMethod = service.getMethods().get(1); assertEquals("Bar", barMethod.getName()); assertEquals(UnittestProto.BarRequest.getDescriptor(), barMethod.getInputType()); assertEquals(UnittestProto.BarResponse.getDescriptor(), barMethod.getOutputType()); assertEquals(barMethod, service.findMethodByName("Bar")); assertNull(service.findMethodByName("NoSuchMethod")); for (int i = 0; i < service.getMethods().size(); i++) { assertEquals(i, service.getMethods().get(i).getIndex()); } }
/** * Get matching method. */ public MethodDescriptor getMethod(String methodName, ServiceDescriptor descriptor) throws MethodNotFoundException { MethodDescriptor method = descriptor.findMethodByName(methodName); if (method == null) { throw new MethodNotFoundException( String.format("Could not find method %s in service %s", methodName, descriptor.getFullName()) ); } return method; }
/** Lists all of the services found in the file descriptors */ public Iterable<ServiceDescriptor> listServices() { ArrayList<ServiceDescriptor> serviceDescriptors = new ArrayList<ServiceDescriptor>(); for (FileDescriptor fileDescriptor: fileDescriptors) { serviceDescriptors.addAll(fileDescriptor.getServices()); } return serviceDescriptors; }
private MethodDescriptor resolveServiceMethod( String serviceName, String methodName, String packageName) { ServiceDescriptor service = findService(serviceName, packageName); MethodDescriptor method = service.findMethodByName(methodName); if (method == null) { throw new IllegalArgumentException( "Unable to find method " + methodName + " in service " + serviceName); } return method; }
private ServiceDescriptor findService(String serviceName, String packageName) { // TODO(dino): Consider creating an index. for (FileDescriptor fileDescriptor : fileDescriptors) { if (!fileDescriptor.getPackage().equals(packageName)) { // Package does not match this file, ignore. continue; } ServiceDescriptor serviceDescriptor = fileDescriptor.findServiceByName(serviceName); if (serviceDescriptor != null) { return serviceDescriptor; } } throw new IllegalArgumentException("Unable to find service with name: " + serviceName); }
/** Lists the methods on the service (the methodFilter will be applied if non-empty) */ private static void listMethods( Output output, String protoDiscoveryRoot, ServiceDescriptor descriptor, Optional<String> methodFilter, Optional<Boolean> withMessage) { boolean printedService = false; // Due to the way the protos are discovered, the leaf directly of the protoDiscoveryRoot // is the same as the root directory as the proto file File protoDiscoveryDir = new File(protoDiscoveryRoot).getParentFile(); for (MethodDescriptor method : descriptor.getMethods()) { if (!methodFilter.isPresent() || method.getName().contains(methodFilter.get())) { // Only print the service name once - and only if a method is going to be printed if (!printedService) { File pFile = new File(protoDiscoveryDir, descriptor.getFile().getName()); output.writeLine(descriptor.getFullName() + " -> " + pFile.getAbsolutePath()); printedService = true; } output.writeLine(" " + descriptor.getFullName() + "/" + method.getName()); // If requested, add the message definition if (withMessage.isPresent() && withMessage.get()) { output.writeLine(renderDescriptor(method.getInputType(), " ")); output.newLine(); } } } if (printedService) { output.newLine(); } }
/** * Get matching method. */ private MethodDescriptor getMethod(SocketRpcProtos.Request rpcRequest, ServiceDescriptor descriptor) throws RpcException { MethodDescriptor method = descriptor.findMethodByName( rpcRequest.getMethodName()); if (method == null) { throw new RpcException( ErrorReason.METHOD_NOT_FOUND, String.format("Could not find method %s in service %s", rpcRequest.getMethodName(), descriptor.getFullName()), null); } return method; }
public void testServiceDescriptor() throws Exception { ServiceDescriptor service = TestService.getDescriptor(); assertEquals("TestService", service.getName()); assertEquals("protobuf_unittest.TestService", service.getFullName()); assertEquals(UnittestProto.getDescriptor(), service.getFile()); MethodDescriptor fooMethod = service.getMethods().get(0); assertEquals("Foo", fooMethod.getName()); assertEquals(UnittestProto.FooRequest.getDescriptor(), fooMethod.getInputType()); assertEquals(UnittestProto.FooResponse.getDescriptor(), fooMethod.getOutputType()); assertEquals(fooMethod, service.findMethodByName("Foo")); MethodDescriptor barMethod = service.getMethods().get(1); assertEquals("Bar", barMethod.getName()); assertEquals(UnittestProto.BarRequest.getDescriptor(), barMethod.getInputType()); assertEquals(UnittestProto.BarResponse.getDescriptor(), barMethod.getOutputType()); assertEquals(barMethod, service.findMethodByName("Bar")); assertNull(service.findMethodByName("NoSuchMethod")); for (int i = 0; i < service.getMethods().size(); i++) { assertEquals(i, service.getMethods().get(i).getIndex()); } }
/** * Returns the name to use for coprocessor service calls. For core HBase services * (in the hbase.pb protobuf package), this returns the unqualified name in order to provide * backward compatibility across the package name change. For all other services, * the fully-qualified service name is used. */ public static String getServiceName(Descriptors.ServiceDescriptor service) { if (service.getFullName().startsWith(hbaseServicePackage)) { return service.getName(); } return service.getFullName(); }
public static MethodDescriptor getMethodDescriptor(final String methodName, final ServiceDescriptor serviceDesc) throws UnknownProtocolException { Descriptors.MethodDescriptor methodDesc = serviceDesc.findMethodByName(methodName); if (methodDesc == null) { throw new UnknownProtocolException("Unknown method " + methodName + " called on service " + serviceDesc.getFullName()); } return methodDesc; }
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())); } }
/** * Checks for updates to the server's mutable services and updates the index if any changes are * detected. A change is any addition or removal in the set of file descriptors attached to the * mutable services or a change in the service names. * * @return The (potentially updated) index. */ private ServerReflectionIndex updateIndexIfNecessary() { synchronized (lock) { if (serverReflectionIndex == null) { serverReflectionIndex = new ServerReflectionIndex(server.getImmutableServices(), server.getMutableServices()); return serverReflectionIndex; } Set<FileDescriptor> serverFileDescriptors = new HashSet<FileDescriptor>(); Set<String> serverServiceNames = new HashSet<String>(); List<ServerServiceDefinition> serverMutableServices = server.getMutableServices(); for (ServerServiceDefinition mutableService : serverMutableServices) { io.grpc.ServiceDescriptor serviceDescriptor = mutableService.getServiceDescriptor(); if (serviceDescriptor.getSchemaDescriptor() instanceof ProtoFileDescriptorSupplier) { String serviceName = serviceDescriptor.getName(); FileDescriptor fileDescriptor = ((ProtoFileDescriptorSupplier) serviceDescriptor.getSchemaDescriptor()) .getFileDescriptor(); serverFileDescriptors.add(fileDescriptor); serverServiceNames.add(serviceName); } } // Replace the index if the underlying mutable services have changed. Check both the file // descriptors and the service names, because one file descriptor can define multiple // services. FileDescriptorIndex mutableServicesIndex = serverReflectionIndex.getMutableServicesIndex(); if (!mutableServicesIndex.getServiceFileDescriptors().equals(serverFileDescriptors) || !mutableServicesIndex.getServiceNames().equals(serverServiceNames)) { serverReflectionIndex = new ServerReflectionIndex(server.getImmutableServices(), serverMutableServices); } return serverReflectionIndex; } }
FileDescriptorIndex(List<ServerServiceDefinition> services) { Queue<FileDescriptor> fileDescriptorsToProcess = new ArrayDeque<FileDescriptor>(); Set<String> seenFiles = new HashSet<String>(); for (ServerServiceDefinition service : services) { io.grpc.ServiceDescriptor serviceDescriptor = service.getServiceDescriptor(); if (serviceDescriptor.getSchemaDescriptor() instanceof ProtoFileDescriptorSupplier) { FileDescriptor fileDescriptor = ((ProtoFileDescriptorSupplier) serviceDescriptor.getSchemaDescriptor()) .getFileDescriptor(); String serviceName = serviceDescriptor.getName(); checkState( !serviceNames.contains(serviceName), "Service already defined: %s", serviceName); serviceFileDescriptors.add(fileDescriptor); serviceNames.add(serviceName); if (!seenFiles.contains(fileDescriptor.getName())) { seenFiles.add(fileDescriptor.getName()); fileDescriptorsToProcess.add(fileDescriptor); } } } while (!fileDescriptorsToProcess.isEmpty()) { FileDescriptor currentFd = fileDescriptorsToProcess.remove(); processFileDescriptor(currentFd); for (FileDescriptor dependencyFd : currentFd.getDependencies()) { if (!seenFiles.contains(dependencyFd.getName())) { seenFiles.add(dependencyFd.getName()); fileDescriptorsToProcess.add(dependencyFd); } } } }
private void processFileDescriptor(FileDescriptor fd) { String fdName = fd.getName(); checkState(!fileDescriptorsByName.containsKey(fdName), "File name already used: %s", fdName); fileDescriptorsByName.put(fdName, fd); for (ServiceDescriptor service : fd.getServices()) { processService(service, fd); } for (Descriptor type : fd.getMessageTypes()) { processType(type, fd); } for (FieldDescriptor extension : fd.getExtensions()) { processExtension(extension, fd); } }
public static <RequestT extends Message, ResponseT extends Message> RpcMethod<RequestT, ResponseT> create( ServiceDescriptor descriptor, String methodName, RequestT requestDefaultInstance, ResponseT responseDefaultInstance) { MethodDescriptor method = descriptor.findMethodByName(methodName); Preconditions.checkArgument(method != null); Preconditions.checkArgument(requestDefaultInstance.getDescriptorForType() == method.getInputType()); Preconditions.checkArgument(responseDefaultInstance.getDescriptorForType() == method.getOutputType()); return new RpcMethod<RequestT, ResponseT>(method, responseDefaultInstance); }
public void generateProtoFromDescriptor(FileDescriptor descriptor, Appendable out, Descriptor wrapperMessage) throws IOException { String package1 = descriptor.getPackage(); if (package1 != null) { out.append("package " + package1 + ";\n"); } FileOptions options = descriptor.getOptions(); String javaPackage = options.getJavaPackage(); if (javaPackage != null) { out.append("option java_package = \"" + javaPackage + "\";\n"); } String javaOuterClassname = options.getJavaOuterClassname(); if (javaOuterClassname != null) { out.append("option java_outer_classname = \"" + javaOuterClassname + "\";\n"); } for (ServiceDescriptor serviceDescriptor : descriptor.getServices()) { generateProtoFromDescriptor(serviceDescriptor, out); } for (Descriptor messageDescriptor : descriptor.getMessageTypes()) { if (wrapperMessage != null && messageDescriptor.equals(wrapperMessage)) { out.append("// This is the message you can send to this service (wrapper message):\n"); } generateProtoFromDescriptor(messageDescriptor, out, "", new LinkedHashMap<Descriptor, Boolean>()); } for (EnumDescriptor enumDescriptor : descriptor.getEnumTypes()) { generateProtoFromDescriptor(enumDescriptor, out, ""); } }
private void generateProtoFromDescriptor(ServiceDescriptor descriptor, Appendable out) throws IOException { out.append("service " + descriptor.getName() + " {\n"); for (MethodDescriptor methodDescriptor : descriptor.getMethods()) { generateProtoFromDescriptor(methodDescriptor, out); } out.append("}\n"); }
public void testFileDescriptor() throws Exception { FileDescriptor file = UnittestProto.getDescriptor(); assertEquals("google/protobuf/unittest.proto", file.getName()); assertEquals("protobuf_unittest", file.getPackage()); assertEquals("UnittestProto", file.getOptions().getJavaOuterClassname()); assertEquals("google/protobuf/unittest.proto", file.toProto().getName()); assertEquals(Arrays.asList(UnittestImport.getDescriptor()), file.getDependencies()); Descriptor messageType = TestAllTypes.getDescriptor(); assertEquals(messageType, file.getMessageTypes().get(0)); assertEquals(messageType, file.findMessageTypeByName("TestAllTypes")); assertNull(file.findMessageTypeByName("NoSuchType")); assertNull(file.findMessageTypeByName("protobuf_unittest.TestAllTypes")); for (int i = 0; i < file.getMessageTypes().size(); i++) { assertEquals(i, file.getMessageTypes().get(i).getIndex()); } EnumDescriptor enumType = ForeignEnum.getDescriptor(); assertEquals(enumType, file.getEnumTypes().get(0)); assertEquals(enumType, file.findEnumTypeByName("ForeignEnum")); assertNull(file.findEnumTypeByName("NoSuchType")); assertNull(file.findEnumTypeByName("protobuf_unittest.ForeignEnum")); assertEquals(Arrays.asList(ImportEnum.getDescriptor()), UnittestImport.getDescriptor().getEnumTypes()); for (int i = 0; i < file.getEnumTypes().size(); i++) { assertEquals(i, file.getEnumTypes().get(i).getIndex()); } ServiceDescriptor service = TestService.getDescriptor(); assertEquals(service, file.getServices().get(0)); assertEquals(service, file.findServiceByName("TestService")); assertNull(file.findServiceByName("NoSuchType")); assertNull(file.findServiceByName("protobuf_unittest.TestService")); assertEquals(Collections.emptyList(), UnittestImport.getDescriptor().getServices()); for (int i = 0; i < file.getServices().size(); i++) { assertEquals(i, file.getServices().get(i).getIndex()); } FieldDescriptor extension = UnittestProto.optionalInt32Extension.getDescriptor(); assertEquals(extension, file.getExtensions().get(0)); assertEquals(extension, file.findExtensionByName("optional_int32_extension")); assertNull(file.findExtensionByName("no_such_ext")); assertNull(file.findExtensionByName( "protobuf_unittest.optional_int32_extension")); assertEquals(Collections.emptyList(), UnittestImport.getDescriptor().getExtensions()); for (int i = 0; i < file.getExtensions().size(); i++) { assertEquals(i, file.getExtensions().get(i).getIndex()); } }
public void testCustomOptions() throws Exception { Descriptor descriptor = UnittestCustomOptions.TestMessageWithCustomOptions.getDescriptor(); assertTrue( descriptor.getOptions().hasExtension(UnittestCustomOptions.messageOpt1)); assertEquals(Integer.valueOf(-56), descriptor.getOptions().getExtension(UnittestCustomOptions.messageOpt1)); FieldDescriptor field = descriptor.findFieldByName("field1"); assertNotNull(field); assertTrue( field.getOptions().hasExtension(UnittestCustomOptions.fieldOpt1)); assertEquals(Long.valueOf(8765432109L), field.getOptions().getExtension(UnittestCustomOptions.fieldOpt1)); EnumDescriptor enumType = UnittestCustomOptions.TestMessageWithCustomOptions.AnEnum.getDescriptor(); assertTrue( enumType.getOptions().hasExtension(UnittestCustomOptions.enumOpt1)); assertEquals(Integer.valueOf(-789), enumType.getOptions().getExtension(UnittestCustomOptions.enumOpt1)); ServiceDescriptor service = UnittestCustomOptions.TestServiceWithCustomOptions.getDescriptor(); assertTrue( service.getOptions().hasExtension(UnittestCustomOptions.serviceOpt1)); assertEquals(Long.valueOf(-9876543210L), service.getOptions().getExtension(UnittestCustomOptions.serviceOpt1)); MethodDescriptor method = service.findMethodByName("Foo"); assertNotNull(method); assertTrue( method.getOptions().hasExtension(UnittestCustomOptions.methodOpt1)); assertEquals(UnittestCustomOptions.MethodOpt1.METHODOPT1_VAL2, method.getOptions().getExtension(UnittestCustomOptions.methodOpt1)); }
public void testCustomOptions() throws Exception { // Get the descriptor indirectly from a dependent proto class. This is to // ensure that when a proto class is loaded, custom options defined in its // dependencies are also properly initialized. Descriptor descriptor = TestCustomOptions.TestMessageWithCustomOptionsContainer.getDescriptor() .findFieldByName("field").getMessageType(); assertTrue( descriptor.getOptions().hasExtension(UnittestCustomOptions.messageOpt1)); assertEquals(Integer.valueOf(-56), descriptor.getOptions().getExtension(UnittestCustomOptions.messageOpt1)); FieldDescriptor field = descriptor.findFieldByName("field1"); assertNotNull(field); assertTrue( field.getOptions().hasExtension(UnittestCustomOptions.fieldOpt1)); assertEquals(Long.valueOf(8765432109L), field.getOptions().getExtension(UnittestCustomOptions.fieldOpt1)); EnumDescriptor enumType = UnittestCustomOptions.TestMessageWithCustomOptions.AnEnum.getDescriptor(); assertTrue( enumType.getOptions().hasExtension(UnittestCustomOptions.enumOpt1)); assertEquals(Integer.valueOf(-789), enumType.getOptions().getExtension(UnittestCustomOptions.enumOpt1)); ServiceDescriptor service = UnittestCustomOptions.TestServiceWithCustomOptions.getDescriptor(); assertTrue( service.getOptions().hasExtension(UnittestCustomOptions.serviceOpt1)); assertEquals(Long.valueOf(-9876543210L), service.getOptions().getExtension(UnittestCustomOptions.serviceOpt1)); MethodDescriptor method = service.findMethodByName("Foo"); assertNotNull(method); assertTrue( method.getOptions().hasExtension(UnittestCustomOptions.methodOpt1)); assertEquals(UnittestCustomOptions.MethodOpt1.METHODOPT1_VAL2, method.getOptions().getExtension(UnittestCustomOptions.methodOpt1)); }
ServiceEntry(ServiceDescriptor service, List<EndpointInfo> endpointInfos) { this.service = service; this.endpointInfos = ImmutableList.copyOf(endpointInfos); }
ServiceEntryBuilder(ServiceDescriptor service) { this.service = service; }
/** * Find the service and process the remote RPC. */ @SuppressWarnings("unchecked") public void messageReceived(final IoSession session, Object message) throws Exception { RpcMessage request = (RpcMessage) message; final int id = request.getId(); final String className = request.getClassName(); final String methodName = request.getMethod(); final String serviceName = request.getService(); Class reqClass = null; try { reqClass = Class.forName(className); } catch (Exception e) { logger.warn("Cannot find " + className); sendResponse(session, id, Result.ERR, className, serviceName, methodName, null); return; } Method parseFromMethod = reqClass.getDeclaredMethod(PARSE_FROM_METHOD, PRASE_FROM_ARG); Message reqMessage = null; ByteString payload = request.getPayload(); if ( payload != null ) { reqMessage = (Message)parseFromMethod.invoke(reqClass, payload.toByteArray()); } if ( logger.isDebugEnabled() ) { logger.debug("messageReceived id:{}, serviceName:{}, methodName:{}", new Object[]{id, serviceName, methodName}); } SecureRpcCallback cb = new SecureRpcCallback(session, id, serviceName, methodName); Service service = GameContext.getInstance().findRpcService( serviceName, methodName); if ( service != null ) { ServiceDescriptor serviceDesc = service.getDescriptorForType(); MethodDescriptor methodDesc = serviceDesc.findMethodByName(methodName); if ( methodDesc != null ) { service.callMethod(methodDesc, rpcController, reqMessage, cb); // if ( !cb.isCalled() ) { // logger.warn("You may forgot to call RpcCallback.run method in {}", serviceName); // sendResponse(session, id, Result.ERR, className, serviceName, // methodName, null); // } } else { logger.debug("Do not find the remote method: {}", methodName); sendResponse(session, id, Result.ERR, className, serviceName, methodName, null); } } else { logger.debug("Do not find the remote service: {}", serviceName); sendResponse(session, id, Result.ERR, className, serviceName, methodName, null); } }
public List<ServiceDescriptor> getServices() { return delegate.getServices(); }
public ServiceDescriptor findServiceByName(final String name) { return delegate.findServiceByName(name); }
public void testFileDescriptor() throws Exception { FileDescriptor file = UnittestProto.getDescriptor(); assertEquals("google/protobuf/unittest.proto", file.getName()); assertEquals("protobuf_unittest", file.getPackage()); assertEquals("UnittestProto", file.getOptions().getJavaOuterClassname()); assertEquals("google/protobuf/unittest.proto", file.toProto().getName()); assertEquals(Arrays.asList(UnittestImport.getDescriptor()), file.getDependencies()); Descriptor messageType = TestAllTypes.getDescriptor(); assertEquals(messageType, file.getMessageTypes().get(0)); assertEquals(messageType, file.findMessageTypeByName("TestAllTypes")); assertNull(file.findMessageTypeByName("NoSuchType")); assertNull(file.findMessageTypeByName("protobuf_unittest.TestAllTypes")); for (int i = 0; i < file.getMessageTypes().size(); i++) { assertEquals(i, file.getMessageTypes().get(i).getIndex()); } EnumDescriptor enumType = ForeignEnum.getDescriptor(); assertEquals(enumType, file.getEnumTypes().get(0)); assertEquals(enumType, file.findEnumTypeByName("ForeignEnum")); assertNull(file.findEnumTypeByName("NoSuchType")); assertNull(file.findEnumTypeByName("protobuf_unittest.ForeignEnum")); assertEquals(Arrays.asList(ImportEnum.getDescriptor(), ImportEnumForMap.getDescriptor()), UnittestImport.getDescriptor().getEnumTypes()); for (int i = 0; i < file.getEnumTypes().size(); i++) { assertEquals(i, file.getEnumTypes().get(i).getIndex()); } ServiceDescriptor service = TestService.getDescriptor(); assertEquals(service, file.getServices().get(0)); assertEquals(service, file.findServiceByName("TestService")); assertNull(file.findServiceByName("NoSuchType")); assertNull(file.findServiceByName("protobuf_unittest.TestService")); assertEquals(Collections.emptyList(), UnittestImport.getDescriptor().getServices()); for (int i = 0; i < file.getServices().size(); i++) { assertEquals(i, file.getServices().get(i).getIndex()); } FieldDescriptor extension = UnittestProto.optionalInt32Extension.getDescriptor(); assertEquals(extension, file.getExtensions().get(0)); assertEquals(extension, file.findExtensionByName("optional_int32_extension")); assertNull(file.findExtensionByName("no_such_ext")); assertNull(file.findExtensionByName( "protobuf_unittest.optional_int32_extension")); assertEquals(Collections.emptyList(), UnittestImport.getDescriptor().getExtensions()); for (int i = 0; i < file.getExtensions().size(); i++) { assertEquals(i, file.getExtensions().get(i).getIndex()); } }
public void testCustomOptions() throws Exception { // Get the descriptor indirectly from a dependent proto class. This is to // ensure that when a proto class is loaded, custom options defined in its // dependencies are also properly initialized. Descriptor descriptor = TestCustomOptions.TestMessageWithCustomOptionsContainer.getDescriptor() .findFieldByName("field").getMessageType(); assertTrue( descriptor.getOptions().hasExtension(UnittestCustomOptions.messageOpt1)); assertEquals(Integer.valueOf(-56), descriptor.getOptions().getExtension(UnittestCustomOptions.messageOpt1)); FieldDescriptor field = descriptor.findFieldByName("field1"); assertNotNull(field); assertTrue( field.getOptions().hasExtension(UnittestCustomOptions.fieldOpt1)); assertEquals(Long.valueOf(8765432109L), field.getOptions().getExtension(UnittestCustomOptions.fieldOpt1)); OneofDescriptor oneof = descriptor.getOneofs().get(0); assertNotNull(oneof); assertTrue( oneof.getOptions().hasExtension(UnittestCustomOptions.oneofOpt1)); assertEquals(Integer.valueOf(-99), oneof.getOptions().getExtension(UnittestCustomOptions.oneofOpt1)); EnumDescriptor enumType = UnittestCustomOptions.TestMessageWithCustomOptions.AnEnum.getDescriptor(); assertTrue( enumType.getOptions().hasExtension(UnittestCustomOptions.enumOpt1)); assertEquals(Integer.valueOf(-789), enumType.getOptions().getExtension(UnittestCustomOptions.enumOpt1)); ServiceDescriptor service = UnittestCustomOptions.TestServiceWithCustomOptions.getDescriptor(); assertTrue( service.getOptions().hasExtension(UnittestCustomOptions.serviceOpt1)); assertEquals(Long.valueOf(-9876543210L), service.getOptions().getExtension(UnittestCustomOptions.serviceOpt1)); MethodDescriptor method = service.findMethodByName("Foo"); assertNotNull(method); assertTrue( method.getOptions().hasExtension(UnittestCustomOptions.methodOpt1)); assertEquals(UnittestCustomOptions.MethodOpt1.METHODOPT1_VAL2, method.getOptions().getExtension(UnittestCustomOptions.methodOpt1)); }
public ServiceDescriptorContainer(ServiceDescriptor serviceDescriptor) { this.serviceDescriptor = serviceDescriptor; for (MethodDescriptor methodDescriptor : serviceDescriptor.getMethods()) { this.methodDescriptors.put(methodDescriptor.getName(), new MethodDescriptorContainer(this, methodDescriptor)); } }