fix(3.2): injvm invoker throws ClassCastException (#14346)

* fix injvm invoker throws ClassCastException

* fix unit test

* fix unit test

* fix NPE

---------

Co-authored-by: caoyanan <caoyanan@growingio.com>
This commit is contained in:
caoyanan666 2024-06-20 09:41:36 +08:00 committed by GitHub
parent 9d333ba906
commit 4be8594bde
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 13 additions and 5 deletions

View File

@ -36,6 +36,7 @@ import org.apache.dubbo.rpc.Result;
import org.apache.dubbo.rpc.RpcContext; import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.RpcException; import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.RpcInvocation; 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.MethodDescriptor;
import org.apache.dubbo.rpc.model.ServiceModel; import org.apache.dubbo.rpc.model.ServiceModel;
import org.apache.dubbo.rpc.protocol.AbstractInvoker; import org.apache.dubbo.rpc.protocol.AbstractInvoker;
@ -317,11 +318,18 @@ public class InjvmInvoker<T> extends AbstractInvoker<T> {
} }
Object value = originValue; Object value = originValue;
ClassLoader cl = Thread.currentThread().getContextClassLoader(); ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
try { try {
ServiceModel consumerServiceModel = getUrl().getServiceModel(); // 1. By default, the classloader of the current Thread is the consumer class loader.
if (consumerServiceModel != null) { ClassLoader consumerClassLoader = contextClassLoader;
Thread.currentThread().setContextClassLoader(consumerServiceModel.getClassLoader()); 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); Type[] returnTypes = RpcUtils.getReturnTypes(invocation);
if (returnTypes == null) { if (returnTypes == null) {
return originValue; return originValue;
@ -334,7 +342,7 @@ public class InjvmInvoker<T> extends AbstractInvoker<T> {
} }
return value; return value;
} finally { } finally {
Thread.currentThread().setContextClassLoader(cl); Thread.currentThread().setContextClassLoader(contextClassLoader);
} }
} }