From 07012421ad7bd3d46c2f07ec33fa74fd1d105b52 Mon Sep 17 00:00:00 2001 From: Philipp Kern Date: Thu, 3 Sep 2020 23:27:18 +0200 Subject: [PATCH] core: add accessor for bare method name in MethodDescriptor (#7339) Added getBareMethodName and extractBareMethodName for accessing the unqualified method name. --- .../main/java/io/grpc/MethodDescriptor.java | 27 +++++++++++++++ .../java/io/grpc/MethodDescriptorTest.java | 33 +++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/api/src/main/java/io/grpc/MethodDescriptor.java b/api/src/main/java/io/grpc/MethodDescriptor.java index 08692592e46..c1b8a9ed723 100644 --- a/api/src/main/java/io/grpc/MethodDescriptor.java +++ b/api/src/main/java/io/grpc/MethodDescriptor.java @@ -262,6 +262,17 @@ public String getServiceName() { return serviceName; } + /** + * A convenience method for {@code extractBareMethodName(getFullMethodName())}. + * + * @since 1.32.0 + */ + @Nullable + @ExperimentalApi("https://github.com/grpc/grpc-java/issues/5635") + public String getBareMethodName() { + return extractBareMethodName(fullMethodName); + } + /** * Parse a response payload from the given {@link InputStream}. * @@ -398,6 +409,22 @@ public static String extractFullServiceName(String fullMethodName) { return fullMethodName.substring(0, index); } + /** + * Extract the method name out of a fully qualified method name. May return {@code null} + * if the input is malformed, but you cannot rely on it for the validity of the input. + * + * @since 1.32.0 + */ + @Nullable + @ExperimentalApi("https://github.com/grpc/grpc-java/issues/5635") + public static String extractBareMethodName(String fullMethodName) { + int index = checkNotNull(fullMethodName, "fullMethodName").lastIndexOf('/'); + if (index == -1) { + return null; + } + return fullMethodName.substring(index + 1); + } + /** * Creates a new builder for a {@link MethodDescriptor}. * diff --git a/api/src/test/java/io/grpc/MethodDescriptorTest.java b/api/src/test/java/io/grpc/MethodDescriptorTest.java index 68637648ce4..ec89976a016 100644 --- a/api/src/test/java/io/grpc/MethodDescriptorTest.java +++ b/api/src/test/java/io/grpc/MethodDescriptorTest.java @@ -177,6 +177,39 @@ public void getServiceName_returnsNull() { assertNull(md.getServiceName()); } + @Test + public void getBareMethodName_extractsMethod() { + Marshaller marshaller = TestMethodDescriptors.voidMarshaller(); + MethodDescriptor md = MethodDescriptor.newBuilder(marshaller, marshaller) + .setType(MethodType.UNARY) + .setFullMethodName("foo/bar") + .build(); + + assertEquals("bar", md.getBareMethodName()); + } + + @Test + public void getBareMethodName_returnsNull() { + Marshaller marshaller = TestMethodDescriptors.voidMarshaller(); + MethodDescriptor md = MethodDescriptor.newBuilder(marshaller, marshaller) + .setType(MethodType.UNARY) + .setFullMethodName("foo-bar") + .build(); + + assertNull(md.getBareMethodName()); + } + + @Test + public void getBareMethodName_returnsEmptyStringWithMethodMissing() { + Marshaller marshaller = TestMethodDescriptors.voidMarshaller(); + MethodDescriptor md = MethodDescriptor.newBuilder(marshaller, marshaller) + .setType(MethodType.UNARY) + .setFullMethodName("foo/") + .build(); + + assertTrue(md.getBareMethodName().isEmpty()); + } + @Test public void toBuilderTest() { MethodDescriptor md1 = MethodDescriptor.newBuilder()