@SPI("dubbo")
public interface AdaptiveExt {
@Adaptive
// 单元测试方法 4 的注解为 @Adaptive({"t"})
String echo(String msg, URL url);
}
public class DubboAdaptiveExt implements AdaptiveExt {
@Override
public String echo(String msg, URL url) {return "dubbo";}
}
public class SpringCloudAdaptiveExt implements AdaptiveExt {
@Override
public String echo(String msg, URL url) {return "spring cloud";}
}
// 单元测试 3 中加上 @Adaptive 注解, 其余不加
@Adaptive
public class ThriftAdaptiveExt implements AdaptiveExt {
@Override
public String echo(String msg, URL url) {return "thrift";}
}
// SPI 上有注解,@SPI("dubbo"),url 无参数, 没有类上添加 @Adaptive 注解, 方法 @Adaptive 注解上无参数, 输出 dubbo
@Test
public void test1(){ExtensionLoader<AdaptiveExt> loader = ExtensionLoader.getExtensionLoader(AdaptiveExt.class);
AdaptiveExt adaptiveExtension = loader.getAdaptiveExtension();
URL url = URL.valueOf("test://localhost/test");
System.out.println(adaptiveExtension.echo("d", url));
}
public T getAdaptiveExtension() {Object instance = cachedAdaptiveInstance.get();
if (instance == null) {if (createAdaptiveInstanceError == null) {synchronized (cachedAdaptiveInstance) {instance = cachedAdaptiveInstance.get();
if (instance == null) {
try {
// 创建自适应拓展代理类对象并放入缓存
instance = createAdaptiveExtension();
cachedAdaptiveInstance.set(instance);
} catch (Throwable t) {// 抛异常}
}
}
} else {// 抛异常}
}
return (T) instance;
}
private T createAdaptiveExtension() {
try {
// 分为 3 步:1 是创建自适应拓展代理类 Class 对象,2 是通过反射创建对象,3 是给创建的对象按需依赖注入
return injectExtension((T) getAdaptiveExtensionClass().newInstance());
} catch (Exception e) {// 抛异常}
}
private Class<?> getAdaptiveExtensionClass() {
// 从默认目录中加载标注了 @SPI 注解的实现类
getExtensionClasses();
// 如果有标注了 @Adaptive 注解实现类, 那么 cachedAdaptiveClass 不为空, 直接返回
if (cachedAdaptiveClass != null) {return cachedAdaptiveClass;}
// 创建自适应拓展代理类 class 文件
return cachedAdaptiveClass = createAdaptiveExtensionClass();}
private Class<?> createAdaptiveExtensionClass() {
// code 就是保存了创建的 class 字符串数据
String code = createAdaptiveExtensionClassCode();
ClassLoader classLoader = findClassLoader();
Compiler compiler = ExtensionLoader.getExtensionLoader(Compiler.class).getAdaptiveExtension();
return compiler.compile(code, classLoader);
}
private String createAdaptiveExtensionClassCode() {
// 用来存放生成的代理类 class 文件
StringBuilder codeBuilder = new StringBuilder();
// 遍历标注有 @SPI 注解的接口的所有方法, 这里分析的是 com.alibaba.dubbo.demo.provider.adaptive.AdaptiveExt
Method[] methods = type.getMethods();
// 这些方法中应当致至少有一个方法被 @Adaptive 注解标注, 否则不需要生成自适应代理类, 直接抛出异常
boolean hasAdaptiveAnnotation = false;
for (Method m : methods) {if (m.isAnnotationPresent(Adaptive.class)) {
hasAdaptiveAnnotation = true;
break;
}
}
// no need to generate adaptive class since there's no adaptive method found.
if (!hasAdaptiveAnnotation)
// 抛异常
// 生成包信息, 形如 package com.alibaba.dubbo.demo.provider.adaptive;
codeBuilder.append("package").append(type.getPackage().getName()).append(";");
// 生成导包信息, 形如 import com.alibaba.dubbo.common.extension.ExtensionLoader;
codeBuilder.append("\nimport").append(ExtensionLoader.class.getName()).append(";");
// 生成类名, 形如 public class AdaptiveExt$Adaptive
// implements com.alibaba.dubbo.demo.provider.adaptive.AdaptiveExt {codeBuilder.append("\npublic class").append(type.getSimpleName()).append("$Adaptive").
append("implements").append(type.getCanonicalName()).append("{");
// 遍历所有方法, 为 SPI 接口的所有方法生成代理方法
for (Method method : methods) {
// 方法返回值、参数、抛出异常
Class<?> rt = method.getReturnType();
Class<?>[] pts = method.getParameterTypes();
Class<?>[] ets = method.getExceptionTypes();
// 获取方法上的 Adaptive 注解, 如果方法上没有该注解, 直接为该方法抛出异常
Adaptive adaptiveAnnotation = method.getAnnotation(Adaptive.class);
StringBuilder code = new StringBuilder(512);
if (adaptiveAnnotation == null) {code.append("throw new UnsupportedOperationException(\"method ")
.append(method.toString()).append("of interface")
.append(type.getName()).append("is not adaptive method!\");");
} else {// urlTypeIndex 用来记录 URL 这个参数在第几个参数位置上, 这里 String echo(String msg, URL url);
// 在位置 1 上
int urlTypeIndex = -1;
for (int i = 0; i < pts.length; ++i) {if (pts[i].equals(URL.class)) {
urlTypeIndex = i;
break;
}
}
// 找到了 URL 参数
if (urlTypeIndex != -1) {
// 空指针检查
// s 形如:if (arg1 == null) throw new IllegalArgumentException("url == null");
String s = String.format("\nif (arg%d == null)
throw new IllegalArgumentException(\"url == null\");",urlTypeIndex);
code.append(s);
// s 形如:com.alibaba.dubbo.common.URL url = arg1;
s = String.format("\n%s url = arg%d;", URL.class.getName(), urlTypeIndex);
code.append(s);
}
// 没找到, 暂不分析,TODO
// 获取方法上的 Adaptive 注解的值,@Adaptive({"t"}), 这里是 t
String[] value = adaptiveAnnotation.value();
// 如果 @Adaptive 注解没有值, 对应第二种测试情况, 从接口名生成从 url 中获取参数的 key,
// key 为 adaptive.ext, 获取参数为 url.getParameter("adaptive.ext", "dubbo")
// 因为第二种情况 URL 中传递了 adaptive.ext 这个参数,
// 所以 String extName = url.getParameter("t", "dubbo"); 中获取的是 cloud
if (value.length == 0) {char[] charArray = type.getSimpleName().toCharArray();
StringBuilder sb = new StringBuilder(128);
for (int i = 0; i < charArray.length; i++) {if (Character.isUpperCase(charArray[i])) {if (i != 0) {sb.append(".");
}
sb.append(Character.toLowerCase(charArray[i]));
} else {sb.append(charArray[i]);
}
}
value = new String[]{sb.toString()};
}
// hasInvocation 暂不分析,TODO
// defaultExtName 是 dubbo,cachedDefaultName = names[0], 这个值是 @SPI("dubbo") 里的
String defaultExtName = cachedDefaultName;
String getNameCode = null;
for (int i = value.length - 1; i >= 0; --i) {if (i == value.length - 1) {if (null != defaultExtName) {if (!"protocol".equals(value[i]))
if (hasInvocation)
getNameCode =
String.format("url.getMethodParameter(methodName,
\"%s\", \"%s\")", value[i], defaultExtName);
else
// 形如:url.getParameter("t", "dubbo");
// 理解就是看 url 中有没有传 t 参数, 传了就以 url 中为准, 否则就取
// @SPI("dubbo") 中的为默认值
getNameCode = String.format("url.getParameter(\"%s\", \"%s\")",
value[i], defaultExtName);
else
getNameCode = String.format("( url.getProtocol() == null ?
\"%s\" : url.getProtocol())", defaultExtName);
}
else {if (!"protocol".equals(value[i]))
if (hasInvocation)
getNameCode = String.format("url.getMethodParameter(methodName,
\"%s\", \"%s\")", value[i], defaultExtName);
else
getNameCode = String.format("url.getParameter(\"%s\")", value[i]);
else
getNameCode = "url.getProtocol()";}
} else {if (!"protocol".equals(value[i]))
if (hasInvocation)
getNameCode = String.format("url.getMethodParameter(methodName,
\"%s\", \"%s\")", value[i], defaultExtName);
else
getNameCode = String.format("url.getParameter(\"%s\", %s)",
value[i], getNameCode);
else
getNameCode = String.format("url.getProtocol() == null ?
(%s) : url.getProtocol()", getNameCode);
}
}
// 形如:String extName = url.getParameter("t", "dubbo");
// 这个 extName 就是要为 @SPI 标注的接口生成哪个代理类
code.append("\nString extName =").append(getNameCode).append(";");
// check extName == null?
// 形如:if (extName == null) throw new IllegalStateException("...");
String s = String.format("\nif(extName == null)" +
"throw new IllegalStateException(\"Fail to get extension(%s)
name from url(\"+ url.toString() + \") use keys(%s)\");",
type.getName(), Arrays.toString(value));
code.append(s);
// AdaptiveExt extension = (AdaptiveExt)
// ExtensionLoader.getExtensionLoader(AdaptiveExt.class).getExtension(extName);
s = String.format("\n%s extension = (%<s)%s.getExtensionLoader(%s.class).
getExtension(extName);",type.getName(),
ExtensionLoader.class.getSimpleName(), type.getName());
code.append(s);
// return statement
if (!rt.equals(void.class)) {code.append("\nreturn");
}
// 形如:return extension.echo(arg0, arg1);
s = String.format("extension.%s(", method.getName());
code.append(s);
for (int i = 0; i < pts.length; i++) {if (i != 0)
code.append(",");
code.append("arg").append(i);
}
code.append(");");
}
// 加上方法名, 形如:public java.lang.String echo(java.lang.String arg0,
// com.alibaba.dubbo.common.URL arg1) {codeBuilder.append("\npublic").append(rt.getCanonicalName()).append(" ").
append(method.getName()).append("(");
for (int i = 0; i < pts.length; i++) {if (i > 0) {codeBuilder.append(",");
}
codeBuilder.append(pts[i].getCanonicalName());
codeBuilder.append(" ");
codeBuilder.append("arg").append(i);
}
codeBuilder.append(")");
// 异常
if (ets.length > 0) {codeBuilder.append("throws");
for (int i = 0; i < ets.length; i++) {if (i > 0) {codeBuilder.append(",");
}
codeBuilder.append(ets[i].getCanonicalName());
}
}
codeBuilder.append("{");
codeBuilder.append(code.toString());
codeBuilder.append("\n}");
}
codeBuilder.append("\n}");
if (logger.isDebugEnabled()) {logger.debug(codeBuilder.toString());
}
return codeBuilder.toString();}