From 813fcbab3d1125120bd84b1dc62ab20da63a7612 Mon Sep 17 00:00:00 2001 From: liujingxing <327744707@qq.com> Date: Sun, 17 Oct 2021 22:02:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=BC=BA@Converter=E6=B3=A8=E8=A7=A3?= =?UTF-8?q?=E7=9A=84=E9=80=BB=E8=BE=91=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/rxhttp/compiler/ConverterVisitor.kt | 46 +++++++++++-------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/rxhttp-compiler/src/main/java/com/rxhttp/compiler/ConverterVisitor.kt b/rxhttp-compiler/src/main/java/com/rxhttp/compiler/ConverterVisitor.kt index d0c073f8..3b3b4915 100755 --- a/rxhttp-compiler/src/main/java/com/rxhttp/compiler/ConverterVisitor.kt +++ b/rxhttp-compiler/src/main/java/com/rxhttp/compiler/ConverterVisitor.kt @@ -58,24 +58,32 @@ private fun checkConverterValidClass(element: VariableElement, types: Types) { ) } var classType = element.asType() - if ("rxhttp.wrapper.callback.IConverter" != classType.toString()) { - while (true) { - //TypeMirror转TypeElement - val currentClass = types.asElement(classType) as TypeElement - //遍历实现的接口有没有IConverter接口 - for (mirror in currentClass.interfaces) { - if (mirror.toString() == "rxhttp.wrapper.callback.IConverter") { - return - } - } - //未遍历到IConverter,则找到父类继续,一直循环下去,直到最顶层的父类 - classType = currentClass.superclass - if (classType.kind == TypeKind.NONE) { - throw ProcessingException( - element, - "The variable ${element.simpleName} is not a IConverter" - ) - } + val iConverter = "rxhttp.wrapper.callback.IConverter" + while (true) { + if (iConverter == classType.toString()) return + //TypeMirror转TypeElement + val currentClass = types.asElement(classType) as TypeElement + //遍历实现的接口有没有IConverter接口 + if (currentClass.findIConverter(types, iConverter)) return + //未遍历到IConverter,则找到父类继续,一直循环下去,直到最顶层的父类 + classType = currentClass.superclass + if (classType.kind == TypeKind.NONE) { + throw ProcessingException( + element, + "The variable ${element.simpleName} is not a IConverter" + ) + } + } +} + +fun TypeElement.findIConverter(types: Types, iConverter: String): Boolean { + for (mirror in interfaces) { + return if (mirror.toString() == "rxhttp.wrapper.callback.IConverter") { + true + } else { + (types.asElement(mirror) as? TypeElement)?.findIConverter(types, iConverter) + ?: return false } } -} \ No newline at end of file + return false +}