Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ public final class {{className}} {
{{#javaDoc}}
{{{javaDoc}}}
{{/javaDoc}}
private static final StubMethodDescriptor {{methodName}}Method = new StubMethodDescriptor("{{originMethodName}}",
{{inputType}}.class, {{outputType}}.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
private static final StubMethodDescriptor {{methodName}}AsyncMethod = new StubMethodDescriptor("{{originMethodName}}",
{{inputType}}.class, java.util.concurrent.CompletableFuture.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), {{inputType}}::parseFrom,
{{outputType}}::parseFrom);

private static final StubMethodDescriptor {{methodName}}AsyncMethod = new StubMethodDescriptor("{{originMethodName}}",
{{inputType}}.class, java.util.concurrent.CompletableFuture.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
private static final StubMethodDescriptor {{methodName}}Method = new StubMethodDescriptor("{{originMethodName}}",
{{inputType}}.class, {{outputType}}.class, serviceDescriptor, MethodDescriptor.RpcType.UNARY,
obj -> ((Message) obj).toByteArray(), obj -> ((Message) obj).toByteArray(), {{inputType}}::parseFrom,
{{outputType}}::parseFrom);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.apache.dubbo.rpc.model.MethodDescriptor;
import org.apache.dubbo.rpc.proxy.InvocationUtil;

import java.lang.reflect.Type;

public class StubInvocationUtil {

public static <T, R> R unaryCall(Invoker<?> invoker, MethodDescriptor methodDescriptor,
Expand Down Expand Up @@ -54,12 +56,27 @@ public static <T, R> void serverStreamCall(Invoker<?> invoker, MethodDescriptor

private static Object call(Invoker<?> invoker, MethodDescriptor methodDescriptor,
Object[] arguments) {
//Due to the design of ServiceDescriptor, the MethodDescriptor with the same name will override each other in ServiceDescriptor.
//The sync and async version of MethodDescriptor share the same method name, and usually the sync version will replace another one
//(Because it's declared after the async version).
//So this RpcInvocation always uses MethodDescriptor of sync version to initialize.
RpcInvocation rpcInvocation = new RpcInvocation(invoker.getUrl().getServiceModel(),
methodDescriptor.getMethodName(), invoker.getInterface().getName(),
invoker.getUrl().getProtocolServiceKey(), methodDescriptor.getParameterClasses(),
arguments);

//When there are multiple MethodDescriptors with the same method name, the return type will be wrong
rpcInvocation.setReturnType(methodDescriptor.getReturnClass());
Type[] returnTypes = methodDescriptor.getReturnTypes();
//If this MethodDescriptor from an async method, its return class should be CompletableFuture.
//If this MethodDescriptor from a sync method, its return class is real return type.
//The definition of returnTypes:
//returnTypes[0], the concrete type.
//returnTypes[1], the parameterized type.
returnTypes[0] = methodDescriptor.getReturnClass();
returnTypes[1] = methodDescriptor.getReturnClass();
rpcInvocation.setReturnTypes(returnTypes);

try {
return InvocationUtil.invoke(invoker, rpcInvocation);
} catch (Throwable e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.lang.reflect.Type;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
Expand Down Expand Up @@ -78,6 +79,7 @@ private MethodDescriptor createMockMethodDescriptor() {
MethodDescriptor method = Mockito.mock(MethodDescriptor.class);
when(method.getParameterClasses()).thenReturn(new Class[]{String.class});
when(method.getMethodName()).thenReturn("sayHello");
when(method.getReturnTypes()).thenReturn(new Type[]{String.class,String.class});
return method;
}

Expand Down