Skip to content

Commit

Permalink
1、修复suspend函数多次调用问题
Browse files Browse the repository at this point in the history
  • Loading branch information
FlyJingFish committed Jul 9, 2024
1 parent 6f4043a commit 917951f
Show file tree
Hide file tree
Showing 11 changed files with 324 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,6 @@ public void setArgClasses(Class[] argClasses) {

public Object joinPointExecute(Continuation continuation) {
isSuspend = continuation != null;
if (isSuspend && !isStartClass()){
Object returnValue = null;
if (invokeMethod != null){
returnValue = invokeMethod.invoke(target,mArgs);
}else if (targetMethod != null){
try {
returnValue = targetMethod.invoke(target,mArgs);
} catch (IllegalAccessException | InvocationTargetException ignore) {
}
}
return returnValue;
}

ProceedJoinPoint proceedJoinPoint = new ProceedJoinPoint(targetClass, mArgs,target,isSuspend);
proceedJoinPoint.setOriginalMethod(originalMethod);
Expand Down Expand Up @@ -224,15 +212,4 @@ private static String getRealMethodName(String staticMethodName) {
return null;
}

private boolean isStartClass() {
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
for (StackTraceElement element : stackTrace) {
String fromClassName = element.getClassName();
if (fromClassName.startsWith(targetClass.getName() + "$"+ originalMethodName+"$")){
return false;
}

}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.flyjingfish.android_aop_plugin.beans

data class ReplaceInnerClassInfo(
val className:String,
val methodName:String,
val methodDescriptor:String,
val targetMethodName:String
)
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ open class MethodReplaceInvokeVisitor(
access: Int,
name: String,
signature: String?,
superName: String?,
superName: String,
interfaces: Array<out String>?
) {
super.visit(version, access, name, signature, superName, interfaces)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,21 @@ open class ReplaceBaseClassVisitor(
classVisitor: ClassVisitor
) : ClassVisitor(Opcodes.ASM9, classVisitor) {
lateinit var thisClassName:String
private var oldSuperName:String?=null
lateinit var clazzName:String
lateinit var oldSuperName:String
var modifyExtendsClassName:String?=null
var isHasStaticClock = false
var hasCollect = false
var modifyed = false
override fun visit(
version: Int,
access: Int,
name: String,
signature: String?,
superName: String?,
superName: String,
interfaces: Array<out String>?
) {
clazzName = name
oldSuperName = superName
thisClassName = slashToDotClassName(name)
hasCollect = WovenInfoUtils.aopCollectClassMap[thisClassName] != null
Expand Down Expand Up @@ -68,6 +71,12 @@ open class ReplaceBaseClassVisitor(
if (modifyExtendsClassName != null && name == "<init>"){
mv = MethodInitAdapter(mv)
}
mv = ReplaceInvokeMethodVisitor(mv,clazzName,oldSuperName)
mv.onResultListener = object :ReplaceInvokeMethodVisitor.OnResultListener{
override fun onBack() {
modifyed = true
}
}
return mv
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.flyjingfish.android_aop_plugin.scanner_visitor

import com.flyjingfish.android_aop_plugin.utils.WovenInfoUtils
import com.flyjingfish.android_aop_plugin.utils.printLog
import org.objectweb.asm.AnnotationVisitor
import org.objectweb.asm.MethodVisitor
import org.objectweb.asm.Opcodes

class ReplaceInvokeMethodVisitor(methodVisitor: MethodVisitor?,private val clazzName:String,
private val superClazzName:String) :
MethodVisitor(Opcodes.ASM9, methodVisitor) {
interface OnResultListener{
fun onBack()
}
var onResultListener : OnResultListener?= null
override fun visitMethodInsn(
opcode: Int,
owner: String,
name: String,
descriptor: String,
isInterface: Boolean
) {
val info = WovenInfoUtils.getAopMethodCutInnerClassInfo(owner,name,descriptor,clazzName,superClazzName)
val invokeName = if (info == null){
name
}else{
if (owner == info.className
&& info.methodName == name
&& descriptor == info.methodDescriptor
&& WovenInfoUtils.isHasAopMethodCutInnerClassInfoInvokeMethod(owner,info.targetMethodName,descriptor)){
onResultListener?.onBack()
info.targetMethodName
}else{
name
}
}
super.visitMethodInsn(opcode, owner, invokeName, descriptor, isInterface)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.flyjingfish.android_aop_plugin.utils.WovenInfoUtils.isLeaf
import com.flyjingfish.android_aop_plugin.utils.instanceof
import com.flyjingfish.android_aop_plugin.utils.isHasMethodBody
import com.flyjingfish.android_aop_plugin.utils.isStaticMethod
import com.flyjingfish.android_aop_plugin.utils.printLog
import org.objectweb.asm.AnnotationVisitor
import org.objectweb.asm.Handle
import org.objectweb.asm.MethodVisitor
Expand All @@ -30,6 +31,7 @@ import org.objectweb.asm.commons.Method
import org.objectweb.asm.tree.ClassNode
import org.objectweb.asm.tree.InvokeDynamicInsnNode
import org.objectweb.asm.tree.MethodNode
import java.io.File
import java.util.UUID
import java.util.regex.Matcher
import java.util.regex.Pattern
Expand Down Expand Up @@ -252,6 +254,7 @@ class SearchAopMethodVisitor(val onCallBackMethod: OnCallBackMethod?) :
methodName.cutInfo[UUID.randomUUID().toString()] = cutInfo
}
onCallBackMethod?.onBackMethodRecord(methodName)
WovenInfoUtils.addAopMethodCutInnerClassInfo(className,methodname,methoddescriptor)
}
}

Expand Down Expand Up @@ -326,6 +329,8 @@ class SearchAopMethodVisitor(val onCallBackMethod: OnCallBackMethod?) :
)
methodRecord.cutInfo[UUID.randomUUID().toString()] = cutInfo
onCallBackMethod?.onBackMethodRecord(methodRecord)

WovenInfoUtils.addAopMethodCutInnerClassInfo(className,name,descriptor)
}
// printLog("$aopMatchCut === ${aopMatchCut.isMatchAllMethod()}")
if (aopMatchCut.isMatchAllMethod() ){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import java.util.regex.Matcher
import java.util.regex.Pattern

object WovenIntoCode {
private const val METHOD_SUFFIX = "\$\$AndroidAOP"
const val METHOD_SUFFIX = "\$\$AndroidAOP"
@Throws(Exception::class)
fun modifyClass(
inputStreamBytes: ByteArray?,
Expand Down Expand Up @@ -79,7 +79,7 @@ object WovenIntoCode {
access: Int,
name: String,
signature: String?,
superName: String?,
superName: String,
interfaces: Array<out String>?
) {
super.visit(version, access, name, signature, superName, interfaces)
Expand Down Expand Up @@ -114,7 +114,7 @@ object WovenIntoCode {
access: Int,
name: String,
signature: String?,
superName: String?,
superName: String,
interfaces: Array<out String>?
) {
super.visit(version, access, name, signature, superName, interfaces)
Expand Down Expand Up @@ -157,7 +157,7 @@ object WovenIntoCode {
access: Int,
name: String,
signature: String?,
superName: String?,
superName: String,
interfaces: Array<out String>?
) {
super.visit(version, access, name, signature, superName, interfaces)
Expand Down Expand Up @@ -228,9 +228,10 @@ object WovenIntoCode {
}else{
ACC_PUBLIC + ACC_FINAL
}
val newMethodName = "$oldMethodName$$$classNameMd5$METHOD_SUFFIX"
var mv: MethodVisitor? = super.visitMethod(
newAccess,
"$oldMethodName$$$classNameMd5$METHOD_SUFFIX",
newMethodName,
descriptor,
signature,
exceptions
Expand All @@ -239,6 +240,7 @@ object WovenIntoCode {
if (hasReplace && mv != null && access.isHasMethodBody()) {
mv = MethodReplaceInvokeAdapter(className,"$name$descriptor",mv)
}
WovenInfoUtils.addAopMethodCutInnerClassInfoInvokeMethod(className,newMethodName,descriptor)
RemoveAnnotation(mv)
} else {
null
Expand Down
Loading

0 comments on commit 917951f

Please sign in to comment.