Skip to content

Commit

Permalink
fix(3.2): injvm invoker throws ClassCastException (#14346)
Browse files Browse the repository at this point in the history
* fix injvm invoker throws ClassCastException

* fix unit test

* fix unit test

* fix NPE

---------

Co-authored-by: caoyanan <caoyanan@growingio.com>
  • Loading branch information
caoyanan666 and caoyanan authored Jun 20, 2024
1 parent 9d333ba commit 4be8594
Showing 1 changed file with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.RpcInvocation;
import org.apache.dubbo.rpc.model.ConsumerModel;
import org.apache.dubbo.rpc.model.MethodDescriptor;
import org.apache.dubbo.rpc.model.ServiceModel;
import org.apache.dubbo.rpc.protocol.AbstractInvoker;
Expand Down Expand Up @@ -317,11 +318,18 @@ private Object rebuildValue(Invocation invocation, Invoker<?> invoker, Object or
}

Object value = originValue;
ClassLoader cl = Thread.currentThread().getContextClassLoader();
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
try {
ServiceModel consumerServiceModel = getUrl().getServiceModel();
if (consumerServiceModel != null) {
Thread.currentThread().setContextClassLoader(consumerServiceModel.getClassLoader());
// 1. By default, the classloader of the current Thread is the consumer class loader.
ClassLoader consumerClassLoader = contextClassLoader;
ServiceModel serviceModel = getUrl().getServiceModel();
// 2. If there is a ConsumerModel in the url, the classloader of the ConsumerModel is consumerLoader
if (Objects.nonNull(serviceModel) && serviceModel instanceof ConsumerModel) {
consumerClassLoader = serviceModel.getClassLoader();
}
// 3. request result copy
if (Objects.nonNull(consumerClassLoader)) {
Thread.currentThread().setContextClassLoader(consumerClassLoader);
Type[] returnTypes = RpcUtils.getReturnTypes(invocation);
if (returnTypes == null) {
return originValue;
Expand All @@ -334,7 +342,7 @@ private Object rebuildValue(Invocation invocation, Invoker<?> invoker, Object or
}
return value;
} finally {
Thread.currentThread().setContextClassLoader(cl);
Thread.currentThread().setContextClassLoader(contextClassLoader);
}
}

Expand Down

0 comments on commit 4be8594

Please sign in to comment.