From ad2b60f9978877efdbbfeb01d50a6eef744a8477 Mon Sep 17 00:00:00 2001 From: xchb <704566072@qq.com> Date: Mon, 20 Dec 2021 17:02:26 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20SPI=20=E8=87=AA=E9=80=82=E5=BA=94?= =?UTF-8?q?=E6=8B=93=E5=B1=95=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/extension/AdaptiveClassCodeGenerator.java | 7 +++++++ .../apache/dubbo/common/extension/ExtensionLoader.java | 10 ++++++++++ 2 files changed, 17 insertions(+) diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/extension/AdaptiveClassCodeGenerator.java b/dubbo-common/src/main/java/org/apache/dubbo/common/extension/AdaptiveClassCodeGenerator.java index 61a9a8d3cdd0..5702b3ed7a74 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/extension/AdaptiveClassCodeGenerator.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/extension/AdaptiveClassCodeGenerator.java @@ -218,9 +218,11 @@ private String generateUrlNullCheck(int index) { private String generateMethodContent(Method method) { Adaptive adaptiveAnnotation = method.getAnnotation(Adaptive.class); StringBuilder code = new StringBuilder(512); + // 如果方法上无 Adaptive 注解,则生成 throw new UnsupportedOperationException(...) 代码 if (adaptiveAnnotation == null) { return generateUnsupported(method); } else { + // 遍历参数列表,确定 URL 参数位置 int urlTypeIndex = getUrlTypeIndex(method); // found parameter in URL type @@ -374,6 +376,11 @@ private String generateUrlAssignmentIndirectly(Method method) { for (int i = 0; i < pts.length; ++i) { for (Method m : pts[i].getMethods()) { String name = m.getName(); + // 1. 方法名以 get 开头,或方法名大于3个字符 + // 2. 方法的访问权限为 public + // 3. 非静态方法 + // 4. 方法参数数量为0 + // 5. 方法返回值类型为 URL if ((name.startsWith("get") || name.length() > 3) && Modifier.isPublic(m.getModifiers()) && !Modifier.isStatic(m.getModifiers()) diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/extension/ExtensionLoader.java b/dubbo-common/src/main/java/org/apache/dubbo/common/extension/ExtensionLoader.java index fe62e2911245..7a5b24790347 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/extension/ExtensionLoader.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/extension/ExtensionLoader.java @@ -702,6 +702,7 @@ public void replaceExtension(String name, Class clazz) { @SuppressWarnings("unchecked") public T getAdaptiveExtension() { checkDestroyed(); + // 从缓存中获取自适应拓展 Object instance = cachedAdaptiveInstance.get(); if (instance == null) { if (createAdaptiveInstanceError != null) { @@ -714,7 +715,9 @@ public T getAdaptiveExtension() { instance = cachedAdaptiveInstance.get(); if (instance == null) { try { + // 创建自适应拓展 instance = createAdaptiveExtension(); + // 设置自适应拓展到缓存中 cachedAdaptiveInstance.set(instance); } catch (Throwable t) { createAdaptiveInstanceError = t; @@ -1255,8 +1258,10 @@ private String findAnnotationName(Class clazz) { @SuppressWarnings("unchecked") private T createAdaptiveExtension() { try { + // 获取自适应拓展类,并通过反射实例化 T instance = (T) getAdaptiveExtensionClass().newInstance(); instance = postProcessBeforeInitialization(instance, null); + //注入依赖 instance = injectExtension(instance); instance = postProcessAfterInitialization(instance, null); initExtension(instance); @@ -1267,10 +1272,12 @@ private T createAdaptiveExtension() { } private Class getAdaptiveExtensionClass() { + // 通过 SPI 获取所有的拓展类 getExtensionClasses(); if (cachedAdaptiveClass != null) { return cachedAdaptiveClass; } + // 创建自适应拓展类 return cachedAdaptiveClass = createAdaptiveExtensionClass(); } @@ -1284,9 +1291,12 @@ private Class createAdaptiveExtensionClass() { } catch (Throwable ignore) { } + // 构建自适应拓展代码 String code = new AdaptiveClassCodeGenerator(type, cachedDefaultName).generate(); + // 获取编译器实现类 org.apache.dubbo.common.compiler.Compiler compiler = extensionDirector.getExtensionLoader( org.apache.dubbo.common.compiler.Compiler.class).getAdaptiveExtension(); + // 编译代码,生成 Class return compiler.compile(code, classLoader); }