Skip to content

Commit

Permalink
修复替换切面也替换super和构造方法的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
FlyJingFish committed Nov 18, 2024
1 parent 3ad4c76 commit e4e2124
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down Expand Up @@ -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 == "<init>" && 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)
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/com/flyjingfish/androidaop/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -76,6 +77,8 @@ class MainActivity: BaseActivity2(), PermissionRejectListener{
}

})
val bbean = BaseBean(0,0)
bbean.test()
// binding.btnIOThread.setOnClickListener {
// onIOThread()
// }
Expand Down
Original file line number Diff line number Diff line change
@@ -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(){

}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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("<init>(int,int)")
@JvmStatic
fun getBaseBean(testBean: BaseBean) : BaseBean {
return testBean
}

@AndroidAopReplaceMethod("void test()")
@JvmStatic
fun test(testBean: BaseBean){
testBean.test()
}
}
Original file line number Diff line number Diff line change
@@ -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("<init>(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();
}
}

0 comments on commit e4e2124

Please sign in to comment.