Skip to content

Commit

Permalink
1、修复修改继承类切面的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
FlyJingFish committed May 16, 2024
1 parent b21df0b commit 6d7aaae
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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(
Expand All @@ -25,6 +28,7 @@ open class ReplaceBaseClassVisitor(
superName: String?,
interfaces: Array<out String>?
) {
oldSuperName = superName
thisClassName = slashToDotClassName(name)
hasCollect = WovenInfoUtils.aopCollectClassMap[thisClassName] != null
val replaceExtendsClassName = WovenInfoUtils.getModifyExtendsClass(slashToDotClassName(name))
Expand All @@ -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)
}
Expand All @@ -58,12 +63,15 @@ open class ReplaceBaseClassVisitor(
)
if (hasCollect && name == "<clinit>"){
isHasStaticClock = true
mv = MyMethodAdapter(mv, access, name, descriptor)
mv = MethodStaticAdapter(mv, access, name, descriptor)
}
if (modifyExtendsClassName != null && name == "<init>"){
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) {
Expand All @@ -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 == "<init>" && 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
}
}
}
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion app/src/main/res/layout/activity_second.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
/>
<Button
android:id="@+id/btn_inner"
Expand Down Expand Up @@ -88,6 +88,11 @@
app:layout_constraintTop_toTopOf="parent"
android:textAllCaps="false"
android:text="\@Delay 2 Stop"/>
<ImageView
android:id="@+id/iv_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/ic_launcher_round"/>
<androidx.core.widget.NestedScrollView
android:layout_width="0dp"
android:layout_height="0dp"
Expand Down

0 comments on commit 6d7aaae

Please sign in to comment.