From c1c2ac119dd2ea6da2277783588af7b6a6bc24e8 Mon Sep 17 00:00:00 2001 From: Philipp Kern Date: Wed, 19 Aug 2020 11:06:37 +0200 Subject: [PATCH 1/3] 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() From 8de2605b4000ec7aff8bfaa0a354f5f9c54b06e1 Mon Sep 17 00:00:00 2001 From: Philipp Kern Date: Wed, 26 Aug 2020 10:15:44 +0200 Subject: [PATCH 2/3] Address code review comments --- api/src/main/java/io/grpc/MethodDescriptor.java | 9 ++++----- api/src/test/java/io/grpc/MethodDescriptorTest.java | 12 ++++++------ 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/api/src/main/java/io/grpc/MethodDescriptor.java b/api/src/main/java/io/grpc/MethodDescriptor.java index a1712bbf949..500755de8e4 100644 --- a/api/src/main/java/io/grpc/MethodDescriptor.java +++ b/api/src/main/java/io/grpc/MethodDescriptor.java @@ -42,7 +42,6 @@ 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; @@ -226,7 +225,6 @@ 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; @@ -271,8 +269,8 @@ public String getServiceName() { */ @Nullable @ExperimentalApi("https://github.com/grpc/grpc-java/issues/5635") - public String getMethodName() { - return methodName; + public String getBareMethodName() { + return extractBareMethodName(fullMethodName); } /** @@ -418,7 +416,8 @@ public static String extractFullServiceName(String fullMethodName) { * @since 1.32.0 */ @Nullable - public static String extractMethodName(String fullMethodName) { + @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; diff --git a/api/src/test/java/io/grpc/MethodDescriptorTest.java b/api/src/test/java/io/grpc/MethodDescriptorTest.java index 4fdf10778f7..ec89976a016 100644 --- a/api/src/test/java/io/grpc/MethodDescriptorTest.java +++ b/api/src/test/java/io/grpc/MethodDescriptorTest.java @@ -178,36 +178,36 @@ public void getServiceName_returnsNull() { } @Test - public void getMethodName_extractsMethod() { + public void getBareMethodName_extractsMethod() { Marshaller marshaller = TestMethodDescriptors.voidMarshaller(); MethodDescriptor md = MethodDescriptor.newBuilder(marshaller, marshaller) .setType(MethodType.UNARY) .setFullMethodName("foo/bar") .build(); - assertEquals("bar", md.getMethodName()); + assertEquals("bar", md.getBareMethodName()); } @Test - public void getMethodName_returnsNull() { + public void getBareMethodName_returnsNull() { Marshaller marshaller = TestMethodDescriptors.voidMarshaller(); MethodDescriptor md = MethodDescriptor.newBuilder(marshaller, marshaller) .setType(MethodType.UNARY) .setFullMethodName("foo-bar") .build(); - assertNull(md.getMethodName()); + assertNull(md.getBareMethodName()); } @Test - public void getMethodName_returnsEmptyStringWithMethodMissing() { + public void getBareMethodName_returnsEmptyStringWithMethodMissing() { Marshaller marshaller = TestMethodDescriptors.voidMarshaller(); MethodDescriptor md = MethodDescriptor.newBuilder(marshaller, marshaller) .setType(MethodType.UNARY) .setFullMethodName("foo/") .build(); - assertTrue(md.getMethodName().isEmpty()); + assertTrue(md.getBareMethodName().isEmpty()); } @Test From 4cd3e038974e9817259c28c7bdaac811958e5512 Mon Sep 17 00:00:00 2001 From: Chengyuan Zhang Date: Thu, 3 Sep 2020 13:46:55 -0700 Subject: [PATCH 3/3] Fix Javadoc typo. --- api/src/main/java/io/grpc/MethodDescriptor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/src/main/java/io/grpc/MethodDescriptor.java b/api/src/main/java/io/grpc/MethodDescriptor.java index 500755de8e4..c1b8a9ed723 100644 --- a/api/src/main/java/io/grpc/MethodDescriptor.java +++ b/api/src/main/java/io/grpc/MethodDescriptor.java @@ -263,7 +263,7 @@ public String getServiceName() { } /** - * A convenience method for {@code extractMethodName(getFullMethodName())}. + * A convenience method for {@code extractBareMethodName(getFullMethodName())}. * * @since 1.32.0 */