Skip to content

Commit cdb14b2

Browse files
authored
Replace runCatching-map-getOrDefault in caching (#2248)
The runCatching-map-getOrDefault logic is too complicated and takes a detour in mind. A simple try-catch returning true or false should do the trick.
1 parent 8c74daf commit cdb14b2

File tree

1 file changed

+5
-2
lines changed
  • core/jvmMain/src/kotlinx/serialization/internal

1 file changed

+5
-2
lines changed

core/jvmMain/src/kotlinx/serialization/internal/Caching.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ import kotlin.reflect.KTypeProjection
1717
* but ClassValue is not available on Android, thus we attempt to check it dynamically
1818
* and fallback to ConcurrentHashMap-based cache.
1919
*/
20-
private val useClassValue = runCatching {
20+
private val useClassValue = try {
2121
Class.forName("java.lang.ClassValue")
22-
}.map { true }.getOrDefault(false)
22+
true
23+
} catch (_: Throwable) {
24+
false
25+
}
2326

2427
/**
2528
* Creates a **strongly referenced** cache of values associated with [Class].

0 commit comments

Comments
 (0)