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

Transform 失败不会中断编译,异常被 catch 了 #474

Open
woxin123 opened this issue Sep 25, 2024 · 0 comments
Open

Transform 失败不会中断编译,异常被 catch 了 #474

woxin123 opened this issue Sep 25, 2024 · 0 comments

Comments

@woxin123
Copy link

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 catch 住了,只是打印了一下 Broken class,不利于排查 transform 失败的原因。这里能否不 catch 异常,而是让异常抛出来呢?

@woxin123 woxin123 changed the title Transform 失败不会中断编译, Transform 失败不会中断编译,异常被 catch 了 Sep 25, 2024
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

1 participant