Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZipFile.transform catch all transform exception #428

Open
gentrio opened this issue Aug 7, 2023 · 2 comments
Open

ZipFile.transform catch all transform exception #428

gentrio opened this issue Aug 7, 2023 · 2 comments

Comments

@gentrio
Copy link

gentrio commented Aug 7, 2023

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
Copy link
Collaborator

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

@gentrio
Copy link
Author

gentrio commented Sep 7, 2023

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants