Skip to content

Commit e93a9e7

Browse files
Copilotoshai
andauthored
Fix native build error: update GraalVM substitution onlyWith condition to check if target class exists
Agent-Logs-Url: https://github.com/oshai/kotlin-logging/sessions/728f006b-d368-4f73-92f3-ca9855e1798f Co-authored-by: oshai <2683969+oshai@users.noreply.github.com>
1 parent 5594aa9 commit e93a9e7

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

src/jvmMain/kotlin/io/github/oshai/kotlinlogging/internal/Target_io_github_oshai_kotlinlogging_internal_KLoggerFactory.kt

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import java.util.function.BooleanSupplier
1919
internal class Target_io_github_oshai_kotlinlogging_internal_KLoggerFactory {
2020
@TargetClass(
2121
className = "io.github.oshai.kotlinlogging.internal.KLoggerFactory",
22-
onlyWith = [LogbackNotOnClasspath::class],
22+
onlyWith = [KLoggerFactoryExistsAndLogbackNotOnClasspath::class],
2323
)
2424
companion object {
2525
@Substitute
@@ -35,8 +35,25 @@ internal class Target_io_github_oshai_kotlinlogging_internal_KLoggerFactory {
3535
}
3636
}
3737

38-
internal class LogbackNotOnClasspath : BooleanSupplier {
38+
/**
39+
* Condition that returns true only when:
40+
* 1. The substitution target class (`io.github.oshai.kotlinlogging.internal.KLoggerFactory`)
41+
* actually exists on the classpath (it was removed in v8.x), AND
42+
* 2. Logback is not on the classpath.
43+
*
44+
* This prevents a GraalVM native image build error when the target class no longer exists. See
45+
* [https://github.com/oshai/kotlin-logging/issues/385].
46+
*/
47+
internal class KLoggerFactoryExistsAndLogbackNotOnClasspath : BooleanSupplier {
3948
override fun getAsBoolean(): Boolean {
49+
// First check if the substitution target class exists (it doesn't in v8.x+).
50+
// If it doesn't exist, the substitution must be inactive to avoid a native build error.
51+
try {
52+
Class.forName("io.github.oshai.kotlinlogging.internal.KLoggerFactory")
53+
} catch (_: ClassNotFoundException) {
54+
return false
55+
}
56+
// Target class exists; only apply substitution when Logback is not on the classpath.
4057
try {
4158
Class.forName("ch.qos.logback.classic.LoggerContext")
4259
return false

0 commit comments

Comments
 (0)