Skip to content

Commit ea9c21b

Browse files
committed
core: add getMethodName to MethodDescriptor
1 parent a91acec commit ea9c21b

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

api/src/main/java/io/grpc/MethodDescriptor.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public final class MethodDescriptor<ReqT, RespT> {
4242
private final MethodType type;
4343
private final String fullMethodName;
4444
@Nullable private final String serviceName;
45+
@Nullable private final String methodName;
4546
private final Marshaller<ReqT> requestMarshaller;
4647
private final Marshaller<RespT> responseMarshaller;
4748
private final @Nullable Object schemaDescriptor;
@@ -225,6 +226,7 @@ private MethodDescriptor(
225226
this.type = Preconditions.checkNotNull(type, "type");
226227
this.fullMethodName = Preconditions.checkNotNull(fullMethodName, "fullMethodName");
227228
this.serviceName = extractFullServiceName(fullMethodName);
229+
this.methodName = extractMethodName(fullMethodName);
228230
this.requestMarshaller = Preconditions.checkNotNull(requestMarshaller, "requestMarshaller");
229231
this.responseMarshaller = Preconditions.checkNotNull(responseMarshaller, "responseMarshaller");
230232
this.schemaDescriptor = schemaDescriptor;
@@ -262,6 +264,17 @@ public String getServiceName() {
262264
return serviceName;
263265
}
264266

267+
/**
268+
* A convenience method for {@code extractMethodName(getFullMethodName())}.
269+
*
270+
* @since 1.32.0
271+
*/
272+
@Nullable
273+
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/5635")
274+
public String getMethodName() {
275+
return methodName;
276+
}
277+
265278
/**
266279
* Parse a response payload from the given {@link InputStream}.
267280
*
@@ -398,6 +411,21 @@ public static String extractFullServiceName(String fullMethodName) {
398411
return fullMethodName.substring(0, index);
399412
}
400413

414+
/**
415+
* Extract the method name out of a fully qualified method name. May return {@code null}
416+
* if the input is malformed, but you cannot rely on it for the validity of the input.
417+
*
418+
* @since 1.32.0
419+
*/
420+
@Nullable
421+
public static String extractMethodName(String fullMethodName) {
422+
int index = checkNotNull(fullMethodName, "fullMethodName").lastIndexOf('/');
423+
if (index == -1) {
424+
return null;
425+
}
426+
return fullMethodName.substring(index + 1);
427+
}
428+
401429
/**
402430
* Creates a new builder for a {@link MethodDescriptor}.
403431
*

api/src/test/java/io/grpc/MethodDescriptorTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,39 @@ public void getServiceName_returnsNull() {
177177
assertNull(md.getServiceName());
178178
}
179179

180+
@Test
181+
public void getMethodName_extractsMethod() {
182+
Marshaller<Void> marshaller = TestMethodDescriptors.voidMarshaller();
183+
MethodDescriptor<?, ?> md = MethodDescriptor.newBuilder(marshaller, marshaller)
184+
.setType(MethodType.UNARY)
185+
.setFullMethodName("foo/bar")
186+
.build();
187+
188+
assertEquals("bar", md.getMethodName());
189+
}
190+
191+
@Test
192+
public void getMethodName_returnsNull() {
193+
Marshaller<Void> marshaller = TestMethodDescriptors.voidMarshaller();
194+
MethodDescriptor<?, ?> md = MethodDescriptor.newBuilder(marshaller, marshaller)
195+
.setType(MethodType.UNARY)
196+
.setFullMethodName("foo-bar")
197+
.build();
198+
199+
assertNull(md.getMethodName());
200+
}
201+
202+
@Test
203+
public void getMethodName_returnsEmptyStringWithMethodMissing() {
204+
Marshaller<Void> marshaller = TestMethodDescriptors.voidMarshaller();
205+
MethodDescriptor<?, ?> md = MethodDescriptor.newBuilder(marshaller, marshaller)
206+
.setType(MethodType.UNARY)
207+
.setFullMethodName("foo/")
208+
.build();
209+
210+
assertTrue(md.getMethodName().isEmpty());
211+
}
212+
180213
@Test
181214
public void toBuilderTest() {
182215
MethodDescriptor<String, String> md1 = MethodDescriptor.<String, String>newBuilder()

0 commit comments

Comments
 (0)