Skip to content

Commit

Permalink
feat: SPI 自适应拓展分析
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaochangbai committed Dec 20, 2021
1 parent 7727611 commit ad2b60f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -714,7 +715,9 @@ public T getAdaptiveExtension() {
instance = cachedAdaptiveInstance.get();
if (instance == null) {
try {
// 创建自适应拓展
instance = createAdaptiveExtension();
// 设置自适应拓展到缓存中
cachedAdaptiveInstance.set(instance);
} catch (Throwable t) {
createAdaptiveInstanceError = t;
Expand Down Expand Up @@ -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);
Expand All @@ -1267,10 +1272,12 @@ private T createAdaptiveExtension() {
}

private Class<?> getAdaptiveExtensionClass() {
// 通过 SPI 获取所有的拓展类
getExtensionClasses();
if (cachedAdaptiveClass != null) {
return cachedAdaptiveClass;
}
// 创建自适应拓展类
return cachedAdaptiveClass = createAdaptiveExtensionClass();
}

Expand All @@ -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);
}

Expand Down

0 comments on commit ad2b60f

Please sign in to comment.