From c1c2ac119dd2ea6da2277783588af7b6a6bc24e8 Mon Sep 17 00:00:00 2001 From: Philipp Kern Date: Wed, 19 Aug 2020 11:06:37 +0200 Subject: [PATCH] core: add getMethodName to MethodDescriptor --- .../main/java/io/grpc/MethodDescriptor.java | 28 ++++++++++++++++ .../java/io/grpc/MethodDescriptorTest.java | 33 +++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/api/src/main/java/io/grpc/MethodDescriptor.java b/api/src/main/java/io/grpc/MethodDescriptor.java index 08692592e46..a1712bbf949 100644 --- a/api/src/main/java/io/grpc/MethodDescriptor.java +++ b/api/src/main/java/io/grpc/MethodDescriptor.java @@ -42,6 +42,7 @@ public final class MethodDescriptor { private final MethodType type; private final String fullMethodName; @Nullable private final String serviceName; + @Nullable private final String methodName; private final Marshaller requestMarshaller; private final Marshaller responseMarshaller; private final @Nullable Object schemaDescriptor; @@ -225,6 +226,7 @@ private MethodDescriptor( this.type = Preconditions.checkNotNull(type, "type"); this.fullMethodName = Preconditions.checkNotNull(fullMethodName, "fullMethodName"); this.serviceName = extractFullServiceName(fullMethodName); + this.methodName = extractMethodName(fullMethodName); this.requestMarshaller = Preconditions.checkNotNull(requestMarshaller, "requestMarshaller"); this.responseMarshaller = Preconditions.checkNotNull(responseMarshaller, "responseMarshaller"); this.schemaDescriptor = schemaDescriptor; @@ -262,6 +264,17 @@ public String getServiceName() { return serviceName; } + /** + * A convenience method for {@code extractMethodName(getFullMethodName())}. + * + * @since 1.32.0 + */ + @Nullable + @ExperimentalApi("https://github.com/grpc/grpc-java/issues/5635") + public String getMethodName() { + return methodName; + } + /** * Parse a response payload from the given {@link InputStream}. * @@ -398,6 +411,21 @@ 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 + public static String extractMethodName(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..4fdf10778f7 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 getMethodName_extractsMethod() { + Marshaller marshaller = TestMethodDescriptors.voidMarshaller(); + MethodDescriptor md = MethodDescriptor.newBuilder(marshaller, marshaller) + .setType(MethodType.UNARY) + .setFullMethodName("foo/bar") + .build(); + + assertEquals("bar", md.getMethodName()); + } + + @Test + public void getMethodName_returnsNull() { + Marshaller marshaller = TestMethodDescriptors.voidMarshaller(); + MethodDescriptor md = MethodDescriptor.newBuilder(marshaller, marshaller) + .setType(MethodType.UNARY) + .setFullMethodName("foo-bar") + .build(); + + assertNull(md.getMethodName()); + } + + @Test + public void getMethodName_returnsEmptyStringWithMethodMissing() { + Marshaller marshaller = TestMethodDescriptors.voidMarshaller(); + MethodDescriptor md = MethodDescriptor.newBuilder(marshaller, marshaller) + .setType(MethodType.UNARY) + .setFullMethodName("foo/") + .build(); + + assertTrue(md.getMethodName().isEmpty()); + } + @Test public void toBuilderTest() { MethodDescriptor md1 = MethodDescriptor.newBuilder()