Skip to content

Commit

Permalink
1、完善替换方法调用切面
Browse files Browse the repository at this point in the history
  • Loading branch information
FlyJingFish committed Apr 2, 2024
1 parent 9360dc5 commit 9b83281
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 排除继承中的类名数组(包含包名)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<File> = 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<String,ClassMethodRecord>()
Expand All @@ -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){

Expand Down Expand Up @@ -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))
}
}
}
}
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ object FileHashUtils {
private var fileHashMap2: HashMap<String, String> = HashMap()
private var fileHashMap3: HashMap<String, String> = HashMap()
var isChangeAopMatch = true
var isChangeAopReplace = true
fun isAsmScan(file: String, fileBytes: ByteArray,step: Int): Boolean {
if (!AndroidAopConfig.increment){
return true
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ object WovenInfoUtils {
private val invokeMethodCuts = mutableListOf<AopReplaceCut>()
private val realInvokeMethodMap = HashMap<String, String>()
private val invokeMethodMap = HashMap<String, String>()
private val lastInvokeMethodMap = HashMap<String, String>()
private val replaceMethodMap = HashMap<String, String>()
private val replaceMethodInfoMap = HashMap<String, HashMap<String, ReplaceMethodInfo>>()
val replaceMethodInfoMapUse = HashMap<String, ReplaceMethodInfo>()
Expand Down Expand Up @@ -169,6 +168,8 @@ object WovenInfoUtils {
replaceMethodInfoMapUse.clear()
modifyExtendsClassMap.clear()
invokeMethodCuts.clear()
realInvokeMethodMap.clear()
invokeMethodCutCache = null
if (!AndroidAopConfig.increment) {
aopMethodCuts.clear()
aopMatchCuts.clear()
Expand All @@ -189,8 +190,6 @@ object WovenInfoUtils {
lastAopMatchCuts.clear()
lastAopMatchCuts.putAll(aopMatchCuts)

lastInvokeMethodMap.clear()
lastInvokeMethodMap.putAll(invokeMethodMap)
aopMethodCuts.clear()
aopMatchCuts.clear()
classPaths.clear()
Expand Down Expand Up @@ -320,15 +319,14 @@ object WovenInfoUtils {
return lastAopMatchCuts != aopMatchCuts
}

fun aopReplacesChanged(): Boolean {
return lastInvokeMethodMap != invokeMethodMap
}

private var invokeMethodCutCache : MutableList<AopReplaceCut>?= 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 {
Expand All @@ -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) {

Expand Down

0 comments on commit 9b83281

Please sign in to comment.