Skip to content

Commit 813fcba

Browse files
committed
加强@converter注解的逻辑判断
1 parent e44b87a commit 813fcba

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

rxhttp-compiler/src/main/java/com/rxhttp/compiler/ConverterVisitor.kt

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,24 +58,32 @@ private fun checkConverterValidClass(element: VariableElement, types: Types) {
5858
)
5959
}
6060
var classType = element.asType()
61-
if ("rxhttp.wrapper.callback.IConverter" != classType.toString()) {
62-
while (true) {
63-
//TypeMirror转TypeElement
64-
val currentClass = types.asElement(classType) as TypeElement
65-
//遍历实现的接口有没有IConverter接口
66-
for (mirror in currentClass.interfaces) {
67-
if (mirror.toString() == "rxhttp.wrapper.callback.IConverter") {
68-
return
69-
}
70-
}
71-
//未遍历到IConverter,则找到父类继续,一直循环下去,直到最顶层的父类
72-
classType = currentClass.superclass
73-
if (classType.kind == TypeKind.NONE) {
74-
throw ProcessingException(
75-
element,
76-
"The variable ${element.simpleName} is not a IConverter"
77-
)
78-
}
61+
val iConverter = "rxhttp.wrapper.callback.IConverter"
62+
while (true) {
63+
if (iConverter == classType.toString()) return
64+
//TypeMirror转TypeElement
65+
val currentClass = types.asElement(classType) as TypeElement
66+
//遍历实现的接口有没有IConverter接口
67+
if (currentClass.findIConverter(types, iConverter)) return
68+
//未遍历到IConverter,则找到父类继续,一直循环下去,直到最顶层的父类
69+
classType = currentClass.superclass
70+
if (classType.kind == TypeKind.NONE) {
71+
throw ProcessingException(
72+
element,
73+
"The variable ${element.simpleName} is not a IConverter"
74+
)
75+
}
76+
}
77+
}
78+
79+
fun TypeElement.findIConverter(types: Types, iConverter: String): Boolean {
80+
for (mirror in interfaces) {
81+
return if (mirror.toString() == "rxhttp.wrapper.callback.IConverter") {
82+
true
83+
} else {
84+
(types.asElement(mirror) as? TypeElement)?.findIConverter(types, iConverter)
85+
?: return false
7986
}
8087
}
81-
}
88+
return false
89+
}

0 commit comments

Comments
 (0)