Skip to content

Commit

Permalink
RpcInvocation adds returnType assignment in initParameterDesc (apache…
Browse files Browse the repository at this point in the history
…#7746)

* fix apache#7745 RpcInvocation adds returnType assignment in initParameterDesc

* Code optimization
  • Loading branch information
xiaoheng1 committed May 14, 2021
1 parent 24400d0 commit 8e4ef09
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ public RpcInvocation(Method method, String serviceName, String protocolServiceKe

public RpcInvocation(Method method, String serviceName, String protocolServiceKey, Object[] arguments, Map<String, Object> attachment, Map<Object, Object> attributes) {
this(method.getName(), serviceName, protocolServiceKey, method.getParameterTypes(), arguments, attachment, null, attributes);
this.returnType = method.getReturnType();
}

public RpcInvocation(String methodName, String serviceName, String protocolServiceKey, Class<?>[] parameterTypes, Object[] arguments) {
Expand Down Expand Up @@ -159,6 +158,7 @@ private void initParameterDesc() {
this.parameterTypesDesc = methodDescriptor.getParamDesc();
this.compatibleParamSignatures = methodDescriptor.getCompatibleParamSignatures();
this.returnTypes = methodDescriptor.getReturnTypes();
this.returnType = methodDescriptor.getReturnClass();
}
}
}
Expand All @@ -167,6 +167,7 @@ private void initParameterDesc() {
this.parameterTypesDesc = ReflectUtils.getDesc(this.getParameterTypes());
this.compatibleParamSignatures = Stream.of(this.parameterTypes).map(Class::getName).toArray(String[]::new);
this.returnTypes = RpcUtils.getReturnTypes(this);
this.returnType = RpcUtils.getReturnType(this);
}
}

Expand All @@ -179,10 +180,12 @@ public void setInvoker(Invoker<?> invoker) {
this.invoker = invoker;
}

@Override
public Object put(Object key, Object value) {
return attributes.put(key, value);
}

@Override
public Object get(Object key) {
return attributes.get(key);
}
Expand Down Expand Up @@ -241,6 +244,7 @@ public void setParameterTypesDesc(String parameterTypesDesc) {
this.parameterTypesDesc = parameterTypesDesc;
}

@Override
public String[] getCompatibleParamSignatures() {
return compatibleParamSignatures;
}
Expand Down Expand Up @@ -285,6 +289,7 @@ public void setObjectAttachments(Map<String, Object> attachments) {
this.attachments = attachments == null ? new HashMap<>() : attachments;
}

@Override
public void setAttachment(String key, Object value) {
setObjectAttachment(key, value);
}
Expand All @@ -302,6 +307,7 @@ public void setAttachmentIfAbsent(String key, String value) {
setObjectAttachmentIfAbsent(key, value);
}

@Override
public void setAttachmentIfAbsent(String key, Object value) {
setObjectAttachmentIfAbsent(key, value);
}
Expand Down Expand Up @@ -396,6 +402,7 @@ public String getAttachment(String key, String defaultValue) {
}

@Deprecated
@Override
public Object getObjectAttachment(String key, Object defaultValue) {
if (attachments == null) {
return defaultValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.RpcInvocation;

import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.model.ServiceRepository;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -355,4 +357,22 @@ public void testIsEcho() {
Assertions.assertFalse(RpcUtils.isEcho("Ljava/lang/Object;", "testMethod"));
Assertions.assertFalse(RpcUtils.isEcho("Ljava/lang/String;", "$echo"));
}
@Test
public void testIsReturnTypeFuture() {
Class<?> demoServiceClass = DemoService.class;
String serviceName = demoServiceClass.getName();
Invoker invoker = mock(Invoker.class);
given(invoker.getUrl()).willReturn(URL.valueOf(
"test://127.0.0.1:1/org.apache.dubbo.rpc.support.DemoService?interface=org.apache.dubbo.rpc.support.DemoService"));

RpcInvocation inv = new RpcInvocation("testReturnType", serviceName, "", new Class<?>[] {String.class}, null, null, invoker, null);
Assertions.assertFalse(RpcUtils.isReturnTypeFuture(inv));

ServiceRepository repository = ApplicationModel.getServiceRepository();
repository.registerService(demoServiceClass);

inv = new RpcInvocation("testReturnType4", serviceName, "", new Class<?>[] {String.class}, null, null, invoker, null);
Assertions.assertTrue(RpcUtils.isReturnTypeFuture(inv));
}

}

0 comments on commit 8e4ef09

Please sign in to comment.