Skip to content

Commit 73d9cd3

Browse files
authored
further enhancement for pull request #3297, also fix an issue introduced in this pull request (#3303)
* further enhancement for pull request #3297, also fix an issue introduced in this pull request * rename the variable * enhance the readability
1 parent 215ed36 commit 73d9cd3

File tree

1 file changed

+40
-21
lines changed

1 file changed

+40
-21
lines changed

dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DecodeableRpcResult.java

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.apache.dubbo.common.logger.LoggerFactory;
2121
import org.apache.dubbo.common.serialize.Cleanable;
2222
import org.apache.dubbo.common.serialize.ObjectInput;
23+
import org.apache.dubbo.common.utils.ArrayUtils;
2324
import org.apache.dubbo.common.utils.Assert;
2425
import org.apache.dubbo.common.utils.StringUtils;
2526
import org.apache.dubbo.remoting.Channel;
@@ -79,19 +80,21 @@ public Object decode(Channel channel, InputStream input) throws IOException {
7980
case DubboCodec.RESPONSE_NULL_VALUE:
8081
break;
8182
case DubboCodec.RESPONSE_VALUE:
82-
setResponseResult(in, true, false, false);
83+
handleValue(in);
8384
break;
8485
case DubboCodec.RESPONSE_WITH_EXCEPTION:
85-
setResponseResult(in, false, true, false);
86+
handleException(in);
8687
break;
8788
case DubboCodec.RESPONSE_NULL_VALUE_WITH_ATTACHMENTS:
88-
setResponseResult(in, false, false, true);
89+
handleAttachment(in);
8990
break;
9091
case DubboCodec.RESPONSE_VALUE_WITH_ATTACHMENTS:
91-
setResponseResult(in, true, false, true);
92+
handleValue(in);
93+
handleAttachment(in);
9294
break;
9395
case DubboCodec.RESPONSE_WITH_EXCEPTION_WITH_ATTACHMENTS:
94-
setResponseResult(in, false, true, false);
96+
handleException(in);
97+
handleAttachment(in);
9598
break;
9699
default:
97100
throw new IOException("Unknown result flag, expect '0' '1' '2', get " + flag);
@@ -119,28 +122,44 @@ public void decode() throws Exception {
119122
}
120123
}
121124

122-
private void setResponseResult(ObjectInput in, boolean hasValue, boolean hasException, boolean hasAttachments) throws IOException {
125+
private void handleValue(ObjectInput in) throws IOException {
123126
try {
124-
if (hasValue) {
125-
Type[] returnType = RpcUtils.getReturnTypes(invocation);
126-
setValue(returnType == null || returnType.length == 0 ? in.readObject() :
127-
(returnType.length == 1 ? in.readObject((Class<?>) returnType[0])
128-
: in.readObject((Class<?>) returnType[0], returnType[1])));
127+
Type[] returnTypes = RpcUtils.getReturnTypes(invocation);
128+
Object value = null;
129+
if (ArrayUtils.isEmpty(returnTypes)) {
130+
value = in.readObject();
131+
} else if (returnTypes.length == 1) {
132+
value = in.readObject((Class<?>) returnTypes[0]);
133+
} else {
134+
value = in.readObject((Class<?>) returnTypes[0], returnTypes[1]);
129135
}
130-
if (hasException) {
131-
Object obj = in.readObject();
132-
if (obj instanceof Throwable == false) {
133-
throw new IOException("Response data error, expect Throwable, but get " + obj);
134-
}
135-
setException((Throwable) obj);
136-
}
137-
if (hasAttachments) {
138-
setAttachments((Map<String, String>) in.readObject(Map.class));
136+
setValue(value);
137+
} catch (ClassNotFoundException e) {
138+
rethrow(e);
139+
}
140+
}
141+
142+
private void handleException(ObjectInput in) throws IOException {
143+
try {
144+
Object obj = in.readObject();
145+
if (!(obj instanceof Throwable)) {
146+
throw new IOException("Response data error, expect Throwable, but get " + obj);
139147
}
148+
setException((Throwable) obj);
140149
} catch (ClassNotFoundException e) {
141-
throw new IOException(StringUtils.toString("Read response data failed.", e));
150+
rethrow(e);
142151
}
152+
}
143153

154+
private void handleAttachment(ObjectInput in) throws IOException {
155+
try {
156+
setAttachments((Map<String, String>) in.readObject(Map.class));
157+
} catch (ClassNotFoundException e) {
158+
rethrow(e);
159+
}
144160
}
145161

162+
private void rethrow(Exception e) throws IOException {
163+
throw new IOException(StringUtils.toString("Read response data failed.", e));
164+
}
146165
}

0 commit comments

Comments
 (0)