From e4e2124b46d7f0b089947b02a995ce07f5484b81 Mon Sep 17 00:00:00 2001 From: FlyJingFish Date: Mon, 18 Nov 2024 21:20:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=9B=BF=E6=8D=A2=E5=88=87?= =?UTF-8?q?=E9=9D=A2=E4=B9=9F=E6=9B=BF=E6=8D=A2super=E5=92=8C=E6=9E=84?= =?UTF-8?q?=E9=80=A0=E6=96=B9=E6=B3=95=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MethodReplaceInvokeAdapter.kt | 12 +++++++++-- .../flyjingfish/androidaop/MainActivity.kt | 3 +++ .../androidaop/testReplace/BaseBean.java | 19 +++++++++++++++++ .../androidaop/testReplace/BeanH.java | 12 +++++++++++ .../androidaop/testReplace/ReplaceBaseBean.kt | 19 +++++++++++++++++ .../testReplace/ReplaceBaseBean2.java | 21 +++++++++++++++++++ 6 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/com/flyjingfish/androidaop/testReplace/BaseBean.java create mode 100644 app/src/main/java/com/flyjingfish/androidaop/testReplace/BeanH.java create mode 100644 app/src/main/java/com/flyjingfish/androidaop/testReplace/ReplaceBaseBean.kt create mode 100644 app/src/main/java/com/flyjingfish/androidaop/testReplace/ReplaceBaseBean2.java diff --git a/android-aop-plugin/src/main/kotlin/com/flyjingfish/android_aop_plugin/scanner_visitor/MethodReplaceInvokeAdapter.kt b/android-aop-plugin/src/main/kotlin/com/flyjingfish/android_aop_plugin/scanner_visitor/MethodReplaceInvokeAdapter.kt index 0e1fe991..94748bf2 100644 --- a/android-aop-plugin/src/main/kotlin/com/flyjingfish/android_aop_plugin/scanner_visitor/MethodReplaceInvokeAdapter.kt +++ b/android-aop-plugin/src/main/kotlin/com/flyjingfish/android_aop_plugin/scanner_visitor/MethodReplaceInvokeAdapter.kt @@ -62,6 +62,8 @@ class MethodReplaceInvokeAdapter(private val className:String,private val superN descriptor: String, isInterface: Boolean ) { + val isInMethodInner = opcode == Opcodes.INVOKESPECIAL && owner == superName && name == methodName && descriptor == methodDesc + var replaceMethodInfo = getReplaceInfo(owner, name, "") var isReplaceClass = replaceMethodInfo != null && replaceMethodInfo.replaceType == ReplaceMethodInfo.ReplaceType.NEW && replaceMethodInfo.newClassName.isNotEmpty() if (!isReplaceClass){ @@ -91,10 +93,16 @@ class MethodReplaceInvokeAdapter(private val className:String,private val superN } else { descriptor.replace("(", "(L${replaceMethodInfo.oldOwner};") == replaceMethodInfo.newMethodDesc || descriptor.replace("(", "(Ljava/lang/Object;") == replaceMethodInfo.newMethodDesc } - if (shouldReplace) { + if (shouldReplace && !isInMethodInner) { + val isThisInit = owner == className && methodName == "" && methodName == name + val isInitAop = replaceMethodInfo.replaceType == ReplaceMethodInfo.ReplaceType.INIT + if (isInitAop && isThisInit){ + super.visitMethodInsn(opcode, owner, name, descriptor, isInterface) + return + } if (replaceMethodInfo.replaceType == ReplaceMethodInfo.ReplaceType.NEW && replaceMethodInfo.isCallNew()) { super.visitMethodInsn(opcode, replaceMethodInfo.newClassName, name, descriptor, isInterface) - }else if (replaceMethodInfo.replaceType == ReplaceMethodInfo.ReplaceType.INIT) { + }else if (isInitAop) { super.visitMethodInsn(opcode, owner, name, descriptor, isInterface) } InitConfig.addReplaceMethodInfo(replaceMethodInfo) diff --git a/app/src/main/java/com/flyjingfish/androidaop/MainActivity.kt b/app/src/main/java/com/flyjingfish/androidaop/MainActivity.kt index 9f204b9b..e27e03a7 100644 --- a/app/src/main/java/com/flyjingfish/androidaop/MainActivity.kt +++ b/app/src/main/java/com/flyjingfish/androidaop/MainActivity.kt @@ -27,6 +27,7 @@ import com.flyjingfish.androidaop.test2.StaticClass import com.flyjingfish.androidaop.test.TestBean import com.flyjingfish.test_lib.mycut.TestParams import com.flyjingfish.androidaop.test.TestReplace +import com.flyjingfish.androidaop.testReplace.BaseBean import com.flyjingfish.test_lib.BaseActivity import com.flyjingfish.test_lib.annotation.MyAnno3 import com.flyjingfish.test_lib.PermissionRejectListener @@ -76,6 +77,8 @@ class MainActivity: BaseActivity2(), PermissionRejectListener{ } }) + val bbean = BaseBean(0,0) + bbean.test() // binding.btnIOThread.setOnClickListener { // onIOThread() // } diff --git a/app/src/main/java/com/flyjingfish/androidaop/testReplace/BaseBean.java b/app/src/main/java/com/flyjingfish/androidaop/testReplace/BaseBean.java new file mode 100644 index 00000000..e7f0e7c0 --- /dev/null +++ b/app/src/main/java/com/flyjingfish/androidaop/testReplace/BaseBean.java @@ -0,0 +1,19 @@ +package com.flyjingfish.androidaop.testReplace; + +public class BaseBean { + int num1; + int num2; + + public BaseBean(int num1, int num2) { + this.num1 = num1; + this.num2 = num2; + } + + public BaseBean(int num1) { + this(num1,0); + } + + public void test(){ + + } +} diff --git a/app/src/main/java/com/flyjingfish/androidaop/testReplace/BeanH.java b/app/src/main/java/com/flyjingfish/androidaop/testReplace/BeanH.java new file mode 100644 index 00000000..b510d8f3 --- /dev/null +++ b/app/src/main/java/com/flyjingfish/androidaop/testReplace/BeanH.java @@ -0,0 +1,12 @@ +package com.flyjingfish.androidaop.testReplace; + +public class BeanH extends BaseBean{ + public BeanH(int num1, int num2) { + super(num1, num2); + } + + @Override + public void test() { + super.test(); + } +} diff --git a/app/src/main/java/com/flyjingfish/androidaop/testReplace/ReplaceBaseBean.kt b/app/src/main/java/com/flyjingfish/androidaop/testReplace/ReplaceBaseBean.kt new file mode 100644 index 00000000..4b03022e --- /dev/null +++ b/app/src/main/java/com/flyjingfish/androidaop/testReplace/ReplaceBaseBean.kt @@ -0,0 +1,19 @@ +package com.flyjingfish.androidaop.testReplace + +import com.flyjingfish.android_aop_annotation.anno.AndroidAopReplaceClass +import com.flyjingfish.android_aop_annotation.anno.AndroidAopReplaceMethod + +//@AndroidAopReplaceClass("com.flyjingfish.androidaop.testReplace.BaseBean") +object ReplaceBaseBean { + @AndroidAopReplaceMethod("(int,int)") + @JvmStatic + fun getBaseBean(testBean: BaseBean) : BaseBean { + return testBean + } + + @AndroidAopReplaceMethod("void test()") + @JvmStatic + fun test(testBean: BaseBean){ + testBean.test() + } +} \ No newline at end of file diff --git a/app/src/main/java/com/flyjingfish/androidaop/testReplace/ReplaceBaseBean2.java b/app/src/main/java/com/flyjingfish/androidaop/testReplace/ReplaceBaseBean2.java new file mode 100644 index 00000000..67fc5804 --- /dev/null +++ b/app/src/main/java/com/flyjingfish/androidaop/testReplace/ReplaceBaseBean2.java @@ -0,0 +1,21 @@ +package com.flyjingfish.androidaop.testReplace; + +import android.util.Log; + +import com.flyjingfish.android_aop_annotation.anno.AndroidAopReplaceClass; +import com.flyjingfish.android_aop_annotation.anno.AndroidAopReplaceMethod; + +@AndroidAopReplaceClass("com.flyjingfish.androidaop.testReplace.BaseBean") +public class ReplaceBaseBean2 { + @AndroidAopReplaceMethod("(int,int)") + public static BaseBean getBaseBean(BaseBean testBean) { + Log.e("ReplaceBaseBean2","getBaseBean"); + return testBean; + } + + @AndroidAopReplaceMethod("void test()") + public static void test(BaseBean testBean){ + Log.e("ReplaceBaseBean2","test"); + testBean.test(); + } +}