From 06616659b8015ebe90fc33865958914601775402 Mon Sep 17 00:00:00 2001 From: LoveSy Date: Mon, 2 Dec 2024 23:16:00 +0800 Subject: [PATCH] Only desugar ZipEntry's methods --- .../main/java/DesugarClassVisitorFactory.kt | 71 ++++++++++--------- 1 file changed, 39 insertions(+), 32 deletions(-) diff --git a/buildSrc/src/main/java/DesugarClassVisitorFactory.kt b/buildSrc/src/main/java/DesugarClassVisitorFactory.kt index c99c6c79a..a34ec1ec8 100644 --- a/buildSrc/src/main/java/DesugarClassVisitorFactory.kt +++ b/buildSrc/src/main/java/DesugarClassVisitorFactory.kt @@ -9,19 +9,21 @@ import org.objectweb.asm.Opcodes.ASM9 private const val DESUGAR_CLASS_NAME = "com.topjohnwu.magisk.core.utils.Desugar" private const val ZIP_ENTRY_GET_TIME_DESC = "()Ljava/nio/file/attribute/FileTime;" -private const val DESUGAR_GET_TIME_DESC = "(Ljava/util/zip/ZipEntry;)Ljava/nio/file/attribute/FileTime;" +private const val DESUGAR_GET_TIME_DESC = + "(Ljava/util/zip/ZipEntry;)Ljava/nio/file/attribute/FileTime;" abstract class DesugarClassVisitorFactory : AsmClassVisitorFactory { override fun createClassVisitor( classContext: ClassContext, nextClassVisitor: ClassVisitor ): ClassVisitor { - return DesugarClassVisitor(nextClassVisitor) + return DesugarClassVisitor(classContext, nextClassVisitor) } override fun isInstrumentable(classData: ClassData) = classData.className != DESUGAR_CLASS_NAME - class DesugarClassVisitor(cv: ClassVisitor) : ClassVisitor(ASM9, cv) { + class DesugarClassVisitor(private val classContext: ClassContext, cv: ClassVisitor) : + ClassVisitor(ASM9, cv) { override fun visitMethod( access: Int, name: String?, @@ -29,39 +31,44 @@ abstract class DesugarClassVisitorFactory : AsmClassVisitorFactory? ): MethodVisitor { - return DesugarMethodVisitor(super.visitMethod(access, name, descriptor, signature, exceptions)) - } - } - - class DesugarMethodVisitor(mv: MethodVisitor?) : MethodVisitor(ASM9, mv) { - override fun visitMethodInsn( - opcode: Int, - owner: String, - name: String, - descriptor: String, - isInterface: Boolean - ) { - if (!process(name, descriptor)) { - super.visitMethodInsn(opcode, owner, name, descriptor, isInterface) - } + return DesugarMethodVisitor( + super.visitMethod(access, name, descriptor, signature, exceptions) + ) } - private fun process(name: String, descriptor: String): Boolean { - if (descriptor != ZIP_ENTRY_GET_TIME_DESC) - return false - when (name) { - "getLastModifiedTime", "getLastAccessTime", "getCreationTime" -> { - mv.visitMethodInsn( - Opcodes.INVOKESTATIC, - DESUGAR_CLASS_NAME.replace('.', '/'), - name, - DESUGAR_GET_TIME_DESC, - false - ) + inner class DesugarMethodVisitor(mv: MethodVisitor?) : + MethodVisitor(ASM9, mv) { + override fun visitMethodInsn( + opcode: Int, + owner: String, + name: String, + descriptor: String, + isInterface: Boolean + ) { + if (!classContext.loadClassData(owner.replace("/", "."))?.superClasses.orEmpty() + .contains("java.util.zip.ZipEntry") || !process(name, descriptor) + ) { + super.visitMethodInsn(opcode, owner, name, descriptor, isInterface) + } + } + + private fun process(name: String, descriptor: String): Boolean { + if (descriptor != ZIP_ENTRY_GET_TIME_DESC) + return false + return when (name) { + "getLastModifiedTime", "getLastAccessTime", "getCreationTime" -> { + mv.visitMethodInsn( + Opcodes.INVOKESTATIC, + DESUGAR_CLASS_NAME.replace('.', '/'), + name, + DESUGAR_GET_TIME_DESC, + false + ) + true + } + else -> false } - else -> return false } - return true } } }