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

Improve runtime compatibility with kotlin 1.5.31 #7343

Merged
merged 2 commits into from Jun 21, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 10 additions & 6 deletions okhttp/src/jvmMain/kotlin/okhttp3/CacheControl.kt
Expand Up @@ -25,9 +25,6 @@ import okhttp3.internal.commonClampToInt
import okhttp3.internal.commonForceCache
import okhttp3.internal.commonForceNetwork
import okhttp3.internal.commonImmutable
import okhttp3.internal.commonMaxAge
import okhttp3.internal.commonMaxStale
import okhttp3.internal.commonMinFresh
import okhttp3.internal.commonNoCache
import okhttp3.internal.commonNoStore
import okhttp3.internal.commonNoTransform
Expand Down Expand Up @@ -153,11 +150,18 @@ actual class CacheControl internal actual constructor(

actual fun immutable() = commonImmutable()

actual fun maxAge(maxAge: Int, timeUnit: DurationUnit) = commonMaxAge(maxAge, timeUnit)
// We are compiling with kotlin 1.6 but need to run with older versions at runtime. For
// maximum compat we therefore need to handle both the case where DurationUnit is typealiased
// to TimeUnit (as it was pre-1.6) and where it is a distinct wrapper enum (in 1.6). We also
// can't use durationUnit.toTimeUnit() because that doesn't exist in the typealiased case.
// This function should work in either case.
internal fun toJavaTimeUnit(durationUnit: DurationUnit) = TimeUnit.valueOf(durationUnit.name)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look to bad, a lookup in a Hashmap, from a constant name (at least on JVM).

https://github.com/frohoff/jdk8u-dev-jdk/blob/da0da73ab82ed714dc5be94acd2f0d00fbdfe2e9/src/share/classes/java/lang/Class.java#L3339


actual fun maxStale(maxStale: Int, timeUnit: DurationUnit) = commonMaxStale(maxStale, timeUnit)
actual fun maxAge(maxAge: Int, timeUnit: DurationUnit) = maxAge(maxAge, toJavaTimeUnit(timeUnit))

actual fun minFresh(minFresh: Int, timeUnit: DurationUnit) = commonMinFresh(minFresh, timeUnit)
actual fun maxStale(maxStale: Int, timeUnit: DurationUnit) = maxStale(maxStale, toJavaTimeUnit(timeUnit))

actual fun minFresh(minFresh: Int, timeUnit: DurationUnit) = minFresh(minFresh, toJavaTimeUnit(timeUnit))

/**
* Sets the maximum age of a cached response. If the cache response's age exceeds [maxAge], it
Expand Down