didi / booster

🚀Optimizer for mobile applications
https://booster.johnsonlee.io
Apache License 2.0
4.9k stars 579 forks source link

ZipFile.transform catch all transform exception #428

Open gentrio opened 1 year ago

gentrio commented 1 year ago
fun ZipFile.transform(
        output: OutputStream,
        entryFactory: (ZipEntry) -> ZipArchiveEntry = ::ZipArchiveEntry,
        transformer: (ByteArray) -> ByteArray = { it -> it }
) {
    val entries = mutableSetOf<String>()
    val creator = ParallelScatterZipCreator(ThreadPoolExecutor(NCPU, NCPU, 0L, TimeUnit.MILLISECONDS, LinkedBlockingQueue<Runnable>(), Executors.defaultThreadFactory(), RejectedExecutionHandler { runnable, _ ->
        runnable.run()
    }))

    entries().asSequence().filterNot {
        isJarSignatureRelatedFiles(it.name)
    }.forEach { entry ->
        if (!entries.contains(entry.name)) {
            val zae = entryFactory(entry)
            val stream = InputStreamSupplier {
                when (entry.name.substringAfterLast('.', "")) {
                    "class" -> getInputStream(entry).use { src ->
                        try {
                            src.transform(transformer).inputStream()
                        } catch (e: Throwable) {
                            System.err.println("Broken class: ${this.name}!/${entry.name}")
                            getInputStream(entry)
                        }
                    }
                    else -> getInputStream(entry)
                }
            }

            creator.addArchiveEntry(zae, stream)
            entries.add(entry.name)
        } else {
            System.err.println("Duplicated jar entry: ${this.name}!/${entry.name}")
        }
    }

    ZipArchiveOutputStream(output).use(creator::writeTo)
}

捕获了所有所有的transform阶段的所有异常,导致transform问题不会中断构建过程,只是在控制台输出 Broken class; 无法暴露编译期的存在的问题,以及无法看到原始异常堆栈(未打印错误堆栈),是否考虑取消catch 或者 仅catch部分异常,将原始异常 throw 出去

johnsonlee commented 1 year ago

可以具体说说遇到了什么问题吗?

gentrio commented 1 year ago

可以具体说说遇到了什么问题吗?

就是我在tranaform方法中使用asm出现了问题,没办法直接在日志中看到异常的堆栈,因为被框架捕获了。