Skip to content

Commit 2018cb7

Browse files
committed
新增保持代码
1 parent 4aaef32 commit 2018cb7

4 files changed

Lines changed: 39 additions & 5 deletions

File tree

android-aop-annotation/src/main/java/com/flyjingfish/android_aop_annotation/AndroidAopJoinPoint.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,11 +300,17 @@ private void getTargetMethod(){
300300
try {
301301
originalMethod = tClass.getDeclaredMethod(originalMethodName, classes);
302302
} catch (NoSuchMethodException exc) {
303-
String realMethodName = getRealMethodName(originalMethodName);
304-
if (realMethodName == null){
303+
// String realMethodName = getRealMethodName(originalMethodName);
304+
// if (realMethodName == null){
305+
// throw new RuntimeException(exc);
306+
// }
307+
// originalMethod = tClass.getDeclaredMethod(realMethodName, classes);
308+
System.out.println("=======>"+originalMethodName);
309+
originalMethod = Utils.INSTANCE.findMethodWithKeepName(tClass,originalMethodName,classes);
310+
if (originalMethod == null){
305311
throw new RuntimeException(exc);
306312
}
307-
originalMethod = tClass.getDeclaredMethod(realMethodName, classes);
313+
System.out.println("=======>"+originalMethodName+"<==Success="+originalMethod.getName());
308314
}
309315
targetMethod.setAccessible(true);
310316
originalMethod.setAccessible(true);
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.flyjingfish.android_aop_annotation.aop_anno
22

33

4-
@Retention(AnnotationRetention.BINARY)
4+
@Retention(AnnotationRetention.RUNTIME)
55
@Target(
66
AnnotationTarget.TYPE,
77
AnnotationTarget.ANNOTATION_CLASS,
@@ -11,4 +11,4 @@ package com.flyjingfish.android_aop_annotation.aop_anno
1111
AnnotationTarget.PROPERTY_GETTER,
1212
AnnotationTarget.FIELD
1313
)
14-
annotation class AopKeep
14+
annotation class AopKeep(val keepName: String = "")

android-aop-annotation/src/main/java/com/flyjingfish/android_aop_annotation/utils/Utils.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.flyjingfish.android_aop_annotation.utils
22

33
import com.flyjingfish.android_aop_annotation.AndroidAopJoinPoint
44
import com.flyjingfish.android_aop_annotation.ProceedReturn
5+
import com.flyjingfish.android_aop_annotation.aop_anno.AopKeep
56
import com.flyjingfish.android_aop_annotation.base.OnBaseSuspendReturnListener
67
import com.flyjingfish.android_aop_annotation.base.OnSuspendReturnListener
78
import com.flyjingfish.android_aop_annotation.base.OnSuspendReturnListener2
@@ -154,4 +155,22 @@ internal object Utils {
154155
val matcher: Matcher = InvokeStaticClassPattern.matcher(className)
155156
return matcher.find()
156157
}
158+
159+
fun findMethodWithKeepName(
160+
clazz: Class<*>,
161+
keepName: String,
162+
parameterTypes: Array<Class<*>>
163+
): Method? {
164+
return clazz.declaredMethods.firstOrNull { method ->
165+
// 1. 参数列表必须完全匹配
166+
if (!method.parameterTypes.contentEquals(parameterTypes)) {
167+
return@firstOrNull false
168+
}
169+
// 2. 注解必须存在并且 keepName 一致
170+
val annotation = method.getAnnotation(AopKeep::class.java)
171+
annotation?.keepName == keepName
172+
}
173+
}
174+
175+
157176
}

android-aop-plugin/src/main/kotlin/com/flyjingfish/android_aop_plugin/scanner_visitor/WovenIntoCode.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import javassist.bytecode.AnnotationsAttribute
3838
import javassist.bytecode.AttributeInfo
3939
import javassist.bytecode.ConstPool
4040
import javassist.bytecode.annotation.Annotation
41+
import javassist.bytecode.annotation.StringMemberValue
4142
import kotlinx.coroutines.Deferred
4243
import kotlinx.coroutines.Dispatchers
4344
import kotlinx.coroutines.async
@@ -954,6 +955,14 @@ object WovenIntoCode {
954955
val ctMethodNoneHasKeep = annotationsAttribute.getAnnotation(KEEP_CLASS) == null
955956
if (ctMethodNoneHasKeep){
956957
val annotation = Annotation(KEEP_CLASS, constPool)
958+
959+
// 给注解添加参数 keepName = 方法名
960+
val methodName = this.name
961+
annotation.addMemberValue(
962+
"keepName",
963+
StringMemberValue(methodName, constPool)
964+
)
965+
957966
annotationsAttribute.addAnnotation(annotation)
958967
if (visibleTagAttribute == null){
959968
methodInfo.addAttribute(annotationsAttribute)

0 commit comments

Comments
 (0)