From d9deb2f5da99091409be0e5e2fa718db57dff2bd Mon Sep 17 00:00:00 2001 From: shifujun Date: Tue, 12 Dec 2023 15:36:53 +0800 Subject: [PATCH] =?UTF-8?q?fix(core.transform-kit):=20=E6=A3=80=E6=9F=A5?= =?UTF-8?q?=E7=BC=BA=E5=B0=91=E6=96=B9=E6=B3=95=E6=97=B6=E5=85=88=E7=9C=8B?= =?UTF-8?q?=E6=97=A7=E7=B1=BB=E6=98=AF=E5=90=A6=E6=9C=89=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 典型的场景应该是androidx等库对新版本Android SDK新增API的依赖非常及时, 但这些依赖并不总是有用。在低版本Android SDK上编译时,新增API不存在对 这些库实际上没有影响。 我们检查替换类名时也应该先看看原先依赖的方法是否能找到。如果本来就找不到, 那我们的新类也没必要一定实现缺失的方法。 #1251 --- .../core/transform_kit/ReplaceClassName.kt | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/projects/sdk/core/transform-kit/src/main/kotlin/com/tencent/shadow/core/transform_kit/ReplaceClassName.kt b/projects/sdk/core/transform-kit/src/main/kotlin/com/tencent/shadow/core/transform_kit/ReplaceClassName.kt index 86c5d7059..cbfa63005 100644 --- a/projects/sdk/core/transform-kit/src/main/kotlin/com/tencent/shadow/core/transform_kit/ReplaceClassName.kt +++ b/projects/sdk/core/transform-kit/src/main/kotlin/com/tencent/shadow/core/transform_kit/ReplaceClassName.kt @@ -25,7 +25,7 @@ import javassist.expr.ExprEditor import javassist.expr.MethodCall object ReplaceClassName { - private val mNewNames = mutableSetOf() + private val oldToNewRenameMap = mutableMapOf() /** * MutableMap>> @@ -34,13 +34,13 @@ object ReplaceClassName { mutableMapOf() fun resetErrorCount() { - mNewNames.clear() + oldToNewRenameMap.clear() errorResult.clear() } fun replaceClassName(ctClass: CtClass, oldName: String, newName: String) { ctClass.replaceClassName(oldName, newName) - mNewNames.add(newName) + oldToNewRenameMap[oldName] = newName } fun checkAll( @@ -49,9 +49,12 @@ object ReplaceClassName { ): Map>> { inputClassNames.forEach { inputClassName -> val inputClass = classPool[inputClassName] - if (inputClass.refClasses.any { mNewNames.contains(it) }) { - mNewNames.forEach { newName -> - inputClass.checkMethodExist(classPool[newName]) + val oldNames = oldToNewRenameMap.keys + val newNames = oldToNewRenameMap.values + if (inputClass.refClasses.any { newNames.contains(it) }) { + oldNames.forEach { oldName -> + val newName = oldToNewRenameMap[oldName] + inputClass.checkMethodExist(classPool[oldName], classPool[newName]) } } } @@ -61,14 +64,21 @@ object ReplaceClassName { /** * 检查ctClass对refClassName引用的方法确实都存在 */ - private fun CtClass.checkMethodExist(refClass: CtClass) { + private fun CtClass.checkMethodExist(oldClass: CtClass, newClass: CtClass) { val invokeClass = name - val refClassName = refClass.name + val refClassName = newClass.name instrument(object : ExprEditor() { override fun edit(m: MethodCall) { if (m.className == refClassName) { try { - refClass.getMethod(m.methodName, m.signature) + oldClass.getMethod(m.methodName, m.signature) + } catch (ignored: NotFoundException) { + //替换前旧的类就没有这个方法,就不用管替换后的类是否实现了。 + return + } + + try { + newClass.getMethod(m.methodName, m.signature) } catch (ignored: NotFoundException) { val methodString = "${m.methodName}:${m.signature}" var methodMap = errorResult[refClassName]