From 6d7aaae5331166146d00b355478fb9f3b72a43b4 Mon Sep 17 00:00:00 2001 From: tianxiangyu <96164429+FlyJingFish@users.noreply.github.com> Date: Thu, 16 May 2024 14:41:16 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E4=BF=AE=E5=A4=8D=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E7=BB=A7=E6=89=BF=E7=B1=BB=E5=88=87=E9=9D=A2=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ReplaceBaseClassVisitor.kt | 45 +++++++++++++++++-- app/build.gradle | 2 +- .../androidaop/SecondActivity.java | 1 + app/src/main/res/layout/activity_second.xml | 7 ++- 4 files changed, 49 insertions(+), 6 deletions(-) diff --git a/android-aop-plugin/src/main/kotlin/com/flyjingfish/android_aop_plugin/scanner_visitor/ReplaceBaseClassVisitor.kt b/android-aop-plugin/src/main/kotlin/com/flyjingfish/android_aop_plugin/scanner_visitor/ReplaceBaseClassVisitor.kt index fa9b6c0a..f1d84721 100644 --- a/android-aop-plugin/src/main/kotlin/com/flyjingfish/android_aop_plugin/scanner_visitor/ReplaceBaseClassVisitor.kt +++ b/android-aop-plugin/src/main/kotlin/com/flyjingfish/android_aop_plugin/scanner_visitor/ReplaceBaseClassVisitor.kt @@ -1,9 +1,10 @@ package com.flyjingfish.android_aop_plugin.scanner_visitor +import com.flyjingfish.android_aop_plugin.utils.ClassPoolUtils import com.flyjingfish.android_aop_plugin.utils.InitConfig -import com.flyjingfish.android_aop_plugin.utils.Utils import com.flyjingfish.android_aop_plugin.utils.Utils.computeMD5 import com.flyjingfish.android_aop_plugin.utils.Utils.dotToSlash +import com.flyjingfish.android_aop_plugin.utils.Utils.slashToDot import com.flyjingfish.android_aop_plugin.utils.Utils.slashToDotClassName import com.flyjingfish.android_aop_plugin.utils.WovenInfoUtils import org.objectweb.asm.ClassVisitor @@ -15,6 +16,8 @@ open class ReplaceBaseClassVisitor( classVisitor: ClassVisitor ) : ClassVisitor(Opcodes.ASM9, classVisitor) { lateinit var thisClassName:String + private var oldSuperName:String?=null + private var modifyExtendsClassName:String?=null var isHasStaticClock = false var hasCollect = false override fun visit( @@ -25,6 +28,7 @@ open class ReplaceBaseClassVisitor( superName: String?, interfaces: Array? ) { + oldSuperName = superName thisClassName = slashToDotClassName(name) hasCollect = WovenInfoUtils.aopCollectClassMap[thisClassName] != null val replaceExtendsClassName = WovenInfoUtils.getModifyExtendsClass(slashToDotClassName(name)) @@ -35,7 +39,8 @@ open class ReplaceBaseClassVisitor( } if (!replaceExtendsClassName.isNullOrEmpty() && !newReplaceExtendsClassName.isNullOrEmpty()){ InitConfig.useModifyClassInfo(slashToDotClassName(name)) - super.visit(version, access, name, signature, dotToSlash(newReplaceExtendsClassName), interfaces) + modifyExtendsClassName = dotToSlash(newReplaceExtendsClassName) + super.visit(version, access, name, signature, modifyExtendsClassName, interfaces) }else{ super.visit(version, access, name, signature, superName, interfaces) } @@ -58,12 +63,15 @@ open class ReplaceBaseClassVisitor( ) if (hasCollect && name == ""){ isHasStaticClock = true - mv = MyMethodAdapter(mv, access, name, descriptor) + mv = MethodStaticAdapter(mv, access, name, descriptor) + } + if (modifyExtendsClassName != null && name == ""){ + mv = MethodInitAdapter(mv) } return mv } - inner class MyMethodAdapter(mv: MethodVisitor, access: Int, name: String, desc: String?) : + inner class MethodStaticAdapter(mv: MethodVisitor, access: Int, name: String, desc: String?) : AdviceAdapter(Opcodes.ASM9, mv, access, name, desc) { override fun visitInsn(opcode: Int) { @@ -86,4 +94,33 @@ open class ReplaceBaseClassVisitor( super.onMethodExit(opcode) } } + + inner class MethodInitAdapter(methodVisitor: MethodVisitor?) : + MethodVisitor(Opcodes.ASM9, methodVisitor) { + override fun visitMethodInsn( + opcode: Int, + owner: String, + name: String, + descriptor: String, + isInterface: Boolean + ) { + val extendClass = modifyExtendsClassName + if (name == "" && oldSuperName == owner && extendClass != null && hasConstructor(slashToDot(extendClass),descriptor)) { + super.visitMethodInsn(opcode, extendClass, name, descriptor, isInterface) + } else { + super.visitMethodInsn(opcode, owner, name, descriptor, isInterface) + } + } + + private fun hasConstructor(extendClass:String, descriptor: String):Boolean{ + val constructor = try { + val cp = ClassPoolUtils.getNewClassPool() + val ctClass = cp.get(extendClass) + ctClass.getConstructor(descriptor) + } catch (e: Exception) { + null + } + return constructor != null + } + } } \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 8c10486f..3719f867 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -13,7 +13,7 @@ androidAopConfig { // debug 为true打开日志方便调试 debug true // 测试 - include 'com.flyjingfish' + include 'com.flyjingfish','androidx.appcompat.widget','com.google.android.material.textview' //默认关闭,开启后将会生成切点信息json文件在 /build/tmp/cutInfo.json cutInfoJson true increment true diff --git a/app/src/main/java/com/flyjingfish/androidaop/SecondActivity.java b/app/src/main/java/com/flyjingfish/androidaop/SecondActivity.java index 3328da55..c62505f8 100644 --- a/app/src/main/java/com/flyjingfish/androidaop/SecondActivity.java +++ b/app/src/main/java/com/flyjingfish/androidaop/SecondActivity.java @@ -74,6 +74,7 @@ public void testIntArray(int[] intArray,int[][] intArray1,int[][][] intArray2,in private void onSingleClick() { testIntArray(new int[]{0,1},null,null,0,null,null,null,null,null,null,null,null); Log.e("Test_click", "onSingleClick"); + binding.ivImage.setImageResource(R.mipmap.ic_launcher); } @Override diff --git a/app/src/main/res/layout/activity_second.xml b/app/src/main/res/layout/activity_second.xml index 6d64b665..3177648c 100644 --- a/app/src/main/res/layout/activity_second.xml +++ b/app/src/main/res/layout/activity_second.xml @@ -14,7 +14,7 @@ app:flow_wrapMode="chain" app:constraint_referenced_ids="btn_inner, btn_onScheduled1, btn_onScheduled2,btn_onScheduled1_stop, - btn_onScheduled2_stop,btn_onDelay1,btn_onDelay2,btn_onDelay1_stop,btn_onDelay2_stop" + btn_onScheduled2_stop,btn_onDelay1,btn_onDelay2,btn_onDelay1_stop,btn_onDelay2_stop,iv_image" />