From 9b8328133608c6823d6e797b78b97b4526b550ae Mon Sep 17 00:00:00 2001 From: FlyJingFish Date: Tue, 2 Apr 2024 22:05:45 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E5=AE=8C=E5=96=84=E6=9B=BF=E6=8D=A2?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E8=B0=83=E7=94=A8=E5=88=87=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../anno/AndroidAopReplaceClass.kt | 4 +- .../AssembleAndroidAopTask.kt | 53 ++++++++++++------- .../android_aop_plugin/utils/FileHashUtils.kt | 6 --- .../utils/WovenInfoUtils.kt | 20 ++++--- 4 files changed, 46 insertions(+), 37 deletions(-) diff --git a/android-aop-annotation/src/main/java/com/flyjingfish/android_aop_annotation/anno/AndroidAopReplaceClass.kt b/android-aop-annotation/src/main/java/com/flyjingfish/android_aop_annotation/anno/AndroidAopReplaceClass.kt index 33c84f86..b60456da 100644 --- a/android-aop-annotation/src/main/java/com/flyjingfish/android_aop_annotation/anno/AndroidAopReplaceClass.kt +++ b/android-aop-annotation/src/main/java/com/flyjingfish/android_aop_annotation/anno/AndroidAopReplaceClass.kt @@ -14,10 +14,10 @@ annotation class AndroidAopReplaceClass( */ val value: String, /** - * + * 当所设置的值 ***不是 [MatchType.SELF] 时*** 会拖慢打包速度 * @return 返回匹配类型 [MatchType] */ - val type: MatchType = MatchType.EXTENDS, + val type: MatchType = MatchType.SELF, /** * * @return 排除继承中的类名数组(包含包名) diff --git a/android-aop-plugin/src/main/kotlin/com/flyjingfish/android_aop_plugin/AssembleAndroidAopTask.kt b/android-aop-plugin/src/main/kotlin/com/flyjingfish/android_aop_plugin/AssembleAndroidAopTask.kt index f222fd5a..1b89c938 100644 --- a/android-aop-plugin/src/main/kotlin/com/flyjingfish/android_aop_plugin/AssembleAndroidAopTask.kt +++ b/android-aop-plugin/src/main/kotlin/com/flyjingfish/android_aop_plugin/AssembleAndroidAopTask.kt @@ -11,6 +11,7 @@ import com.flyjingfish.android_aop_plugin.scanner_visitor.MethodReplaceInvokeVis import com.flyjingfish.android_aop_plugin.scanner_visitor.RegisterMapWovenInfoCode import com.flyjingfish.android_aop_plugin.scanner_visitor.ReplaceBaseClassVisitor import com.flyjingfish.android_aop_plugin.scanner_visitor.WovenIntoCode +import com.flyjingfish.android_aop_plugin.utils.AndroidConfig import com.flyjingfish.android_aop_plugin.utils.ClassPoolUtils import com.flyjingfish.android_aop_plugin.utils.FileHashUtils import com.flyjingfish.android_aop_plugin.utils.InitConfig @@ -166,10 +167,33 @@ abstract class AssembleAndroidAopTask : DefaultTask() { // InitConfig.saveBuildConfig() ClassPoolUtils.initClassPool() FileHashUtils.isChangeAopMatch = WovenInfoUtils.aopMatchsChanged() - FileHashUtils.isChangeAopReplace = WovenInfoUtils.aopReplacesChanged() } private fun searchJoinPointLocation(){ + val androidConfig = AndroidConfig(project) + val list: List = androidConfig.getBootClasspath() + for (file in list) { + try { + val jarFile = JarFile(file) + val enumeration = jarFile.entries() + while (enumeration.hasMoreElements()) { + val jarEntry = enumeration.nextElement() + try { + val entryName = jarEntry.name + if (entryName.endsWith(Utils._CLASS)) { + val className = entryName.replace(".class","") + WovenInfoUtils.addExtendsReplace(Utils.slashToDot(className)) + } + } catch (_: Exception) { + + } + } + jarFile.close() + } catch (e: Exception) { + e.printStackTrace() + } + } + val includes = AndroidAopConfig.includes val excludes = AndroidAopConfig.excludes val addClassMethodRecords = mutableMapOf() @@ -187,14 +211,6 @@ abstract class AssembleAndroidAopTask : DefaultTask() { val bytes = inputs.readAllBytes() if (bytes.isNotEmpty()){ - val inAsm3 = FileHashUtils.isAsmScan(file.absolutePath,bytes,3) - if (inAsm3){ - - if (file.absolutePath.endsWith(_CLASS)){ - val className = file.absolutePath.replace("$directoryPath/","").replace(".class","") - WovenInfoUtils.addExtendsReplace(Utils.slashToDot(className)) - } - } val inAsm = FileHashUtils.isAsmScan(file.absolutePath,bytes,2) if (inAsm){ @@ -225,6 +241,11 @@ abstract class AssembleAndroidAopTask : DefaultTask() { } } } + + if (file.absolutePath.endsWith(_CLASS)){ + val className = file.absolutePath.replace("$directoryPath/","").replace(".class","") + WovenInfoUtils.addExtendsReplace(Utils.slashToDot(className)) + } } } } @@ -235,7 +256,7 @@ abstract class AssembleAndroidAopTask : DefaultTask() { val jarEntry = enumeration.nextElement() try { val entryName = jarEntry.name - if (jarEntry.isDirectory || jarEntry.name.isEmpty()) { + if (jarEntry.isDirectory || entryName.isEmpty() || entryName.startsWith("META-INF/") || "module-info.class" == entryName) { continue } val isClassFile = entryName.endsWith(_CLASS) @@ -247,14 +268,6 @@ abstract class AssembleAndroidAopTask : DefaultTask() { jarFile.getInputStream(jarEntry).use { inputs -> val bytes = inputs.readAllBytes(); if (bytes.isNotEmpty()){ - val inAsm3 = FileHashUtils.isAsmScan(entryName,bytes,3) - if (inAsm3){ - - if (entryName.endsWith(_CLASS)){ - val className = entryName.replace(".class","") - WovenInfoUtils.addExtendsReplace(Utils.slashToDot(className)) - } - } val inAsm = FileHashUtils.isAsmScan(entryName,bytes,2) if (inAsm){ WovenInfoUtils.deleteClassMethodRecord(entryName) @@ -284,6 +297,10 @@ abstract class AssembleAndroidAopTask : DefaultTask() { } } } + if (entryName.endsWith(_CLASS)){ + val className = entryName.replace(".class","") + WovenInfoUtils.addExtendsReplace(Utils.slashToDot(className)) + } } catch (e: Exception) { if (!(e is ZipException && e.message?.startsWith("duplicate entry:") == true)) { logger.error("Merge jar error entry:[${jarEntry.name}], error message:$e") diff --git a/android-aop-plugin/src/main/kotlin/com/flyjingfish/android_aop_plugin/utils/FileHashUtils.kt b/android-aop-plugin/src/main/kotlin/com/flyjingfish/android_aop_plugin/utils/FileHashUtils.kt index b3e3f9b7..a977a8b6 100644 --- a/android-aop-plugin/src/main/kotlin/com/flyjingfish/android_aop_plugin/utils/FileHashUtils.kt +++ b/android-aop-plugin/src/main/kotlin/com/flyjingfish/android_aop_plugin/utils/FileHashUtils.kt @@ -9,7 +9,6 @@ object FileHashUtils { private var fileHashMap2: HashMap = HashMap() private var fileHashMap3: HashMap = HashMap() var isChangeAopMatch = true - var isChangeAopReplace = true fun isAsmScan(file: String, fileBytes: ByteArray,step: Int): Boolean { if (!AndroidAopConfig.increment){ return true @@ -18,11 +17,6 @@ object FileHashUtils { val hash = getSHA256Hash(fileBytes) WovenInfoUtils.removeClassCache(file) if (step == 2 && isChangeAopMatch){ -// printLog("isAsmScan1 = $file") - putFileHash(file, fileBytes,step) - return true - } - if (step == 3 && isChangeAopReplace){ // printLog("isAsmScan1 = $file") putFileHash(file, fileBytes,step) return true 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 734dd6c6..754ea4c0 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 @@ -28,7 +28,6 @@ object WovenInfoUtils { private val invokeMethodCuts = mutableListOf() private val realInvokeMethodMap = HashMap() private val invokeMethodMap = HashMap() - private val lastInvokeMethodMap = HashMap() private val replaceMethodMap = HashMap() private val replaceMethodInfoMap = HashMap>() val replaceMethodInfoMapUse = HashMap() @@ -169,6 +168,8 @@ object WovenInfoUtils { replaceMethodInfoMapUse.clear() modifyExtendsClassMap.clear() invokeMethodCuts.clear() + realInvokeMethodMap.clear() + invokeMethodCutCache = null if (!AndroidAopConfig.increment) { aopMethodCuts.clear() aopMatchCuts.clear() @@ -189,8 +190,6 @@ object WovenInfoUtils { lastAopMatchCuts.clear() lastAopMatchCuts.putAll(aopMatchCuts) - lastInvokeMethodMap.clear() - lastInvokeMethodMap.putAll(invokeMethodMap) aopMethodCuts.clear() aopMatchCuts.clear() classPaths.clear() @@ -320,15 +319,14 @@ object WovenInfoUtils { return lastAopMatchCuts != aopMatchCuts } - fun aopReplacesChanged(): Boolean { - return lastInvokeMethodMap != invokeMethodMap - } - + private var invokeMethodCutCache : MutableList?= null fun addExtendsReplace(className:String){ - val invokeCuts = invokeMethodCuts.filter { - it.matchType != AopMatchCut.MatchType.SELF.name + if (invokeMethodCutCache == null){ + invokeMethodCutCache = invokeMethodCuts.filter { + it.matchType != AopMatchCut.MatchType.SELF.name + }.toMutableList() } - if (invokeCuts.isEmpty()){ + if (invokeMethodCutCache?.isEmpty() == true){ return } val ctClass = try { @@ -354,7 +352,7 @@ object WovenInfoUtils { return } - invokeCuts.forEach { aopReplaceCut -> + invokeMethodCutCache?.forEach { aopReplaceCut -> val target = Utils.dotToSlash(aopReplaceCut.targetClassName) if (AopMatchCut.MatchType.SELF.name != aopReplaceCut.matchType) {