From e32b21a88f73347daef589f4d31b0f6e63380790 Mon Sep 17 00:00:00 2001 From: tianxiangyu <96164429+FlyJingFish@users.noreply.github.com> Date: Fri, 10 May 2024 18:35:39 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E5=AE=8C=E5=96=84=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E7=9A=84=E5=88=87=E9=9D=A2=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AndroidAopSymbolProcessor.kt | 26 ++++++++++++++++- .../android_aop_plugin/beans/AopCollectCut.kt | 6 +++- .../utils/WovenInfoUtils.kt | 2 +- .../AndroidAopProcessor.java | 28 +++++++++++++++++-- .../test_lib/collect/InitCollect.kt | 6 ++++ .../test_lib/collect/InitCollect2.java | 5 ++++ 6 files changed, 68 insertions(+), 5 deletions(-) diff --git a/android-aop-ksp/src/main/java/com/flyjingfish/android_aop_ksp/AndroidAopSymbolProcessor.kt b/android-aop-ksp/src/main/java/com/flyjingfish/android_aop_ksp/AndroidAopSymbolProcessor.kt index 669e6350..516c7911 100644 --- a/android-aop-ksp/src/main/java/com/flyjingfish/android_aop_ksp/AndroidAopSymbolProcessor.kt +++ b/android-aop-ksp/src/main/java/com/flyjingfish/android_aop_ksp/AndroidAopSymbolProcessor.kt @@ -38,6 +38,8 @@ import com.squareup.kotlinpoet.KModifier import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy import com.squareup.kotlinpoet.TypeSpec import java.lang.annotation.ElementType +import java.security.MessageDigest +import java.security.NoSuchAlgorithmException import java.util.Locale class AndroidAopSymbolProcessor(private val codeGenerator: CodeGenerator, @@ -441,6 +443,7 @@ class AndroidAopSymbolProcessor(private val codeGenerator: CodeGenerator, clazzName = parent.toString() parent = parent?.parent } + if (symbol.parameters.isEmpty()){ throw IllegalArgumentException("注意:函数$className${symbol} 必须设置您想收集的类作为参数") }else if (symbol.parameters.size != 1){ @@ -482,7 +485,7 @@ class AndroidAopSymbolProcessor(private val codeGenerator: CodeGenerator, // logger.error("invokeClassName=$invokeClassName") val parameter = symbol.parameters[0] val collectClassName = "${parameter.type.resolve().declaration.packageName.asString()}.${parameter.type}" - + clazzName += computeMD5("$symbol($collectClassName)") val fileName = "${clazzName}\$\$AndroidAopClass"; val typeBuilder = TypeSpec.classBuilder( fileName @@ -551,4 +554,25 @@ class AndroidAopSymbolProcessor(private val codeGenerator: CodeGenerator, private fun whatsMyName(name: String): FunSpec.Builder { return FunSpec.builder(name).addModifiers(KModifier.FINAL) } + + private fun computeMD5(string: String): String? { + return try { + val messageDigest = MessageDigest.getInstance("MD5") + val digestBytes = messageDigest.digest(string.toByteArray()) + bytesToHex(digestBytes) + } catch (var3: NoSuchAlgorithmException) { + throw IllegalStateException(var3) + } + } + private fun bytesToHex(bytes: ByteArray): String? { + val hexString = StringBuilder() + for (b in bytes) { + val hex = Integer.toHexString(0xff and b.toInt()) + if (hex.length == 1) { + hexString.append('0') + } + hexString.append(hex) + } + return hexString.toString() + } } \ No newline at end of file diff --git a/android-aop-plugin/src/main/kotlin/com/flyjingfish/android_aop_plugin/beans/AopCollectCut.kt b/android-aop-plugin/src/main/kotlin/com/flyjingfish/android_aop_plugin/beans/AopCollectCut.kt index b1a6a290..d5b0935a 100644 --- a/android-aop-plugin/src/main/kotlin/com/flyjingfish/android_aop_plugin/beans/AopCollectCut.kt +++ b/android-aop-plugin/src/main/kotlin/com/flyjingfish/android_aop_plugin/beans/AopCollectCut.kt @@ -4,4 +4,8 @@ data class AopCollectCut( val collectClassName: String, val invokeClassName: String, val invokeMethod: String, -) +){ + fun getKey():String{ + return invokeClassName + invokeMethod + collectClassName + } +} diff --git a/android-aop-plugin/src/main/kotlin/com/flyjingfish/android_aop_plugin/utils/WovenInfoUtils.kt b/android-aop-plugin/src/main/kotlin/com/flyjingfish/android_aop_plugin/utils/WovenInfoUtils.kt index d5ae4753..316126e2 100644 --- a/android-aop-plugin/src/main/kotlin/com/flyjingfish/android_aop_plugin/utils/WovenInfoUtils.kt +++ b/android-aop-plugin/src/main/kotlin/com/flyjingfish/android_aop_plugin/utils/WovenInfoUtils.kt @@ -446,7 +446,7 @@ object WovenInfoUtils { } fun addCollectConfig(aopCollectCut: AopCollectCut){ - aopCollectInfoMap[aopCollectCut.invokeClassName] = aopCollectCut + aopCollectInfoMap[aopCollectCut.getKey()] = aopCollectCut } fun addCollectClass(aopCollectCut: AopCollectClass){ diff --git a/android-aop-processor/src/main/java/com/flyjingfish/android_aop_processor/AndroidAopProcessor.java b/android-aop-processor/src/main/java/com/flyjingfish/android_aop_processor/AndroidAopProcessor.java index 93b517de..5563de9d 100644 --- a/android-aop-processor/src/main/java/com/flyjingfish/android_aop_processor/AndroidAopProcessor.java +++ b/android-aop-processor/src/main/java/com/flyjingfish/android_aop_processor/AndroidAopProcessor.java @@ -31,6 +31,8 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.reflect.Method; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; import java.util.LinkedHashSet; import java.util.Map; import java.util.Set; @@ -385,8 +387,13 @@ private void processCollectMethod(Set set, RoundEnvironme throw new IllegalArgumentException("注意:函数"+element.getEnclosingElement()+"."+name1+" 参数必须设置一个"); } VariableElement variableElement = executableElement.getParameters().get(0); - - TypeSpec.Builder typeBuilder = TypeSpec.classBuilder(element.getEnclosingElement().getSimpleName()+"$$AndroidAopClass") + String clazzName; + try { + clazzName = element.getEnclosingElement().getSimpleName()+computeMD5(name1+"("+variableElement.asType()+")"); + } catch (NoSuchAlgorithmException e) { + clazzName = element.getEnclosingElement().getSimpleName().toString(); + } + TypeSpec.Builder typeBuilder = TypeSpec.classBuilder(clazzName+"$$AndroidAopClass") .addAnnotation(AopClass.class) .addModifiers(Modifier.PUBLIC, Modifier.FINAL); MethodSpec.Builder whatsMyName1 = whatsMyName(AOP_METHOD_NAME) @@ -410,6 +417,23 @@ private void processCollectMethod(Set set, RoundEnvironme } } } + private static String computeMD5(String message) throws NoSuchAlgorithmException { + MessageDigest md = MessageDigest.getInstance("MD5"); + byte[] digest = md.digest(message.getBytes()); + return bytesToHex(digest); + } + + private static String bytesToHex(byte[] bytes) { + StringBuilder hexString = new StringBuilder(); + for (byte b : bytes) { + String hex = Integer.toHexString(0xff & b); + if (hex.length() == 1) { + hexString.append('0'); + } + hexString.append(hex); + } + return hexString.toString(); + } private static MethodSpec.Builder whatsMyName(String name) { return MethodSpec.methodBuilder(name) .addModifiers(Modifier.PUBLIC, Modifier.FINAL); diff --git a/test-lib/src/main/java/com/flyjingfish/test_lib/collect/InitCollect.kt b/test-lib/src/main/java/com/flyjingfish/test_lib/collect/InitCollect.kt index cd989f54..345a7d45 100644 --- a/test-lib/src/main/java/com/flyjingfish/test_lib/collect/InitCollect.kt +++ b/test-lib/src/main/java/com/flyjingfish/test_lib/collect/InitCollect.kt @@ -15,6 +15,12 @@ object InitCollect { Collects.add(sub) } + @AndroidAopCollectMethod + @JvmStatic + fun collect2(sub: SubApplication){ + Collects.add(sub) + } + @MyAnno fun init(application: Application){ Log.e("InitCollect","----init----"); diff --git a/test-lib/src/main/java/com/flyjingfish/test_lib/collect/InitCollect2.java b/test-lib/src/main/java/com/flyjingfish/test_lib/collect/InitCollect2.java index 85bf8daa..de0681d5 100644 --- a/test-lib/src/main/java/com/flyjingfish/test_lib/collect/InitCollect2.java +++ b/test-lib/src/main/java/com/flyjingfish/test_lib/collect/InitCollect2.java @@ -17,6 +17,11 @@ public static void collect(SubApplication2 sub){ collects.add(sub); } + @AndroidAopCollectMethod + public static void collect2(SubApplication2 sub){ + collects.add(sub); + } + @MyAnno public static void init(Application application){ Log.e("InitCollect2","----init----");