-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KTOR-7698 Make ServiceLoader.load calls optimizable by R8
- Loading branch information
Showing
9 changed files
with
64 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 13 additions & 15 deletions
28
...nt/ktor-client-plugins/ktor-client-json/jvm/src/io/ktor/client/plugins/json/DefaultJvm.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,22 @@ | ||
/* | ||
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package io.ktor.client.plugins.json | ||
|
||
import java.util.* | ||
import io.ktor.util.reflect.* | ||
import io.ktor.utils.io.* | ||
|
||
@OptIn(InternalAPI::class) | ||
@Suppress("DEPRECATION_ERROR") | ||
public actual fun defaultSerializer(): JsonSerializer { | ||
val serializers = ServiceLoader.load(JsonSerializer::class.java) | ||
.toList() | ||
|
||
if (serializers.isEmpty()) { | ||
error( | ||
"""Fail to find serializer. Consider to add one of the following dependencies: | ||
- ktor-client-gson | ||
- ktor-client-json | ||
- ktor-client-serialization""" | ||
) | ||
} | ||
|
||
return serializers.maxByOrNull { it::javaClass.name }!! | ||
val serializers = loadService<JsonSerializer>() | ||
return serializers.maxByOrNull { it::javaClass.name } ?: error( | ||
""" | ||
Failed to find serializer. Consider adding one of the following dependencies: | ||
- ktor-client-gson | ||
- ktor-client-json | ||
- ktor-client-serialization | ||
""".trimIndent() | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...r-serialization-kotlinx/jsAndWasmShared/src/io/ktor/serialization/kotlinx/ExtensionsJs.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
/* | ||
* Copyright 2014-2022 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package io.ktor.serialization.kotlinx | ||
|
||
internal actual val providers: List<KotlinxSerializationExtensionProvider> | ||
internal actual val providers: Iterable<KotlinxSerializationExtensionProvider> | ||
get() = emptyList() |
12 changes: 6 additions & 6 deletions
12
...ization/ktor-serialization-kotlinx/jvm/src/io/ktor/serialization/kotlinx/ExtensionsJvm.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
/* | ||
* Copyright 2014-2022 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package io.ktor.serialization.kotlinx | ||
|
||
import java.util.* | ||
import io.ktor.util.reflect.* | ||
import io.ktor.utils.io.* | ||
|
||
internal actual val providers: List<KotlinxSerializationExtensionProvider> = | ||
KotlinxSerializationExtensionProvider::class.java.let { | ||
ServiceLoader.load(it, it.classLoader).toList() | ||
} | ||
@OptIn(InternalAPI::class) | ||
internal actual val providers: Iterable<KotlinxSerializationExtensionProvider> = | ||
loadService<KotlinxSerializationExtensionProvider>() |
4 changes: 2 additions & 2 deletions
4
...on/ktor-serialization-kotlinx/posix/src/io/ktor/serialization/kotlinx/ExtensionsNative.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package io.ktor.util.reflect | ||
|
||
import io.ktor.utils.io.* | ||
import java.util.* | ||
|
||
/** | ||
* Loads implementations of service [T] using [ServiceLoader.load]. | ||
* | ||
* NOTE: ServiceLoader should use specific call convention to be optimized by R8 on Android: | ||
* `ServiceLoader.load(X.class, X.class.getClassLoader()).iterator()` | ||
* See: https://r8.googlesource.com/r8/+/refs/heads/main/src/main/java/com/android/tools/r8/ir/optimize/ServiceLoaderRewriter.java | ||
*/ | ||
@InternalAPI | ||
public inline fun <reified T> loadService(): Iterable<T> { | ||
val iterator = ServiceLoader.load( | ||
T::class.java, | ||
T::class.java.classLoader, | ||
).iterator() | ||
|
||
return Iterable { iterator } | ||
} |