[Dubbo-2446]Fix ClassNotFoundException when load Service Interface by parent ClassLoader (#2447)

* 用jarslink做的module隔离,dubbo调用时会抛ClassNotFoundException.经查是ClassLoader不一致。为了严谨,做此修改,以确保Service Interface和Invocation Interface的ClassLoader是同一个。

* invokerInterface非空判断
This commit is contained in:
邓志 2018-09-11 15:32:25 +08:00 committed by Jerrick Zhu
parent 51f0677c8e
commit e18810baba
2 changed files with 14 additions and 2 deletions

View File

@ -591,6 +591,14 @@ public final class ReflectUtils {
}
}
public static Class<?> forName(ClassLoader cl, String name) {
try {
return name2class(cl, name);
} catch (ClassNotFoundException e) {
throw new IllegalStateException("Not found class " + name + ", cause: " + e.getMessage(), e);
}
}
/**
* name to class.
* "boolean" => boolean.class

View File

@ -49,7 +49,9 @@ public class RpcUtils {
&& !invocation.getMethodName().startsWith("$")) {
String service = invocation.getInvoker().getUrl().getServiceInterface();
if (service != null && service.length() > 0) {
Class<?> cls = ReflectUtils.forName(service);
Class<?> invokerInterface = invocation.getInvoker().getInterface();
Class<?> cls = invokerInterface != null ? ReflectUtils.forName(invokerInterface.getClassLoader(), service)
: ReflectUtils.forName(service);
Method method = cls.getMethod(invocation.getMethodName(), invocation.getParameterTypes());
if (method.getReturnType() == void.class) {
return null;
@ -71,7 +73,9 @@ public class RpcUtils {
&& !invocation.getMethodName().startsWith("$")) {
String service = invocation.getInvoker().getUrl().getServiceInterface();
if (service != null && service.length() > 0) {
Class<?> cls = ReflectUtils.forName(service);
Class<?> invokerInterface = invocation.getInvoker().getInterface();
Class<?> cls = invokerInterface != null ? ReflectUtils.forName(invokerInterface.getClassLoader(), service)
: ReflectUtils.forName(service);
Method method = cls.getMethod(invocation.getMethodName(), invocation.getParameterTypes());
if (method.getReturnType() == void.class) {
return null;